+ few additional check to avoid segfaults if ZIM file is not there

This commit is contained in:
kelson42 2011-07-27 19:25:25 +00:00
parent 97881fbbb6
commit 4b6caa48ce
1 changed files with 79 additions and 79 deletions

View File

@ -206,88 +206,88 @@ namespace kiwix {
/* Get a content from a zim file */ /* Get a content from a zim file */
bool Reader::getContentByUrl(const string &urlStr, string &content, unsigned int &contentLength, string &contentType) { bool Reader::getContentByUrl(const string &urlStr, string &content, unsigned int &contentLength, string &contentType) {
bool retVal = false; bool retVal = false;
const char *url = urlStr.c_str(); content="";
contentType="";
contentLength = 0;
/* Offset to visit the url */ if (this->zimFileHandler != NULL) {
unsigned int urlLength = strlen(url); const char *url = urlStr.c_str();
unsigned int offset = 0;
/* Ignore the '/' */ /* Offset to visit the url */
while ((offset < urlLength) && (url[offset] == '/')) offset++; unsigned int urlLength = strlen(url);
unsigned int offset = 0;
/* Get namespace */ /* Ignore the '/' */
char ns[1024]; while ((offset < urlLength) && (url[offset] == '/')) offset++;
unsigned int nsOffset = 0;
while ((offset < urlLength) && (url[offset] != '/')) {
ns[nsOffset] = url[offset];
offset++;
nsOffset++;
}
ns[nsOffset] = 0;
/* Ignore the '/' */ /* Get namespace */
while ((offset < urlLength) && (url[offset] == '/')) offset++; char ns[1024];
unsigned int nsOffset = 0;
/* Get content title */ while ((offset < urlLength) && (url[offset] != '/')) {
char title[1024]; ns[nsOffset] = url[offset];
unsigned int titleOffset = 0; offset++;
while (offset < urlLength) { nsOffset++;
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();
} }
} ns[nsOffset] = 0;
/* Extract the content from the zim file */ /* Ignore the '/' */
std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findx(ns[0], titleStr); while ((offset < urlLength) && (url[offset] == '/')) offset++;
/* Test if the article was found */ /* Get content title */
if (resultPair.first == true) { char title[1024];
unsigned int titleOffset = 0;
while (offset < urlLength) {
title[titleOffset] = url[offset];
offset++;
titleOffset++;
}
title[titleOffset] = 0;
/* Get the article */ /* unescape url */
zim::Article article = zimFileHandler->getArticle(resultPair.second.getIndex()); string titleStr = string(title);
unescapeUrl(titleStr);
/* If redirect */ /* Main page */
unsigned int loopCounter = 0; if (titleStr == "" && strcmp(ns, "") == 0) {
while (article.isRedirect() && loopCounter++<42) { if (zimFileHandler->getFileheader().hasMainPage()) {
article = article.getRedirectArticle(); zim::Article article = zimFileHandler->getArticle(zimFileHandler->getFileheader().getMainPage());
ns[0] = article.getNamespace();
titleStr = article.getUrl();
}
} }
/* Get the content mime-type */ /* Extract the content from the zim file */
contentType = string(article.getMimeType().data(), article.getMimeType().size()); std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findx(ns[0], titleStr);
/* Get the data */ /* Test if the article was found */
content = string(article.getData().data(), article.getArticleSize()); if (resultPair.first == true) {
/* Try to set a stub HTML header/footer if necesssary */ /* Get the article */
if (contentType == "text/html" && std::string::npos == content.find("<body>")) { zim::Article article = zimFileHandler->getArticle(resultPair.second.getIndex());
content = "<html><head><title>" + article.getTitle() + "</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>" + content + "</body></html>";
/* 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; return retVal;