Commit fe550d25 authored by jan.koester's avatar jan.koester
Browse files

HttpClient h2/h3: caller's Host header becomes :authority

:authority was always the URL's host:port and the caller's Host header was
dropped, so a reverse proxy (proxyplus) talking h2/h3 to its backend sent the
backend's address instead of the client's host name -- virtual-hosted
backends (groupi) answered "unknown domain". Test in http2_checksum_test
(fails without the fix).

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent d3d8ab6a
Loading
Loading
Loading
Loading
+14 −4
Changes for src/http.cpp: 14 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -57,6 +57,16 @@
#include <netplus/connection.h>
#include <netplus/exception.h>

// :authority of an H2/H3 request: the caller's Host header when it set one (a reverse
// proxy forwards the client's host name -- the backend's address in its place made
// virtual-hosted backends answer "unknown domain"), else the URL's host:port.
static std::string requestAuthority(libhttppp::HttpHeader &req, const std::string &fallback) {
  if (auto *h = req.getHeaderData("host"))
    if (auto *v = h->getfirstValue())
      if (!v->getvalue().empty()) return v->getvalue();
  return fallback;
}

// RFC 9113 §8.2.2 / RFC 9114 §4.2: connection-specific header fields are not allowed in
// HTTP/2 and HTTP/3 messages -- strict peers (e.g. Fastly) answer them with GOAWAY. "te" is
// only allowed with the value "trailers".
@@ -943,7 +953,7 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
            std::string path = nreq.getRequestURL().empty() ? std::string() : nreq.getRequest();
            if (path.empty()) path = _url.getPath();
            std::string scheme = (_url.getProtocol() == HttpUrl::HTTP) ? "http" : "https";
            std::string authority = _url.getHost() + ":" + std::to_string(_url.getPort());
            std::string authority = requestAuthority(nreq, _url.getHost() + ":" + std::to_string(_url.getPort()));

            std::vector<qpack::HeaderField> extra;
            for (HttpHeader::HeaderData *hd = nreq.getfirstHeaderData(); hd; hd = hd->nextHeaderData()) {
@@ -1115,7 +1125,7 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
            std::string path = nreq.getRequestURL().empty() ? std::string() : nreq.getRequest();
            if (path.empty()) path = _url.getPath();
            std::string scheme = (_url.getProtocol() == HttpUrl::HTTPS) ? "https" : "http";
            std::string auth = _url.getHost() + ":" + std::to_string(_url.getPort());
            std::string auth = requestAuthority(nreq, _url.getHost() + ":" + std::to_string(_url.getPort()));

            std::vector<hpack::HeaderField> extra;
            for (HttpHeader::HeaderData *hd = nreq.getfirstHeaderData(); hd; hd = hd->nextHeaderData()) {
@@ -2593,7 +2603,7 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(

        std::string scheme = (_url.getProtocol() == HttpUrl::HTTPS) ? "https" : "http";

        std::string auth = _url.getHost() + ":" + std::to_string(_url.getPort());
        std::string auth = requestAuthority(nreq, _url.getHost() + ":" + std::to_string(_url.getPort()));

        // Collect extra headers from the request
        bool has_content_length = false;
@@ -3043,7 +3053,7 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(

      std::string scheme = (_url.getProtocol() == HttpUrl::HTTP) ? "http" : "https";

      std::string authority = _url.getHost() + ":" + std::to_string(_url.getPort());
      std::string authority = requestAuthority(nreq, _url.getHost() + ":" + std::to_string(_url.getPort()));

      bool include_content_length = false;
      size_t content_length = 0;
+19 −0
Changes for test/http2_checksum_test.cpp: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -476,6 +476,25 @@ int main() {
                check(seen.find("te=trailers") != std::string::npos && seen.find("x-probe=1") != std::string::npos,
                      "HttpClient h2: te: trailers and ordinary headers still sent");
            }
            // a reverse proxy forwards the client's host name: it must become :authority
            // (the backend's own address there made virtual-hosted backends 404)
            {
                libhttppp::HttpRequest req;
                req.setRequestURL("/gen/7");
                req.setHeaderData("host")->push_back("groupware.example.org");
                httpClt.Get(req, /*maxTries=*/1);
                std::string seen;
                for (auto &h : server->probeHeaders()) seen += h + ";";
                check(seen.find("host=groupware.example.org;") != std::string::npos,
                      "HttpClient h2: caller's Host header becomes :authority");
                libhttppp::HttpRequest plain;
                plain.setRequestURL("/gen/7");
                httpClt.Get(plain, /*maxTries=*/1);
                seen.clear();
                for (auto &h : server->probeHeaders()) seen += h + ";";
                check(seen.find("host=127.0.0.1:" + std::to_string(kTlsPort) + ";") != std::string::npos,
                      "HttpClient h2: without Host, :authority is the URL's host:port");
            }

            for (size_t size : sweepSizes()) {
                std::string label = "size=" + std::to_string(size);