From 74c77926a80f261840892d0390e808836d930664 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 16 Jan 2016 12:49:18 +0900 Subject: [PATCH] nghttpx: Refactor and simplify Downstream::rewrite_location_response_header --- src/shrpx_downstream.cc | 51 +++++++----------------------------- src/shrpx_downstream_test.cc | 35 +++++++------------------ 2 files changed, 20 insertions(+), 66 deletions(-) diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index e4c7f6a1..c9f148b9 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -540,56 +540,25 @@ void Downstream::rewrite_location_response_header( if (!hd) { return; } + + if (request_downstream_host_.empty() || req_.authority.empty()) { + return; + } + http_parser_url u{}; - int rv = - http_parser_parse_url((*hd).value.c_str(), (*hd).value.size(), 0, &u); + auto rv = http_parser_parse_url(hd->value.c_str(), hd->value.size(), 0, &u); if (rv != 0) { return; } - std::string new_uri; - if (get_config()->no_host_rewrite || req_.method == HTTP_CONNECT) { - if (!req_.authority.empty()) { - 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); - } - } - } + + auto new_uri = http2::rewrite_location_uri( + hd->value, u, request_downstream_host_, req_.authority, upstream_scheme); if (new_uri.empty()) { return; } - (*hd).value = std::move(new_uri); + hd->value = std::move(new_uri); } bool Downstream::get_chunked_response() const { return chunked_response_; } diff --git a/src/shrpx_downstream_test.cc b/src/shrpx_downstream_test.cc index d0ba04e7..c7b559a4 100644 --- a/src/shrpx_downstream_test.cc +++ b/src/shrpx_downstream_test.cc @@ -124,31 +124,16 @@ void test_downstream_assemble_request_cookie(void) { } void test_downstream_rewrite_location_response_header(void) { - { - Downstream d(nullptr, nullptr, 0); - auto &req = d.request(); - auto &resp = d.response(); - d.set_request_downstream_host("localhost:3000"); - req.fs.add_header("host", "localhost"); - resp.fs.add_header("location", "http://localhost:3000/"); - req.fs.index_headers(); - resp.fs.index_headers(); - d.rewrite_location_response_header("https"); - auto location = resp.fs.header(http2::HD_LOCATION); - CU_ASSERT("https://localhost/" == (*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); - } + Downstream d(nullptr, nullptr, 0); + auto &req = d.request(); + auto &resp = d.response(); + d.set_request_downstream_host("localhost2"); + req.authority = "localhost:8443"; + resp.fs.add_header("location", "http://localhost2:3000/"); + resp.fs.index_headers(); + d.rewrite_location_response_header("https"); + auto location = resp.fs.header(http2::HD_LOCATION); + CU_ASSERT("https://localhost:8443/" == (*location).value); } } // namespace shrpx