From 9a29874f9f2f388ab4bf7e286f2e8e94c938cea7 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 4 Dec 2017 15:08:18 +0000 Subject: [PATCH 1/3] Better verbose message. The parsing of the request in the `RequestContext` constructor may be buggy and make kiwix-serve crash. If we print debug information only after the request is parsed, we will never print the debug information if the parsing crash. It is better to, at least, write that we've got a new request to avoid us to try to debug previous request where everything were ok. --- src/server/kiwix-serve.cpp | 16 +++++++++++----- src/server/request_context.cpp | 2 -- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index 1102fdb..29862ee 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -724,17 +724,23 @@ static int accessHandlerCallback(void* cls, size_t* upload_data_size, void** ptr) { + if (isVerbose.load() ) { + printf("======================\n"); + printf("Requesting : \n"); + printf("full_url : %s\n", url); + } RequestContext request(connection, rootLocation, url, method, version); + + if (isVerbose.load() ) { + request.print_debug_info(); + } /* Unexpected method */ if (request.get_method() != RequestMethod::GET && request.get_method() != RequestMethod::POST) { + printf("Reject request because of unhandled request method.\n"); + printf("----------------------\n"); return MHD_NO; } - if (isVerbose.load()) { - printf("======================\n"); - request.print_debug_info(); - } - /* Prepare the variables */ struct MHD_Response* response; request.httpResponseCode = request.has_range() ? MHD_HTTP_PARTIAL_CONTENT : MHD_HTTP_OK; diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 9caa1dc..212f660 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -127,8 +127,6 @@ int RequestContext::fill_argument(void *__this, enum MHD_ValueKind kind, } void RequestContext::print_debug_info() { - printf("Requesting : \n"); - printf("full_url : %s\n", full_url.c_str()); printf("method : %s (%d)\n", method==RequestMethod::GET ? "GET" : method==RequestMethod::POST ? "POST" : "OTHER", method); From 9eaf512bf94e1729a2aeb146198d18d5fdb06a96 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 4 Dec 2017 15:10:45 +0000 Subject: [PATCH 2/3] Correctly handle NULL string for request arguments. When iterating over arguments of a request, the value can be a null pointer if the argument is provided without a `=`. https://www.gnu.org/software/libmicrohttpd/manual/html_node/microhttpd_002drequests.html We have to handle this correctly. Should fix #116. --- src/server/request_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 212f660..8ccd5f6 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -122,7 +122,7 @@ int RequestContext::fill_argument(void *__this, enum MHD_ValueKind kind, const char *key, const char* value) { RequestContext *_this = static_cast(__this); - _this->arguments[key] = value; + _this->arguments[key] = value == nullptr ? "" : value; return MHD_YES; } From 5c11ff1a305e463d0b78782555b14acb458d99ad Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 4 Dec 2017 15:12:27 +0000 Subject: [PATCH 3/3] Correctly cast to int in debug message. A `class enum` is not implicitly cast to an `int`. It is better to explicitly cast it to avoid future error with different versions of compiler. --- src/server/request_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 8ccd5f6..d2e680a 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -129,7 +129,7 @@ int RequestContext::fill_argument(void *__this, enum MHD_ValueKind kind, void RequestContext::print_debug_info() { printf("method : %s (%d)\n", method==RequestMethod::GET ? "GET" : method==RequestMethod::POST ? "POST" : - "OTHER", method); + "OTHER", (int)method); printf("version : %s\n", version.c_str()); printf("headers :\n"); for (auto it=headers.begin(); it!=headers.end(); it++) {