Feature: Catch All Handler so the process does not die on failure

This commit is contained in:
Maurice Grönwoldt 2024-05-13 17:21:52 +02:00
parent 2bd34c2bb1
commit 516e33d165

View file

@ -1,6 +1,10 @@
#include "Includes/VWeb.h"
#include "StringUtils.h"
#include <cstdio>
#include <exception>
#include <sys/types.h>
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<Request> 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<Cookies>();
MRequest->SessionData = CreateRef<Session>();
Ref<Response> 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<Cookies>();
MRequest->SessionData = CreateRef<Session>();
Ref<Response> 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<SocketManager> &manager)