gateway: httpgateway: Add exception handler

Something at some point changed so this becomes necessar
This commit is contained in:
2025-11-03 20:02:02 +01:00
rodzic e90b08dbde
commit f0f5846a4a

Wyświetl plik

@@ -34,6 +34,15 @@ bool HttpGateway::keepReading()
Request HttpGateway::convertRequest(httplib::Request request)
{
for(auto &c : request.target)
{
if( !(c >= ' ' && c <= '~'))
{
throw std::runtime_error("Invalid chars in URI: " + utils::catv(request.target));
}
}
Request result;
result.setRequestMethod(request.method);
result.setUrl(request.target);
@@ -82,6 +91,26 @@ httplib::Response HttpGateway::convertResponse(Response response)
void HttpGateway::work(RequestWorker &worker)
{
httplib::Server server;
server.set_exception_handler([](const httplib::Request& req, httplib::Response& res, std::exception_ptr ep) {
auto fmt = "<h1>Error 500</h1><p>%s</p>";
char buf[BUFSIZ];
try
{
std::rethrow_exception(ep);
}
catch (std::exception &e)
{
snprintf(buf, sizeof(buf), fmt, e.what());
Logger::error() << "Exception caught in Httpgateway::work():" << utils::catv(e.what());
}
catch (...)
{
snprintf(buf, sizeof(buf), fmt, "Unknown Exception");
Logger::error() << "Unknown exception caught in Httpgateway::work()";
}
res.set_content(buf, "text/html");
res.status = 500;
});
server.set_payload_max_length(this->maxPayloadLength);
auto handler = [&](const httplib::Request &req, httplib::Response &res)
{
@@ -94,4 +123,5 @@ void HttpGateway::work(RequestWorker &worker)
server.Get("/(.*)", handler);
server.Post("/(.*)", handler);
server.listen(this->listenaddr.c_str(), this->listenport);
}