diff --git a/Source/RequestHandler.cpp b/Source/RequestHandler.cpp index 9cb5f60..2d8a132 100644 --- a/Source/RequestHandler.cpp +++ b/Source/RequestHandler.cpp @@ -1,6 +1,10 @@ #include "Includes/VWeb.h" #include "StringUtils.h" +#include +#include +#include + namespace VWeb { HttpMethod StringToHTTPMethod(std::string &method) { @@ -82,6 +86,12 @@ void ParseParameters(Request &request, RequestHandler &requestHandler) { } } } +std::string faultyRequest = R"_( +HTTP/1.1 500 Bad Gateway\r +Content-Type: text/html\r +Content-Length: 0\r +ETag: "66369d26-1f1"\r\n +)_"; struct RequestJob : public WorkerJob { Ref MRequest{}; @@ -90,28 +100,36 @@ struct RequestJob : public WorkerJob { RequestHandler *MRequestHandler{nullptr}; void Execute() override { - if (!ParseRequest(MRequest)) { - fprintf(stderr, "[Request] >> Request failed to parse\n"); - SocketUtils::Close(MRequest->ID); - return; + try { + if (!ParseRequest(MRequest)) { + fprintf(stderr, "[Request] >> Request failed to parse\n"); + SocketUtils::Close(MRequest->ID); + return; + } + ParseParameters(*MRequest, *MRequestHandler); + MRequest->CookieData = CreateRef(); + MRequest->SessionData = CreateRef(); + Ref response; + auto preValue = MMiddleWareHandler->HandlePre(MRequest); + if (preValue.has_value()) { + response = preValue.value(); + response->SessionData = MRequest->SessionData; + response->CookieData = MRequest->CookieData; + } else { + response = MRouter->HandleRoute(MRequest); + } + MMiddleWareHandler->HandlePost(MRequest, response); + auto content = response->GetResponse(); + MRequestHandler->AddSendResponse( + {0, (ssize_t)content.size(), MRequest->ID, content}); + MMiddleWareHandler->Shutdown(MRequest, response); + } catch (const std::exception &e) { + fprintf(stderr, + "[VWEB] >> Failed to handle Requests... Error thrown\n%s\n", + e.what()); + MRequestHandler->AddSendResponse( + {0, (ssize_t)faultyRequest.size(), MRequest->ID, faultyRequest}); } - ParseParameters(*MRequest, *MRequestHandler); - MRequest->CookieData = CreateRef(); - MRequest->SessionData = CreateRef(); - Ref response; - auto preValue = MMiddleWareHandler->HandlePre(MRequest); - if (preValue.has_value()) { - response = preValue.value(); - response->SessionData = MRequest->SessionData; - response->CookieData = MRequest->CookieData; - } else { - response = MRouter->HandleRoute(MRequest); - } - MMiddleWareHandler->HandlePost(MRequest, response); - auto content = response->GetResponse(); - MRequestHandler->AddSendResponse( - {0, (ssize_t)content.size(), MRequest->ID, content}); - MMiddleWareHandler->Shutdown(MRequest, response); } }; RequestHandler::RequestHandler(const Ref &manager)