diff --git a/gateway/httpgateway.cpp b/gateway/httpgateway.cpp index c0fdaea..248630a 100644 --- a/gateway/httpgateway.cpp +++ b/gateway/httpgateway.cpp @@ -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 = "
%s
"; + 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); + }