From 54c3a55b78056e9c1d43b14b65e27afa794dd62a Mon Sep 17 00:00:00 2001 From: Lord-Turmoil Date: Sun, 1 Dec 2024 13:35:06 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=92=EF=B8=8Fadd=20default=20error=20handl?= =?UTF-8?q?ers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/demo.sh | 2 -- src/common/Http.cpp | 2 ++ src/core/IRequestDispatcher.cpp | 7 ++--- src/core/WebHostBuilder.cpp | 5 ++++ src/impl/DefaultHandlers.cpp | 46 +++++++++++++++++++++++++++++++++ src/impl/DefaultHandlers.h | 24 +++++++++++++++++ 6 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/impl/DefaultHandlers.cpp create mode 100644 src/impl/DefaultHandlers.h diff --git a/scripts/demo.sh b/scripts/demo.sh index f8a84da..cd5682b 100755 --- a/scripts/demo.sh +++ b/scripts/demo.sh @@ -43,8 +43,6 @@ function send_requests() { curl -s -X POST -H "Content-Type: text/plain" -d "$text" $BASE_URL/echo fi echo "" - # rand=$(( RANDOM % 3 )) # sleep 0-3 seconds - # sleep $rand done } diff --git a/src/common/Http.cpp b/src/common/Http.cpp index ee3f403..ff7980a 100644 --- a/src/common/Http.cpp +++ b/src/common/Http.cpp @@ -43,6 +43,8 @@ const char* StatusCodeToDescription(int statusCode) return "Bad Request"; case NOT_FOUND: return "Not Found"; + case METHOD_NOT_ALLOWED: + return "Method Not Allowed"; case INTERNAL_SERVER_ERROR: return "Internal Server Error"; default: diff --git a/src/core/IRequestDispatcher.cpp b/src/core/IRequestDispatcher.cpp index 1bbf389..77ea5cf 100644 --- a/src/core/IRequestDispatcher.cpp +++ b/src/core/IRequestDispatcher.cpp @@ -84,12 +84,13 @@ void IRequestDispatcher::Dispatch(const Ref& context) } else { - _logger->Warn("No handler found for path '{}'", path); + _logger->Warn("No handler found for '{} {}'", http::HttpMethodToString(context->Request.Method), path); } if (statusCode != http::status::OK) { - _logger->Error("Error handling request {}: {} {}", path, statusCode, http::StatusCodeToDescription(statusCode)); + _logger->Debug("Error occurred when handling request {}: {} {}", path, statusCode, + http::StatusCodeToDescription(statusCode)); handler = _GetErrorHandler(statusCode); if (handler) { @@ -102,7 +103,7 @@ void IRequestDispatcher::Dispatch(const Ref& context) } DestroyHttpContext(context); - _logger->Debug("Request {} handled", path); + _logger->Debug("Request '{}' handled", path); } void IRequestDispatcher::SetLogger(const Ref& logger) diff --git a/src/core/WebHostBuilder.cpp b/src/core/WebHostBuilder.cpp index df65dd2..61f7bfb 100644 --- a/src/core/WebHostBuilder.cpp +++ b/src/core/WebHostBuilder.cpp @@ -8,6 +8,8 @@ #include "components/MayhemServer.h" #include "components/RequestDispatcher.h" +#include "impl/DefaultHandlers.h" + #include #include #include @@ -190,6 +192,9 @@ void WebHostBuilder::_InitializeComponents() auto dispatcher = _container->Resolve(); dispatcher->SetLogger(GetLogger("RequestDispatcher")); + + // Register default handlers. + impl::RegisterDefaultHandlers(shared_from_this()); } void WebHostBuilder::_Preamble() const diff --git a/src/impl/DefaultHandlers.cpp b/src/impl/DefaultHandlers.cpp new file mode 100644 index 0000000..fa3f414 --- /dev/null +++ b/src/impl/DefaultHandlers.cpp @@ -0,0 +1,46 @@ +#include "impl/DefaultHandlers.h" + +#include "minet/core/WebHostBuilder.h" + +MINET_BEGIN + +namespace impl +{ + +static void DefaultErrorHandler(int statusCode, TextResponse& response) +{ + response.SetStatusCode(statusCode); + response.Text().assign(http::StatusCodeToDescription(statusCode)); +} + +void BadRequestHandler(const TextRequest& request, TextResponse& response) +{ + DefaultErrorHandler(http::status::BAD_REQUEST, response); +} + +void MethodNotAllowedHandler(const TextRequest& request, TextResponse& response) +{ + DefaultErrorHandler(http::status::METHOD_NOT_ALLOWED, response); +} + +void NotFoundHandler(const TextRequest& request, TextResponse& response) +{ + DefaultErrorHandler(http::status::NOT_FOUND, response); +} + +void InternalServerErrorHandler(const TextRequest& request, TextResponse& response) +{ + DefaultErrorHandler(http::status::INTERNAL_SERVER_ERROR, response); +} + +void RegisterDefaultHandlers(const Ref& builder) +{ + builder->Error(http::status::BAD_REQUEST, RequestHandler::Bind(BadRequestHandler)) + ->Error(http::status::METHOD_NOT_ALLOWED, RequestHandler::Bind(MethodNotAllowedHandler)) + ->Error(http::status::NOT_FOUND, RequestHandler::Bind(NotFoundHandler)) + ->Error(http::status::INTERNAL_SERVER_ERROR, RequestHandler::Bind(InternalServerErrorHandler)); +} + +}; // namespace impl + +MINET_END \ No newline at end of file diff --git a/src/impl/DefaultHandlers.h b/src/impl/DefaultHandlers.h new file mode 100644 index 0000000..1412363 --- /dev/null +++ b/src/impl/DefaultHandlers.h @@ -0,0 +1,24 @@ +#pragma once + +#include "minet/components/RequestHandler.h" + +MINET_BEGIN + +class WebHostBuilder; + +namespace impl +{ + +void BadRequestHandler(const TextRequest& request, TextResponse& response); + +void MethodNotAllowedHandler(const TextRequest& request, TextResponse& response); + +void NotFoundHandler(const TextRequest& request, TextResponse& response); + +void InternalServerErrorHandler(const TextRequest& request, TextResponse& response); + +void RegisterDefaultHandlers(const Ref& builder); + +} // namespace impl + +MINET_END