+ implement the HTTP 404 error in kiwix-serve

This commit is contained in:
kelson42 2011-01-08 13:45:12 +00:00
parent 11bab4f9ac
commit ffc79aa842

View File

@ -199,6 +199,8 @@ static int accessHandlerCallback(void *cls,
string content = ""; string content = "";
string mimeType = ""; string mimeType = "";
unsigned int contentLength = 0; unsigned int contentLength = 0;
bool found = true;
int httpResponseCode = MHD_HTTP_OK;
if (!strcmp(url, "/search") && hasSearchIndex) { if (!strcmp(url, "/search") && hasSearchIndex) {
const char* pattern = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "pattern"); 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; cout << "Loading '" << urlStr << "'... " << endl;
try { try {
reader->getContentByUrl(urlStr, content, contentLength, mimeType); found = reader->getContentByUrl(urlStr, content, contentLength, mimeType);
if (verboseFlag) { if (verboseFlag) {
cout << "content size: " << contentLength << endl; if (found) {
cout << "mimeType: " << mimeType << endl; cout << "Found " << urlStr << endl;
cout << "content size: " << contentLength << endl;
cout << "mimeType: " << mimeType << endl;
} else {
cout << "Failed to find " << urlStr << endl;
content = "<h1>Not Found</h1><p>The requested URL " + urlStr + "was not found on this server.</p>" ;
mimeType = "text/html";
httpResponseCode = MHD_HTTP_NOT_FOUND;
}
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
@ -279,9 +289,6 @@ static int accessHandlerCallback(void *cls,
pthread_mutex_unlock(&compressorLock); pthread_mutex_unlock(&compressorLock);
} }
/* clear context pointer */
*ptr = NULL;
/* Create the response */ /* Create the response */
response = MHD_create_response_from_data(contentLength, response = MHD_create_response_from_data(contentLength,
(void *)content.data(), (void *)content.data(),
@ -296,12 +303,15 @@ static int accessHandlerCallback(void *cls,
/* Specify the mime type */ /* Specify the mime type */
MHD_add_response_header(response, "Content-Type", mimeType.c_str()); 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) */ /* Force to close the connection - cf. 100% CPU usage with v. 4.4 (in Lucid) */
MHD_add_response_header(response, "Connection", "close"); MHD_add_response_header(response, "Connection", "close");
/* Queue the response */ /* Queue the response */
int ret = MHD_queue_response(connection, int ret = MHD_queue_response(connection,
MHD_HTTP_OK, httpResponseCode,
response); response);
MHD_destroy_response(response); MHD_destroy_response(response);