Merge branch 'master' of ssh://git.code.sf.net/p/kiwix/kiwix

Conflicts:
	CHANGELOG
This commit is contained in:
Kelson42 2015-01-08 12:55:53 +01:00
commit 78db06e127
5 changed files with 69 additions and 21 deletions

View File

@ -376,6 +376,60 @@ namespace kiwix {
return true; return true;
} }
/* Return article by url */
bool Reader::getArticleObjectByDecodedUrl(const string &url, zim::Article &article) {
bool retVal = false;
if (this->zimFileHandler != NULL) {
/* Parse the url */
char ns = 0;
string titleStr;
this->parseUrl(url, &ns, titleStr);
/* Main page */
if (titleStr.empty() && ns == 0) {
this->parseUrl(this->getMainPageUrl(), &ns, titleStr);
}
/* Extract the content from the zim file */
std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findx(ns, titleStr);
/* Test if the article was found */
if (resultPair.first == true) {
article = zimFileHandler->getArticle(resultPair.second.getIndex());
retVal = true;
}
}
return retVal;
}
/* Return the mimeType without the content */
bool Reader::getMimeTypeByUrl(const string &url, string &mimeType) {
bool retVal = false;
if (this->zimFileHandler != NULL) {
zim::Article article;
if (this->getArticleObjectByDecodedUrl(url, article)) {
try {
mimeType = string(article.getMimeType().data(), article.getMimeType().size());
} catch (exception &e) {
cerr << "Unable to get the mimetype for "<< url << ":" << e.what() << endl;
mimeType = "application/octet-stream";
}
retVal = true;
} else {
mimeType = "";
}
}
return retVal;
}
/* Get a content from a zim file */ /* Get a content from a zim file */
bool Reader::getContentByUrl(const string &url, string &content, unsigned int &contentLength, string &contentType) { bool Reader::getContentByUrl(const string &url, string &content, unsigned int &contentLength, string &contentType) {
return this->getContentByEncodedUrl(url, content, contentLength, contentType); return this->getContentByEncodedUrl(url, content, contentLength, contentType);
@ -402,25 +456,9 @@ namespace kiwix {
contentLength = 0; contentLength = 0;
if (this->zimFileHandler != NULL) { if (this->zimFileHandler != NULL) {
/* Parse the url */ zim::Article article;
char ns = 0; if (this->getArticleObjectByDecodedUrl(url, article)) {
string titleStr;
this->parseUrl(url, &ns, titleStr);
/* Main page */
if (titleStr.empty() && ns == 0) {
this->parseUrl(this->getMainPageUrl(), &ns, titleStr);
}
/* Extract the content from the zim file */
std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findx(ns, titleStr);
/* Test if the article was found */
if (resultPair.first == true) {
/* Get the article */
zim::Article article = zimFileHandler->getArticle(resultPair.second.getIndex());
/* If redirect */ /* If redirect */
unsigned int loopCounter = 0; unsigned int loopCounter = 0;
while (article.isRedirect() && loopCounter++<42) { while (article.isRedirect() && loopCounter++<42) {

View File

@ -61,6 +61,7 @@ namespace kiwix {
string getOrigId(); string getOrigId();
bool getFavicon(string &content, string &mimeType); bool getFavicon(string &content, string &mimeType);
bool getPageUrlFromTitle(const string &title, string &url); bool getPageUrlFromTitle(const string &title, string &url);
bool getMimeTypeByUrl(const string &url, string &mimeType);
bool getContentByUrl(const string &url, string &content, unsigned int &contentLength, string &contentType); bool getContentByUrl(const string &url, string &content, unsigned int &contentLength, string &contentType);
bool getContentByEncodedUrl(const string &url, string &content, unsigned int &contentLength, string &contentType, string &baseUrl); bool getContentByEncodedUrl(const string &url, string &content, unsigned int &contentLength, string &contentType, string &baseUrl);
bool getContentByEncodedUrl(const string &url, string &content, unsigned int &contentLength, string &contentType); bool getContentByEncodedUrl(const string &url, string &content, unsigned int &contentLength, string &contentType);
@ -89,7 +90,7 @@ namespace kiwix {
private: private:
std::map<std::string, unsigned int> parseCounterMetadata(); std::map<std::string, unsigned int> parseCounterMetadata();
bool getArticleObjectByDecodedUrl(const string &url, zim::Article &article);
}; };
} }

View File

@ -179,7 +179,7 @@ namespace kiwix {
oData["pages"] = pagesCDT; oData["pages"] = pagesCDT;
oData["count"] = kiwix::beautifyInteger(this->estimatedResultCount); oData["count"] = kiwix::beautifyInteger(this->estimatedResultCount);
oData["searchPattern"] = this->searchPattern; oData["searchPattern"] = kiwix::encodeDiples(this->searchPattern);
oData["searchPatternEncoded"] = urlEncode(this->searchPattern); oData["searchPatternEncoded"] = urlEncode(this->searchPattern);
oData["resultStart"] = this->resultStart + 1; oData["resultStart"] = this->resultStart + 1;
oData["resultEnd"] = (this->resultEnd > this->estimatedResultCount ? this->estimatedResultCount : this->resultEnd); oData["resultEnd"] = (this->resultEnd > this->estimatedResultCount ? this->estimatedResultCount : this->resultEnd);

View File

@ -104,6 +104,14 @@ void kiwix::stringReplacement(std::string& str, const std::string& oldStr, const
} }
} }
/* Encode string to avoid XSS attacks */
std::string kiwix::encodeDiples(const std::string& str) {
std::string result = str;
kiwix::stringReplacement(result, "<", "&lt;");
kiwix::stringReplacement(result, ">", "&gt;");
return result;
}
// Urlencode // Urlencode
//based on javascript encodeURIComponent() //based on javascript encodeURIComponent()

View File

@ -48,6 +48,7 @@ namespace kiwix {
void printStringInHexadecimal(const char *s); void printStringInHexadecimal(const char *s);
void printStringInHexadecimal(UnicodeString s); void printStringInHexadecimal(UnicodeString s);
void stringReplacement(std::string& str, const std::string& oldStr, const std::string& newStr); void stringReplacement(std::string& str, const std::string& oldStr, const std::string& newStr);
std::string encodeDiples(const std::string& str);
#endif #endif