Merge pull request #258 from kiwix/redirect

[server] Correctly implement 302 redirection if the entry is a redirection
This commit is contained in:
Matthieu Gautier 2019-02-20 17:14:52 +01:00 committed by GitHub
commit 7fa0579ea1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -766,10 +766,22 @@ static struct MHD_Response* handle_content(RequestContext* request)
}
auto urlStr = request->get_url().substr(humanReadableBookId.size()+1);
if (urlStr[0] == '/') {
urlStr = urlStr.substr(1);
}
try {
entry = reader->getEntryFromPath(urlStr);
entry = entry.getFinalEntry();
if (entry.isRedirect() || urlStr.empty()) {
// If urlStr is empty, we want to mainPage.
// We must do a redirection to the real page.
entry = entry.getFinalEntry();
std::string httpRedirection = (
rootLocation + "/" + humanReadableBookId + "/" +
kiwix::urlEncode(entry.getPath()));
request->httpResponseCode = MHD_HTTP_FOUND;
return build_response("", 0, httpRedirection, "", false, false);
}
} catch(kiwix::NoEntry& e) {
if (isVerbose.load())
printf("Failed to find %s\n", urlStr.c_str());
@ -794,29 +806,8 @@ static struct MHD_Response* handle_content(RequestContext* request)
zim::Blob raw_content = entry.getBlob();
content = string(raw_content.data(), raw_content.size());
/* Special rewrite URL in case of ZIM file use intern *asbolute* url like
* /A/Kiwix */
if (mimeType.find("text/html") != string::npos) {
baseUrl = "/" + entry.getPath();
pthread_mutex_lock(&regexLock);
content = replaceRegex(content,
"$1$2" + rootLocation + "/" + humanReadableBookId + "/$3/",
"(href|src)(=[\"|\']{0,1})/([A-Z|\\-])/");
content = replaceRegex(content,
"$1$2" + rootLocation + "/" + humanReadableBookId + "/$3/",
"(@import[ ]+)([\"|\']{0,1})/([A-Z|\\-])/");
content = replaceRegex(
content,
"<head><base href=\"" + rootLocation + "/" + humanReadableBookId + baseUrl + "\" />",
"<head>");
pthread_mutex_unlock(&regexLock);
introduceTaskbar(content, humanReadableBookId);
} else if (mimeType.find("text/css") != string::npos) {
pthread_mutex_lock(&regexLock);
content = replaceRegex(content,
"$1$2" + rootLocation + "/" + humanReadableBookId + "/$3/",
"(url|URL)(\\([\"|\']{0,1})/([A-Z|\\-])/");
pthread_mutex_unlock(&regexLock);
}
bool deflated