Update kiwix-serve to the new kiwix-lib API.

Related to kiwix/kiwix-lib#123
This commit is contained in:
Matthieu Gautier 2018-04-19 16:01:48 +02:00
parent d25329ecb4
commit 6f49e78eb4

View File

@ -295,22 +295,18 @@ static struct MHD_Response* build_homepage(RequestContext* request)
} }
struct RunningResponse { struct RunningResponse {
zim::Article* article; kiwix::Entry entry;
int range_start; int range_start;
RunningResponse(zim::Article* article, RunningResponse(kiwix::Entry entry,
int range_start) : int range_start) :
article(article), entry(entry),
range_start(range_start) range_start(range_start)
{} {}
~RunningResponse() {
delete article;
}
}; };
ssize_t callback_reader_from_article(void* cls, ssize_t callback_reader_from_entry(void* cls,
uint64_t pos, uint64_t pos,
char* buf, char* buf,
size_t max) size_t max)
@ -319,39 +315,39 @@ ssize_t callback_reader_from_article(void* cls,
size_t max_size_to_set = min<size_t>( size_t max_size_to_set = min<size_t>(
max, max,
response->article->getArticleSize() - pos - response->range_start); response->entry.getSize() - pos - response->range_start);
if (max_size_to_set <= 0) { if (max_size_to_set <= 0) {
return MHD_CONTENT_READER_END_WITH_ERROR; return MHD_CONTENT_READER_END_WITH_ERROR;
} }
zim::Blob blob = response->article->getData(response->range_start+pos, max_size_to_set); zim::Blob blob = response->entry.getBlob(response->range_start+pos, max_size_to_set);
memcpy(buf, blob.data(), max_size_to_set); memcpy(buf, blob.data(), max_size_to_set);
return max_size_to_set; return max_size_to_set;
} }
void callback_free_article(void* cls) void callback_free_response(void* cls)
{ {
RunningResponse* response = static_cast<RunningResponse*>(cls); RunningResponse* response = static_cast<RunningResponse*>(cls);
delete response; delete response;
} }
static struct MHD_Response* build_callback_response_from_article( static struct MHD_Response* build_callback_response_from_entry(
zim::Article& article, int range_start, int range_len, const std::string& mimeType) kiwix::Entry entry, int range_start, int range_len, const std::string& mimeType)
{ {
RunningResponse* run_response = RunningResponse* run_response =
new RunningResponse(new zim::Article(article), range_start); new RunningResponse(entry, range_start);
struct MHD_Response* response struct MHD_Response* response
= MHD_create_response_from_callback(article.getArticleSize(), = MHD_create_response_from_callback(entry.getSize(),
16384, 16384,
callback_reader_from_article, callback_reader_from_entry,
run_response, run_response,
callback_free_article); callback_free_response);
/* Tell the client that byte ranges are accepted */ /* Tell the client that byte ranges are accepted */
MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes"); MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes");
std::ostringstream oss; std::ostringstream oss;
oss << "bytes " << range_start << "-" << range_start + range_len - 1 << "/" << article.getArticleSize(); oss << "bytes " << range_start << "-" << range_start + range_len - 1 << "/" << entry.getSize();
MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_RANGE, oss.str().c_str()); MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_RANGE, oss.str().c_str());
@ -556,8 +552,14 @@ static struct MHD_Response* handle_search(RequestContext* request)
auto variantsItr = variants.begin(); auto variantsItr = variants.begin();
while (patternCorrespondingUrl.empty() && variantsItr != variants.end()) { while (patternCorrespondingUrl.empty() && variantsItr != variants.end()) {
reader->getPageUrlFromTitle(*variantsItr, patternCorrespondingUrl); try {
variantsItr++; auto entry = reader->getEntryFromTitle(*variantsItr);
entry = entry.getFinalEntry();
patternCorrespondingUrl = entry.getPath();
break;
} catch(kiwix::NoEntry& e) {
variantsItr++;
}
} }
/* If article found then redirect directly to it */ /* If article found then redirect directly to it */
@ -634,10 +636,15 @@ static struct MHD_Response* handle_random(RequestContext* request)
return build_homepage(request); return build_homepage(request);
} }
std::string randomUrl = reader->getRandomPageUrl(); try {
httpRedirection auto entry = reader->getRandomPage();
= rootLocation + "/" + humanReadableBookId + "/" + kiwix::urlEncode(randomUrl); entry = entry.getFinalEntry();
return build_response("", 0, httpRedirection, "", false, false); httpRedirection
= rootLocation + "/" + humanReadableBookId + "/" + kiwix::urlEncode(entry.getPath());
return build_response("", 0, httpRedirection, "", false, false);
} catch(kiwix::NoEntry& e) {
return build_404(request, humanReadableBookId);
}
} }
static struct MHD_Response* handle_catalog(RequestContext* request) static struct MHD_Response* handle_catalog(RequestContext* request)
@ -710,8 +717,7 @@ static struct MHD_Response* handle_content(RequestContext* request)
std::string content; std::string content;
std::string mimeType; std::string mimeType;
bool found = false; kiwix::Entry entry;
zim::Article article;
std::string humanReadableBookId; std::string humanReadableBookId;
try { try {
@ -728,25 +734,9 @@ static struct MHD_Response* handle_content(RequestContext* request)
auto urlStr = request->get_url().substr(humanReadableBookId.size()+1); auto urlStr = request->get_url().substr(humanReadableBookId.size()+1);
try { try {
found = reader->getArticleObjectByDecodedUrl(urlStr, article); entry = reader->getEntryFromPath(urlStr);
entry = entry.getFinalEntry();
if (found) { } catch(kiwix::NoEntry& e) {
/* If redirect */
unsigned int loopCounter = 0;
while (article.isRedirect() && ++loopCounter < 42) {
article = article.getRedirectArticle();
}
/* To many loop */
if (loopCounter == 42)
found = false;
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
found = false;
}
if (!found) {
if (isVerbose.load()) if (isVerbose.load())
printf("Failed to find %s\n", urlStr.c_str()); printf("Failed to find %s\n", urlStr.c_str());
@ -754,7 +744,7 @@ static struct MHD_Response* handle_content(RequestContext* request)
} }
try { try {
mimeType = article.getMimeType(); mimeType = entry.getMimetype();
} catch (exception& e) { } catch (exception& e) {
mimeType = "application/octet-stream"; mimeType = "application/octet-stream";
} }
@ -767,14 +757,13 @@ static struct MHD_Response* handle_content(RequestContext* request)
if (mimeType.find("text/") != string::npos if (mimeType.find("text/") != string::npos
|| mimeType.find("application/javascript") != string::npos || mimeType.find("application/javascript") != string::npos
|| mimeType.find("application/json") != string::npos) { || mimeType.find("application/json") != string::npos) {
zim::Blob raw_content = article.getData(); zim::Blob raw_content = entry.getBlob();
content = string(raw_content.data(), raw_content.size()); content = string(raw_content.data(), raw_content.size());
/* Special rewrite URL in case of ZIM file use intern *asbolute* url like /* Special rewrite URL in case of ZIM file use intern *asbolute* url like
* /A/Kiwix */ * /A/Kiwix */
if (mimeType.find("text/html") != string::npos) { if (mimeType.find("text/html") != string::npos) {
baseUrl = "/" + std::string(1, article.getNamespace()) + "/" baseUrl = "/" + entry.getPath();
+ article.getUrl();
pthread_mutex_lock(&regexLock); pthread_mutex_lock(&regexLock);
content = replaceRegex(content, content = replaceRegex(content,
"$1$2" + rootLocation + "/" + humanReadableBookId + "/$3/", "$1$2" + rootLocation + "/" + humanReadableBookId + "/$3/",
@ -803,12 +792,12 @@ static struct MHD_Response* handle_content(RequestContext* request)
} else { } else {
int range_len; int range_len;
if (request->get_range().second == -1) { if (request->get_range().second == -1) {
range_len = article.getArticleSize() - request->get_range().first; range_len = entry.getSize() - request->get_range().first;
} else { } else {
range_len = request->get_range().second - request->get_range().first; range_len = request->get_range().second - request->get_range().first;
} }
return build_callback_response_from_article( return build_callback_response_from_entry(
article, entry,
request->get_range().first, request->get_range().first,
range_len, range_len,
mimeType); mimeType);