nghttpx: Use StringRef for http::create_forwarded parameter

This commit is contained in:
Tatsuhiro Tsujikawa 2016-02-28 23:54:02 +09:00
parent 124d4c9fad
commit 284691253f
7 changed files with 35 additions and 28 deletions

View File

@ -1138,18 +1138,18 @@ int ClientHandler::proxy_protocol_read() {
return on_proxy_protocol_finish(); return on_proxy_protocol_finish();
} }
StringRef ClientHandler::get_forwarded_by() { StringRef ClientHandler::get_forwarded_by() const {
auto &fwdconf = get_config()->http.forwarded; auto &fwdconf = get_config()->http.forwarded;
if (fwdconf.by_node_type == FORWARDED_NODE_OBFUSCATED) { if (fwdconf.by_node_type == FORWARDED_NODE_OBFUSCATED) {
return StringRef(fwdconf.by_obfuscated); return StringRef(fwdconf.by_obfuscated);
} }
return StringRef(faddr_->hostport); return StringRef{faddr_->hostport};
} }
const std::string &ClientHandler::get_forwarded_for() const { StringRef ClientHandler::get_forwarded_for() const {
return forwarded_for_; return StringRef{forwarded_for_};
} }
} // namespace shrpx } // namespace shrpx

View File

@ -135,10 +135,10 @@ public:
// Returns string suitable for use in "by" parameter of Forwarded // Returns string suitable for use in "by" parameter of Forwarded
// header field. // header field.
StringRef get_forwarded_by(); StringRef get_forwarded_by() const;
// Returns string suitable for use in "for" parameter of Forwarded // Returns string suitable for use in "for" parameter of Forwarded
// header field. // header field.
const std::string &get_forwarded_for() const; StringRef get_forwarded_for() const;
private: private:
Connection conn_; Connection conn_;

View File

@ -62,9 +62,8 @@ std::string create_via_header_value(int major, int minor) {
} }
std::string create_forwarded(int params, const StringRef &node_by, std::string create_forwarded(int params, const StringRef &node_by,
const std::string &node_for, const StringRef &node_for, const StringRef &host,
const std::string &host, const StringRef &proto) {
const std::string &proto) {
std::string res; std::string res;
if ((params & FORWARDED_BY) && !node_by.empty()) { if ((params & FORWARDED_BY) && !node_by.empty()) {
// This must be quoted-string unless it is obfuscated version // This must be quoted-string unless it is obfuscated version

View File

@ -43,8 +43,8 @@ std::string create_via_header_value(int major, int minor);
// |params| is bitwise-OR of zero or more of shrpx_forwarded_param // |params| is bitwise-OR of zero or more of shrpx_forwarded_param
// defined in shrpx_config.h. // defined in shrpx_config.h.
std::string create_forwarded(int params, const StringRef &node_by, std::string create_forwarded(int params, const StringRef &node_by,
const std::string &node_for, const StringRef &node_for, const StringRef &host,
const std::string &host, const std::string &proto); const StringRef &proto);
// Adds ANSI color codes to HTTP headers |hdrs|. // Adds ANSI color codes to HTTP headers |hdrs|.
std::string colorizeHeaders(const char *hdrs); std::string colorizeHeaders(const char *hdrs);

View File

@ -359,9 +359,9 @@ int Http2DownstreamConnection::push_request_headers() {
params &= ~FORWARDED_PROTO; params &= ~FORWARDED_PROTO;
} }
auto value = http::create_forwarded(params, handler->get_forwarded_by(), auto value = http::create_forwarded(
handler->get_forwarded_for(), params, handler->get_forwarded_by(), handler->get_forwarded_for(),
req.authority, req.scheme); StringRef{req.authority}, StringRef{req.scheme});
if (fwd || !value.empty()) { if (fwd || !value.empty()) {
if (fwd) { if (fwd) {
forwarded_value = fwd->value; forwarded_value = fwd->value;

View File

@ -366,9 +366,9 @@ int HttpDownstreamConnection::push_request_headers() {
params &= ~FORWARDED_PROTO; params &= ~FORWARDED_PROTO;
} }
auto value = http::create_forwarded(params, handler->get_forwarded_by(), auto value = http::create_forwarded(
handler->get_forwarded_for(), params, handler->get_forwarded_by(), handler->get_forwarded_for(),
req.authority, req.scheme); StringRef{req.authority}, StringRef{req.scheme});
if (fwd || !value.empty()) { if (fwd || !value.empty()) {
buf->append("Forwarded: "); buf->append("Forwarded: ");
if (fwd) { if (fwd) {

View File

@ -43,25 +43,33 @@ void test_shrpx_http_create_forwarded(void) {
http::create_forwarded(FORWARDED_BY | FORWARDED_FOR | http::create_forwarded(FORWARDED_BY | FORWARDED_FOR |
FORWARDED_HOST | FORWARDED_PROTO, FORWARDED_HOST | FORWARDED_PROTO,
StringRef::from_lit("example.com:3000"), StringRef::from_lit("example.com:3000"),
"[::1]", "www.example.com", "https")); StringRef::from_lit("[::1]"),
StringRef::from_lit("www.example.com"),
StringRef::from_lit("https")));
CU_ASSERT("for=192.168.0.1" == CU_ASSERT("for=192.168.0.1" ==
http::create_forwarded(FORWARDED_FOR, StringRef::from_lit("alpha"), http::create_forwarded(FORWARDED_FOR, StringRef::from_lit("alpha"),
"192.168.0.1", "bravo", "charlie")); StringRef::from_lit("192.168.0.1"),
StringRef::from_lit("bravo"),
StringRef::from_lit("charlie")));
CU_ASSERT("by=_hidden;for=\"[::1]\"" == CU_ASSERT("by=_hidden;for=\"[::1]\"" ==
http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, http::create_forwarded(
StringRef::from_lit("_hidden"), "[::1]", "", FORWARDED_BY | FORWARDED_FOR, StringRef::from_lit("_hidden"),
"")); StringRef::from_lit("[::1]"), StringRef::from_lit(""),
StringRef::from_lit("")));
CU_ASSERT("by=\"[::1]\";for=_hidden" == CU_ASSERT("by=\"[::1]\";for=_hidden" ==
http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, http::create_forwarded(
StringRef::from_lit("[::1]"), "_hidden", "", FORWARDED_BY | FORWARDED_FOR, StringRef::from_lit("[::1]"),
"")); StringRef::from_lit("_hidden"), StringRef::from_lit(""),
StringRef::from_lit("")));
CU_ASSERT("" == http::create_forwarded(FORWARDED_BY | FORWARDED_FOR | CU_ASSERT("" ==
FORWARDED_HOST | FORWARDED_PROTO, http::create_forwarded(
StringRef::from_lit(""), "", "", "")); FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO,
StringRef::from_lit(""), StringRef::from_lit(""),
StringRef::from_lit(""), StringRef::from_lit("")));
} }
} // namespace shrpx } // namespace shrpx