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

added h1 offload test

parent 7117d188
Loading
Loading
Loading
Loading
+12 −0
Changes for test/CMakeLists.txt: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,18 @@ endif()

add_test(httpd_h1_offload_test httpd_h1_offload_test)

add_executable(httpd_h1_deferred_stream_test
    httpd_h1_deferred_stream_test.cpp
)

if(WIN32)
    target_link_libraries (httpd_h1_deferred_stream_test httppp-static ws2_32)
else()
    target_link_libraries (httpd_h1_deferred_stream_test httppp-static)
endif()

add_test(httpd_h1_deferred_stream_test httpd_h1_deferred_stream_test)

add_executable(httpd_h2_window_credit_race_test
    httpd_h2_window_credit_race_test.cpp
)
+215 −0
Changes for test/httpd_h1_deferred_stream_test.cpp: 215 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
Copyright (c) 2026, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

// Regression test for a deferred/streaming response (RequestEvent calling
// HttpResponse::send() with a negative datalen, then producing the body
// across several later ResponseEvent calls) over an H1-*offloaded* connection.
//
// H2/H3 self-drive this pattern via _resumeH2Streams' WINDOW_UPDATE handling
// (see httpd_h2_window_credit_race_test.cpp for that side). Plain HTTP/1.x has
// no such mechanism: before markDeferredIfStreaming()/the ResponsePending loop
// in _dispatchH1Request (http.cpp / httpd.cpp), a headers-only send(-1) over
// H1 offload left the connection reattached with its body never sent and
// ResponseEvent never called again -- the client would hang waiting for a
// body that was never coming, discoverable only via a caller's own idle
// timeout (see blogi's plugins/media/media.cpp STREAM_IDLE_TIMEOUT reaper,
// the real-world bug this test is modeled on).
//
// This fails (client times out waiting for the body) against the pre-fix
// code and passes (receives the full body, in several chunks) against the
// fix.

#include <chrono>
#include <cstring>
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>
#include <unordered_map>

#include <netplus/socket.h>

#include "http.h"
#include "httpd.h"
#include "raw_tcp_client.h"

namespace {

// Total body size and per-ResponseEvent chunk size -- several chunks needed,
// so a single (accidental, spurious) extra ResponseEvent call couldn't
// coincidentally finish the body and mask a real regression.
constexpr size_t kTotalBody = 5000;
constexpr size_t kChunkSize = 1000;

class H1DeferredStreamServer : public libhttppp::HttpEvent {
public:
    explicit H1DeferredStreamServer(std::vector<netplus::socket*> serversocket)
        : HttpEvent(serversocket, 1000, /*h2OffloadThreads=*/0, /*idleTimeoutSeconds=*/0,
                    /*h1OffloadThreads=*/4) {}

    bool shouldOffloadH1Dispatch(libhttppp::HttpRequest &) const override { return true; }

    void RequestEvent(libhttppp::HttpRequest &req, const int, ULONG_PTR) override {
        std::lock_guard<std::mutex> lk(_mtx);
        _produced[reinterpret_cast<uintptr_t>(&req)] = 0;

        libhttppp::HttpResponse res;
        res.setContentType("application/octet-stream");
        res.setContentLength(kTotalBody);
        res.send(req, "", -1); // headers only -- body arrives via ResponseEvent below
    }

    void ResponseEvent(libhttppp::HttpRequest &req, const int, ULONG_PTR) override {
        std::lock_guard<std::mutex> lk(_mtx);
        auto it = _produced.find(reinterpret_cast<uintptr_t>(&req));
        if (it == _produced.end()) return;
        size_t &produced = it->second;
        if (produced >= kTotalBody) return;

        size_t chunk = (std::min)(kChunkSize, kTotalBody - produced);
        // Content is deterministic (byte value == its offset mod 251, a prime
        // close to 256) so the client can verify the full body arrived intact
        // and in order, not just the right length.
        std::string data(chunk, '\0');
        for (size_t i = 0; i < chunk; ++i)
            data[i] = static_cast<char>((produced + i) % 251);
        req.SendData.append(data.data(), data.size());
        produced += chunk;

        if (produced < kTotalBody)
            req.slots[0].ResponsePending.store(true, std::memory_order_release);
    }

private:
    std::mutex _mtx;
    std::unordered_map<uintptr_t, size_t> _produced;
};

struct RunningServer {
    std::unique_ptr<H1DeferredStreamServer> server;
    std::thread thread;
};

RunningServer startServer(int &port) {
    auto sock = std::make_unique<netplus::tcp>("127.0.0.1", 0, 128, -1);
    sock->bind();
    port = rawtcp::boundPort(*sock);
    sock->listen();
    netplus::tcp *sockPtr = sock.get();
    static std::vector<std::unique_ptr<netplus::tcp>> keepAlive;
    keepAlive.push_back(std::move(sock));

    RunningServer rs;
    rs.server = std::make_unique<H1DeferredStreamServer>(
        std::vector<netplus::socket*>{sockPtr});
    netplus::event::Running = true;
    H1DeferredStreamServer *serverPtr = rs.server.get();
    rs.thread = std::thread([serverPtr] { serverPtr->runEventloop(); });
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
    return rs;
}

void stopServer(RunningServer &rs) {
    netplus::event::requestStop();
    if (rs.thread.joinable())
        rs.thread.join();
}

std::string doGet(netplus::tcp &sock, const std::string &path, std::chrono::seconds timeout) {
    std::ostringstream req;
    req << "GET " << path << " HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n";
    rawtcp::sendAllOrThrow(sock, req.str());

    std::string buf;
    auto deadline = std::chrono::steady_clock::now() + timeout;
    for (;;) {
        size_t headerEnd = buf.find("\r\n\r\n");
        if (headerEnd != std::string::npos) {
            size_t clPos = buf.find("content-length:");
            if (clPos == std::string::npos || clPos > headerEnd)
                throw std::runtime_error("doGet: no content-length in response");
            size_t clStart = clPos + std::strlen("content-length:");
            size_t clEnd = buf.find("\r\n", clStart);
            size_t contentLength = std::stoul(buf.substr(clStart, clEnd - clStart));
            size_t bodyStart = headerEnd + 4;
            if (buf.size() >= bodyStart + contentLength)
                return buf.substr(bodyStart, contentLength);
        }
        if (std::chrono::steady_clock::now() > deadline)
            throw std::runtime_error("doGet: timed out waiting for response");
        netplus::buffer rbuf(4096);
        size_t n = sock.recvData(rbuf);
        if (n == 0) throw std::runtime_error("doGet: connection closed early");
        buf.append(rbuf.data.buf, n);
    }
}

} // namespace

int main() {
    int failures = 0;
    int port = 0;
    RunningServer rs = startServer(port);

    try {
        netplus::tcp sock;
        rawtcp::connectLocal(sock, port);
        std::string body = doGet(sock, "/stream", std::chrono::seconds(5));
        sock.close();

        if (body.size() != kTotalBody) {
            std::cerr << "FAIL: expected " << kTotalBody << " bytes, got "
                      << body.size() << std::endl;
            ++failures;
        } else {
            bool contentOk = true;
            for (size_t i = 0; i < body.size(); ++i) {
                if (static_cast<unsigned char>(body[i]) != (i % 251)) {
                    contentOk = false;
                    break;
                }
            }
            if (!contentOk) {
                std::cerr << "FAIL: body received but content corrupted/out of order"
                          << std::endl;
                ++failures;
            } else {
                std::cout << "[deferred-h1] received all " << kTotalBody
                          << " bytes across multiple ResponseEvent calls, in order"
                          << std::endl;
            }
        }
    } catch (std::exception &e) {
        std::cerr << "FAIL: " << e.what() << std::endl;
        ++failures;
    }

    stopServer(rs);

    std::cout << (failures == 0 ? "PASS" : "FAIL") << std::endl;
    return failures == 0 ? 0 : 1;
}