Rename variable request_context to request.

pull/111/head
Matthieu Gautier 9 years ago
parent 5c6a1870b9
commit 09e2039aff

@ -285,18 +285,18 @@ static struct MHD_Response* build_response(const void* data,
} }
static struct MHD_Response* build_404(RequestContext* request_context) { static struct MHD_Response* build_404(RequestContext* request) {
std::string content std::string 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 \""
+ request_context->urlStr + "\" was not found on this server.</p></body></html>"; + request->urlStr + "\" was not found on this server.</p></body></html>";
auto mimeType = "text/html"; auto mimeType = "text/html";
request_context->httpResponseCode = MHD_HTTP_NOT_FOUND; request->httpResponseCode = MHD_HTTP_NOT_FOUND;
introduceTaskbar(content, request_context->humanReadableBookId); introduceTaskbar(content, request->humanReadableBookId);
bool deflated bool deflated
= request_context->acceptEncodingDeflate && compress_content(content, mimeType); = request->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);
} }
@ -380,7 +380,7 @@ static struct MHD_Response* build_callback_response_from_article(
return response; return response;
} }
static struct MHD_Response* handle_suggest(RequestContext* request_context) static struct MHD_Response* handle_suggest(RequestContext* request)
{ {
std::string content; std::string content;
std::string mimeType; std::string mimeType;
@ -390,7 +390,7 @@ static struct MHD_Response* handle_suggest(RequestContext* request_context)
/* Get the suggestion pattern from the HTTP request */ /* Get the suggestion pattern from the HTTP request */
const char* cTerm = MHD_lookup_connection_value( const char* cTerm = MHD_lookup_connection_value(
request_context->connection, MHD_GET_ARGUMENT_KIND, "term"); request->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());
@ -399,10 +399,10 @@ static struct MHD_Response* handle_suggest(RequestContext* request_context)
pthread_mutex_lock(&searchLock); pthread_mutex_lock(&searchLock);
/* Get the suggestions */ /* Get the suggestions */
content = "["; content = "[";
if (request_context->reader != NULL) { if (request->reader != NULL) {
/* Get the suggestions */ /* Get the suggestions */
request_context->reader->searchSuggestionsSmart(term, maxSuggestionCount); request->reader->searchSuggestionsSmart(term, maxSuggestionCount);
while (request_context->reader->getNextSuggestion(suggestion)) { while (request->reader->getNextSuggestion(suggestion)) {
kiwix::stringReplacement(suggestion, "\"", "\\\""); kiwix::stringReplacement(suggestion, "\"", "\\\"");
content += (content == "[" ? "" : ","); content += (content == "[" ? "" : ",");
content += "{\"value\":\"" + suggestion + "\",\"label\":\"" + suggestion content += "{\"value\":\"" + suggestion + "\",\"label\":\"" + suggestion
@ -413,7 +413,7 @@ static struct MHD_Response* handle_suggest(RequestContext* request_context)
pthread_mutex_unlock(&searchLock); pthread_mutex_unlock(&searchLock);
/* Propose the fulltext search if possible */ /* Propose the fulltext search if possible */
if (request_context->searcher != NULL) { if (request->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)
@ -422,26 +422,26 @@ static struct MHD_Response* handle_suggest(RequestContext* request_context)
content += "]"; content += "]";
mimeType = "application/json; charset=utf-8"; mimeType = "application/json; charset=utf-8";
bool deflated = request_context->acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request->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(RequestContext* request_context) static struct MHD_Response* handle_skin(RequestContext* request)
{ {
std::string content; std::string content;
try { try {
content = getResource(request_context->urlStr.substr(rootLocation.size() + 6)); content = getResource(request->urlStr.substr(rootLocation.size() + 6));
} catch (const ResourceNotFound& e) { } catch (const ResourceNotFound& e) {
return build_404(request_context); return build_404(request);
} }
std::string mimeType = getMimeTypeForFile(request_context->urlStr); std::string mimeType = getMimeTypeForFile(request->urlStr);
bool deflated = request_context->acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request->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(RequestContext* request_context) static struct MHD_Response* handle_search(RequestContext* request)
{ {
std::string content; std::string content;
std::string mimeType; std::string mimeType;
@ -449,7 +449,7 @@ static struct MHD_Response* handle_search(RequestContext* request_context)
/* Retrieve the pattern to search */ /* Retrieve the pattern to search */
const char* pattern = MHD_lookup_connection_value( const char* pattern = MHD_lookup_connection_value(
request_context->connection, MHD_GET_ARGUMENT_KIND, "pattern"); request->connection, MHD_GET_ARGUMENT_KIND, "pattern");
std::string patternString std::string patternString
= kiwix::urlDecode(pattern == NULL ? "" : string(pattern)); = kiwix::urlDecode(pattern == NULL ? "" : string(pattern));
@ -457,11 +457,11 @@ static struct MHD_Response* handle_search(RequestContext* request_context)
/* Retrive geo search */ /* Retrive geo search */
bool has_geo_query = false; bool has_geo_query = false;
const char* latitude = MHD_lookup_connection_value( const char* latitude = MHD_lookup_connection_value(
request_context->connection, MHD_GET_ARGUMENT_KIND, "latitude"); request->connection, MHD_GET_ARGUMENT_KIND, "latitude");
const char* longitude = MHD_lookup_connection_value( const char* longitude = MHD_lookup_connection_value(
request_context->connection, MHD_GET_ARGUMENT_KIND, "longitude"); request->connection, MHD_GET_ARGUMENT_KIND, "longitude");
const char* distance = MHD_lookup_connection_value( const char* distance = MHD_lookup_connection_value(
request_context->connection, MHD_GET_ARGUMENT_KIND, "distance"); request->connection, MHD_GET_ARGUMENT_KIND, "distance");
float latitudeFloat(0), longitudeFloat(0), distanceFloat(0); float latitudeFloat(0), longitudeFloat(0), distanceFloat(0);
if (latitude != nullptr && longitude != nullptr && distance != nullptr) { if (latitude != nullptr && longitude != nullptr && distance != nullptr) {
@ -476,35 +476,35 @@ static struct MHD_Response* handle_search(RequestContext* request_context)
/* Search results for searches from the welcome page should not /* Search results for searches from the welcome page should not
be cached be cached
*/ */
bool cacheEnabled = !(request_context->searcher == globalSearcher); bool cacheEnabled = !(request->searcher == globalSearcher);
std::string patternCorrespondingUrl; std::string patternCorrespondingUrl;
/* Try first to load directly the article */ /* Try first to load directly the article */
if (request_context->reader != NULL) { if (request->reader != NULL) {
std::vector<std::string> variants = request_context->reader->getTitleVariants(patternString); std::vector<std::string> variants = request->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()) {
request_context->reader->getPageUrlFromTitle(*variantsItr, patternCorrespondingUrl); request->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
= rootLocation + "/" + request_context->humanReadableBookId + "/" + patternCorrespondingUrl; = rootLocation + "/" + request->humanReadableBookId + "/" + patternCorrespondingUrl;
request_context->httpResponseCode = MHD_HTTP_FOUND; request->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 (request_context->searcher != NULL) { if (request->searcher != NULL) {
const char* start = MHD_lookup_connection_value( const char* start = MHD_lookup_connection_value(
request_context->connection, MHD_GET_ARGUMENT_KIND, "start"); request->connection, MHD_GET_ARGUMENT_KIND, "start");
const char* end = MHD_lookup_connection_value( const char* end = MHD_lookup_connection_value(
request_context->connection, MHD_GET_ARGUMENT_KIND, "end"); request->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;
@ -512,27 +512,27 @@ static struct MHD_Response* handle_search(RequestContext* request_context)
pthread_mutex_lock(&searchLock); pthread_mutex_lock(&searchLock);
try { try {
if (patternString.empty() && has_geo_query) { if (patternString.empty() && has_geo_query) {
request_context->searcher->geo_search(latitudeFloat, longitudeFloat, distanceFloat, request->searcher->geo_search(latitudeFloat, longitudeFloat, distanceFloat,
startNumber, endNumber, isVerbose.load()); startNumber, endNumber, isVerbose.load());
} else { } else {
request_context->searcher->search(patternString, request->searcher->search(patternString,
startNumber, endNumber, isVerbose.load()); startNumber, endNumber, isVerbose.load());
} }
content = request_context->searcher->getHtml(); content = request->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>";
request_context->httpResponseCode = MHD_HTTP_NOT_FOUND; request->httpResponseCode = MHD_HTTP_NOT_FOUND;
} }
mimeType = "text/html; charset=utf-8"; mimeType = "text/html; charset=utf-8";
introduceTaskbar(content, request_context->humanReadableBookId); introduceTaskbar(content, request->humanReadableBookId);
bool deflated = request_context->acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request->acceptEncodingDeflate && compress_content(content, mimeType);
return build_response(content.data(), return build_response(content.data(),
content.size(), content.size(),
httpRedirection, httpRedirection,
@ -541,19 +541,19 @@ static struct MHD_Response* handle_search(RequestContext* request_context)
cacheEnabled); cacheEnabled);
} }
static struct MHD_Response* handle_random(RequestContext* request_context) static struct MHD_Response* handle_random(RequestContext* request)
{ {
std::string httpRedirection; std::string httpRedirection;
request_context->httpResponseCode = MHD_HTTP_FOUND; request->httpResponseCode = MHD_HTTP_FOUND;
if (request_context->reader != NULL) { if (request->reader != NULL) {
std::string randomUrl = request_context->reader->getRandomPageUrl(); std::string randomUrl = request->reader->getRandomPageUrl();
httpRedirection httpRedirection
= rootLocation + "/" + request_context->humanReadableBookId + "/" + kiwix::urlEncode(randomUrl); = rootLocation + "/" + request->humanReadableBookId + "/" + kiwix::urlEncode(randomUrl);
} }
return build_response("", 0, httpRedirection, "", false, false); return build_response("", 0, httpRedirection, "", false, false);
} }
static struct MHD_Response* handle_content(RequestContext* request_context) static struct MHD_Response* handle_content(RequestContext* request)
{ {
std::string baseUrl; std::string baseUrl;
std::string content; std::string content;
@ -562,7 +562,7 @@ static struct MHD_Response* handle_content(RequestContext* request_context)
bool found = false; bool found = false;
zim::Article article; zim::Article article;
try { try {
found = request_context->reader->getArticleObjectByDecodedUrl(request_context->urlStr, article); found = request->reader->getArticleObjectByDecodedUrl(context->urlStr, article);
if (found) { if (found) {
/* If redirect */ /* If redirect */
@ -582,9 +582,9 @@ static struct MHD_Response* handle_content(RequestContext* request_context)
if (!found) { if (!found) {
if (isVerbose.load()) if (isVerbose.load())
printf("Failed to find %s\n", request_context->urlStr.c_str()); printf("Failed to find %s\n", request->urlStr.c_str());
return build_404(request_context); return build_404(request);
} }
try { try {
@ -594,7 +594,7 @@ static struct MHD_Response* handle_content(RequestContext* request_context)
} }
if (isVerbose.load()) { if (isVerbose.load()) {
printf("Found %s\n", request_context->urlStr.c_str()); printf("Found %s\n", request->urlStr.c_str());
printf("mimeType: %s\n", mimeType.c_str()); printf("mimeType: %s\n", mimeType.c_str());
} }
@ -611,51 +611,51 @@ static struct MHD_Response* handle_content(RequestContext* request_context)
+ article.getUrl(); + article.getUrl();
pthread_mutex_lock(&regexLock); pthread_mutex_lock(&regexLock);
content = replaceRegex(content, content = replaceRegex(content,
"$1$2" + rootLocation + "/" + request_context->humanReadableBookId + "/$3/", "$1$2" + rootLocation + "/" + request->humanReadableBookId + "/$3/",
"(href|src)(=[\"|\']{0,1})/([A-Z|\\-])/"); "(href|src)(=[\"|\']{0,1})/([A-Z|\\-])/");
content = replaceRegex(content, content = replaceRegex(content,
"$1$2" + rootLocation + "/" + request_context->humanReadableBookId + "/$3/", "$1$2" + rootLocation + "/" + request->humanReadableBookId + "/$3/",
"(@import[ ]+)([\"|\']{0,1})/([A-Z|\\-])/"); "(@import[ ]+)([\"|\']{0,1})/([A-Z|\\-])/");
content = replaceRegex( content = replaceRegex(
content, content,
"<head><base href=\"" + rootLocation + "/" + request_context->humanReadableBookId + baseUrl + "\" />", "<head><base href=\"" + rootLocation + "/" + request->humanReadableBookId + baseUrl + "\" />",
"<head>"); "<head>");
pthread_mutex_unlock(&regexLock); pthread_mutex_unlock(&regexLock);
introduceTaskbar(content, request_context->humanReadableBookId); introduceTaskbar(content, request->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" + rootLocation + "/" + request_context->humanReadableBookId + "/$3/", "$1$2" + rootLocation + "/" + request->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
= request_context->acceptEncodingDeflate && compress_content(content, mimeType); = request->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 {
int range_len; int range_len;
if (request_context->range_end == -1) { if (request->range_end == -1) {
range_len = article.getArticleSize() - request_context->range_start; range_len = article.getArticleSize() - request->range_start;
} else { } else {
range_len = request_context->range_end - request_context->range_start; range_len = request->range_end - request->range_start;
} }
return build_callback_response_from_article( return build_callback_response_from_article(
article, article,
request_context->range_start, request->range_start,
range_len, range_len,
mimeType); mimeType);
} }
} }
static struct MHD_Response* handle_default(RequestContext* request_context) static struct MHD_Response* handle_default(RequestContext* request)
{ {
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 = request_context->acceptEncodingDeflate && compress_content(content, mimeType); bool deflated = request->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);
} }
@ -769,7 +769,7 @@ static int accessHandlerCallback(void* cls,
humanReadableBookId = ""; humanReadableBookId = "";
} }
RequestContext request_context(connection, httpResponseCode, RequestContext request(connection, httpResponseCode,
reader, searcher, reader, searcher,
urlStr, humanReadableBookId, urlStr, humanReadableBookId,
acceptEncodingDeflate, acceptEncodingDeflate,
@ -778,36 +778,36 @@ static int accessHandlerCallback(void* cls,
/* Get suggestions */ /* Get suggestions */
if ((urlStr == (rootLocation + "/" + "suggest")) && reader != NULL) { if ((urlStr == (rootLocation + "/" + "suggest")) && reader != NULL) {
response = handle_suggest(&request_context); response = handle_suggest(&request);
} }
/* Get static skin stuff */ /* Get static skin stuff */
else if (urlStr.size() > rootLocation.size() + 5 && urlStr.substr(rootLocation.size() , 6) == "/skin/") { else if (urlStr.size() > rootLocation.size() + 5 && urlStr.substr(rootLocation.size() , 6) == "/skin/") {
response = handle_skin(&request_context); response = handle_skin(&request);
} }
/* Display the search restults */ /* Display the search restults */
else if (urlStr == (rootLocation + "/" + "search")) { else if (urlStr == (rootLocation + "/" + "search")) {
response = handle_search(&request_context); response = handle_search(&request);
} }
/* Display a random article */ /* Display a random article */
else if (urlStr == (rootLocation + "/" + "random")) { else if (urlStr == (rootLocation + "/" + "random")) {
response = handle_random(&request_context); response = handle_random(&request);
} }
/* 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(&request_context); response = handle_content(&request);
} }
/* Display the global Welcome page */ /* Display the global Welcome page */
else { else {
response = handle_default(&request_context); response = handle_default(&request);
} }
/* Queue the response */ /* Queue the response */
int ret = MHD_queue_response(connection, request_context.httpResponseCode, response); int ret = MHD_queue_response(connection, request.httpResponseCode, response);
MHD_destroy_response(response); MHD_destroy_response(response);
return ret; return ret;

Loading…
Cancel
Save