/* This file is part of libhttpserver Copyright (C) 2011-2026 Sebastiano Merlino This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ // Demonstrates the solution to issue #273: short-circuit large uploads // with a 413 BEFORE any body bytes are consumed. // // Register a `request_received` hook that inspects Content-Length; if // it exceeds the configured cap, return a respond_with(413) action. // libhttpserver aborts the upload -- the resource handler is not // invoked and the body bytes never cross the daemon's I/O boundary. #include #include #include #include #include namespace hs = httpserver; namespace { constexpr std::size_t kMaxUploadBytes = 1 * 1024 * 1024; // 1 MB } // namespace class upload_resource : public hs::http_resource { public: hs::http_response render_post(const hs::http_request&) override { return hs::http_response::string("UPLOAD OK"); } }; int main() { hs::webserver ws{hs::create_webserver(8080)}; auto h = ws.add_hook(hs::hook_phase::request_received, std::function( [](hs::request_received_ctx& ctx) { std::string cl{ctx.request->get_header("Content-Length")}; if (cl.empty()) return hs::hook_action::pass(); try { if (std::stoull(cl) > kMaxUploadBytes) { return hs::hook_action::respond_with( hs::http_response::empty().with_status(413)); } } catch (...) { // Malformed Content-Length: let the normal pipeline // produce a 400 elsewhere. } return hs::hook_action::pass(); })); auto resource = std::make_shared(); ws.register_path("/upload", resource); ws.start(true); // blocking return 0; }