From ffc79aa8423926e5882f27f64e0f7100b4892ad8 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Sat, 8 Jan 2011 13:45:12 +0000 Subject: [PATCH] + implement the HTTP 404 error in kiwix-serve --- src/server/kiwix-serve.cpp | 40 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index 6ba1f62..6def91a 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -199,6 +199,8 @@ static int accessHandlerCallback(void *cls, string content = ""; string mimeType = ""; unsigned int contentLength = 0; + bool found = true; + int httpResponseCode = MHD_HTTP_OK; if (!strcmp(url, "/search") && hasSearchIndex) { const char* pattern = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "pattern"); @@ -243,11 +245,19 @@ static int accessHandlerCallback(void *cls, cout << "Loading '" << urlStr << "'... " << endl; try { - reader->getContentByUrl(urlStr, content, contentLength, mimeType); + found = reader->getContentByUrl(urlStr, content, contentLength, mimeType); if (verboseFlag) { - cout << "content size: " << contentLength << endl; - cout << "mimeType: " << mimeType << endl; + if (found) { + cout << "Found " << urlStr << endl; + cout << "content size: " << contentLength << endl; + cout << "mimeType: " << mimeType << endl; + } else { + cout << "Failed to find " << urlStr << endl; + content = "

Not Found

The requested URL " + urlStr + "was not found on this server.

" ; + mimeType = "text/html"; + httpResponseCode = MHD_HTTP_NOT_FOUND; + } } } catch (const std::exception& e) { std::cerr << e.what() << std::endl; @@ -256,52 +266,52 @@ static int accessHandlerCallback(void *cls, /* Mutex unlock */ pthread_mutex_unlock(&readerLock); } - + /* Rewrite the content (add the search box) */ if (hasSearchIndex && mimeType.find("text/html") != string::npos) { appendToFirstOccurence(content, "", HTMLScripts); appendToFirstOccurence(content, "]*>", HTMLDiv); } - + /* Compute the lengh */ contentLength = content.size(); - + /* Compress the content if necessary */ if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) { pthread_mutex_lock(&compressorLock); comprLen = COMPRESSOR_BUFFER_SIZE; - + compress(compr, &comprLen, (const Bytef*)(content.data()), contentLength); - + content = string((char *)compr, comprLen); contentLength = comprLen; - + pthread_mutex_unlock(&compressorLock); } - - /* clear context pointer */ - *ptr = NULL; /* Create the response */ response = MHD_create_response_from_data(contentLength, (void *)content.data(), MHD_NO, MHD_YES); - + /* Add if necessary the content-encoding */ if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) { MHD_add_response_header(response, "Content-encoding", "deflate"); } - + /* Specify the mime type */ MHD_add_response_header(response, "Content-Type", mimeType.c_str()); + /* clear context pointer */ + *ptr = NULL; + /* Force to close the connection - cf. 100% CPU usage with v. 4.4 (in Lucid) */ MHD_add_response_header(response, "Connection", "close"); /* Queue the response */ int ret = MHD_queue_response(connection, - MHD_HTTP_OK, + httpResponseCode, response); MHD_destroy_response(response);