From 9a1425265a1d518aaf87cde01f9aef741a609b2d Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 20 Oct 2023 14:44:53 +0200 Subject: improve error handling when parsing http requests (#489) * catch exceptions in HttpRequestParser::OnMessageComplete() callback --- src/zenhttp/servers/httpparser.cpp | 42 ++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'src/zenhttp/servers/httpparser.cpp') diff --git a/src/zenhttp/servers/httpparser.cpp b/src/zenhttp/servers/httpparser.cpp index 6b987151a..c64134c95 100644 --- a/src/zenhttp/servers/httpparser.cpp +++ b/src/zenhttp/servers/httpparser.cpp @@ -2,6 +2,7 @@ #include "httpparser.h" +#include #include #include @@ -62,7 +63,6 @@ HttpRequestParser::ConsumeData(const char* InputData, size_t DataSize) ZEN_WARN("HTTP parser error {} ('{}'). Closing connection", http_errno_name(HttpErrno), http_errno_description(HttpErrno)); return ~0ull; } - return ConsumedBytes; } @@ -360,11 +360,41 @@ HttpRequestParser::OnMessageBegin() int HttpRequestParser::OnMessageComplete() { - m_Connection.HandleRequest(); - - ResetState(); - - return 0; + try + { + m_Connection.HandleRequest(); + ResetState(); + return 0; + } + catch (std::system_error& SystemError) + { + if (IsOOM(SystemError.code())) + { + ZEN_WARN("out of memory when processing http request: '{}'", SystemError.what()); + } + else if (IsOOD(SystemError.code())) + { + ZEN_WARN("out of disk space when processing http request: '{}'", SystemError.what()); + } + else + { + ZEN_ERROR("failed processing http request: '{}'", SystemError.what()); + } + ResetState(); + return 1; + } + catch (std::bad_alloc& BadAlloc) + { + ZEN_WARN("out of memory when processing http request: '{}'", BadAlloc.what()); + ResetState(); + return 1; + } + catch (std::exception& Ex) + { + ZEN_ERROR("failed processing http request: '{}'", Ex.what()); + ResetState(); + return 1; + } } } // namespace zen -- cgit v1.2.3