nghttpx: Remove downstream_port from location rewrite code

This commit is contained in:
Tatsuhiro Tsujikawa 2013-12-21 18:35:53 +09:00
parent 9afebcb229
commit e955598923
9 changed files with 23 additions and 35 deletions

View File

@ -443,8 +443,7 @@ std::string rewrite_location_uri(const std::string& uri,
const http_parser_url& u, const http_parser_url& u,
const std::string& request_host, const std::string& request_host,
const std::string& upstream_scheme, const std::string& upstream_scheme,
uint16_t upstream_port, uint16_t upstream_port)
uint16_t downstream_port)
{ {
// We just rewrite host and optionally port. We don't rewrite https // We just rewrite host and optionally port. We don't rewrite https
// link. Not sure it happens in practice. // link. Not sure it happens in practice.

View File

@ -175,8 +175,7 @@ void dump_nv(FILE *out, const nghttp2_nv *nva, size_t nvlen);
// stores the result of parsed |uri|. The |request_host| is the host // stores the result of parsed |uri|. The |request_host| is the host
// or :authority header field value in the request. The // or :authority header field value in the request. The
// |upstream_scheme| is either "https" or "http" in the upstream // |upstream_scheme| is either "https" or "http" in the upstream
// interface. The |downstream_port| is the port in the downstream // interface.
// connection.
// //
// This function returns the new rewritten URI on success. If the // This function returns the new rewritten URI on success. If the
// location URI is not subject to the rewrite, this function returns // location URI is not subject to the rewrite, this function returns
@ -185,8 +184,7 @@ std::string rewrite_location_uri(const std::string& uri,
const http_parser_url& u, const http_parser_url& u,
const std::string& request_host, const std::string& request_host,
const std::string& upstream_scheme, const std::string& upstream_scheme,
uint16_t upstream_port, uint16_t upstream_port);
uint16_t downstream_port);
} // namespace http2 } // namespace http2

View File

@ -229,15 +229,13 @@ void check_rewrite_location_uri(const std::string& new_uri,
const std::string& uri, const std::string& uri,
const std::string& req_host, const std::string& req_host,
const std::string& upstream_scheme, const std::string& upstream_scheme,
uint16_t upstream_port, uint16_t upstream_port)
uint16_t downstream_port)
{ {
http_parser_url u; http_parser_url u;
CU_ASSERT(0 == http_parser_parse_url(uri.c_str(), uri.size(), 0, &u)); CU_ASSERT(0 == http_parser_parse_url(uri.c_str(), uri.size(), 0, &u));
CU_ASSERT(new_uri == CU_ASSERT(new_uri ==
http2::rewrite_location_uri(uri, u, req_host, http2::rewrite_location_uri(uri, u, req_host,
upstream_scheme, upstream_port, upstream_scheme, upstream_port));
downstream_port));
} }
} // namespace } // namespace
@ -245,31 +243,31 @@ void test_http2_rewrite_location_uri(void)
{ {
check_rewrite_location_uri("https://localhost:3000/alpha?bravo#charlie", check_rewrite_location_uri("https://localhost:3000/alpha?bravo#charlie",
"http://localhost:3001/alpha?bravo#charlie", "http://localhost:3001/alpha?bravo#charlie",
"localhost:3001", "https", 3000, 3001); "localhost:3001", "https", 3000);
check_rewrite_location_uri("https://localhost/", check_rewrite_location_uri("https://localhost/",
"http://localhost:3001/", "http://localhost:3001/",
"localhost:3001", "https", 443, 3001); "localhost:3001", "https", 443);
check_rewrite_location_uri("http://localhost/", check_rewrite_location_uri("http://localhost/",
"http://localhost:3001/", "http://localhost:3001/",
"localhost:3001", "http", 80, 3001); "localhost:3001", "http", 80);
check_rewrite_location_uri("http://localhost:443/", check_rewrite_location_uri("http://localhost:443/",
"http://localhost:3001/", "http://localhost:3001/",
"localhost:3001", "http", 443, 3001); "localhost:3001", "http", 443);
check_rewrite_location_uri("https://localhost:80/", check_rewrite_location_uri("https://localhost:80/",
"http://localhost:3001/", "http://localhost:3001/",
"localhost:3001", "https", 80, 3001); "localhost:3001", "https", 80);
check_rewrite_location_uri("", check_rewrite_location_uri("",
"http://localhost:3001/", "http://localhost:3001/",
"127.0.0.1", "https", 3000, 3001); "127.0.0.1", "https", 3000);
check_rewrite_location_uri("https://localhost:3000/", check_rewrite_location_uri("https://localhost:3000/",
"http://localhost:3001/", "http://localhost:3001/",
"localhost", "https", 3000, 3001); "localhost", "https", 3000);
check_rewrite_location_uri("", check_rewrite_location_uri("",
"https://localhost:3001/", "https://localhost:3001/",
"localhost", "https", 3000, 3001); "localhost", "https", 3000);
check_rewrite_location_uri("https://localhost:3000/", check_rewrite_location_uri("https://localhost:3000/",
"http://localhost/", "http://localhost/",
"localhost", "https", 3000, 80); "localhost", "https", 3000);
} }
} // namespace shrpx } // namespace shrpx

View File

@ -489,8 +489,7 @@ Headers::const_iterator Downstream::get_norm_response_header
void Downstream::rewrite_norm_location_response_header void Downstream::rewrite_norm_location_response_header
(const std::string& upstream_scheme, (const std::string& upstream_scheme,
uint16_t upstream_port, uint16_t upstream_port)
uint16_t downstream_port)
{ {
auto hd = get_norm_header(response_headers_, "location"); auto hd = get_norm_header(response_headers_, "location");
if(hd == std::end(response_headers_)) { if(hd == std::end(response_headers_)) {
@ -506,8 +505,7 @@ void Downstream::rewrite_norm_location_response_header
if(!request_http2_authority_.empty()) { if(!request_http2_authority_.empty()) {
new_uri = http2::rewrite_location_uri((*hd).second, u, new_uri = http2::rewrite_location_uri((*hd).second, u,
request_http2_authority_, request_http2_authority_,
upstream_scheme, upstream_port, upstream_scheme, upstream_port);
downstream_port);
} }
if(new_uri.empty()) { if(new_uri.empty()) {
auto host = get_norm_request_header("host"); auto host = get_norm_request_header("host");
@ -515,8 +513,7 @@ void Downstream::rewrite_norm_location_response_header
return; return;
} }
new_uri = http2::rewrite_location_uri((*hd).second, u, (*host).second, new_uri = http2::rewrite_location_uri((*hd).second, u, (*host).second,
upstream_scheme, upstream_port, upstream_scheme, upstream_port);
downstream_port);
} }
if(!new_uri.empty()) { if(!new_uri.empty()) {
(*hd).second = std::move(new_uri); (*hd).second = std::move(new_uri);

View File

@ -160,8 +160,7 @@ public:
// normalize_request_headers(). // normalize_request_headers().
void rewrite_norm_location_response_header void rewrite_norm_location_response_header
(const std::string& upstream_scheme, (const std::string& upstream_scheme,
uint16_t upstream_port, uint16_t upstream_port);
uint16_t downstream_port);
void add_response_header(std::string name, std::string value); void add_response_header(std::string name, std::string value);
void set_last_response_header_value(std::string value); void set_last_response_header_value(std::string value);

View File

@ -152,7 +152,7 @@ void test_downstream_rewrite_norm_location_response_header(void)
Downstream d(nullptr, 0, 0); Downstream d(nullptr, 0, 0);
d.add_request_header("host", "localhost:3000"); d.add_request_header("host", "localhost:3000");
d.add_response_header("location", "http://localhost:3000/"); d.add_response_header("location", "http://localhost:3000/");
d.rewrite_norm_location_response_header("https", 443, 3000); d.rewrite_norm_location_response_header("https", 443);
auto location = d.get_norm_response_header("location"); auto location = d.get_norm_response_header("location");
CU_ASSERT("https://localhost/" == (*location).second); CU_ASSERT("https://localhost/" == (*location).second);
} }
@ -160,7 +160,7 @@ void test_downstream_rewrite_norm_location_response_header(void)
Downstream d(nullptr, 0, 0); Downstream d(nullptr, 0, 0);
d.set_request_http2_authority("localhost"); d.set_request_http2_authority("localhost");
d.add_response_header("location", "http://localhost/"); d.add_response_header("location", "http://localhost/");
d.rewrite_norm_location_response_header("https", 443, 80); d.rewrite_norm_location_response_header("https", 443);
auto location = d.get_norm_response_header("location"); auto location = d.get_norm_response_header("location");
CU_ASSERT("https://localhost/" == (*location).second); CU_ASSERT("https://localhost/" == (*location).second);
} }

View File

@ -946,8 +946,7 @@ int Http2Upstream::on_downstream_header_complete(Downstream *downstream)
} }
downstream->normalize_response_headers(); downstream->normalize_response_headers();
downstream->rewrite_norm_location_response_header downstream->rewrite_norm_location_response_header
(get_client_handler()->get_upstream_scheme(), get_config()->port, (get_client_handler()->get_upstream_scheme(), get_config()->port);
get_config()->downstream_port);
downstream->concat_norm_response_headers(); downstream->concat_norm_response_headers();
auto end_headers = std::end(downstream->get_response_headers()); auto end_headers = std::end(downstream->get_response_headers());
size_t nheader = downstream->get_response_headers().size(); size_t nheader = downstream->get_response_headers().size();

View File

@ -657,8 +657,7 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream)
hdrs += "\r\n"; hdrs += "\r\n";
downstream->normalize_response_headers(); downstream->normalize_response_headers();
downstream->rewrite_norm_location_response_header downstream->rewrite_norm_location_response_header
(get_client_handler()->get_upstream_scheme(), get_config()->port, (get_client_handler()->get_upstream_scheme(), get_config()->port);
get_config()->downstream_port);
auto end_headers = std::end(downstream->get_response_headers()); auto end_headers = std::end(downstream->get_response_headers());
http2::build_http1_headers_from_norm_headers http2::build_http1_headers_from_norm_headers
(hdrs, downstream->get_response_headers()); (hdrs, downstream->get_response_headers());

View File

@ -841,8 +841,7 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream)
} }
downstream->normalize_response_headers(); downstream->normalize_response_headers();
downstream->rewrite_norm_location_response_header downstream->rewrite_norm_location_response_header
(get_client_handler()->get_upstream_scheme(), get_config()->port, (get_client_handler()->get_upstream_scheme(), get_config()->port);
get_config()->downstream_port);
size_t nheader = downstream->get_response_headers().size(); size_t nheader = downstream->get_response_headers().size();
// 6 means :status, :version and possible via header field. // 6 means :status, :version and possible via header field.
auto nv = util::make_unique<const char*[]>(nheader * 2 + 6 + 1); auto nv = util::make_unique<const char*[]>(nheader * 2 + 6 + 1);