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

HttpClient: never send connection-specific headers on h2/h3; bounded default retries

- RFC 9113 8.2.2 / RFC 9114 4.2: connection, keep-alive, proxy-connection,
  transfer-encoding, upgrade (and te other than trailers) are dropped when a
  request goes out over HTTP/2 or HTTP/3. A caller's 'connection: close'
  made Fastly (spamassassin.apache.org, ...) answer with GOAWAY.
- maxTries == 0 retried forever: with a peer that always fails, Get() spun
  reconnecting indefinitely, ignoring every timeout. Now 3 attempts.
- regression check in http2_checksum_test (fails without the fix)

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 0436da93
Loading
Loading
Loading
Loading
+28 −4
Changes for src/http.cpp: 28 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <cctype>
#include <algorithm>
#include <array>
#include <string_view>
@@ -56,6 +57,24 @@
#include <netplus/connection.h>
#include <netplus/exception.h>

// 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".
static bool isConnectionSpecificHeader(const std::string &keyIn, const std::string &value) {
    std::string key;
    for (char c : keyIn) key += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
    if (key == "connection" || key == "keep-alive" || key == "proxy-connection" || key == "transfer-encoding" ||
        key == "upgrade")
        return true;
    if (key == "te") {
        std::string v;
        for (char c : value) if (!std::isspace(static_cast<unsigned char>(c))) v += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
        return v != "trailers";
    }
    return false;
}


libhttppp::HttpUrl::HttpUrl(){
  _protocol=HTTP;
  _origProtocol=HTTP;
@@ -930,9 +949,9 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
            for (HttpHeader::HeaderData *hd = nreq.getfirstHeaderData(); hd; hd = hd->nextHeaderData()) {
                const std::string &key = hd->getkey();
                if (key.empty() || key[0] == ':') continue;
                if (key == "host" || key == "connection") continue;
                if (key == "host") continue;
                for (HttpHeader::HeaderData::Values *v = hd->getfirstValue(); v; v = v->nextvalue())
                    extra.push_back({key, v->getvalue(), false});
                    if (!isConnectionSpecificHeader(key, v->getvalue())) extra.push_back({key, v->getvalue(), false});
            }

            std::vector<uint8_t> headers = qpack::Encoder::encodeRequestHeaders(
@@ -1104,7 +1123,7 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
                if (key.empty() || key[0] == ':') continue;
                if (key == "host") continue;
                for (HttpHeader::HeaderData::Values *v = hd->getfirstValue(); v; v = v->nextvalue())
                    extra.push_back({key, v->getvalue(), false});
                    if (!isConnectionSpecificHeader(key, v->getvalue())) extra.push_back({key, v->getvalue(), false});
            }

            std::string hpack_block = hpack::Encoder::encodeRequestHeaders(
@@ -2585,6 +2604,7 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(
            if (key == "host") continue; // :authority replaces host in H2
          if (key == "content-length") has_content_length = true;
            for (HttpHeader::HeaderData::Values *v = hd->getfirstValue(); v; v = v->nextvalue()) {
                if (isConnectionSpecificHeader(key, v->getvalue())) continue;
                extra.push_back({key, v->getvalue(), false});
            }
        }
@@ -3032,9 +3052,10 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(
      for (HttpHeader::HeaderData *hd = nreq.getfirstHeaderData(); hd; hd = hd->nextHeaderData()) {
        const std::string &key = hd->getkey();
        if (key.empty() || key[0] == ':') continue;
        if (key == "host" || key == "connection") continue;
        if (key == "host") continue;

        for (HttpHeader::HeaderData::Values *v = hd->getfirstValue(); v; v = v->nextvalue()) {
          if (isConnectionSpecificHeader(key, v->getvalue())) continue;
          if (key == "content-length") {
            include_content_length = true;
            content_length = static_cast<size_t>(std::stoul(v->getvalue()));
@@ -3273,6 +3294,9 @@ const std::vector<char> libhttppp::HttpClient::_doH1Request(
    size_t tries = 0;
    int redirects = 0;
    bool convertedToGet = false;
    // maxTries == 0 used to retry forever: a permanent failure (protocol error, server
    // that always resets) then hung the caller regardless of any timeout
    if (maxTries == 0) maxTries = 3;

    for (;;) {
        try {
+41 −0
Changes for test/http2_checksum_test.cpp: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ public:
        for (auto &h : headers) {
            if (h.name == ":path") { path = h.value; break; }
        }

        if (path != "/stream") return false;

        std::lock_guard<std::mutex> lk(_mu);
@@ -137,9 +138,27 @@ public:
        }
    }

    // header-hygiene probe, see main(): records what arrived for /gen/7
    void RequestEvent(libhttppp::HttpRequest &req, const int tid, ULONG_PTR args) override {
        if (req.getRequestURL() == "/gen/7") {
            std::lock_guard<std::mutex> lk(_mu);
            _probeHeaders.clear();
            for (auto *hd = req.getfirstHeaderData(); hd; hd = hd->nextHeaderData())
                for (auto *v = hd->getfirstValue(); v; v = v->nextvalue())
                    _probeHeaders.push_back(hd->getkey() + "=" + v->getvalue());
        }
        ChecksumEvent::RequestEvent(req, tid, args);
    }

    std::vector<std::string> probeHeaders() {
        std::lock_guard<std::mutex> lk(_mu);
        return _probeHeaders;
    }

private:
    std::mutex _mu;
    std::map<uint32_t, std::string> _streamBodies;
    std::vector<std::string> _probeHeaders;
};

netplus::ssl::CertificateBundle buildTestCertBundle() {
@@ -436,6 +455,28 @@ int main() {
            libhttppp::HttpUrl url("https://127.0.0.1:" + std::to_string(kTlsPort) + "/");
            libhttppp::HttpClient httpClt(url, /*vers=*/2, /*timeoutSec=*/5);

            // HTTP/1-only header fields never go out on h2 (RFC 9113 8.2.2): a caller
            // that adds "connection: close" must still be able to talk to strict peers
            {
                libhttppp::HttpRequest req;
                req.setRequestURL("/gen/7");
                req.setHeaderData("connection")->push_back("close");
                req.setHeaderData("keep-alive")->push_back("timeout=5");
                req.setHeaderData("upgrade")->push_back("h2c");
                req.setHeaderData("te")->push_back("trailers");
                req.setHeaderData("x-probe")->push_back("1");
                std::vector<char> body = httpClt.Get(req, /*maxTries=*/1);
                check(httpClt.lastStatusCode() == 200 && body.size() == 7,
                      "HttpClient h2: request with connection-specific headers succeeds");
                std::string seen;
                for (auto &h : server->probeHeaders()) seen += h + ";";
                check(!seen.empty() && seen.find("connection=") == std::string::npos && seen.find("keep-alive=") == std::string::npos &&
                          seen.find("upgrade=") == std::string::npos,
                      "HttpClient h2: connection/keep-alive/upgrade not sent");
                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");
            }

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