mirror of https://github.com/kiwix/libkiwix.git
+ few additional check to avoid segfaults if ZIM file is not there
This commit is contained in:
parent
97881fbbb6
commit
4b6caa48ce
|
@ -206,88 +206,88 @@ namespace kiwix {
|
|||
/* Get a content from a zim file */
|
||||
bool Reader::getContentByUrl(const string &urlStr, string &content, unsigned int &contentLength, string &contentType) {
|
||||
bool retVal = false;
|
||||
const char *url = urlStr.c_str();
|
||||
content="";
|
||||
contentType="";
|
||||
contentLength = 0;
|
||||
|
||||
/* Offset to visit the url */
|
||||
unsigned int urlLength = strlen(url);
|
||||
unsigned int offset = 0;
|
||||
if (this->zimFileHandler != NULL) {
|
||||
const char *url = urlStr.c_str();
|
||||
|
||||
/* Ignore the '/' */
|
||||
while ((offset < urlLength) && (url[offset] == '/')) offset++;
|
||||
/* Offset to visit the url */
|
||||
unsigned int urlLength = strlen(url);
|
||||
unsigned int offset = 0;
|
||||
|
||||
/* Get namespace */
|
||||
char ns[1024];
|
||||
unsigned int nsOffset = 0;
|
||||
while ((offset < urlLength) && (url[offset] != '/')) {
|
||||
ns[nsOffset] = url[offset];
|
||||
offset++;
|
||||
nsOffset++;
|
||||
}
|
||||
ns[nsOffset] = 0;
|
||||
/* Ignore the '/' */
|
||||
while ((offset < urlLength) && (url[offset] == '/')) offset++;
|
||||
|
||||
/* Ignore the '/' */
|
||||
while ((offset < urlLength) && (url[offset] == '/')) offset++;
|
||||
|
||||
/* Get content title */
|
||||
char title[1024];
|
||||
unsigned int titleOffset = 0;
|
||||
while (offset < urlLength) {
|
||||
title[titleOffset] = url[offset];
|
||||
offset++;
|
||||
titleOffset++;
|
||||
}
|
||||
title[titleOffset] = 0;
|
||||
|
||||
/* unescape url */
|
||||
string titleStr = string(title);
|
||||
unescapeUrl(titleStr);
|
||||
|
||||
/* Main page */
|
||||
if (titleStr == "" && strcmp(ns, "") == 0) {
|
||||
if (zimFileHandler->getFileheader().hasMainPage()) {
|
||||
zim::Article article = zimFileHandler->getArticle(zimFileHandler->getFileheader().getMainPage());
|
||||
ns[0] = article.getNamespace();
|
||||
titleStr = article.getUrl();
|
||||
/* Get namespace */
|
||||
char ns[1024];
|
||||
unsigned int nsOffset = 0;
|
||||
while ((offset < urlLength) && (url[offset] != '/')) {
|
||||
ns[nsOffset] = url[offset];
|
||||
offset++;
|
||||
nsOffset++;
|
||||
}
|
||||
}
|
||||
ns[nsOffset] = 0;
|
||||
|
||||
/* Extract the content from the zim file */
|
||||
std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findx(ns[0], titleStr);
|
||||
/* Ignore the '/' */
|
||||
while ((offset < urlLength) && (url[offset] == '/')) offset++;
|
||||
|
||||
/* Test if the article was found */
|
||||
if (resultPair.first == true) {
|
||||
/* Get content title */
|
||||
char title[1024];
|
||||
unsigned int titleOffset = 0;
|
||||
while (offset < urlLength) {
|
||||
title[titleOffset] = url[offset];
|
||||
offset++;
|
||||
titleOffset++;
|
||||
}
|
||||
title[titleOffset] = 0;
|
||||
|
||||
/* Get the article */
|
||||
zim::Article article = zimFileHandler->getArticle(resultPair.second.getIndex());
|
||||
/* unescape url */
|
||||
string titleStr = string(title);
|
||||
unescapeUrl(titleStr);
|
||||
|
||||
/* If redirect */
|
||||
unsigned int loopCounter = 0;
|
||||
while (article.isRedirect() && loopCounter++<42) {
|
||||
article = article.getRedirectArticle();
|
||||
/* Main page */
|
||||
if (titleStr == "" && strcmp(ns, "") == 0) {
|
||||
if (zimFileHandler->getFileheader().hasMainPage()) {
|
||||
zim::Article article = zimFileHandler->getArticle(zimFileHandler->getFileheader().getMainPage());
|
||||
ns[0] = article.getNamespace();
|
||||
titleStr = article.getUrl();
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the content mime-type */
|
||||
contentType = string(article.getMimeType().data(), article.getMimeType().size());
|
||||
/* Extract the content from the zim file */
|
||||
std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findx(ns[0], titleStr);
|
||||
|
||||
/* Get the data */
|
||||
content = string(article.getData().data(), article.getArticleSize());
|
||||
/* Test if the article was found */
|
||||
if (resultPair.first == true) {
|
||||
|
||||
/* Try to set a stub HTML header/footer if necesssary */
|
||||
if (contentType == "text/html" && std::string::npos == content.find("<body>")) {
|
||||
content = "<html><head><title>" + article.getTitle() + "</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>" + content + "</body></html>";
|
||||
/* Get the article */
|
||||
zim::Article article = zimFileHandler->getArticle(resultPair.second.getIndex());
|
||||
|
||||
/* If redirect */
|
||||
unsigned int loopCounter = 0;
|
||||
while (article.isRedirect() && loopCounter++<42) {
|
||||
article = article.getRedirectArticle();
|
||||
}
|
||||
|
||||
/* Get the content mime-type */
|
||||
contentType = string(article.getMimeType().data(), article.getMimeType().size());
|
||||
|
||||
/* Get the data */
|
||||
content = string(article.getData().data(), article.getArticleSize());
|
||||
|
||||
/* Try to set a stub HTML header/footer if necesssary */
|
||||
if (contentType == "text/html" && std::string::npos == content.find("<body>")) {
|
||||
content = "<html><head><title>" + article.getTitle() + "</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>" + content + "</body></html>";
|
||||
}
|
||||
|
||||
/* Get the data length */
|
||||
contentLength = article.getArticleSize();
|
||||
|
||||
/* Set return value */
|
||||
retVal = true;
|
||||
}
|
||||
|
||||
/* Get the data length */
|
||||
contentLength = article.getArticleSize();
|
||||
|
||||
/* Set return value */
|
||||
retVal = true;
|
||||
} else {
|
||||
/* The found article is not the good one */
|
||||
content="";
|
||||
contentType="";
|
||||
contentLength = 0;
|
||||
retVal = false;
|
||||
}
|
||||
|
||||
return retVal;
|
||||
|
|
Loading…
Reference in New Issue