diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index 7e3e901a..4a21892e 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -55,6 +55,7 @@ int main(int argc, char *argv[]) { SSL_library_init(); shrpx::create_config(); + shrpx::mod_config()->no_host_rewrite = true; // initialize the CUnit test registry if (CUE_SUCCESS != CU_initialize_registry()) diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index 86d828dd..038aa896 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -548,17 +548,25 @@ Downstream::rewrite_location_response_header(const std::string &upstream_scheme, return; } std::string new_uri; - if (!request_http2_authority_.empty()) { - new_uri = - http2::rewrite_location_uri((*hd).value, u, request_http2_authority_, - upstream_scheme, upstream_port); - } - if (new_uri.empty()) { - auto host = get_request_header(http2::HD_HOST); - if (!host) { - return; + if (get_config()->no_host_rewrite) { + if (!request_http2_authority_.empty()) { + new_uri = + http2::rewrite_location_uri((*hd).value, u, request_http2_authority_, + upstream_scheme, upstream_port); } - new_uri = http2::rewrite_location_uri((*hd).value, u, (*host).value, + if (new_uri.empty()) { + auto host = get_request_header(http2::HD_HOST); + if (!host) { + return; + } + new_uri = http2::rewrite_location_uri((*hd).value, u, (*host).value, + upstream_scheme, upstream_port); + } + } else { + assert(dconn_); + auto request_host = + get_config()->downstream_addrs[dconn_->get_addr_idx()].host.get(); + new_uri = http2::rewrite_location_uri((*hd).value, u, request_host, upstream_scheme, upstream_port); } if (!new_uri.empty()) { diff --git a/src/shrpx_downstream_connection.h b/src/shrpx_downstream_connection.h index 5594ccf7..7851aff9 100644 --- a/src/shrpx_downstream_connection.h +++ b/src/shrpx_downstream_connection.h @@ -58,6 +58,8 @@ public: virtual void on_upstream_change(Upstream *uptream) = 0; virtual int on_priority_change(int32_t pri) = 0; + virtual size_t get_addr_idx() const = 0; + void set_client_handler(ClientHandler *client_handler); ClientHandler *get_client_handler(); Downstream *get_downstream(); diff --git a/src/shrpx_http2_downstream_connection.cc b/src/shrpx_http2_downstream_connection.cc index 6b2b17a2..2152ea3b 100644 --- a/src/shrpx_http2_downstream_connection.cc +++ b/src/shrpx_http2_downstream_connection.cc @@ -578,4 +578,6 @@ int Http2DownstreamConnection::on_timeout() { return submit_rst_stream(downstream_, NGHTTP2_NO_ERROR); } +size_t Http2DownstreamConnection::get_addr_idx() const { return 0; } + } // namespace shrpx diff --git a/src/shrpx_http2_downstream_connection.h b/src/shrpx_http2_downstream_connection.h index cee6265a..55807f36 100644 --- a/src/shrpx_http2_downstream_connection.h +++ b/src/shrpx_http2_downstream_connection.h @@ -62,6 +62,8 @@ public: virtual void on_upstream_change(Upstream *upstream) {} virtual int on_priority_change(int32_t pri); + virtual size_t get_addr_idx() const; + int send(); void attach_stream_data(StreamData *sd); diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 61a11c87..74fb098c 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -767,4 +767,6 @@ void HttpDownstreamConnection::on_upstream_change(Upstream *upstream) {} void HttpDownstreamConnection::signal_write() { conn_.wlimit.startw(); } +size_t HttpDownstreamConnection::get_addr_idx() const { return addr_idx_; } + } // namespace shrpx diff --git a/src/shrpx_http_downstream_connection.h b/src/shrpx_http_downstream_connection.h index 2eec5def..280c6738 100644 --- a/src/shrpx_http_downstream_connection.h +++ b/src/shrpx_http_downstream_connection.h @@ -59,6 +59,8 @@ public: virtual void on_upstream_change(Upstream *upstream); virtual int on_priority_change(int32_t pri) { return 0; } + virtual size_t get_addr_idx() const; + int on_connect(); void signal_write();