mirror of
https://github.com/kiwix/kiwix-tools.git
synced 2025-09-27 22:31:09 -04:00
+ implement the HTTP 404 error in kiwix-serve
This commit is contained in:
parent
11bab4f9ac
commit
ffc79aa842
@ -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;
|
||||||
@ -256,52 +266,52 @@ static int accessHandlerCallback(void *cls,
|
|||||||
/* Mutex unlock */
|
/* Mutex unlock */
|
||||||
pthread_mutex_unlock(&readerLock);
|
pthread_mutex_unlock(&readerLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Rewrite the content (add the search box) */
|
/* Rewrite the content (add the search box) */
|
||||||
if (hasSearchIndex && mimeType.find("text/html") != string::npos) {
|
if (hasSearchIndex && mimeType.find("text/html") != string::npos) {
|
||||||
appendToFirstOccurence(content, "<head>", HTMLScripts);
|
appendToFirstOccurence(content, "<head>", HTMLScripts);
|
||||||
appendToFirstOccurence(content, "<body[^>]*>", HTMLDiv);
|
appendToFirstOccurence(content, "<body[^>]*>", HTMLDiv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Compute the lengh */
|
/* Compute the lengh */
|
||||||
contentLength = content.size();
|
contentLength = content.size();
|
||||||
|
|
||||||
/* Compress the content if necessary */
|
/* Compress the content if necessary */
|
||||||
if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) {
|
if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) {
|
||||||
pthread_mutex_lock(&compressorLock);
|
pthread_mutex_lock(&compressorLock);
|
||||||
comprLen = COMPRESSOR_BUFFER_SIZE;
|
comprLen = COMPRESSOR_BUFFER_SIZE;
|
||||||
|
|
||||||
compress(compr, &comprLen, (const Bytef*)(content.data()), contentLength);
|
compress(compr, &comprLen, (const Bytef*)(content.data()), contentLength);
|
||||||
|
|
||||||
content = string((char *)compr, comprLen);
|
content = string((char *)compr, comprLen);
|
||||||
contentLength = comprLen;
|
contentLength = comprLen;
|
||||||
|
|
||||||
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(),
|
||||||
MHD_NO,
|
MHD_NO,
|
||||||
MHD_YES);
|
MHD_YES);
|
||||||
|
|
||||||
/* Add if necessary the content-encoding */
|
/* Add if necessary the content-encoding */
|
||||||
if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) {
|
if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) {
|
||||||
MHD_add_response_header(response, "Content-encoding", "deflate");
|
MHD_add_response_header(response, "Content-encoding", "deflate");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user