From c909462935ad1aad41976ed5de0665cdc0a5ad7b Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:26:38 +0000 Subject: [PATCH 1/6] HttpServer: add a minimal Qt-free HTTP/1.1 server on AbstractServerSocket A minimal HTTP/1.1 server for a single local client that issues one request at a time (e.g. an MCP client), built on the socket library instead of Qt: - One connection at a time (the server socket's model); requests are processed in order, so pipelined bytes simply wait in the buffer. - The per-connection task does all parsing and writing; the socket's receive callbacks only append data and flip flags. The user handler may block on another thread (e.g. the program thread) and can poll HandlerContext::connection_alive() to bail out of long-running work; responses are discarded if the connection dies meanwhile. - Content-Length bodies only (chunked -> 411), sizes capped (8 KB request line, 64 KB headers, 1 MiB body, 2 MiB buffer), "Expect: 100-continue" answered, strict request/header validation (400), responses always carry Content-Length. - start()/stop() are fully synchronous from the caller's point of view once on_listen_finished has fired: stop() closes the connection, waits for the in-flight handler, and joins the socket's worker threads, so the server can be started again afterwards. No TLS, no upgrades, no method routing or auth: the user handler owns all of that. 10 loopback tests (raw ClientSocket clients) cover the happy path, keep-alive, pipelining, Connection: close, 400/411/413, 100-continue, disconnect and stop() during a handler, and second-client rejection. Part of the groundwork for the in-app MCP server; stacks on the ServerSocket factory alias PR. --- Common/Cpp/HttpServer/HttpServer.cpp | 636 ++++++++++ Common/Cpp/HttpServer/HttpServer.h | 241 ++++ Common/Cpp/HttpServer/HttpServer_Tests.cpp | 1033 +++++++++++++++++ Common/Cpp/HttpServer/HttpServer_Tests.h | 28 + .../ComputerPrograms_UnitTestRunner.cpp | 2 + SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 1944 insertions(+) create mode 100644 Common/Cpp/HttpServer/HttpServer.cpp create mode 100644 Common/Cpp/HttpServer/HttpServer.h create mode 100644 Common/Cpp/HttpServer/HttpServer_Tests.cpp create mode 100644 Common/Cpp/HttpServer/HttpServer_Tests.h diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp new file mode 100644 index 0000000000..faa1c2b3ec --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -0,0 +1,636 @@ +/* HTTP Server + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/HttpServer/HttpServer.h" +#include "Common/Cpp/Sockets/ServerSocket.h" + +namespace PokemonAutomation{ + +namespace{ + + +constexpr size_t MAX_REQUEST_LINE_BYTES = 8192; +constexpr size_t MAX_HEADER_SECTION_BYTES = 65536; +constexpr size_t MAX_HEADER_COUNT = 100; +constexpr size_t MAX_BODY_BYTES = 1u << 20; // 1 MiB +// Headers + a full body + room for a few pipelined requests. +constexpr size_t MAX_BUFFER_BYTES = 2u << 20; + +// A stalled peer is declared dead after this; loopback writes of a few MB +// finish in milliseconds, so this is very generous. +constexpr std::chrono::milliseconds WRITE_TIMEOUT{30000}; + + +bool is_token_char(char c){ + return std::isalnum((unsigned char)c) != 0 + || std::strchr("-!#$%&'*+.^_`|~", c) != nullptr; +} + + +std::string to_lower(std::string value){ + std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c){ + return (char)std::tolower(c); + }); + return value; +} + + +// Strips the leading and trailing spaces and tabs (OWS). +std::string trim_ows(std::string value){ + static const char OWS[] = " \t"; + const auto begin = value.find_first_not_of(OWS); + if (begin == std::string::npos){ + return ""; + } + const auto end = value.find_last_not_of(OWS); + return value.substr(begin, end - begin + 1); +} + + + +} // anonymous namespace + + + +const char* HttpServer::status_reason(int status){ + switch (status){ + case 100: + return "Continue"; + case 200: + return "OK"; + case 201: + return "Created"; + case 204: + return "No Content"; + case 301: + return "Moved Permanently"; + case 302: + return "Found"; + case 304: + return "Not Modified"; + case 400: + return "Bad Request"; + case 401: + return "Unauthorized"; + case 403: + return "Forbidden"; + case 404: + return "Not Found"; + case 405: + return "Method Not Allowed"; + case 408: + return "Request Timeout"; + case 411: + return "Length Required"; + case 413: + return "Payload Too Large"; + case 426: + return "Upgrade Required"; + case 500: + return "Internal Server Error"; + case 501: + return "Not Implemented"; + case 503: + return "Service Unavailable"; + case 505: + return "Version Not Supported"; + default: + return "Unknown Status"; + } +} + + +std::string HttpServer::format_response(const Response& response, bool keep_alive){ + std::string out; + out += "HTTP/1.1 "; + out += std::to_string(response.status); + out += " "; + const char* reason = response.reason.empty() ? status_reason(response.status) : response.reason.c_str(); + out += reason; + out += "\r\n"; + + for (const auto& header : response.headers){ + // The server sets both of these below. + if (to_lower(header.first) == "content-length" || + to_lower(header.first) == "connection" + ){ + continue; + } + out += header.first; + out += ": "; + out += header.second; + out += "\r\n"; + } + + out += "Content-Length: "; + out += std::to_string(response.body.size()); + out += "\r\n"; + out += "Connection: "; + out += keep_alive ? "keep-alive" : "close"; + out += "\r\n\r\n"; + out += response.body; + + return out; +} + + +const char* HttpServer::Request::header(const std::string& name) const{ + const auto it = headers.find(to_lower(name)); + return (it == headers.end()) ? "" : it->second.c_str(); +} + + +bool HttpServer::HandlerContext::connection_alive() const{ + return !m_dead.load(std::memory_order_acquire); +} + + + +HttpServer::HttpServer(ThreadPool& thread_pool) + : m_thread_pool(thread_pool) +{} + + +HttpServer::~HttpServer(){ + stop(); +} + + +void HttpServer::start(const std::string& address, uint16_t port, Handler handler){ + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + if (m_socket != nullptr){ + return; + } + m_handler = std::move(handler); + m_stop_requested.store(false, std::memory_order_release); + m_socket = std::make_unique(m_thread_pool); + // Register before start(): the accept thread is dispatched by + // start() and may report on_listen_finished() as soon as it runs. + m_socket->add_listener(*this); + socket = m_socket.get(); + } + + // Asynchronous: on_listen_finished() fires on a thread from the pool. + socket->start(address, port); +} + + +void HttpServer::stop(){ + m_stop_requested.store(true, std::memory_order_release); + + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + socket = m_socket.get(); + } + if (socket == nullptr){ + return; + } + + // Kill the active connection, if any: it cancels in-flight writes and + // makes the socket's connection thread exit. The socket keeps + // listening until it is destroyed below, so it may accept a few more + // clients in the meantime; on_connect() sees m_stop_requested and + // closes them without dispatching a connection task. + socket->close_connection(); + + // Wait for the in-flight connection task. Loop, because a task may + // have been dispatched between the previous one being consumed and + // the socket being destroyed. + while (true){ + AsyncTask task; + { + std::lock_guard lg(m_lock); + task = std::move(m_connection_task); + m_connection_task = AsyncTask(); + } + if (!task){ + break; + } + task.wait_and_ignore_exceptions(); + } + + { + std::lock_guard lg(m_lock); + m_socket = nullptr; + m_connection = nullptr; + m_handler = Handler(); + } +} + + +bool HttpServer::is_running() const{ + std::lock_guard lg(m_lock); + return (m_socket != nullptr); +} + + + +void HttpServer::on_listen_finished(const std::string& error_message){ + m_listeners.run_method(&Listener::on_listen_finished, error_message); +} + + +void HttpServer::on_connect(){ + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + if (m_stop_requested.load(std::memory_order_acquire)){ + // Stopping: close the connection without a connection task. + socket = m_socket.get(); + }else{ + auto conn = std::make_shared(); + m_connection = conn; + try{ + m_connection_task = m_thread_pool.dispatch( + [this, conn, socket = m_socket.get()]{ + connection_task(conn, socket); + } + ); + }catch (...){ + // The task could not be started: drop the connection and + // keep listening. + m_connection.reset(); + socket = m_socket.get(); + } + } + } + if (socket != nullptr){ + socket->close_connection(); + } + m_listeners.run_method(&Listener::on_connect); +} + + +void HttpServer::on_connect_rejected(){ + m_listeners.run_method(&Listener::on_connect_rejected); +} + + +void HttpServer::on_receive_data(const void* data, size_t bytes){ + std::shared_ptr conn; + { + std::lock_guard lg(m_lock); + conn = m_connection; + } + if (conn == nullptr){ + return; + } + { + std::lock_guard lg(conn->lock); + const char* ptr = (const char*)data; + conn->buffer.append(ptr, bytes); + } + conn->cv.notify_all(); +} + + +void HttpServer::on_disconnect(const std::string& error_message){ + std::shared_ptr conn; + { + std::lock_guard lg(m_lock); + conn = m_connection; + } + if (conn != nullptr){ + conn->dead.store(true, std::memory_order_release); + conn->cv.notify_all(); + } + m_listeners.run_method(&Listener::on_disconnect, error_message); +} + + + +void HttpServer::connection_task(const std::shared_ptr& conn, AbstractServerSocket* socket){ + // The handler may be replaced or cleared by start()/stop() while this + // task is waiting; a copy taken under m_lock is stable for the task's + // whole lifetime, because stop() waits for the task before clearing it. + Handler handler; + { + std::lock_guard lg(m_lock); + handler = m_handler; + } + + while (true){ + if (m_stop_requested.load(std::memory_order_acquire) || + conn->dead.load(std::memory_order_acquire) + ){ + break; + } + + ParsedRequest parsed; + { + std::lock_guard lg(conn->lock); + parsed = try_parse(conn->buffer); + } + + if (parsed.state == ParseState::NEED_MORE){ + // Wait for the rest of this request, a disconnect, or a stop. + { + std::unique_lock lg(conn->lock); + conn->cv.wait_for(lg, std::chrono::milliseconds(10), [this, &conn]{ + return !conn->buffer.empty() + || conn->dead.load(std::memory_order_relaxed) + || m_stop_requested.load(std::memory_order_relaxed); + }); + } + continue; + } + + if (parsed.state == ParseState::ERROR){ + Response response; + response.status = parsed.error_status; + response.headers = std::move(parsed.error_headers); + response.body = parsed.error_reason + "\n"; + write_response(conn, socket, response, false); + end_connection(conn, socket); + break; + } + + // A complete request is ready to be handled. + if (parsed.expect_continue){ + static const char CONTINUE_RESPONSE[] = "HTTP/1.1 100 Continue\r\n\r\n"; + if (!write_raw(conn, socket, CONTINUE_RESPONSE)){ + break; + } + } + + Response response; + { + HandlerContext context(conn->dead); + try{ + if (handler){ + handler(parsed.request, response, context); + }else{ + response.status = 500; + response.body = "No handler is registered.\n"; + response.connection_close = true; + } + }catch (...){ + response = Response(); + response.status = 500; + response.body = "The handler threw an exception.\n"; + response.connection_close = true; + } + } + + // The connection may have closed while the handler was running; + // discard the response in that case. + if (conn->dead.load(std::memory_order_acquire) || + m_stop_requested.load(std::memory_order_acquire) + ){ + break; + } + + const bool keep_alive = parsed.http_1_1 + && !parsed.connection_close_requested + && !response.connection_close; + if (!write_response(conn, socket, response, keep_alive)){ + break; + } + if (!keep_alive){ + end_connection(conn, socket); + break; + } + // Keep-alive: the next request (possibly already buffered) is + // picked up at the top of the loop. + } + + conn->dead.store(true, std::memory_order_release); + { + std::lock_guard lg(m_lock); + if (m_connection.get() == conn.get()){ + m_connection.reset(); + } + } +} + + +HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ + ParsedRequest result; + + auto error = [&result](int status, const char* reason, std::vector> headers = {}){ + result.state = ParseState::ERROR; + result.error_status = status; + result.error_reason = reason; + result.error_headers = std::move(headers); + }; + + // The header section ends at the blank line. "header_end" points at + // the "\r\n" that terminates the last header (or the request line + // when there are no headers). + const size_t header_end = buffer.find("\r\n\r\n"); + if (header_end == std::string::npos){ + if (buffer.size() > MAX_HEADER_SECTION_BYTES){ + error(400, "Malformed request."); + } + return result; // NEED_MORE + } + + const size_t line_end = buffer.find("\r\n"); + if (line_end == std::string::npos){ + error(400, "Malformed request."); + return result; + } + if (line_end > MAX_REQUEST_LINE_BYTES){ + error(400, "Request line too long."); + return result; + } + + // "METHOD SP TARGET SP VERSION" + const std::string line = buffer.substr(0, line_end); + const size_t sp1 = line.find(' '); + if (sp1 == std::string::npos || sp1 == 0){ + error(400, "Malformed request."); + return result; + } + std::string method = line.substr(0, sp1); + for (char c : method){ + if (c < 'A' || c > 'Z'){ + error(400, "Malformed request."); + return result; + } + } + const size_t sp2 = line.find(' ', sp1 + 1); + if (sp2 == std::string::npos){ + error(400, "Malformed request."); + return result; + } + std::string target = line.substr(sp1 + 1, sp2 - sp1 - 1); + if (target.empty()){ + error(400, "Malformed request."); + return result; + } + for (char c : target){ + if (c == ' ' || c == '\r' || c == '\n' || c == '\0'){ + error(400, "Malformed request."); + return result; + } + } + const std::string version = line.substr(sp2 + 1); + const bool http_1_1 = (version == "HTTP/1.1"); + const bool http_1_0 = (version == "HTTP/1.0"); + if (!http_1_1 && !http_1_0){ + error(505, "HTTP version not supported."); + return result; + } + + // Headers: [line_end + 2, header_end) + const size_t section_begin = line_end + 2; + std::string section; + if (header_end >= section_begin){ + section = buffer.substr(section_begin, header_end - section_begin); + } + size_t header_count = 0; + size_t pos = 0; + while (pos < section.size()){ + const size_t eol = section.find("\r\n", pos); + const size_t length = (eol == std::string::npos) ? section.size() - pos : eol - pos; + const std::string header_line = section.substr(pos, length); + + if (header_line.empty() || ++header_count > MAX_HEADER_COUNT){ + error(400, "Malformed request."); + return result; + } + + const size_t colon = header_line.find(':'); + if (colon == std::string::npos || colon == 0){ + error(400, "Malformed request."); + return result; + } + std::string name = header_line.substr(0, colon); + for (char c : name){ + if (!is_token_char(c)){ + error(400, "Malformed request."); + return result; + } + } + std::string value = trim_ows(header_line.substr(colon + 1)); + for (char c : value){ + if ((unsigned char)c < 0x20 || (unsigned char)c == 0x7F){ + error(400, "Malformed request."); + return result; + } + } + name = to_lower(std::move(name)); + if (name == "content-length" && result.request.headers.count("content-length") != 0){ + error(400, "Duplicate content-length."); + return result; + } + result.request.headers[std::move(name)] = std::move(value); + + if (eol == std::string::npos){ + break; + } + pos = eol + 2; + } + + // Special headers. + if (result.request.headers.count("transfer-encoding") != 0){ + error(411, "Chunked transfer encoding is not supported."); + return result; + } + + uint64_t content_length = 0; + { + const auto content_length_it = result.request.headers.find("content-length"); + if (content_length_it != result.request.headers.end()){ + const std::string& value = content_length_it->second; + if (value.empty() || value.size() > 10){ + error(400, "Malformed content-length."); + return result; + } + for (char c : value){ + if (c < '0' || c > '9'){ + error(400, "Malformed content-length."); + return result; + } + } + content_length = std::stoull(value); + if (content_length > MAX_BODY_BYTES){ + error(413, "Request body too large."); + return result; + } + } + } + + const auto connection_it = result.request.headers.find("connection"); + if (connection_it != result.request.headers.end() && + to_lower(connection_it->second).find("close") != std::string::npos + ){ + result.connection_close_requested = true; + } + + const auto expect_it = result.request.headers.find("expect"); + if (expect_it != result.request.headers.end() && + to_lower(expect_it->second).find("100-continue") != std::string::npos + ){ + result.expect_continue = true; + } + + const size_t consumed = header_end + 4 + (size_t)content_length; + if (buffer.size() < consumed){ + if (buffer.size() > MAX_BUFFER_BYTES){ + error(413, "Request too large."); + return result; + } + return result; // NEED_MORE: the body has not fully arrived yet. + } + + result.state = ParseState::READY; + result.request.method = std::move(method); + const size_t query = target.find('?'); + result.request.path = target.substr(0, query); + if (query != std::string::npos){ + result.request.query = target.substr(query + 1); + } + result.request.body = buffer.substr(header_end + 4, content_length); + buffer.erase(0, consumed); + result.http_1_1 = http_1_1; + return result; +} + + +void HttpServer::end_connection(const std::shared_ptr& conn, AbstractServerSocket* socket){ + conn->dead.store(true, std::memory_order_release); + conn->cv.notify_all(); + socket->close_connection(); +} + + +bool HttpServer::write_raw(const std::shared_ptr& conn, AbstractServerSocket* socket, const std::string& bytes){ + const size_t sent = socket->send(bytes.data(), bytes.size(), WRITE_TIMEOUT); + if (sent != bytes.size()){ + // The peer went away, or the write stalled: the connection is + // unusable either way. + end_connection(conn, socket); + return false; + } + return true; +} + + +bool HttpServer::write_response( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const Response& response, + bool keep_alive +){ + return write_raw(conn, socket, format_response(response, keep_alive)); +} + + + +} diff --git a/Common/Cpp/HttpServer/HttpServer.h b/Common/Cpp/HttpServer/HttpServer.h new file mode 100644 index 0000000000..d34de29834 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer.h @@ -0,0 +1,241 @@ +/* HTTP Server + * + * From: https://github.com/PokemonAutomation/ + * + * A minimal HTTP/1.1 server built on top of "AbstractServerSocket", aimed + * at a single local client that issues one request at a time (for example, + * an MCP client talking to the AI Agent Server). + * + * Design notes: + * - One connection at a time: the underlying server socket handles one + * connection at a time, and a second client gets "on_connect_rejected". + * - Requests are processed strictly in order. Bytes of a pipelined request + * simply wait in the buffer until the previous response has been sent. + * - The user "Handler" may block for a long time; it runs on a thread from + * the given thread pool. Poll "HandlerContext::connection_alive()" + * between long-running steps to bail out early. Once it returns false, + * the response you set is discarded. + * - No TLS. Request bodies: "Content-Length" only (chunked is rejected + * with 411). Response bodies: always "Content-Length" (no chunked). + * No upgrades. + * + * Threading: + * - All "Listener" callbacks run on threads from the thread pool, never + * on the calling thread. + * - Do not call "start()" or "stop()" from within a "Listener" callback: + * "stop()" joins the very pool threads that fire the callbacks. + * - The thread pool should be an unlimited, long-lived one (for example + * "GlobalThreadPools::unlimited_normal()"): the server keeps an accept + * thread and a per-connection thread alive for as long as it runs. + * + */ + +#ifndef PokemonAutomation_HttpServer_H +#define PokemonAutomation_HttpServer_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/ListenerSet.h" +#include "Common/Cpp/Sockets/AbstractServerSocket.h" + +namespace PokemonAutomation{ + +class ThreadPool; + + + +class HttpServer : private AbstractServerSocket::Listener{ +public: + struct Request{ + std::string method; // e.g. "POST" + std::string path; // e.g. "/mcp" (without the query) + std::string query; // the part after '?', if any + std::map headers; // names are lower-cased + std::string body; + + // "name" is lower-cased first; returns "" if the header is absent. + const char* header(const std::string& name) const; + }; + + struct Response{ + int status = 200; + std::string reason; // empty = the standard phrase for "status" + std::vector> headers; + std::string body; + + // End the connection after this response, even on HTTP/1.1. + bool connection_close = false; + }; + + // Passed to the handler for the duration of the call. + struct HandlerContext{ + // False once the connection this request came in on is closed or + // the server is stopping. Once false, the response is discarded + // even if you set one. + bool connection_alive() const; + + private: + explicit HandlerContext(const std::atomic& p_dead) + : m_dead(p_dead) + {} + + const std::atomic& m_dead; + + friend class HttpServer; + }; + + // Runs on a thread from the thread pool, never on the socket's + // connection thread. It may block (for example, while a tool runs on + // the program thread), but it must eventually return: "stop()" blocks + // until the in-flight handler returns. It must not throw; exceptions + // are turned into a 500 response. + using Handler = std::function; + + struct Listener{ + // Called after "start()" finishes. If successful, "error_message" + // is empty. Otherwise it contains the error message and the server + // is not listening; call "stop()" to clean up before retrying. + virtual void on_listen_finished(const std::string& error_message){} + + // Called when a client connects. + virtual void on_connect(){} + + // Called when a second client tries to connect while one is + // already connected; that client is immediately closed. + virtual void on_connect_rejected(){} + + // Called when the client disconnects (a server-side "stop()" does + // not report a disconnect). + virtual void on_disconnect(const std::string& error_message){} + }; + + void add_listener(Listener& listener){ + m_listeners.add(listener); + } + void remove_listener(Listener& listener){ + m_listeners.remove(listener); + } + + +public: + explicit HttpServer(ThreadPool& thread_pool); + ~HttpServer(); + + // Starts listening. Asynchronous: "Listener::on_listen_finished" is + // called on a thread from the thread pool with the result. "handler" + // receives every fully parsed request; if it is null, all requests + // get a 500. Returns without starting if the server is already + // started. + void start(const std::string& address, uint16_t port, Handler handler); + + // Stops the server and blocks until it is fully down: the active + // connection is closed, the in-flight handler (if any) is waited for, + // and the socket's worker threads are joined. Afterwards "start()" + // can be called again. + void stop(); + + // True if "start()" has been called and the server has not been + // stopped again. + bool is_running() const; + + +private: + // AbstractServerSocket::Listener. + virtual void on_listen_finished(const std::string& error_message) override; + virtual void on_connect() override; + virtual void on_connect_rejected() override; + virtual void on_receive_data(const void* data, size_t bytes) override; + virtual void on_disconnect(const std::string& error_message) override; + + // One request that is waiting to be parsed out of the buffer. + enum class ParseState{ + NEED_MORE, + READY, + ERROR, + }; + + struct ParsedRequest{ + ParseState state = ParseState::NEED_MORE; + + // ERROR + int error_status = 400; + std::string error_reason; + std::vector> error_headers; + + // READY + Request request; + bool http_1_1 = true; + bool connection_close_requested = false; + bool expect_continue = false; + }; + + // Per-connection state. The connection task is the only thread that + // parses or writes; "on_receive_data"/"on_disconnect" only append data + // and flip flags. + struct Connection{ + Mutex lock; + ConditionVariable cv; + std::string buffer; + std::atomic dead{false}; + }; + + // Runs on a thread from the thread pool, one instance per connection. + void connection_task(const std::shared_ptr& conn, AbstractServerSocket* socket); + + // Parses one complete request out of "buffer", removing it on success. + // Must be called with "conn->lock" held. + static ParsedRequest try_parse(std::string& buffer); + + // Marks the connection dead and closes it at the socket level. No-op + // if the socket is no longer connected to this client. + void end_connection(const std::shared_ptr& conn, AbstractServerSocket* socket); + + // Writes "bytes" to the connection completely; returns false (and + // ends the connection) if the peer went away or the write stalled. + bool write_raw(const std::shared_ptr& conn, AbstractServerSocket* socket, const std::string& bytes); + + // Formats and writes the final response. + bool write_response( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const Response& response, + bool keep_alive + ); + + // The standard reason phrase for "status", or "Unknown Status". + static const char* status_reason(int status); + + // Builds the wire format of a response. + static std::string format_response(const Response& response, bool keep_alive); + + ThreadPool& m_thread_pool; + ListenerSet m_listeners; + + // Guards m_socket, m_connection, m_connection_task and m_handler. + // Never held while waiting on a condition variable. Mutable so that + // "is_running()" can take it from a const method. + mutable Mutex m_lock; + std::unique_ptr m_socket; + std::shared_ptr m_connection; + AsyncTask m_connection_task; + Handler m_handler; + + // Set by "stop()"; checked by the connection tasks between requests + // and by "HandlerContext". + std::atomic m_stop_requested{false}; +}; + + + +} +#endif diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.cpp b/Common/Cpp/HttpServer/HttpServer_Tests.cpp new file mode 100644 index 0000000000..3410c0e612 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer_Tests.cpp @@ -0,0 +1,1033 @@ +/* HTTP Server Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Loopback tests that run a real "HttpServer" (the platform's default + * server socket backend) on 127.0.0.1 and drive it with raw + * "ClientSocket" clients. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/HttpServer/HttpServer.h" +#include "Common/Cpp/Sockets/ClientSocket.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "HttpServer_Tests.h" + +namespace PokemonAutomation{ + +namespace{ + + +struct Waiter{ + Mutex lock; + ConditionVariable cv; + + bool wait_for(std::chrono::milliseconds timeout, const std::function& pred){ + std::unique_lock lg(lock); + return cv.wait_for(lg, timeout, pred); + } + + void notify(){ + std::lock_guard lg(lock); + cv.notify_all(); + } +}; + + +// Long enough that a moderately loaded machine can make it, short enough +// that a real regression is still reported within the CI timeouts. +constexpr std::chrono::seconds HTTP_TEST_WAIT{60}; + + +// A handler gate: handlers notify "started" when they begin, and block on +// "wait_for_release()" until the test releases them. +struct HandlerGate{ + Waiter started; + + Mutex lock; + ConditionVariable cv; + std::atomic released{false}; + + void wait_for_release(std::chrono::milliseconds timeout){ + std::unique_lock lg(lock); + cv.wait_for(lg, timeout, [this]{ + return released.load(std::memory_order_relaxed); + }); + } + + void release(){ + released.store(true, std::memory_order_release); + std::lock_guard lg(lock); + cv.notify_all(); + } +}; + + +// A raw TCP client that accumulates everything it receives. +struct TestClient{ + Waiter waiter; + std::string received; + std::atomic connected{0}; + std::atomic disconnected{0}; + std::string connect_error; + std::string disconnect_error; + + struct Listener : AbstractClientSocket::Listener{ + TestClient* client; + + virtual void on_connect_finished(const std::string& error_message) override{ + client->connect_error = error_message; + client->connected++; + client->waiter.notify(); + } + + virtual void on_receive_data(const void* data, size_t bytes) override{ + client->received.append((const char*)data, bytes); + client->waiter.notify(); + } + + virtual void on_disconnect(const std::string& error_message) override{ + client->disconnect_error = error_message; + client->disconnected++; + client->waiter.notify(); + } + } listener_impl; + + ClientSocket socket; + + explicit TestClient(ThreadPool& thread_pool) + : socket(thread_pool) + { + listener_impl.client = this; + socket.add_listener(listener_impl); + } + + bool connect(uint16_t port){ + socket.connect("127.0.0.1", port); + return waiter.wait_for( + HTTP_TEST_WAIT, + [this]{ return connected.load() >= 1; } + ); + } + + bool send_all(const std::string& bytes){ + size_t total = 0; + while (total < bytes.size()){ + const size_t sent = socket.send(bytes.data() + total, bytes.size() - total); + if (sent == 0){ + return false; + } + total += sent; + } + return true; + } + + bool wait_for_bytes(size_t n){ + return waiter.wait_for( + HTTP_TEST_WAIT, + [this, n]{ return received.size() >= n; } + ); + } +}; + + +struct TestServerListener : HttpServer::Listener{ + Waiter waiter; + std::atomic listen_finished{0}; + std::string last_listen_error; + std::atomic connected{0}; + std::atomic rejected{0}; + std::atomic disconnected{0}; + + virtual void on_listen_finished(const std::string& error_message) override{ + last_listen_error = error_message; + listen_finished++; + waiter.notify(); + } + + virtual void on_connect() override{ + connected++; + waiter.notify(); + } + + virtual void on_connect_rejected() override{ + rejected++; + waiter.notify(); + } + + virtual void on_disconnect(const std::string& error_message) override{ + disconnected++; + waiter.notify(); + } +}; + + +class TestHttpServer : public UnitTest{ +public: + TestHttpServer(ThreadPool& thread_pool, std::string name) + : UnitTest(std::move(name)) + , m_thread_pool(thread_pool) + { + m_threads = 2; + } + + +protected: + // Starts the server with "handler" on a free loopback port, waiting + // for the listen to finish. "handler" is copied on each attempt. + bool start_server(HttpServer& server, TestServerListener& listener, HttpServer::Handler handler, uint16_t& port) const{ + for (int attempt = 0; attempt < 8; attempt++){ + uint16_t candidate; + { + std::lock_guard lg(m_rng_lock); + candidate = (uint16_t)(40000 + m_rng() % 20000); + } + const int finished_before = listener.listen_finished.load(); + server.start("127.0.0.1", candidate, handler); + if (!listener.waiter.wait_for( + HTTP_TEST_WAIT, + [finished_before, &listener]{ + return listener.listen_finished.load() > finished_before; + } + )){ + server.stop(); + continue; + } + if (listener.last_listen_error.empty()){ + port = candidate; + return true; + } + // A failed listen leaves the server "started but not + // listening"; stop() cleans that up before the next attempt. + server.stop(); + } + return false; + } + +protected: + ThreadPool& m_thread_pool; + +private: + mutable Mutex m_rng_lock; + mutable std::mt19937 m_rng{std::random_device{}()}; +}; + + +// A POST request is parsed (method, path, query, headers, body) and the +// response carries the extra headers and the exact body; the connection +// stays keep-alive. +class Test_Http_PostEcho : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.headers.push_back({"X-Test", "yes"}); + response.body = request.path + "|" + request.query + "|" + request.header("X-Client") + "|" + request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else if (!client.send_all( + "POST /mcp?session=abc HTTP/1.1\r\n" + "Host: 127.0.0.1\r\n" + "X-Client: unit-test\r\n" + "Content-Length: 5\r\n" + "\r\n" + "hello" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "X-Test: yes\r\n" + "Content-Length: 32\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "/mcp|session=abc|unit-test|hello"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive the full response"; + }else if (client.received != expected){ + error = "Bad response:\n" + client.received; + } + } + // The client goes out of scope here and closes the socket. + } + + if (error.empty() && + !server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + ) + ){ + error = "The server did not call on_disconnect"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// Two requests sent back to back on one connection (the second is already +// buffered while the first is being answered): both must be answered, in +// order. +class Test_Http_PipelinedKeepAlive : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "r" + request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST /a HTTP/1.1\r\n" + "Content-Length: 3\r\n" + "\r\n" + "one" + "POST /b HTTP/1.1\r\n" + "Content-Length: 3\r\n" + "\r\n" + "two" + )){ + error = "The client did not send the requests"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "rone" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "rtwo"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive both responses (got " + std::to_string(client.received.size()) + " of " + std::to_string(expected.size()) + " bytes)"; + }else if (client.received != expected){ + error = "Bad response:\n" + client.received; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "Connection: close" in the request: the response says so, the server +// closes the connection, and it keeps serving new clients. +class Test_Http_ConnectionClose : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "done"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "Connection: close\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: close\r\n" + "\r\n" + "done"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive the full response"; + }else if (client.received != expected){ + error = "Bad response:\n" + client.received; + }else if (!client.waiter.wait_for( + HTTP_TEST_WAIT, + [&client]{ return client.disconnected.load() >= 1; } + )){ + error = "The server did not close the connection"; + } + } + } + + if (error.empty()){ + // The server must keep listening for a new client. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "done"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "The reconnected client did not get a good response:\n" + client.received; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A request line that is not "METHOD SP TARGET SP VERSION" gets a 400 and +// a closed connection; the server recovers and serves a new client. +class Test_Http_BadRequest : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all("GARBAGE\r\n\r\n")){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 400 Bad Request\r\n" + "Content-Length: 19\r\n" + "Connection: close\r\n" + "\r\n" + "Malformed request.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "Bad error response:\n" + client.received; + } + } + } + + if (error.empty()){ + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "The server did not recover:\n" + client.received; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// Chunked request bodies are not supported: 411 and a closed connection, +// and the handler is never called. +class Test_Http_ChunkedRejected : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 411 Length Required\r\n" + "Content-Length: 44\r\n" + "Connection: close\r\n" + "\r\n" + "Chunked transfer encoding is not supported.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "Bad error response:\n" + client.received; + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A content-length above the body limit is rejected with 413 before the +// (never sent) body arrives, and the handler is never called. +class Test_Http_BodyTooLarge : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 2097152\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 413 Payload Too Large\r\n" + "Content-Length: 24\r\n" + "Connection: close\r\n" + "\r\n" + "Request body too large.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "Bad error response:\n" + client.received; + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "Expect: 100-continue" is answered with "100 Continue" before the final +// response. +class Test_Http_ExpectContinue : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "Expect: 100-continue\r\n" + "\r\n" + "hello" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 100 Continue\r\n\r\n" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 5\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "hello"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "Bad response:\n" + client.received; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// The client disconnects while the handler is still running: the handler +// sees it via "HandlerContext::connection_alive()", its response is +// discarded, and the server keeps serving new clients. +class Test_Http_DisconnectDuringHandler : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic call_count{0}; + std::atomic first_call_alive{false}; + std::atomic first_call_saw_disconnect{false}; + + auto handler = [&gate, &call_count, &first_call_alive, &first_call_saw_disconnect]( + const HttpServer::Request&, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + const int call = call_count.fetch_add(1) + 1; + if (call == 1){ + first_call_alive = context.connection_alive(); + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + first_call_saw_disconnect = !context.connection_alive(); + } + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_alive]{ return first_call_alive.load(); } + )){ + error = "The handler did not start"; + } + // The client goes out of scope here and closes the socket, + // while the handler is still blocked on the gate. + } + + if (error.empty() && + !server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + ) + ){ + error = "The server did not call on_disconnect"; + } + + if (error.empty()){ + gate.release(); + } + + if (error.empty()){ + // The server must keep serving new clients. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "The server did not recover:\n" + client.received; + } + } + } + + if (error.empty()){ + // Wait for the first handler to have recorded its observation. + if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_saw_disconnect, &call_count]{ + return call_count.load() >= 1 && first_call_saw_disconnect.load(); + } + )){ + error = "The handler did not record the disconnect"; + } + } + + if (error.empty()){ + if (!first_call_alive.load()){ + error = "The handler did not see the connection as alive on entry"; + }else if (!first_call_saw_disconnect.load()){ + error = "The handler did not see the disconnect via connection_alive()"; + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "stop()" while the handler is still running blocks until the handler +// returns, and "start()" works afterwards. +class Test_Http_StopDuringHandler : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic handler_started{false}; + + auto handler = [&gate, &handler_started]( + const HttpServer::Request&, + HttpServer::Response& response, + HttpServer::HandlerContext& + ){ + handler_started = true; + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + response.status = 200; + response.body = "late"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&handler_started]{ return handler_started.load(); } + )){ + error = "The handler did not start"; + } + } + + if (error.empty()){ + // stop() blocks until the in-flight handler returns. Release + // the handler from a pool thread so this thread can call it. + m_thread_pool.dispatch([&gate]{ + gate.release(); + }); + server.stop(); + if (server.is_running()){ + error = "The server is still running after stop()"; + } + } + + if (error.empty()){ + // Start again on the same port and serve a fresh request. + auto simple_handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + if (!start_server(server, server_listener, simple_handler, port)){ + error = "Failed to restart the server"; + }else{ + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect to the restarted server"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "The restarted server did not answer:\n" + client.received; + } + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// While one client is connected, a second one is rejected. +class Test_Http_RejectSecondClient : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient first(m_thread_pool); + if (!first.connect(port)){ + error = "The first client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else{ + TestClient second(m_thread_pool); + second.connect(port); // May complete at the TCP level; the server closes it. + if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.rejected.load() >= 1; } + )){ + error = "The server did not call on_connect_rejected"; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + + +} // anonymous namespace + + +void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool){ + database.add( + thread_pool, "HttpServer - POST request/response with headers, query and body" + ); + database.add( + thread_pool, "HttpServer - Two pipelined requests on one keep-alive connection" + ); + database.add( + thread_pool, "HttpServer - Connection: close and reconnection" + ); + database.add( + thread_pool, "HttpServer - Malformed request line" + ); + database.add( + thread_pool, "HttpServer - Chunked request bodies are rejected" + ); + database.add( + thread_pool, "HttpServer - Oversized content-length is rejected" + ); + database.add( + thread_pool, "HttpServer - Expect: 100-continue" + ); + database.add( + thread_pool, "HttpServer - Disconnect while the handler is running" + ); + database.add( + thread_pool, "HttpServer - stop() while the handler is running" + ); + database.add( + thread_pool, "HttpServer - A second client is rejected" + ); +} + + + +} diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.h b/Common/Cpp/HttpServer/HttpServer_Tests.h new file mode 100644 index 0000000000..6fe6c25cb1 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer_Tests.h @@ -0,0 +1,28 @@ +/* HTTP Server Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_HttpServer_Tests_H +#define PokemonAutomation_HttpServer_Tests_H + +namespace PokemonAutomation{ + +class ThreadPool; +class UnitTestDatabase; + + +// Loopback tests that run a real "HttpServer" (the platform's default +// server socket backend) on 127.0.0.1 and drive it with raw "ClientSocket" +// clients: request/response parsing, keep-alive, pipelining, +// "Connection: close", "Expect: 100-continue", error responses, and the +// lifecycle (disconnect during a handler, "stop()" during a handler, +// restart, second-client rejection). `thread_pool` runs the server and +// the test workers; it must be a long-lived pool (e.g. +// `GlobalThreadPools::unlimited_normal()`). +void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool); + + +} +#endif diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index d2a39b8cba..b09a6b3cdf 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -4,6 +4,7 @@ * */ +#include "Common/Cpp/HttpServer/HttpServer_Tests.h" #include "Common/Cpp/ListenerSet.h" #include "Common/Cpp/MoveOnlyFunction_Tests.h" #include "Common/Cpp/ScopeExit.h" @@ -56,6 +57,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal()); add_tests_Sockets(ret, GlobalThreadPools::unlimited_normal()); + add_tests_HttpServer(ret, GlobalThreadPools::unlimited_normal()); add_tests_BlackBorderDetector(ret); add_tests_ListenerSet(ret); add_tests_MoveOnlyFunction(ret); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index eb81069fbd..cc1596ddbd 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -97,6 +97,10 @@ file(GLOB CORE_LIBRARY_SOURCES ../Common/Cpp/Hardware/Hardware_x86_Linux.tpp ../Common/Cpp/Hardware/Hardware_x86_Windows.tpp ../Common/Cpp/HttpServer/AbstractHttpServer.h + ../Common/Cpp/HttpServer/HttpServer.cpp + ../Common/Cpp/HttpServer/HttpServer.h + ../Common/Cpp/HttpServer/HttpServer_Tests.cpp + ../Common/Cpp/HttpServer/HttpServer_Tests.h ../Common/Cpp/ImageResolution.cpp ../Common/Cpp/ImageResolution.h ../Common/Cpp/Json/JsonArray.cpp From 190f0af5892d93552658cc8b072d4701721ddece Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Tue, 10 Nov 2026 01:02:31 +0000 Subject: [PATCH 2/6] HttpServer: fix CI build failures - Rename ParseState::ERROR to ParseState::ERROR_RESPONSE: ERROR is a macro on Windows (winerror.h), which broke the qualified name on MSVC and clang-cl (C2589). - Keep the AsyncTask handle from ThreadPool::dispatch in the stop-during-handler test: dispatch is [[nodiscard]] and clang's -Wunused-result (CI's -Werror) rejects discarding it. Its destructor also waits for the trivial release task to finish. --- Common/Cpp/HttpServer/HttpServer.cpp | 4 ++-- Common/Cpp/HttpServer/HttpServer.h | 6 ++++-- Common/Cpp/HttpServer/HttpServer_Tests.cpp | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp index faa1c2b3ec..e319f5e3e0 100644 --- a/Common/Cpp/HttpServer/HttpServer.cpp +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -349,7 +349,7 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra continue; } - if (parsed.state == ParseState::ERROR){ + if (parsed.state == ParseState::ERROR_RESPONSE){ Response response; response.status = parsed.error_status; response.headers = std::move(parsed.error_headers); @@ -422,7 +422,7 @@ HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ ParsedRequest result; auto error = [&result](int status, const char* reason, std::vector> headers = {}){ - result.state = ParseState::ERROR; + result.state = ParseState::ERROR_RESPONSE; result.error_status = status; result.error_reason = reason; result.error_headers = std::move(headers); diff --git a/Common/Cpp/HttpServer/HttpServer.h b/Common/Cpp/HttpServer/HttpServer.h index d34de29834..7dfd0d2a43 100644 --- a/Common/Cpp/HttpServer/HttpServer.h +++ b/Common/Cpp/HttpServer/HttpServer.h @@ -161,13 +161,15 @@ class HttpServer : private AbstractServerSocket::Listener{ enum class ParseState{ NEED_MORE, READY, - ERROR, + // Named "ERROR_RESPONSE" rather than "ERROR" because "ERROR" is + // a macro on Windows (winerror.h). + ERROR_RESPONSE, }; struct ParsedRequest{ ParseState state = ParseState::NEED_MORE; - // ERROR + // ERROR_RESPONSE int error_status = 400; std::string error_reason; std::vector> error_headers; diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.cpp b/Common/Cpp/HttpServer/HttpServer_Tests.cpp index 3410c0e612..df952521c5 100644 --- a/Common/Cpp/HttpServer/HttpServer_Tests.cpp +++ b/Common/Cpp/HttpServer/HttpServer_Tests.cpp @@ -890,7 +890,9 @@ class Test_Http_StopDuringHandler : public TestHttpServer{ if (error.empty()){ // stop() blocks until the in-flight handler returns. Release // the handler from a pool thread so this thread can call it. - m_thread_pool.dispatch([&gate]{ + // The handle is kept (dispatch is [[nodiscard]]); its + // destructor waits for the trivial release task to finish. + AsyncTask releaser_task = m_thread_pool.dispatch([&gate]{ gate.release(); }); server.stop(); From f2cadadfe8ca42e4f8734d3d4c64679d943039ae Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Tue, 10 Nov 2026 18:51:38 +0000 Subject: [PATCH 3/6] HttpServer: inherit from AbstractHttpServer; address review Split the request/response types, the handler contract, and the listener events out into AbstractHttpServer (stacked on the extract-abstract-http-server PR); HttpServer now implements that interface, with the types re-exported under its own name. Fixes from the review: - Deadlock in on_connect(): the connection task handle was moved into m_connection_task while holding m_lock, and destroying the old handle blocked on the old task, whose final cleanup takes m_lock. Task handles now live in m_tasks under a dedicated m_task_lock (the socket library's m_task_lock pattern): on_connect() dispatches outside m_lock and never waits on an old task, so a new client is no longer held up while the previous handler is still running. - Deadlock in stop(): the socket was destroyed while holding m_lock, and ~ServerSocket joins the socket's worker threads, whose callbacks take m_lock. The socket is now moved out under m_lock and destroyed after releasing it. - Busy-spin on partial requests: the wait predicate was !buffer.empty(), which is permanently true once a partial request is buffered. It now requires the buffer to have grown past what the parse just saw, plus the dead/stop flags. - Chunked requests: 501 Not Implemented instead of 411 (RFC 9112). - The 413 check at parse time was unreachable (an incomplete request is always smaller than the buffer cap); the 2 MiB buffer cap is now enforced when data is appended instead, closing the connection. - end_connection() only closes the socket's connection while it still belongs to this connection, so a task noticing its own connection died late cannot close the next client's connection. - Documented that start()/stop() are not thread-safe with each other. Test: new case that a connection flooding past the buffer cap is closed without a response while the server keeps listening; chunked case now expects 501. --- Common/Cpp/HttpServer/HttpServer.cpp | 187 +++++++++++++++------ Common/Cpp/HttpServer/HttpServer.h | 169 +++++-------------- Common/Cpp/HttpServer/HttpServer_Tests.cpp | 90 +++++++++- 3 files changed, 267 insertions(+), 179 deletions(-) diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp index e319f5e3e0..f20e589fb3 100644 --- a/Common/Cpp/HttpServer/HttpServer.cpp +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -24,7 +24,8 @@ constexpr size_t MAX_REQUEST_LINE_BYTES = 8192; constexpr size_t MAX_HEADER_SECTION_BYTES = 65536; constexpr size_t MAX_HEADER_COUNT = 100; constexpr size_t MAX_BODY_BYTES = 1u << 20; // 1 MiB -// Headers + a full body + room for a few pipelined requests. +// The total per-connection receive buffer; enforced when data is +// appended, not at parse time. constexpr size_t MAX_BUFFER_BYTES = 2u << 20; // A stalled peer is declared dead after this; loopback writes of a few MB @@ -58,6 +59,22 @@ std::string trim_ows(std::string value){ } +// The concrete HandlerContext: "connection_alive()" is simply the +// connection's dead flag. +struct HandlerContextImpl : AbstractHttpServer::HandlerContext{ + explicit HandlerContextImpl(const std::atomic& dead) + : m_dead(dead) + {} + + virtual bool connection_alive() const override{ + return !m_dead.load(std::memory_order_acquire); + } + +private: + const std::atomic& m_dead; +}; + + } // anonymous namespace @@ -145,17 +162,6 @@ std::string HttpServer::format_response(const Response& response, bool keep_aliv } -const char* HttpServer::Request::header(const std::string& name) const{ - const auto it = headers.find(to_lower(name)); - return (it == headers.end()) ? "" : it->second.c_str(); -} - - -bool HttpServer::HandlerContext::connection_alive() const{ - return !m_dead.load(std::memory_order_acquire); -} - - HttpServer::HttpServer(ThreadPool& thread_pool) : m_thread_pool(thread_pool) @@ -207,25 +213,33 @@ void HttpServer::stop(){ // closes them without dispatching a connection task. socket->close_connection(); - // Wait for the in-flight connection task. Loop, because a task may - // have been dispatched between the previous one being consumed and - // the socket being destroyed. + // Wait for the in-flight connection tasks. The handles are moved out + // under m_task_lock (never destroyed while holding m_lock: a task's + // final cleanup takes m_lock, and destroying a handle blocks until + // the task finishes). Loop, because a task may have been dispatched + // between the previous ones being joined and the socket being + // destroyed. while (true){ - AsyncTask task; + std::vector tasks; { - std::lock_guard lg(m_lock); - task = std::move(m_connection_task); - m_connection_task = AsyncTask(); + std::lock_guard lg(m_task_lock); + tasks.swap(m_tasks); } - if (!task){ + if (tasks.empty()){ break; } - task.wait_and_ignore_exceptions(); + for (auto& task : tasks){ + task.wait_and_ignore_exceptions(); + } } + // Take the socket out under m_lock and destroy it after releasing + // the lock: "~ServerSocket" joins the socket's worker threads, and + // their callbacks (on_connect and friends) take m_lock. + std::unique_ptr owned; { std::lock_guard lg(m_lock); - m_socket = nullptr; + owned = std::move(m_socket); m_connection = nullptr; m_handler = Handler(); } @@ -246,31 +260,64 @@ void HttpServer::on_listen_finished(const std::string& error_message){ void HttpServer::on_connect(){ AbstractServerSocket* socket = nullptr; + std::shared_ptr conn; { std::lock_guard lg(m_lock); - if (m_stop_requested.load(std::memory_order_acquire)){ - // Stopping: close the connection without a connection task. - socket = m_socket.get(); - }else{ - auto conn = std::make_shared(); + socket = m_socket.get(); + if (socket != nullptr + && !m_stop_requested.load(std::memory_order_acquire) + ){ + conn = std::make_shared(); m_connection = conn; - try{ - m_connection_task = m_thread_pool.dispatch( - [this, conn, socket = m_socket.get()]{ - connection_task(conn, socket); - } + } + } + if (conn == nullptr){ + // Stopping (or the socket is gone): close the connection without + // a connection task. + if (socket != nullptr){ + socket->close_connection(); + } + }else{ + // Dispatch outside m_lock: the task's final cleanup takes + // m_lock, and so does stop(). The task handles live in m_tasks + // under a dedicated m_task_lock (the socket library's + // m_task_lock pattern): destroying an AsyncTask handle blocks + // until its task finishes, so a handle must never be destroyed + // while holding m_lock. + try{ + AsyncTask task = m_thread_pool.dispatch( + [this, conn, socket]{ + connection_task(conn, socket); + } + ); + { + std::lock_guard lg(m_task_lock); + // Reap finished handles (destroying one that is already + // finished is a no-op). Keep unfinished ones: the + // previous connection's task may still be finishing up + // (for example, still inside a long handler call) by the + // time this connection is accepted; it holds only its + // own (dead) connection. + m_tasks.erase( + std::remove_if(m_tasks.begin(), m_tasks.end(), [](const AsyncTask& task){ + return task.is_finished(); + }), + m_tasks.end() ); - }catch (...){ - // The task could not be started: drop the connection and - // keep listening. - m_connection.reset(); - socket = m_socket.get(); + m_tasks.push_back(std::move(task)); + } + }catch (...){ + // The task could not be started: drop the connection and + // keep listening. + { + std::lock_guard lg(m_lock); + if (m_connection.get() == conn.get()){ + m_connection = nullptr; + } } + socket->close_connection(); } } - if (socket != nullptr){ - socket->close_connection(); - } m_listeners.run_method(&Listener::on_connect); } @@ -282,19 +329,33 @@ void HttpServer::on_connect_rejected(){ void HttpServer::on_receive_data(const void* data, size_t bytes){ std::shared_ptr conn; + AbstractServerSocket* socket = nullptr; { std::lock_guard lg(m_lock); conn = m_connection; + socket = m_socket.get(); } if (conn == nullptr){ return; } + bool exceeded = false; { std::lock_guard lg(conn->lock); - const char* ptr = (const char*)data; - conn->buffer.append(ptr, bytes); + exceeded = conn->buffer.size() + bytes > MAX_BUFFER_BYTES; + if (!exceeded){ + const char* ptr = (const char*)data; + conn->buffer.append(ptr, bytes); + } } conn->cv.notify_all(); + if (exceeded && socket != nullptr){ + // The stream is longer than we are willing to buffer. It is + // likely in the middle of a request, so there is no reliable + // response left to send: just end the connection. ("socket" can + // be null only while stop() is tearing the socket down, in which + // case there is no connection task left to wake up.) + end_connection(conn, socket); + } } @@ -331,17 +392,23 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra } ParsedRequest parsed; + size_t buffer_size; { std::lock_guard lg(conn->lock); parsed = try_parse(conn->buffer); + buffer_size = conn->buffer.size(); } if (parsed.state == ParseState::NEED_MORE){ - // Wait for the rest of this request, a disconnect, or a stop. + // Wait for the rest of this request, a disconnect, or a + // stop. The predicate requires the buffer to have grown past + // what the parse just saw: with a partial request, + // "!buffer.empty()" alone is permanently true and would spin + // this loop at 100% CPU until the request completes. { std::unique_lock lg(conn->lock); - conn->cv.wait_for(lg, std::chrono::milliseconds(10), [this, &conn]{ - return !conn->buffer.empty() + conn->cv.wait_for(lg, std::chrono::milliseconds(10), [this, &conn, buffer_size]{ + return conn->buffer.size() != buffer_size || conn->dead.load(std::memory_order_relaxed) || m_stop_requested.load(std::memory_order_relaxed); }); @@ -369,7 +436,7 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra Response response; { - HandlerContext context(conn->dead); + HandlerContextImpl context(conn->dead); try{ if (handler){ handler(parsed.request, response, context); @@ -412,7 +479,7 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra { std::lock_guard lg(m_lock); if (m_connection.get() == conn.get()){ - m_connection.reset(); + m_connection = nullptr; } } } @@ -539,7 +606,10 @@ HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ // Special headers. if (result.request.headers.count("transfer-encoding") != 0){ - error(411, "Chunked transfer encoding is not supported."); + // "Not Implemented" rather than "Length Required": chunked + // encoding is a (unsupported) representation of the body, not a + // missing length (RFC 9112). + error(501, "Chunked transfer encoding is not supported."); return result; } @@ -582,11 +652,12 @@ HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ const size_t consumed = header_end + 4 + (size_t)content_length; if (buffer.size() < consumed){ - if (buffer.size() > MAX_BUFFER_BYTES){ - error(413, "Request too large."); - return result; - } - return result; // NEED_MORE: the body has not fully arrived yet. + // NEED_MORE: the body has not fully arrived yet. In this state + // the buffer holds nothing but the bytes of this one incomplete + // request ("consumed" is at most a header section plus the body + // cap), so the buffer cannot exceed MAX_BUFFER_BYTES here; the + // overall cap is enforced when data is appended instead. + return result; } result.state = ParseState::READY; @@ -606,6 +677,16 @@ HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ void HttpServer::end_connection(const std::shared_ptr& conn, AbstractServerSocket* socket){ conn->dead.store(true, std::memory_order_release); conn->cv.notify_all(); + // Close the socket's connection only while it still belongs to this + // connection: by the time a task notices its own connection died, + // the socket may already be serving the next client, and closing + // unconditionally would kill that client's connection instead. + { + std::lock_guard lg(m_lock); + if (m_connection.get() != conn.get()){ + return; + } + } socket->close_connection(); } diff --git a/Common/Cpp/HttpServer/HttpServer.h b/Common/Cpp/HttpServer/HttpServer.h index 7dfd0d2a43..8b6cdfaa68 100644 --- a/Common/Cpp/HttpServer/HttpServer.h +++ b/Common/Cpp/HttpServer/HttpServer.h @@ -2,31 +2,28 @@ * * From: https://github.com/PokemonAutomation/ * - * A minimal HTTP/1.1 server built on top of "AbstractServerSocket", aimed - * at a single local client that issues one request at a time (for example, - * an MCP client talking to the AI Agent Server). + * The "AbstractServerSocket" implementation of "AbstractHttpServer": a + * minimal HTTP/1.1 server on the platform's default server socket, for a + * single local client that issues one request at a time. The request/ + * response types, the handler contract, and the threading rules are in + * "AbstractHttpServer.h"; this file documents the implementation. * - * Design notes: - * - One connection at a time: the underlying server socket handles one - * connection at a time, and a second client gets "on_connect_rejected". - * - Requests are processed strictly in order. Bytes of a pipelined request - * simply wait in the buffer until the previous response has been sent. - * - The user "Handler" may block for a long time; it runs on a thread from - * the given thread pool. Poll "HandlerContext::connection_alive()" - * between long-running steps to bail out early. Once it returns false, - * the response you set is discarded. - * - No TLS. Request bodies: "Content-Length" only (chunked is rejected - * with 411). Response bodies: always "Content-Length" (no chunked). - * No upgrades. - * - * Threading: - * - All "Listener" callbacks run on threads from the thread pool, never - * on the calling thread. - * - Do not call "start()" or "stop()" from within a "Listener" callback: - * "stop()" joins the very pool threads that fire the callbacks. - * - The thread pool should be an unlimited, long-lived one (for example - * "GlobalThreadPools::unlimited_normal()"): the server keeps an accept - * thread and a per-connection thread alive for as long as it runs. + * Implementation notes: + * - The socket's receive/disconnect callbacks only append data to the + * per-connection buffer and flip flags. One "connection task" per + * connection (from the thread pool) does all parsing and writing, so + * the handler may run for a long time without stalling the socket's + * receive loop. + * - Task handles live in "m_tasks" under a dedicated "m_task_lock" + * (the socket library's "m_task_lock" pattern): destroying an + * "AsyncTask" handle blocks until that task finishes, and the task's + * final cleanup takes "m_lock", so a handle must never be destroyed + * while holding "m_lock". "on_connect()" may therefore find the + * previous connection's task still finishing (for example, still in a + * long handler call); its handle is kept and reaped later. + * - "stop()" moves the socket out under "m_lock" and destroys it after + * releasing the lock: "~ServerSocket" joins the socket's worker + * threads, and their callbacks take "m_lock". * */ @@ -36,8 +33,6 @@ #include #include #include -#include -#include #include #include #include @@ -45,7 +40,7 @@ #include "Common/Cpp/Concurrency/AsyncTask.h" #include "Common/Cpp/Concurrency/ConditionVariable.h" #include "Common/Cpp/Concurrency/Mutex.h" -#include "Common/Cpp/ListenerSet.h" +#include "Common/Cpp/HttpServer/AbstractHttpServer.h" #include "Common/Cpp/Sockets/AbstractServerSocket.h" namespace PokemonAutomation{ @@ -54,99 +49,23 @@ class ThreadPool; -class HttpServer : private AbstractServerSocket::Listener{ +class HttpServer : public AbstractHttpServer, private AbstractServerSocket::Listener{ public: - struct Request{ - std::string method; // e.g. "POST" - std::string path; // e.g. "/mcp" (without the query) - std::string query; // the part after '?', if any - std::map headers; // names are lower-cased - std::string body; - - // "name" is lower-cased first; returns "" if the header is absent. - const char* header(const std::string& name) const; - }; - - struct Response{ - int status = 200; - std::string reason; // empty = the standard phrase for "status" - std::vector> headers; - std::string body; - - // End the connection after this response, even on HTTP/1.1. - bool connection_close = false; - }; - - // Passed to the handler for the duration of the call. - struct HandlerContext{ - // False once the connection this request came in on is closed or - // the server is stopping. Once false, the response is discarded - // even if you set one. - bool connection_alive() const; - - private: - explicit HandlerContext(const std::atomic& p_dead) - : m_dead(p_dead) - {} - - const std::atomic& m_dead; - - friend class HttpServer; - }; - - // Runs on a thread from the thread pool, never on the socket's - // connection thread. It may block (for example, while a tool runs on - // the program thread), but it must eventually return: "stop()" blocks - // until the in-flight handler returns. It must not throw; exceptions - // are turned into a 500 response. - using Handler = std::function; - - struct Listener{ - // Called after "start()" finishes. If successful, "error_message" - // is empty. Otherwise it contains the error message and the server - // is not listening; call "stop()" to clean up before retrying. - virtual void on_listen_finished(const std::string& error_message){} - - // Called when a client connects. - virtual void on_connect(){} - - // Called when a second client tries to connect while one is - // already connected; that client is immediately closed. - virtual void on_connect_rejected(){} - - // Called when the client disconnects (a server-side "stop()" does - // not report a disconnect). - virtual void on_disconnect(const std::string& error_message){} - }; - - void add_listener(Listener& listener){ - m_listeners.add(listener); - } - void remove_listener(Listener& listener){ - m_listeners.remove(listener); - } + // The interface's types, re-exported under this class's name. + using AbstractHttpServer::Request; + using AbstractHttpServer::Response; + using AbstractHttpServer::HandlerContext; + using AbstractHttpServer::Handler; + using AbstractHttpServer::Listener; public: explicit HttpServer(ThreadPool& thread_pool); ~HttpServer(); - // Starts listening. Asynchronous: "Listener::on_listen_finished" is - // called on a thread from the thread pool with the result. "handler" - // receives every fully parsed request; if it is null, all requests - // get a 500. Returns without starting if the server is already - // started. - void start(const std::string& address, uint16_t port, Handler handler); - - // Stops the server and blocks until it is fully down: the active - // connection is closed, the in-flight handler (if any) is waited for, - // and the socket's worker threads are joined. Afterwards "start()" - // can be called again. - void stop(); - - // True if "start()" has been called and the server has not been - // stopped again. - bool is_running() const; + virtual void start(const std::string& address, uint16_t port, Handler handler) override; + virtual void stop() override; + virtual bool is_running() const override; private: @@ -182,8 +101,8 @@ class HttpServer : private AbstractServerSocket::Listener{ }; // Per-connection state. The connection task is the only thread that - // parses or writes; "on_receive_data"/"on_disconnect" only append data - // and flip flags. + // parses or writes; "on_receive_data"/"on_disconnect" only append + // data and flip flags. struct Connection{ Mutex lock; ConditionVariable cv; @@ -191,11 +110,12 @@ class HttpServer : private AbstractServerSocket::Listener{ std::atomic dead{false}; }; - // Runs on a thread from the thread pool, one instance per connection. + // Runs on a thread from the thread pool, one instance per + // connection. void connection_task(const std::shared_ptr& conn, AbstractServerSocket* socket); - // Parses one complete request out of "buffer", removing it on success. - // Must be called with "conn->lock" held. + // Parses one complete request out of "buffer", removing it on + // success. Must be called with "conn->lock" held. static ParsedRequest try_parse(std::string& buffer); // Marks the connection dead and closes it at the socket level. No-op @@ -221,17 +141,20 @@ class HttpServer : private AbstractServerSocket::Listener{ static std::string format_response(const Response& response, bool keep_alive); ThreadPool& m_thread_pool; - ListenerSet m_listeners; - // Guards m_socket, m_connection, m_connection_task and m_handler. - // Never held while waiting on a condition variable. Mutable so that - // "is_running()" can take it from a const method. + // Guards m_socket, m_connection, and m_handler. Never held across a + // blocking wait, a task destruction, or a socket destruction. mutable Mutex m_lock; std::unique_ptr m_socket; std::shared_ptr m_connection; - AsyncTask m_connection_task; Handler m_handler; + // Guards m_tasks. Never held while an AsyncTask destructor runs: + // the destructor blocks until the task finishes, and the task's + // final cleanup takes m_lock. + Mutex m_task_lock; + std::vector m_tasks; + // Set by "stop()"; checked by the connection tasks between requests // and by "HandlerContext". std::atomic m_stop_requested{false}; diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.cpp b/Common/Cpp/HttpServer/HttpServer_Tests.cpp index df952521c5..9a1d3d1005 100644 --- a/Common/Cpp/HttpServer/HttpServer_Tests.cpp +++ b/Common/Cpp/HttpServer/HttpServer_Tests.cpp @@ -532,7 +532,7 @@ class Test_Http_BadRequest : public TestHttpServer{ }; -// Chunked request bodies are not supported: 411 and a closed connection, +// Chunked request bodies are not supported: 501 and a closed connection, // and the handler is never called. class Test_Http_ChunkedRejected : public TestHttpServer{ public: @@ -568,7 +568,7 @@ class Test_Http_ChunkedRejected : public TestHttpServer{ error = "The client did not send the request"; }else{ const std::string expected = - "HTTP/1.1 411 Length Required\r\n" + "HTTP/1.1 501 Not Implemented\r\n" "Content-Length: 44\r\n" "Connection: close\r\n" "\r\n" @@ -652,6 +652,87 @@ class Test_Http_BodyTooLarge : public TestHttpServer{ }; +// A connection that sends more data than the receive buffer cap (about +// 2 MiB) is closed without a response, and the server keeps listening. +class Test_Http_BufferCap : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_calls{0}; + auto handler = [&handler_calls](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_calls++; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else{ + // A never-completing request far larger than the buffer + // cap: the server closes the connection without a + // response. (The send may fail partway through once the + // server has hung up; that is expected.) + const std::string flood(3u << 20, 'A'); + client.send_all(flood); + if (!client.waiter.wait_for( + HTTP_TEST_WAIT, + [&client]{ return client.disconnected.load() >= 1; } + )){ + error = "The server did not close the over-cap connection"; + } + } + } + + if (error.empty() && handler_calls.load() != 0){ + error = "The handler was called for a capped connection"; + } + + if (error.empty()){ + // The server must still be listening afterwards. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect after the cap test"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received != expected){ + error = "The server did not answer after the cap test:\n" + client.received; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + // "Expect: 100-continue" is answered with "100 Continue" before the final // response. class Test_Http_ExpectContinue : public TestHttpServer{ @@ -902,7 +983,7 @@ class Test_Http_StopDuringHandler : public TestHttpServer{ } if (error.empty()){ - // Start again on the same port and serve a fresh request. + // Start again (on a fresh port) and serve a fresh request. auto simple_handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ response.status = 200; response.body = "ok"; @@ -1016,6 +1097,9 @@ void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool){ database.add( thread_pool, "HttpServer - Oversized content-length is rejected" ); + database.add( + thread_pool, "HttpServer - A connection over the buffer cap is closed" + ); database.add( thread_pool, "HttpServer - Expect: 100-continue" ); From 252b3323495f4fe8f61eecea579b0e32f95dab7f Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Wed, 11 Nov 2026 20:23:32 +0000 Subject: [PATCH 4/6] HttpServer: close the reconnect write race from the review The socket's connection thread sets the state back to LISTENING before it reports the disconnect, so a fast reconnect can be accepted before the old connection's on_disconnect callback runs. Two consequences: - The old connection's task could then write its final response through socket->send() into the new client's socket (send() has no per-connection identity; it writes whatever the socket is connected to). on_connect() now marks the connection it replaces dead (and notifies it) before the new one takes over, so the old task's post-handler check discards its response. - on_disconnect() marked m_connection dead, which in that window is the new client's connection: it would kill the new connection task. It now only does so while the socket is not already CONNECTED to the next client. Test: a client disconnects while its handler is still blocked on a gate; a second client connects (replacing the first in the server) before the gate is released. The first handler's late response must be discarded, and the second client's stream must contain only its own response. --- Common/Cpp/HttpServer/HttpServer.cpp | 31 ++++- Common/Cpp/HttpServer/HttpServer_Tests.cpp | 128 +++++++++++++++++++++ 2 files changed, 156 insertions(+), 3 deletions(-) diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp index f20e589fb3..47bddc3d25 100644 --- a/Common/Cpp/HttpServer/HttpServer.cpp +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -268,6 +268,19 @@ void HttpServer::on_connect(){ && !m_stop_requested.load(std::memory_order_acquire) ){ conn = std::make_shared(); + // The previous socket connection is over (the socket only + // accepts a new client once the previous one is gone), but + // its task may still be finishing (for example, still inside + // a long handler call), and the socket's on_disconnect + // callback for it may not have run yet: it runs after the + // socket has already moved back to listening. Mark it dead + // now, before the new connection takes over the socket's + // active fd, so the old task discards its response instead + // of delivering it to the new client. + if (m_connection != nullptr){ + m_connection->dead.store(true, std::memory_order_release); + m_connection->cv.notify_all(); + } m_connection = conn; } } @@ -363,7 +376,18 @@ void HttpServer::on_disconnect(const std::string& error_message){ std::shared_ptr conn; { std::lock_guard lg(m_lock); - conn = m_connection; + // The socket may already be serving the next client by the time + // this callback runs: the socket's connection thread sets the + // state back to LISTENING before it reports the disconnect, so a + // fast reconnect can be accepted in between. In that case this + // disconnect is about the previous connection, which on_connect() + // has already marked dead; the current one belongs to the new + // client and must not be killed. + if (m_socket == nullptr + || m_socket->state() != AbstractServerSocket::State::CONNECTED + ){ + conn = m_connection; + } } if (conn != nullptr){ conn->dead.store(true, std::memory_order_release); @@ -453,8 +477,9 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra } } - // The connection may have closed while the handler was running; - // discard the response in that case. + // The connection may have closed or been replaced by a new + // client while the handler was running; discard the response in + // that case. if (conn->dead.load(std::memory_order_acquire) || m_stop_requested.load(std::memory_order_acquire) ){ diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.cpp b/Common/Cpp/HttpServer/HttpServer_Tests.cpp index 9a1d3d1005..f1ccf6233f 100644 --- a/Common/Cpp/HttpServer/HttpServer_Tests.cpp +++ b/Common/Cpp/HttpServer/HttpServer_Tests.cpp @@ -917,6 +917,131 @@ class Test_Http_DisconnectDuringHandler : public TestHttpServer{ }; +// The client disconnects while the handler is still running, and a new +// client connects before the handler returns: the old task must discard +// its response instead of writing it into the new client's socket. +class Test_Http_ReconnectWhileHandlerRunning : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic first_call_alive{false}; + + auto handler = [&gate, &first_call_alive]( + const HttpServer::Request& request, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + if (request.path == "/a"){ + first_call_alive = context.connection_alive(); + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + } + response.status = 200; + response.body = (request.path == "/a") ? "A" : "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + TestClient client_a(m_thread_pool); + TestClient client_b(m_thread_pool); + { + if (!client_a.connect(port)){ + error = "The first client did not connect"; + }else if (!client_a.send_all( + "POST /a HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The first client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_alive]{ return first_call_alive.load(); } + )){ + error = "The handler did not start"; + } + } + + if (error.empty()){ + // Disconnect the first client (closing its socket while it is + // still in scope, so its connection task keeps running) and + // wait for the server to report the disconnect. + client_a.socket.close(); + if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + )){ + error = "The server did not report the first client's disconnect"; + } + } + + if (error.empty()){ + // Connect the second client while the first connection's + // handler is still blocked on the gate. Waiting for the + // second on_connect guarantees the second connection has + // replaced the first one in the server by the time the gate + // is released. + if (!client_b.connect(port)){ + error = "The second client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 2; } + )){ + error = "The server did not accept the second client"; + } + } + + if (error.empty()){ + gate.release(); + } + + if (error.empty()){ + // The second client's first bytes must be its own response: + // the first client's late response was discarded, not + // written into this socket. + if (!client_b.send_all( + "POST /b HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The second client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client_b.wait_for_bytes(expected.size()) || client_b.received != expected){ + error = "The second client received a stray response:\n" + client_b.received; + } + } + } + + if (error.empty() && !first_call_alive.load()){ + error = "The handler did not see the connection as alive on entry"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + // "stop()" while the handler is still running blocks until the handler // returns, and "start()" works afterwards. class Test_Http_StopDuringHandler : public TestHttpServer{ @@ -1106,6 +1231,9 @@ void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool){ database.add( thread_pool, "HttpServer - Disconnect while the handler is running" ); + database.add( + thread_pool, "HttpServer - Reconnect while the previous handler is still running" + ); database.add( thread_pool, "HttpServer - stop() while the handler is running" ); From 9a50e65ab0b5e201af5652f1d54f2f56ee502268 Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:52:38 +0000 Subject: [PATCH 5/6] HttpServer: address the second review round Five items from the fresh review: - [P1] stop() could hang waiting for a handler: the socket suppresses its disconnect callback for a close_connection() close, so a handler polling connection_alive() kept seeing true while shutdown waited for it. stop() now marks the active connection dead before it waits. The stop-during-handler test now polls connection_alive() after the gate, which would spin forever without the mark. - [P1] A response could reach a different client after reconnection: every write now carries the connection's socket id (AbstractServerSocket::connection_id(), new), so the socket refuses a write whose connection was replaced. This closes the last of the 'task outlives the connection' windows; the id check and the transport selection happen under the socket's lock. - [P2] Expect: 100-continue stalled clients: the interim response was sent only after the complete request (including body) was parsed, which a client waiting for it before sending the body never sees. Parsing now reports a separate NEED_BODY state (headers complete and valid, body pending), and the 100 is answered at that point. The test now performs the real two-phase handshake. - [P2] Complete headers bypassed the 64 KiB limit: the in-progress check only fires while the terminator is still missing. The completed header boundary is now checked against the limit too, with a test sending a ~75 KiB header section in one piece. - [P2] The test client's receive buffer was a data race: 'received' and the error strings are now protected by the waiter's mutex, and assertions use synchronized snapshots (same for the server listener's listen error). --- Common/Cpp/HttpServer/HttpServer.cpp | 74 +++++-- Common/Cpp/HttpServer/HttpServer.h | 26 ++- Common/Cpp/HttpServer/HttpServer_Tests.cpp | 215 ++++++++++++++++----- 3 files changed, 251 insertions(+), 64 deletions(-) diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp index 47bddc3d25..828e5efc6b 100644 --- a/Common/Cpp/HttpServer/HttpServer.cpp +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -206,6 +206,18 @@ void HttpServer::stop(){ return; } + // Mark the active connection dead: the socket suppresses its + // disconnect callback for a close_connection() close, so without + // this a handler polling connection_alive() would never see the + // shutdown, and the wait for it below would never end. + { + std::lock_guard lg(m_lock); + if (m_connection != nullptr){ + m_connection->dead.store(true, std::memory_order_release); + m_connection->cv.notify_all(); + } + } + // Kill the active connection, if any: it cancels in-flight writes and // makes the socket's connection thread exit. The socket keeps // listening until it is destroyed below, so it may accept a few more @@ -282,6 +294,9 @@ void HttpServer::on_connect(){ m_connection->cv.notify_all(); } m_connection = conn; + // The token of the connection this one will serve; send() + // refuses to write through a socket that has moved on. + conn->socket_id = socket->connection_id(); } } if (conn == nullptr){ @@ -408,6 +423,10 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra handler = m_handler; } + // "Expect: 100-continue" is answered at most once, and only while + // the body is still pending. + bool sent_continue = false; + while (true){ if (m_stop_requested.load(std::memory_order_acquire) || conn->dead.load(std::memory_order_acquire) @@ -423,7 +442,25 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra buffer_size = conn->buffer.size(); } - if (parsed.state == ParseState::NEED_MORE){ + if (parsed.state == ParseState::NEED_BODY){ + // The headers are complete and valid, but the body has not + // fully arrived. A client that sent "Expect: 100-continue" is + // still waiting for this before sending the body: answer it + // now, not after the (still missing) body arrives. (If the + // whole request was already buffered, the state is READY and + // the final response alone moves the client on; RFC 9110). + if (parsed.expect_continue && !sent_continue){ + static const char CONTINUE_RESPONSE[] = "HTTP/1.1 100 Continue\r\n\r\n"; + if (!write_raw(conn, socket, CONTINUE_RESPONSE)){ + break; + } + sent_continue = true; + } + } + + if (parsed.state == ParseState::NEED_MORE + || parsed.state == ParseState::NEED_BODY + ){ // Wait for the rest of this request, a disconnect, or a // stop. The predicate requires the buffer to have grown past // what the parse just saw: with a partial request, @@ -451,13 +488,6 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra } // A complete request is ready to be handled. - if (parsed.expect_continue){ - static const char CONTINUE_RESPONSE[] = "HTTP/1.1 100 Continue\r\n\r\n"; - if (!write_raw(conn, socket, CONTINUE_RESPONSE)){ - break; - } - } - Response response; { HandlerContextImpl context(conn->dead); @@ -528,7 +558,15 @@ HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ if (buffer.size() > MAX_HEADER_SECTION_BYTES){ error(400, "Malformed request."); } - return result; // NEED_MORE + return result; // NEED_MORE: the headers have not fully arrived yet. + } + // The same limit applies to a complete header section: the + // in-progress check above only fires while the terminator is still + // missing, so a large section that arrives in one piece would + // otherwise sail through. + if (header_end + 4 > MAX_HEADER_SECTION_BYTES){ + error(400, "Malformed request."); + return result; } const size_t line_end = buffer.find("\r\n"); @@ -677,11 +715,13 @@ HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ const size_t consumed = header_end + 4 + (size_t)content_length; if (buffer.size() < consumed){ - // NEED_MORE: the body has not fully arrived yet. In this state - // the buffer holds nothing but the bytes of this one incomplete - // request ("consumed" is at most a header section plus the body - // cap), so the buffer cannot exceed MAX_BUFFER_BYTES here; the - // overall cap is enforced when data is appended instead. + // NEED_BODY: the headers are complete and valid, the body has + // not fully arrived yet. In this state the buffer holds nothing + // but the bytes of this one request ("consumed" is at most a + // header section plus the body cap), so the buffer cannot exceed + // MAX_BUFFER_BYTES here; the overall cap is enforced when data + // is appended instead. + result.state = ParseState::NEED_BODY; return result; } @@ -717,7 +757,11 @@ void HttpServer::end_connection(const std::shared_ptr& conn, Abstrac bool HttpServer::write_raw(const std::shared_ptr& conn, AbstractServerSocket* socket, const std::string& bytes){ - const size_t sent = socket->send(bytes.data(), bytes.size(), WRITE_TIMEOUT); + // "conn->socket_id" is the socket's connection token when this + // connection was accepted: if the socket has moved on to the next + // client since then, the send is refused instead of being delivered + // into the new client's socket. + const size_t sent = socket->send(bytes.data(), bytes.size(), WRITE_TIMEOUT, conn->socket_id); if (sent != bytes.size()){ // The peer went away, or the write stalled: the connection is // unusable either way. diff --git a/Common/Cpp/HttpServer/HttpServer.h b/Common/Cpp/HttpServer/HttpServer.h index 8b6cdfaa68..2391f3428e 100644 --- a/Common/Cpp/HttpServer/HttpServer.h +++ b/Common/Cpp/HttpServer/HttpServer.h @@ -14,6 +14,14 @@ * connection (from the thread pool) does all parsing and writing, so * the handler may run for a long time without stalling the socket's * receive loop. + * - Every write goes through "AbstractServerSocket::send()" with the + * connection's socket id (captured at accept time), so a response + * that outlives its connection - the peer disconnected and the next + * client was accepted in the meantime - is refused by the socket + * instead of reaching the new client's stream. + * - "Expect: 100-continue" is answered as soon as the headers are + * validated, while the body is still pending (a waiting client is + * blocked on that interim response). * - Task handles live in "m_tasks" under a dedicated "m_task_lock" * (the socket library's "m_task_lock" pattern): destroying an * "AsyncTask" handle blocks until that task finishes, and the task's @@ -21,9 +29,13 @@ * while holding "m_lock". "on_connect()" may therefore find the * previous connection's task still finishing (for example, still in a * long handler call); its handle is kept and reaped later. - * - "stop()" moves the socket out under "m_lock" and destroys it after - * releasing the lock: "~ServerSocket" joins the socket's worker - * threads, and their callbacks take "m_lock". + * - "stop()" marks the active connection dead before it waits for the + * in-flight task: the socket suppresses its disconnect callback for a + * close_connection() close, so without the mark a handler polling + * "connection_alive()" would never see the shutdown. "stop()" then + * moves the socket out under "m_lock" and destroys it after releasing + * the lock: "~ServerSocket" joins the socket's worker threads, and + * their callbacks take "m_lock". * */ @@ -79,6 +91,7 @@ class HttpServer : public AbstractHttpServer, private AbstractServerSocket::List // One request that is waiting to be parsed out of the buffer. enum class ParseState{ NEED_MORE, + NEED_BODY, READY, // Named "ERROR_RESPONSE" rather than "ERROR" because "ERROR" is // a macro on Windows (winerror.h). @@ -93,7 +106,7 @@ class HttpServer : public AbstractHttpServer, private AbstractServerSocket::List std::string error_reason; std::vector> error_headers; - // READY + // READY / NEED_BODY Request request; bool http_1_1 = true; bool connection_close_requested = false; @@ -108,6 +121,11 @@ class HttpServer : public AbstractHttpServer, private AbstractServerSocket::List ConditionVariable cv; std::string buffer; std::atomic dead{false}; + + // The server socket's connection_id() when this connection was + // accepted. Passed to every send(), so a write that outlives the + // connection is refused instead of reaching the next client. + uint64_t socket_id = 0; }; // Runs on a thread from the thread pool, one instance per diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.cpp b/Common/Cpp/HttpServer/HttpServer_Tests.cpp index f1ccf6233f..65b4ca31f9 100644 --- a/Common/Cpp/HttpServer/HttpServer_Tests.cpp +++ b/Common/Cpp/HttpServer/HttpServer_Tests.cpp @@ -78,6 +78,8 @@ struct HandlerGate{ // A raw TCP client that accumulates everything it receives. +// "received" and the error strings are protected by "waiter.lock": the +// socket's receive thread writes them, the test thread reads them. struct TestClient{ Waiter waiter; std::string received; @@ -90,18 +92,27 @@ struct TestClient{ TestClient* client; virtual void on_connect_finished(const std::string& error_message) override{ - client->connect_error = error_message; + { + std::lock_guard lg(client->waiter.lock); + client->connect_error = error_message; + } client->connected++; client->waiter.notify(); } virtual void on_receive_data(const void* data, size_t bytes) override{ - client->received.append((const char*)data, bytes); + { + std::lock_guard lg(client->waiter.lock); + client->received.append((const char*)data, bytes); + } client->waiter.notify(); } virtual void on_disconnect(const std::string& error_message) override{ - client->disconnect_error = error_message; + { + std::lock_guard lg(client->waiter.lock); + client->disconnect_error = error_message; + } client->disconnected++; client->waiter.notify(); } @@ -137,28 +148,45 @@ struct TestClient{ } bool wait_for_bytes(size_t n){ + // The predicate runs with "waiter.lock" held, so it may read + // "received" directly. return waiter.wait_for( HTTP_TEST_WAIT, [this, n]{ return received.size() >= n; } ); } + + // A consistent copy of everything received so far, for assertions. + std::string received_snapshot(){ + std::lock_guard lg(waiter.lock); + return received; + } }; struct TestServerListener : HttpServer::Listener{ Waiter waiter; std::atomic listen_finished{0}; + // Protected by "waiter.lock" (written by the socket's thread). std::string last_listen_error; std::atomic connected{0}; std::atomic rejected{0}; std::atomic disconnected{0}; virtual void on_listen_finished(const std::string& error_message) override{ - last_listen_error = error_message; + { + std::lock_guard lg(waiter.lock); + last_listen_error = error_message; + } listen_finished++; waiter.notify(); } + std::string last_listen_error_snapshot(){ + std::lock_guard lg(waiter.lock); + return last_listen_error; + } + virtual void on_connect() override{ connected++; waiter.notify(); @@ -207,7 +235,7 @@ class TestHttpServer : public UnitTest{ server.stop(); continue; } - if (listener.last_listen_error.empty()){ + if (listener.last_listen_error_snapshot().empty()){ port = candidate; return true; } @@ -279,8 +307,8 @@ class Test_Http_PostEcho : public TestHttpServer{ "/mcp|session=abc|unit-test|hello"; if (!client.wait_for_bytes(expected.size())){ error = "The client did not receive the full response"; - }else if (client.received != expected){ - error = "Bad response:\n" + client.received; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); } } // The client goes out of scope here and closes the socket. @@ -355,9 +383,9 @@ class Test_Http_PipelinedKeepAlive : public TestHttpServer{ "\r\n" "rtwo"; if (!client.wait_for_bytes(expected.size())){ - error = "The client did not receive both responses (got " + std::to_string(client.received.size()) + " of " + std::to_string(expected.size()) + " bytes)"; - }else if (client.received != expected){ - error = "Bad response:\n" + client.received; + error = "The client did not receive both responses (got " + std::to_string(client.received_snapshot().size()) + " of " + std::to_string(expected.size()) + " bytes)"; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); } } } @@ -414,8 +442,8 @@ class Test_Http_ConnectionClose : public TestHttpServer{ "done"; if (!client.wait_for_bytes(expected.size())){ error = "The client did not receive the full response"; - }else if (client.received != expected){ - error = "Bad response:\n" + client.received; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); }else if (!client.waiter.wait_for( HTTP_TEST_WAIT, [&client]{ return client.disconnected.load() >= 1; } @@ -444,8 +472,8 @@ class Test_Http_ConnectionClose : public TestHttpServer{ "Connection: keep-alive\r\n" "\r\n" "done"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "The reconnected client did not get a good response:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The reconnected client did not get a good response:\n" + client.received_snapshot(); } } } @@ -494,8 +522,8 @@ class Test_Http_BadRequest : public TestHttpServer{ "Connection: close\r\n" "\r\n" "Malformed request.\n"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "Bad error response:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); } } } @@ -517,8 +545,8 @@ class Test_Http_BadRequest : public TestHttpServer{ "Connection: keep-alive\r\n" "\r\n" "ok"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "The server did not recover:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not recover:\n" + client.received_snapshot(); } } } @@ -573,8 +601,8 @@ class Test_Http_ChunkedRejected : public TestHttpServer{ "Connection: close\r\n" "\r\n" "Chunked transfer encoding is not supported.\n"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "Bad error response:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); } } } @@ -633,8 +661,77 @@ class Test_Http_BodyTooLarge : public TestHttpServer{ "Connection: close\r\n" "\r\n" "Request body too large.\n"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "Bad error response:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A complete header section over the size limit is rejected with 400, +// even though it arrives in one piece (the in-progress limit check only +// fires while the terminator is still missing). +class Test_Http_HeadersTooLarge : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + // 50 headers of about 1.5 KiB each: about 75 KiB of headers - + // under the 100-header count limit, over the 64 KiB size limit. + std::string request = "POST / HTTP/1.1\r\n"; + for (int i = 0; i < 50; i++){ + request += "X-Filler-"; + request += std::to_string(i); + request += ": "; + request += std::string(1490, 'a'); + request += "\r\n"; + } + request += "\r\n"; + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all(request)){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 400 Bad Request\r\n" + "Content-Length: 19\r\n" + "Connection: close\r\n" + "\r\n" + "Malformed request.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); } } } @@ -718,8 +815,8 @@ class Test_Http_BufferCap : public TestHttpServer{ "Connection: keep-alive\r\n" "\r\n" "ok"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "The server did not answer after the cap test:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not answer after the cap test:\n" + client.received_snapshot(); } } } @@ -733,8 +830,10 @@ class Test_Http_BufferCap : public TestHttpServer{ }; -// "Expect: 100-continue" is answered with "100 Continue" before the final -// response. +// "Expect: 100-continue" is answered with "100 Continue" as soon as the +// headers are validated - before the body arrives, which is what a +// client waiting for the interim response is waiting for - and the +// final response carries the body. class Test_Http_ExpectContinue : public TestHttpServer{ public: using TestHttpServer::TestHttpServer; @@ -764,19 +863,27 @@ class Test_Http_ExpectContinue : public TestHttpServer{ "Content-Length: 5\r\n" "Expect: 100-continue\r\n" "\r\n" - "hello" )){ - error = "The client did not send the request"; + error = "The client did not send the headers"; }else{ - const std::string expected = - "HTTP/1.1 100 Continue\r\n\r\n" - "HTTP/1.1 200 OK\r\n" - "Content-Length: 5\r\n" - "Connection: keep-alive\r\n" - "\r\n" - "hello"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "Bad response:\n" + client.received; + const std::string continue_response = "HTTP/1.1 100 Continue\r\n\r\n"; + if (!client.wait_for_bytes(continue_response.size()) + || client.received_snapshot().compare(0, continue_response.size(), continue_response) != 0 + ){ + error = "The server did not answer the interim response:\n" + client.received_snapshot(); + }else if (!client.send_all("hello")){ + error = "The client did not send the body"; + }else{ + const std::string expected = + "HTTP/1.1 100 Continue\r\n\r\n" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 5\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "hello"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + } } } } @@ -882,8 +989,8 @@ class Test_Http_DisconnectDuringHandler : public TestHttpServer{ "Connection: keep-alive\r\n" "\r\n" "ok"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "The server did not recover:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not recover:\n" + client.received_snapshot(); } } } @@ -1023,8 +1130,8 @@ class Test_Http_ReconnectWhileHandlerRunning : public TestHttpServer{ "Connection: keep-alive\r\n" "\r\n" "ok"; - if (!client_b.wait_for_bytes(expected.size()) || client_b.received != expected){ - error = "The second client received a stray response:\n" + client_b.received; + if (!client_b.wait_for_bytes(expected.size()) || client_b.received_snapshot() != expected){ + error = "The second client received a stray response:\n" + client_b.received_snapshot(); } } } @@ -1055,15 +1162,26 @@ class Test_Http_StopDuringHandler : public TestHttpServer{ HandlerGate gate; std::atomic handler_started{false}; + std::atomic saw_disconnect_on_stop{false}; - auto handler = [&gate, &handler_started]( + auto handler = [&gate, &handler_started, &saw_disconnect_on_stop]( const HttpServer::Request&, HttpServer::Response& response, - HttpServer::HandlerContext& + HttpServer::HandlerContext& context ){ handler_started = true; gate.started.notify(); gate.wait_for_release(HTTP_TEST_WAIT); + // Then stay alive while the connection is alive: the pattern + // a long-running tool call uses. stop() marks the connection + // dead before it waits for this handler (the socket's + // close_connection() close suppresses the disconnect + // callback), so the loop must terminate - without that mark + // this would spin forever and stop() would hang. + while (context.connection_alive()){ + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + saw_disconnect_on_stop = !context.connection_alive(); response.status = 200; response.body = "late"; }; @@ -1107,6 +1225,10 @@ class Test_Http_StopDuringHandler : public TestHttpServer{ } } + if (error.empty() && !saw_disconnect_on_stop.load()){ + error = "The handler did not see the connection as gone after stop()"; + } + if (error.empty()){ // Start again (on a fresh port) and serve a fresh request. auto simple_handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ @@ -1132,8 +1254,8 @@ class Test_Http_StopDuringHandler : public TestHttpServer{ "Connection: keep-alive\r\n" "\r\n" "ok"; - if (!client.wait_for_bytes(expected.size()) || client.received != expected){ - error = "The restarted server did not answer:\n" + client.received; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The restarted server did not answer:\n" + client.received_snapshot(); } } } @@ -1222,6 +1344,9 @@ void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool){ database.add( thread_pool, "HttpServer - Oversized content-length is rejected" ); + database.add( + thread_pool, "HttpServer - An oversized header section is rejected" + ); database.add( thread_pool, "HttpServer - A connection over the buffer cap is closed" ); From 682e922d1d47bd0b1a4ba757020d73d32f3f28b5 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Fri, 13 Nov 2026 22:23:54 -0800 Subject: [PATCH 6/6] Cleanup --- Common/Cpp/HttpServer/HttpServer.cpp | 101 ++++++++++++++++++--------- Common/Cpp/HttpServer/HttpServer.h | 49 +++++-------- 2 files changed, 85 insertions(+), 65 deletions(-) diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp index 828e5efc6b..b4c97cbab1 100644 --- a/Common/Cpp/HttpServer/HttpServer.cpp +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -2,6 +2,35 @@ * * From: https://github.com/PokemonAutomation/ * + * Implementation notes: + * - The socket's receive/disconnect callbacks only append data to the + * per-connection buffer and flip flags. One "connection task" per + * connection (from the thread pool) does all parsing and writing, so + * the handler may run for a long time without stalling the socket's + * receive loop. + * - Every write goes through "AbstractServerSocket::send()" with the + * connection's socket id (captured at accept time), so a response + * that outlives its connection - the peer disconnected and the next + * client was accepted in the meantime - is refused by the socket + * instead of reaching the new client's stream. + * - "Expect: 100-continue" is answered as soon as the headers are + * validated, while the body is still pending (a waiting client is + * blocked on that interim response). + * - Task handles live in "m_tasks" under a dedicated "m_task_lock" + * (the socket library's "m_task_lock" pattern): destroying an + * "AsyncTask" handle blocks until that task finishes, and the task's + * final cleanup takes "m_lock", so a handle must never be destroyed + * while holding "m_lock". "on_connect()" may therefore find the + * previous connection's task still finishing (for example, still in a + * long handler call); its handle is kept and reaped later. + * - "stop()" marks the active connection dead before it waits for the + * in-flight task: the socket suppresses its disconnect callback for a + * close_connection() close, so without the mark a handler polling + * "connection_alive()" would never see the shutdown. "stop()" then + * moves the socket out under "m_lock" and destroys it after releasing + * the lock: "~ServerSocket" joins the socket's worker threads, and + * their callbacks take "m_lock". + * */ #include @@ -276,8 +305,8 @@ void HttpServer::on_connect(){ { std::lock_guard lg(m_lock); socket = m_socket.get(); - if (socket != nullptr - && !m_stop_requested.load(std::memory_order_acquire) + if (socket != nullptr && + !m_stop_requested.load(std::memory_order_acquire) ){ conn = std::make_shared(); // The previous socket connection is over (the socket only @@ -318,22 +347,20 @@ void HttpServer::on_connect(){ connection_task(conn, socket); } ); - { - std::lock_guard lg(m_task_lock); - // Reap finished handles (destroying one that is already - // finished is a no-op). Keep unfinished ones: the - // previous connection's task may still be finishing up - // (for example, still inside a long handler call) by the - // time this connection is accepted; it holds only its - // own (dead) connection. - m_tasks.erase( - std::remove_if(m_tasks.begin(), m_tasks.end(), [](const AsyncTask& task){ - return task.is_finished(); - }), - m_tasks.end() - ); - m_tasks.push_back(std::move(task)); - } + std::lock_guard lg(m_task_lock); + // Reap finished handles (destroying one that is already + // finished is a no-op). Keep unfinished ones: the + // previous connection's task may still be finishing up + // (for example, still inside a long handler call) by the + // time this connection is accepted; it holds only its + // own (dead) connection. + m_tasks.erase( + std::remove_if(m_tasks.begin(), m_tasks.end(), [](const AsyncTask& task){ + return task.is_finished(); + }), + m_tasks.end() + ); + m_tasks.push_back(std::move(task)); }catch (...){ // The task could not be started: drop the connection and // keep listening. @@ -398,8 +425,8 @@ void HttpServer::on_disconnect(const std::string& error_message){ // disconnect is about the previous connection, which on_connect() // has already marked dead; the current one belongs to the new // client and must not be killed. - if (m_socket == nullptr - || m_socket->state() != AbstractServerSocket::State::CONNECTED + if (m_socket == nullptr || + m_socket->state() != AbstractServerSocket::State::CONNECTED ){ conn = m_connection; } @@ -413,7 +440,10 @@ void HttpServer::on_disconnect(const std::string& error_message){ -void HttpServer::connection_task(const std::shared_ptr& conn, AbstractServerSocket* socket){ +void HttpServer::connection_task( + const std::shared_ptr& conn, + AbstractServerSocket* socket +){ // The handler may be replaced or cleared by start()/stop() while this // task is waiting; a copy taken under m_lock is stable for the task's // whole lifetime, because stop() waits for the task before clearing it. @@ -458,22 +488,20 @@ void HttpServer::connection_task(const std::shared_ptr& conn, Abstra } } - if (parsed.state == ParseState::NEED_MORE - || parsed.state == ParseState::NEED_BODY + if (parsed.state == ParseState::NEED_MORE || + parsed.state == ParseState::NEED_BODY ){ // Wait for the rest of this request, a disconnect, or a // stop. The predicate requires the buffer to have grown past // what the parse just saw: with a partial request, // "!buffer.empty()" alone is permanently true and would spin // this loop at 100% CPU until the request completes. - { - std::unique_lock lg(conn->lock); - conn->cv.wait_for(lg, std::chrono::milliseconds(10), [this, &conn, buffer_size]{ - return conn->buffer.size() != buffer_size - || conn->dead.load(std::memory_order_relaxed) - || m_stop_requested.load(std::memory_order_relaxed); - }); - } + std::unique_lock lg(conn->lock); + conn->cv.wait_for(lg, std::chrono::milliseconds(10), [this, &conn, buffer_size]{ + return conn->buffer.size() != buffer_size + || conn->dead.load(std::memory_order_relaxed) + || m_stop_requested.load(std::memory_order_relaxed); + }); continue; } @@ -739,7 +767,10 @@ HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ } -void HttpServer::end_connection(const std::shared_ptr& conn, AbstractServerSocket* socket){ +void HttpServer::end_connection( + const std::shared_ptr& conn, + AbstractServerSocket* socket +){ conn->dead.store(true, std::memory_order_release); conn->cv.notify_all(); // Close the socket's connection only while it still belongs to this @@ -756,7 +787,11 @@ void HttpServer::end_connection(const std::shared_ptr& conn, Abstrac } -bool HttpServer::write_raw(const std::shared_ptr& conn, AbstractServerSocket* socket, const std::string& bytes){ +bool HttpServer::write_raw( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const std::string& bytes +){ // "conn->socket_id" is the socket's connection token when this // connection was accepted: if the socket has moved on to the next // client since then, the send is refused instead of being delivered diff --git a/Common/Cpp/HttpServer/HttpServer.h b/Common/Cpp/HttpServer/HttpServer.h index 2391f3428e..a770ea88fb 100644 --- a/Common/Cpp/HttpServer/HttpServer.h +++ b/Common/Cpp/HttpServer/HttpServer.h @@ -8,35 +8,6 @@ * response types, the handler contract, and the threading rules are in * "AbstractHttpServer.h"; this file documents the implementation. * - * Implementation notes: - * - The socket's receive/disconnect callbacks only append data to the - * per-connection buffer and flip flags. One "connection task" per - * connection (from the thread pool) does all parsing and writing, so - * the handler may run for a long time without stalling the socket's - * receive loop. - * - Every write goes through "AbstractServerSocket::send()" with the - * connection's socket id (captured at accept time), so a response - * that outlives its connection - the peer disconnected and the next - * client was accepted in the meantime - is refused by the socket - * instead of reaching the new client's stream. - * - "Expect: 100-continue" is answered as soon as the headers are - * validated, while the body is still pending (a waiting client is - * blocked on that interim response). - * - Task handles live in "m_tasks" under a dedicated "m_task_lock" - * (the socket library's "m_task_lock" pattern): destroying an - * "AsyncTask" handle blocks until that task finishes, and the task's - * final cleanup takes "m_lock", so a handle must never be destroyed - * while holding "m_lock". "on_connect()" may therefore find the - * previous connection's task still finishing (for example, still in a - * long handler call); its handle is kept and reaped later. - * - "stop()" marks the active connection dead before it waits for the - * in-flight task: the socket suppresses its disconnect callback for a - * close_connection() close, so without the mark a handler polling - * "connection_alive()" would never see the shutdown. "stop()" then - * moves the socket out under "m_lock" and destroys it after releasing - * the lock: "~ServerSocket" joins the socket's worker threads, and - * their callbacks take "m_lock". - * */ #ifndef PokemonAutomation_HttpServer_H @@ -88,6 +59,8 @@ class HttpServer : public AbstractHttpServer, private AbstractServerSocket::List virtual void on_receive_data(const void* data, size_t bytes) override; virtual void on_disconnect(const std::string& error_message) override; + +private: // One request that is waiting to be parsed out of the buffer. enum class ParseState{ NEED_MORE, @@ -130,7 +103,10 @@ class HttpServer : public AbstractHttpServer, private AbstractServerSocket::List // Runs on a thread from the thread pool, one instance per // connection. - void connection_task(const std::shared_ptr& conn, AbstractServerSocket* socket); + void connection_task( + const std::shared_ptr& conn, + AbstractServerSocket* socket + ); // Parses one complete request out of "buffer", removing it on // success. Must be called with "conn->lock" held. @@ -138,11 +114,18 @@ class HttpServer : public AbstractHttpServer, private AbstractServerSocket::List // Marks the connection dead and closes it at the socket level. No-op // if the socket is no longer connected to this client. - void end_connection(const std::shared_ptr& conn, AbstractServerSocket* socket); + void end_connection( + const std::shared_ptr& conn, + AbstractServerSocket* socket + ); // Writes "bytes" to the connection completely; returns false (and // ends the connection) if the peer went away or the write stalled. - bool write_raw(const std::shared_ptr& conn, AbstractServerSocket* socket, const std::string& bytes); + bool write_raw( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const std::string& bytes + ); // Formats and writes the final response. bool write_response( @@ -158,6 +141,8 @@ class HttpServer : public AbstractHttpServer, private AbstractServerSocket::List // Builds the wire format of a response. static std::string format_response(const Response& response, bool keep_alive); + +private: ThreadPool& m_thread_pool; // Guards m_socket, m_connection, and m_handler. Never held across a