|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// clf_access_log.cpp -- Common Log Format access logger written as a |
| 22 | +// response_sent lifecycle hook, demonstrating the resolution of issues |
| 23 | +// #281 and #69. libhttpserver v1's log_access(fn) callback handed the |
| 24 | +// user a single string and was invoked at request-arrival time, so the |
| 25 | +// status code, byte count, and request duration were not yet known. |
| 26 | +// Issues #281 and #69 asked for a logger that could emit a real CLF / |
| 27 | +// `time-taken` line. |
| 28 | +// |
| 29 | +// In v2.0 the response_sent hook (DR-012 §4.10) fires immediately after |
| 30 | +// MHD_queue_response and carries the structured context users have been |
| 31 | +// asking for: |
| 32 | +// - ctx.status -- HTTP status code |
| 33 | +// - ctx.bytes_queued -- logical body size (Content-Length, when known) |
| 34 | +// - ctx.elapsed -- steady_clock nanoseconds from |
| 35 | +// answer_to_connection's first invocation |
| 36 | +// |
| 37 | +// This example uses those fields to emit a Common Log Format line on |
| 38 | +// stdout. The library no longer hard-codes a logging format -- you do. |
| 39 | +// |
| 40 | +// Run: |
| 41 | +// ./clf_access_log # blocks; serves on :8080 |
| 42 | +// curl http://localhost:8080/hello |
| 43 | +// curl http://localhost:8080/hello?name=world |
| 44 | +// |
| 45 | +// Expected output (per request): |
| 46 | +// - - - [26/May/2026:14:01:23 +0000] "GET /hello HTTP/1.1" 200 13 1 |
| 47 | + |
| 48 | +#include <chrono> |
| 49 | +#include <cstdint> |
| 50 | +#include <cstdio> |
| 51 | +#include <ctime> |
| 52 | +#include <functional> |
| 53 | +#include <memory> |
| 54 | +#include <string> |
| 55 | +#include <string_view> |
| 56 | + |
| 57 | +#include <httpserver.hpp> |
| 58 | + |
| 59 | +namespace hs = httpserver; |
| 60 | + |
| 61 | +namespace { |
| 62 | + |
| 63 | +class hello_resource : public hs::http_resource { |
| 64 | + public: |
| 65 | + hs::http_response render_get(const hs::http_request&) override { |
| 66 | + return hs::http_response::string("Hello, World!"); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +// SECURITY (CWE-117): sanitize a string_view for CLF output. |
| 71 | +// Replace ASCII control characters (< 0x20 or == 0x7F) with '-' to |
| 72 | +// prevent a client from injecting additional log lines by embedding |
| 73 | +// newlines or carriage-returns in the request path or method. |
| 74 | +std::string sanitize_clf(std::string_view sv) { |
| 75 | + std::string out; |
| 76 | + out.reserve(sv.size()); |
| 77 | + for (unsigned char c : sv) { |
| 78 | + out += (c < 0x20 || c == 0x7f) ? '-' : static_cast<char>(c); |
| 79 | + } |
| 80 | + return out; |
| 81 | +} |
| 82 | + |
| 83 | +void emit_clf_line(const hs::response_sent_ctx& ctx) { |
| 84 | + std::time_t now = std::time(nullptr); |
| 85 | + char ts[32]; |
| 86 | + std::tm tm_buf; |
| 87 | +#if defined(_WIN32) && !defined(__CYGWIN__) |
| 88 | + localtime_s(&tm_buf, &now); |
| 89 | +#else |
| 90 | + localtime_r(&now, &tm_buf); |
| 91 | +#endif |
| 92 | + std::strftime(ts, sizeof(ts), "%d/%b/%Y:%H:%M:%S %z", &tm_buf); |
| 93 | + |
| 94 | + int64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 95 | + ctx.elapsed).count(); |
| 96 | + std::string method, path; |
| 97 | + if (ctx.request != nullptr) { |
| 98 | + // Sanitize before embedding in the CLF line to prevent log injection. |
| 99 | + method = sanitize_clf(ctx.request->get_method()); |
| 100 | + path = sanitize_clf(ctx.request->get_path()); |
| 101 | + } |
| 102 | + std::printf("- - - [%s] \"%s %s HTTP/1.1\" %d %zu %lld\n", |
| 103 | + ts, |
| 104 | + method.c_str(), |
| 105 | + path.c_str(), |
| 106 | + ctx.status, |
| 107 | + ctx.bytes_queued, |
| 108 | + static_cast<long long>(ms)); // NOLINT(runtime/int) |
| 109 | + std::fflush(stdout); |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace |
| 113 | + |
| 114 | +int main() { |
| 115 | + hs::webserver ws{hs::create_webserver(8080)}; |
| 116 | + |
| 117 | + auto h = ws.add_hook(hs::hook_phase::response_sent, |
| 118 | + std::function<void(const hs::response_sent_ctx&)>(emit_clf_line)); |
| 119 | + |
| 120 | + auto resource = std::make_shared<hello_resource>(); |
| 121 | + ws.register_path("/hello", resource); |
| 122 | + |
| 123 | + ws.start(true); |
| 124 | + return 0; |
| 125 | +} |
0 commit comments