nghttpx: Refactor and simplify Downstream::rewrite_location_response_header

This commit is contained in:
Tatsuhiro Tsujikawa 2016-01-16 12:49:18 +09:00
parent 198e253e9d
commit 74c77926a8
2 changed files with 20 additions and 66 deletions

View File

@ -540,56 +540,25 @@ void Downstream::rewrite_location_response_header(
if (!hd) { if (!hd) {
return; return;
} }
if (request_downstream_host_.empty() || req_.authority.empty()) {
return;
}
http_parser_url u{}; http_parser_url u{};
int rv = auto rv = http_parser_parse_url(hd->value.c_str(), hd->value.size(), 0, &u);
http_parser_parse_url((*hd).value.c_str(), (*hd).value.size(), 0, &u);
if (rv != 0) { if (rv != 0) {
return; return;
} }
std::string new_uri;
if (get_config()->no_host_rewrite || req_.method == HTTP_CONNECT) { auto new_uri = http2::rewrite_location_uri(
if (!req_.authority.empty()) { hd->value, u, request_downstream_host_, req_.authority, upstream_scheme);
new_uri = http2::rewrite_location_uri((*hd).value, u, req_.authority,
req_.authority, upstream_scheme);
}
if (new_uri.empty()) {
auto host = req_.fs.header(http2::HD_HOST);
if (host) {
new_uri = http2::rewrite_location_uri((*hd).value, u, (*host).value,
(*host).value, upstream_scheme);
} else if (!request_downstream_host_.empty()) {
new_uri = http2::rewrite_location_uri(
(*hd).value, u, request_downstream_host_, "", upstream_scheme);
} else {
return;
}
}
} else {
if (request_downstream_host_.empty()) {
return;
}
if (!req_.authority.empty()) {
new_uri =
http2::rewrite_location_uri((*hd).value, u, request_downstream_host_,
req_.authority, upstream_scheme);
} else {
auto host = req_.fs.header(http2::HD_HOST);
if (host) {
new_uri = http2::rewrite_location_uri((*hd).value, u,
request_downstream_host_,
(*host).value, upstream_scheme);
} else {
new_uri = http2::rewrite_location_uri(
(*hd).value, u, request_downstream_host_, "", upstream_scheme);
}
}
}
if (new_uri.empty()) { if (new_uri.empty()) {
return; return;
} }
(*hd).value = std::move(new_uri); hd->value = std::move(new_uri);
} }
bool Downstream::get_chunked_response() const { return chunked_response_; } bool Downstream::get_chunked_response() const { return chunked_response_; }

View File

@ -124,31 +124,16 @@ void test_downstream_assemble_request_cookie(void) {
} }
void test_downstream_rewrite_location_response_header(void) { void test_downstream_rewrite_location_response_header(void) {
{
Downstream d(nullptr, nullptr, 0); Downstream d(nullptr, nullptr, 0);
auto &req = d.request(); auto &req = d.request();
auto &resp = d.response(); auto &resp = d.response();
d.set_request_downstream_host("localhost:3000"); d.set_request_downstream_host("localhost2");
req.fs.add_header("host", "localhost"); req.authority = "localhost:8443";
resp.fs.add_header("location", "http://localhost:3000/"); resp.fs.add_header("location", "http://localhost2:3000/");
req.fs.index_headers();
resp.fs.index_headers(); resp.fs.index_headers();
d.rewrite_location_response_header("https"); d.rewrite_location_response_header("https");
auto location = resp.fs.header(http2::HD_LOCATION); auto location = resp.fs.header(http2::HD_LOCATION);
CU_ASSERT("https://localhost/" == (*location).value); CU_ASSERT("https://localhost:8443/" == (*location).value);
}
{
Downstream d(nullptr, nullptr, 0);
auto &req = d.request();
auto &resp = d.response();
d.set_request_downstream_host("localhost");
req.authority = "localhost";
resp.fs.add_header("location", "http://localhost:3000/");
resp.fs.index_headers();
d.rewrite_location_response_header("https");
auto location = resp.fs.header(http2::HD_LOCATION);
CU_ASSERT("https://localhost/" == (*location).value);
}
} }
} // namespace shrpx } // namespace shrpx