From 284691253ffce0b03fb4161f0bfaa32ed2c5839f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 28 Feb 2016 23:54:02 +0900 Subject: [PATCH] nghttpx: Use StringRef for http::create_forwarded parameter --- src/shrpx_client_handler.cc | 8 +++---- src/shrpx_client_handler.h | 4 ++-- src/shrpx_http.cc | 5 ++-- src/shrpx_http.h | 4 ++-- src/shrpx_http2_downstream_connection.cc | 6 ++--- src/shrpx_http_downstream_connection.cc | 6 ++--- src/shrpx_http_test.cc | 30 +++++++++++++++--------- 7 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 7151dbf2..46540d42 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -1138,18 +1138,18 @@ int ClientHandler::proxy_protocol_read() { return on_proxy_protocol_finish(); } -StringRef ClientHandler::get_forwarded_by() { +StringRef ClientHandler::get_forwarded_by() const { auto &fwdconf = get_config()->http.forwarded; if (fwdconf.by_node_type == FORWARDED_NODE_OBFUSCATED) { return StringRef(fwdconf.by_obfuscated); } - return StringRef(faddr_->hostport); + return StringRef{faddr_->hostport}; } -const std::string &ClientHandler::get_forwarded_for() const { - return forwarded_for_; +StringRef ClientHandler::get_forwarded_for() const { + return StringRef{forwarded_for_}; } } // namespace shrpx diff --git a/src/shrpx_client_handler.h b/src/shrpx_client_handler.h index 42b03c26..7d6ace81 100644 --- a/src/shrpx_client_handler.h +++ b/src/shrpx_client_handler.h @@ -135,10 +135,10 @@ public: // Returns string suitable for use in "by" parameter of Forwarded // header field. - StringRef get_forwarded_by(); + StringRef get_forwarded_by() const; // Returns string suitable for use in "for" parameter of Forwarded // header field. - const std::string &get_forwarded_for() const; + StringRef get_forwarded_for() const; private: Connection conn_; diff --git a/src/shrpx_http.cc b/src/shrpx_http.cc index 86870435..7bca56dd 100644 --- a/src/shrpx_http.cc +++ b/src/shrpx_http.cc @@ -62,9 +62,8 @@ std::string create_via_header_value(int major, int minor) { } std::string create_forwarded(int params, const StringRef &node_by, - const std::string &node_for, - const std::string &host, - const std::string &proto) { + const StringRef &node_for, const StringRef &host, + const StringRef &proto) { std::string res; if ((params & FORWARDED_BY) && !node_by.empty()) { // This must be quoted-string unless it is obfuscated version diff --git a/src/shrpx_http.h b/src/shrpx_http.h index bd29df02..56705ecd 100644 --- a/src/shrpx_http.h +++ b/src/shrpx_http.h @@ -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 // defined in shrpx_config.h. std::string create_forwarded(int params, const StringRef &node_by, - const std::string &node_for, - const std::string &host, const std::string &proto); + const StringRef &node_for, const StringRef &host, + const StringRef &proto); // Adds ANSI color codes to HTTP headers |hdrs|. std::string colorizeHeaders(const char *hdrs); diff --git a/src/shrpx_http2_downstream_connection.cc b/src/shrpx_http2_downstream_connection.cc index b5accaa2..028b6e41 100644 --- a/src/shrpx_http2_downstream_connection.cc +++ b/src/shrpx_http2_downstream_connection.cc @@ -359,9 +359,9 @@ int Http2DownstreamConnection::push_request_headers() { params &= ~FORWARDED_PROTO; } - auto value = http::create_forwarded(params, handler->get_forwarded_by(), - handler->get_forwarded_for(), - req.authority, req.scheme); + auto value = http::create_forwarded( + params, handler->get_forwarded_by(), handler->get_forwarded_for(), + StringRef{req.authority}, StringRef{req.scheme}); if (fwd || !value.empty()) { if (fwd) { forwarded_value = fwd->value; diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index fcd9b8e6..a6c238a1 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -366,9 +366,9 @@ int HttpDownstreamConnection::push_request_headers() { params &= ~FORWARDED_PROTO; } - auto value = http::create_forwarded(params, handler->get_forwarded_by(), - handler->get_forwarded_for(), - req.authority, req.scheme); + auto value = http::create_forwarded( + params, handler->get_forwarded_by(), handler->get_forwarded_for(), + StringRef{req.authority}, StringRef{req.scheme}); if (fwd || !value.empty()) { buf->append("Forwarded: "); if (fwd) { diff --git a/src/shrpx_http_test.cc b/src/shrpx_http_test.cc index 03510550..87fbb9c5 100644 --- a/src/shrpx_http_test.cc +++ b/src/shrpx_http_test.cc @@ -43,25 +43,33 @@ void test_shrpx_http_create_forwarded(void) { http::create_forwarded(FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO, 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" == 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]\"" == - http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, - StringRef::from_lit("_hidden"), "[::1]", "", - "")); + http::create_forwarded( + FORWARDED_BY | FORWARDED_FOR, StringRef::from_lit("_hidden"), + StringRef::from_lit("[::1]"), StringRef::from_lit(""), + StringRef::from_lit(""))); CU_ASSERT("by=\"[::1]\";for=_hidden" == - http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, - StringRef::from_lit("[::1]"), "_hidden", "", - "")); + http::create_forwarded( + 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 | - FORWARDED_HOST | FORWARDED_PROTO, - StringRef::from_lit(""), "", "", "")); + CU_ASSERT("" == + http::create_forwarded( + FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO, + StringRef::from_lit(""), StringRef::from_lit(""), + StringRef::from_lit(""), StringRef::from_lit(""))); } } // namespace shrpx