Use a structure RequestContext to pass all the context of a request.

This simplifies the code and avoid to pass a lot of arguments to
each function.
This commit is contained in:
Matthieu Gautier 2017-10-10 15:15:00 +02:00
parent c2ac40d4f5
commit a2324b5e8b

View File

@ -122,6 +122,38 @@ static std::string getMimeTypeForFile(const std::string& filename)
return mimeType; return mimeType;
} }
struct RequestContext {
struct MHD_Connection* connection;
int httpResponseCode;
kiwix::Reader* reader;
kiwix::Searcher* searcher;
const std::string urlStr;
const std::string humanReadableBookId;
bool acceptEncodingDeflate;
bool acceptRange;
int range_start;
int range_end;
RequestContext(struct MHD_Connection* connection, int httpResponseCode,
kiwix::Reader* reader, kiwix::Searcher* searcher,
const std::string& urlStr, const std::string& humanReadableBookId,
bool acceptEncodingDeflate,
bool acceptRange, int range_start, int range_end) :
connection(connection),
httpResponseCode(httpResponseCode),
reader(reader),
searcher(searcher),
urlStr(urlStr),
humanReadableBookId(humanReadableBookId),
acceptEncodingDeflate(acceptEncodingDeflate),
acceptRange(acceptRange),
range_start(range_start),
range_end(range_end)
{}
};
void introduceTaskbar(string& content, const string& humanReadableBookId) void introduceTaskbar(string& content, const string& humanReadableBookId)
{ {
pthread_mutex_lock(&regexLock); pthread_mutex_lock(&regexLock);
@ -198,6 +230,7 @@ static bool compress_content(string& content, const string& mimeType)
return deflated; return deflated;
} }
static struct MHD_Response* build_response(const void* data, static struct MHD_Response* build_response(const void* data,
unsigned int length, unsigned int length,
const std::string& httpRedirection, const std::string& httpRedirection,
@ -300,14 +333,7 @@ static struct MHD_Response* build_callback_response_from_blob(
return response; return response;
} }
static struct MHD_Response* handle_suggest( static struct MHD_Response* handle_suggest(RequestContext* request_context)
struct MHD_Connection* connection,
int& httpResponseCode,
kiwix::Reader* reader,
kiwix::Searcher* searcher,
const std::string& urlStr,
const std::string& humanReadableBookId,
bool acceptEncodingDeflate)
{ {
std::string content; std::string content;
std::string mimeType; std::string mimeType;
@ -316,8 +342,8 @@ static struct MHD_Response* handle_suggest(
std::string suggestion; std::string suggestion;
/* Get the suggestion pattern from the HTTP request */ /* Get the suggestion pattern from the HTTP request */
const char* cTerm const char* cTerm = MHD_lookup_connection_value(
= MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "term"); request_context->connection, MHD_GET_ARGUMENT_KIND, "term");
std::string term = cTerm == NULL ? "" : cTerm; std::string term = cTerm == NULL ? "" : cTerm;
if (isVerbose.load()) { if (isVerbose.load()) {
printf("Searching suggestions for: \"%s\"\n", term.c_str()); printf("Searching suggestions for: \"%s\"\n", term.c_str());
@ -326,18 +352,21 @@ static struct MHD_Response* handle_suggest(
pthread_mutex_lock(&searchLock); pthread_mutex_lock(&searchLock);
/* Get the suggestions */ /* Get the suggestions */
content = "["; content = "[";
reader->searchSuggestionsSmart(term, maxSuggestionCount); if (request_context->reader != NULL) {
while (reader->getNextSuggestion(suggestion)) { /* Get the suggestions */
kiwix::stringReplacement(suggestion, "\"", "\\\""); request_context->reader->searchSuggestionsSmart(term, maxSuggestionCount);
content += (content == "[" ? "" : ","); while (request_context->reader->getNextSuggestion(suggestion)) {
content += "{\"value\":\"" + suggestion + "\",\"label\":\"" + suggestion kiwix::stringReplacement(suggestion, "\"", "\\\"");
+ "\"}"; content += (content == "[" ? "" : ",");
suggestionCount++; content += "{\"value\":\"" + suggestion + "\",\"label\":\"" + suggestion
+ "\"}";
suggestionCount++;
}
} }
pthread_mutex_unlock(&searchLock); pthread_mutex_unlock(&searchLock);
/* Propose the fulltext search if possible */ /* Propose the fulltext search if possible */
if (searcher != NULL) { if (request_context->searcher != NULL) {
content += (suggestionCount == 0 ? "" : ","); content += (suggestionCount == 0 ? "" : ",");
content += "{\"value\":\"" + std::string(term) content += "{\"value\":\"" + std::string(term)
+ " \", \"label\":\"containing '" + std::string(term) + " \", \"label\":\"containing '" + std::string(term)
@ -346,34 +375,21 @@ static struct MHD_Response* handle_suggest(
content += "]"; content += "]";
mimeType = "application/json; charset=utf-8"; mimeType = "application/json; charset=utf-8";
bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request_context->acceptEncodingDeflate && compress_content(content, mimeType);
return build_response( return build_response(
content.data(), content.size(), "", mimeType, deflated, true); content.data(), content.size(), "", mimeType, deflated, true);
} }
static struct MHD_Response* handle_skin(struct MHD_Connection* connection, static struct MHD_Response* handle_skin(RequestContext* request_context)
int& httpResponseCode,
kiwix::Reader* reader,
kiwix::Searcher* searcher,
const std::string& urlStr,
const std::string& humanReadableBookId,
bool acceptEncodingDeflate)
{ {
std::string content = getResource(urlStr.substr(6)); std::string content = getResource(request_context->urlStr.substr(6));
std::string mimeType = getMimeTypeForFile(urlStr); std::string mimeType = getMimeTypeForFile(request_context->urlStr);
bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request_context->acceptEncodingDeflate && compress_content(content, mimeType);
return build_response( return build_response(
content.data(), content.size(), "", mimeType, deflated, true); content.data(), content.size(), "", mimeType, deflated, true);
} }
static struct MHD_Response* handle_search( static struct MHD_Response* handle_search(RequestContext* request_context)
struct MHD_Connection* connection,
int& httpResponseCode,
kiwix::Reader* reader,
kiwix::Searcher* searcher,
const std::string& urlStr,
const std::string& humanReadableBookId,
bool acceptEncodingDeflate)
{ {
std::string content; std::string content;
std::string mimeType; std::string mimeType;
@ -381,58 +397,58 @@ static struct MHD_Response* handle_search(
/* Retrieve the pattern to search */ /* Retrieve the pattern to search */
const char* pattern = MHD_lookup_connection_value( const char* pattern = MHD_lookup_connection_value(
connection, MHD_GET_ARGUMENT_KIND, "pattern"); request_context->connection, MHD_GET_ARGUMENT_KIND, "pattern");
std::string patternString std::string patternString
= kiwix::urlDecode(pattern == NULL ? "" : string(pattern)); = kiwix::urlDecode(pattern == NULL ? "" : string(pattern));
std::string patternCorrespondingUrl; std::string patternCorrespondingUrl;
/* Try first to load directly the article */ /* Try first to load directly the article */
if (reader != NULL) { if (request_context->reader != NULL) {
std::vector<std::string> variants = reader->getTitleVariants(patternString); std::vector<std::string> variants = request_context->reader->getTitleVariants(patternString);
std::vector<std::string>::iterator variantsItr = variants.begin(); std::vector<std::string>::iterator variantsItr = variants.begin();
while (patternCorrespondingUrl.empty() && variantsItr != variants.end()) { while (patternCorrespondingUrl.empty() && variantsItr != variants.end()) {
reader->getPageUrlFromTitle(*variantsItr, patternCorrespondingUrl); request_context->reader->getPageUrlFromTitle(*variantsItr, patternCorrespondingUrl);
variantsItr++; variantsItr++;
} }
/* If article found then redirect directly to it */ /* If article found then redirect directly to it */
if (!patternCorrespondingUrl.empty()) { if (!patternCorrespondingUrl.empty()) {
httpRedirection httpRedirection
= "/" + humanReadableBookId + "/" + patternCorrespondingUrl; = "/" + request_context->humanReadableBookId + "/" + patternCorrespondingUrl;
httpResponseCode = MHD_HTTP_FOUND; request_context->httpResponseCode = MHD_HTTP_FOUND;
return build_response("", 0, httpRedirection, "", false, true); return build_response("", 0, httpRedirection, "", false, true);
} }
} }
/* Make the search */ /* Make the search */
if (searcher != NULL) { if (request_context->searcher != NULL) {
const char* start = MHD_lookup_connection_value( const char* start = MHD_lookup_connection_value(
connection, MHD_GET_ARGUMENT_KIND, "start"); request_context->connection, MHD_GET_ARGUMENT_KIND, "start");
const char* end const char* end = MHD_lookup_connection_value(
= MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "end"); request_context->connection, MHD_GET_ARGUMENT_KIND, "end");
unsigned int startNumber = start != NULL ? atoi(start) : 0; unsigned int startNumber = start != NULL ? atoi(start) : 0;
unsigned int endNumber = end != NULL ? atoi(end) : 25; unsigned int endNumber = end != NULL ? atoi(end) : 25;
/* Get the results */ /* Get the results */
pthread_mutex_lock(&searchLock); pthread_mutex_lock(&searchLock);
try { try {
searcher->search(patternString, startNumber, endNumber, isVerbose.load()); request_context->searcher->search(patternString, startNumber, endNumber, isVerbose.load());
content = searcher->getHtml(); content = request_context->searcher->getHtml();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
pthread_mutex_unlock(&searchLock); pthread_mutex_unlock(&searchLock);
} else { } else {
content = "<!DOCTYPE html>\n<html><head><meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" /><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + kiwix::encodeDiples(patternString) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>"; content = "<!DOCTYPE html>\n<html><head><meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" /><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + kiwix::encodeDiples(patternString) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>";
httpResponseCode = MHD_HTTP_NOT_FOUND; request_context->httpResponseCode = MHD_HTTP_NOT_FOUND;
} }
mimeType = "text/html; charset=utf-8"; mimeType = "text/html; charset=utf-8";
introduceTaskbar(content, humanReadableBookId); introduceTaskbar(content, request_context->humanReadableBookId);
bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request_context->acceptEncodingDeflate && compress_content(content, mimeType);
return build_response(content.data(), return build_response(content.data(),
content.size(), content.size(),
httpRedirection, httpRedirection,
@ -441,33 +457,19 @@ static struct MHD_Response* handle_search(
true); true);
} }
static struct MHD_Response* handle_random( static struct MHD_Response* handle_random(RequestContext* request_context)
struct MHD_Connection* connection,
int& httpResponseCode,
kiwix::Reader* reader,
kiwix::Searcher* searcher,
const std::string& urlStr,
const std::string& humanReadableBookId,
bool acceptEncodingDeflate)
{ {
std::string httpRedirection; std::string httpRedirection;
httpResponseCode = MHD_HTTP_FOUND; request_context->httpResponseCode = MHD_HTTP_FOUND;
if (reader != NULL) { if (request_context->reader != NULL) {
std::string randomUrl = reader->getRandomPageUrl(); std::string randomUrl = request_context->reader->getRandomPageUrl();
httpRedirection httpRedirection
= "/" + humanReadableBookId + "/" + kiwix::urlEncode(randomUrl); = "/" + request_context->humanReadableBookId + "/" + kiwix::urlEncode(randomUrl);
} }
return build_response("", 0, httpRedirection, "", false, false); return build_response("", 0, httpRedirection, "", false, false);
} }
static struct MHD_Response* handle_content( static struct MHD_Response* handle_content(RequestContext* request_context)
struct MHD_Connection* connection,
int& httpResponseCode,
kiwix::Reader* reader,
kiwix::Searcher* searcher,
const std::string& urlStr,
const std::string& humanReadableBookId,
bool acceptEncodingDeflate)
{ {
std::string baseUrl; std::string baseUrl;
std::string content; std::string content;
@ -476,7 +478,7 @@ static struct MHD_Response* handle_content(
bool found = false; bool found = false;
zim::Article article; zim::Article article;
try { try {
found = reader->getArticleObjectByDecodedUrl(urlStr, article); found = request_context->reader->getArticleObjectByDecodedUrl(request_context->urlStr, article);
if (found) { if (found) {
/* If redirect */ /* If redirect */
@ -496,19 +498,19 @@ static struct MHD_Response* handle_content(
if (!found) { if (!found) {
if (isVerbose.load()) if (isVerbose.load())
printf("Failed to find %s\n", urlStr.c_str()); printf("Failed to find %s\n", request_context->urlStr.c_str());
content content
= "<!DOCTYPE html>\n<html><head><meta " = "<!DOCTYPE html>\n<html><head><meta "
"content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" " "content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" "
"/><title>Content not found</title></head><body><h1>Not " "/><title>Content not found</title></head><body><h1>Not "
"Found</h1><p>The requested URL \"" "Found</h1><p>The requested URL \""
+ urlStr + "\" was not found on this server.</p></body></html>"; + request_context->urlStr + "\" was not found on this server.</p></body></html>";
mimeType = "text/html"; mimeType = "text/html";
httpResponseCode = MHD_HTTP_NOT_FOUND; request_context->httpResponseCode = MHD_HTTP_NOT_FOUND;
introduceTaskbar(content, humanReadableBookId); introduceTaskbar(content, request_context->humanReadableBookId);
bool deflated bool deflated
= acceptEncodingDeflate && compress_content(content, mimeType); = request_context->acceptEncodingDeflate && compress_content(content, mimeType);
return build_response( return build_response(
content.data(), content.size(), "", mimeType, deflated, false); content.data(), content.size(), "", mimeType, deflated, false);
} }
@ -520,7 +522,7 @@ static struct MHD_Response* handle_content(
} }
if (isVerbose.load()) { if (isVerbose.load()) {
printf("Found %s\n", urlStr.c_str()); printf("Found %s\n", request_context->urlStr.c_str());
printf("mimeType: %s\n", mimeType.c_str()); printf("mimeType: %s\n", mimeType.c_str());
} }
@ -538,27 +540,27 @@ static struct MHD_Response* handle_content(
+ article.getUrl(); + article.getUrl();
pthread_mutex_lock(&regexLock); pthread_mutex_lock(&regexLock);
content = replaceRegex(content, content = replaceRegex(content,
"$1$2" + humanReadableBookId + "/$3/", "$1$2" + request_context->humanReadableBookId + "/$3/",
"(href|src)(=[\"|\']{0,1}/)([A-Z|\\-])/"); "(href|src)(=[\"|\']{0,1}/)([A-Z|\\-])/");
content = replaceRegex(content, content = replaceRegex(content,
"$1$2" + humanReadableBookId + "/$3/", "$1$2" + request_context->humanReadableBookId + "/$3/",
"(@import[ ]+)([\"|\']{0,1}/)([A-Z|\\-])/"); "(@import[ ]+)([\"|\']{0,1}/)([A-Z|\\-])/");
content = replaceRegex( content = replaceRegex(
content, content,
"<head><base href=\"/" + humanReadableBookId + baseUrl + "\" />", "<head><base href=\"/" + request_context->humanReadableBookId + baseUrl + "\" />",
"<head>"); "<head>");
pthread_mutex_unlock(&regexLock); pthread_mutex_unlock(&regexLock);
introduceTaskbar(content, humanReadableBookId); introduceTaskbar(content, request_context->humanReadableBookId);
} else if (mimeType.find("text/css") != string::npos) { } else if (mimeType.find("text/css") != string::npos) {
pthread_mutex_lock(&regexLock); pthread_mutex_lock(&regexLock);
content = replaceRegex(content, content = replaceRegex(content,
"$1$2" + humanReadableBookId + "/$3/", "$1$2" + request_context->humanReadableBookId + "/$3/",
"(url|URL)(\\([\"|\']{0,1}/)([A-Z|\\-])/"); "(url|URL)(\\([\"|\']{0,1}/)([A-Z|\\-])/");
pthread_mutex_unlock(&regexLock); pthread_mutex_unlock(&regexLock);
} }
bool deflated bool deflated
= acceptEncodingDeflate && compress_content(content, mimeType); = request_context->acceptEncodingDeflate && compress_content(content, mimeType);
return build_response( return build_response(
content.data(), content.size(), "", mimeType, deflated, true); content.data(), content.size(), "", mimeType, deflated, true);
} else { } else {
@ -566,20 +568,13 @@ static struct MHD_Response* handle_content(
} }
} }
static struct MHD_Response* handle_default( static struct MHD_Response* handle_default(RequestContext* request_context)
struct MHD_Connection* connection,
int& httpResponseCode,
kiwix::Reader* reader,
kiwix::Searcher* searcher,
const std::string& urlStr,
const std::string& humanReadableBookId,
bool acceptEncodingDeflate)
{ {
std::string content = welcomeHTML; std::string content = welcomeHTML;
std::string mimeType = "text/html; charset=utf-8"; std::string mimeType = "text/html; charset=utf-8";
bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request_context->acceptEncodingDeflate && compress_content(content, mimeType);
return build_response( return build_response(
content.data(), content.size(), "", mimeType, deflated, true); content.data(), content.size(), "", mimeType, deflated, true);
} }
@ -662,74 +657,45 @@ static int accessHandlerCallback(void* cls,
humanReadableBookId = ""; humanReadableBookId = "";
} }
RequestContext request_context(connection, httpResponseCode,
reader, searcher,
urlStr, humanReadableBookId,
acceptEncodingDeflate,
acceptRange, range_start, range_end);
/* Get suggestions */ /* Get suggestions */
if (!strcmp(url, "/suggest") && reader != NULL) { if (!strcmp(url, "/suggest") && reader != NULL) {
response = handle_suggest(connection, response = handle_suggest(&request_context);
httpResponseCode,
reader,
searcher,
urlStr,
humanReadableBookId,
acceptEncodingDeflate);
} }
/* Get static skin stuff */ /* Get static skin stuff */
else if (urlStr.substr(0, 6) == "/skin/") { else if (urlStr.substr(0, 6) == "/skin/") {
response = handle_skin(connection, response = handle_skin(&request_context);
httpResponseCode,
reader,
searcher,
urlStr,
humanReadableBookId,
acceptEncodingDeflate);
} }
/* Display the search restults */ /* Display the search restults */
else if (!strcmp(url, "/search")) { else if (!strcmp(url, "/search")) {
response = handle_search(connection, response = handle_search(&request_context);
httpResponseCode,
reader,
searcher,
urlStr,
humanReadableBookId,
acceptEncodingDeflate);
} }
/* Display a random article */ /* Display a random article */
else if (!strcmp(url, "/random")) { else if (!strcmp(url, "/random")) {
response = handle_random(connection, response = handle_random(&request_context);
httpResponseCode,
reader,
searcher,
urlStr,
humanReadableBookId,
acceptEncodingDeflate);
} }
/* Display the content of a ZIM content (article, image, ...) */ /* Display the content of a ZIM content (article, image, ...) */
else if (reader != NULL) { else if (reader != NULL) {
response = handle_content(connection, response = handle_content(&request_context);
httpResponseCode,
reader,
searcher,
urlStr,
humanReadableBookId,
acceptEncodingDeflate);
} }
/* Display the global Welcome page */ /* Display the global Welcome page */
else { else {
response = handle_default(connection, response = handle_default(&request_context);
httpResponseCode,
reader,
searcher,
urlStr,
humanReadableBookId,
acceptEncodingDeflate);
} }
/* Queue the response */ /* Queue the response */
int ret = MHD_queue_response(connection, httpResponseCode, response); int ret = MHD_queue_response(connection, request_context.httpResponseCode, response);
MHD_destroy_response(response); MHD_destroy_response(response);
return ret; return ret;