From 4b6caa48ce08dd432eac9a2cb50e970ec6ffc9ff Mon Sep 17 00:00:00 2001 From: kelson42 Date: Wed, 27 Jul 2011 19:25:25 +0000 Subject: [PATCH] + few additional check to avoid segfaults if ZIM file is not there --- src/common/kiwix/reader.cpp | 158 ++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/src/common/kiwix/reader.cpp b/src/common/kiwix/reader.cpp index f86afecf8..8ae7aaef3 100644 --- a/src/common/kiwix/reader.cpp +++ b/src/common/kiwix/reader.cpp @@ -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(); - - /* Offset to visit the url */ - unsigned int urlLength = strlen(url); - unsigned int offset = 0; - - /* Ignore the '/' */ - while ((offset < urlLength) && (url[offset] == '/')) offset++; - - /* 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++; - - /* 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(); - } - } - - /* Extract the content from the zim file */ - std::pair resultPair = zimFileHandler->findx(ns[0], titleStr); - - /* Test if the article was found */ - if (resultPair.first == true) { - - /* 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()); + content=""; + contentType=""; + contentLength = 0; - /* Try to set a stub HTML header/footer if necesssary */ - if (contentType == "text/html" && std::string::npos == content.find("")) { - content = "" + article.getTitle() + "" + content + ""; + if (this->zimFileHandler != NULL) { + const char *url = urlStr.c_str(); + + /* Offset to visit the url */ + unsigned int urlLength = strlen(url); + unsigned int offset = 0; + + /* Ignore the '/' */ + while ((offset < urlLength) && (url[offset] == '/')) offset++; + + /* 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++; + + /* 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 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; + /* Extract the content from the zim file */ + std::pair resultPair = zimFileHandler->findx(ns[0], titleStr); + + /* Test if the article was found */ + if (resultPair.first == true) { + + /* 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("")) { + content = "" + article.getTitle() + "" + content + ""; + } + + /* Get the data length */ + contentLength = article.getArticleSize(); + + /* Set return value */ + retVal = true; + } } return retVal;