Closed
Description
Hi,
I would like to report a recent problem that I found on the latest version 0.18.5
To test it, I compiled this simple program:
#include <httplib.h>
int main(void)
{
using namespace httplib;
Server svr;
svr.set_pre_routing_handler([](const auto& req, auto& res) {
if (req.method == "OPTIONS") {
res.set_content("world", "text/html");
return Server::HandlerResponse::Handled;
}
return Server::HandlerResponse::Unhandled;
});
svr.Get("/hi", [](const Request& req, Response& res) {
res.set_content("Hello World!", "text/plain");
});
// Match the request path against a regular expression
// and extract its captures
svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
auto numbers = req.matches[1];
res.set_content(numbers, "text/plain");
});
// Capture the second segment of the request path as "id" path param
svr.Get("/users/:id", [&](const Request& req, Response& res) {
auto user_id = req.path_params.at("id");
res.set_content(user_id, "text/plain");
});
// Extract values from HTTP headers and URL query params
svr.Get("/body-header-param", [](const Request& req, Response& res) {
if (req.has_header("Content-Length")) {
auto val = req.get_header_value("Content-Length");
}
if (req.has_param("key")) {
auto val = req.get_param_value("key");
}
res.set_content(req.body, "text/plain");
});
svr.Get("/stop", [&](const Request& req, Response& res) {
svr.stop();
});
svr.listen("0.0.0.0", 8080);
}
Upon running it, I place a breakpoint when calling parse_request_line
// Request line and headers
if (!parse_request_line(line_reader.ptr(), req) || // <== breakpoint
!detail::read_headers(strm, req.headers)) {
res.status = StatusCode::BadRequest_400;
return write_response(strm, close_connection, req, res);
}
The buffer of stream_line_reader
seems to contains data from somewhere else:


Please note that, this happens 80% of the time. When it work correctly, the buffer must contains HTTP request data:

When the server first started, the first request never has this problem. This suggest that there maybe race condition between threads when accessing a shared variable.