mirror of https://github.com/kiwix/libkiwix.git
Dropped the 2nd param of urlEncode()
`urlEncode(str)` is now equivalent to the previous `urlEncode(str, true)`.
This commit is contained in:
parent
3bf8211b70
commit
471c5b89f4
|
@ -42,19 +42,19 @@ std::string buildSearchString(const Filter& f)
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
if ( f.hasQuery() )
|
if ( f.hasQuery() )
|
||||||
oss << AMP << "q=" << urlEncode(f.getQuery(), true);
|
oss << AMP << "q=" << urlEncode(f.getQuery());
|
||||||
|
|
||||||
if ( f.hasCategory() )
|
if ( f.hasCategory() )
|
||||||
oss << AMP << "category=" << urlEncode(f.getCategory(), true);
|
oss << AMP << "category=" << urlEncode(f.getCategory());
|
||||||
|
|
||||||
if ( f.hasLang() )
|
if ( f.hasLang() )
|
||||||
oss << AMP << "lang=" << urlEncode(f.getLang(), true);
|
oss << AMP << "lang=" << urlEncode(f.getLang());
|
||||||
|
|
||||||
if ( f.hasName() )
|
if ( f.hasName() )
|
||||||
oss << AMP << "name=" << urlEncode(f.getName(), true);
|
oss << AMP << "name=" << urlEncode(f.getName());
|
||||||
|
|
||||||
if ( !f.getAcceptTags().empty() )
|
if ( !f.getAcceptTags().empty() )
|
||||||
oss << AMP << "tag=" << urlEncode(join(f.getAcceptTags(), ";"), true);
|
oss << AMP << "tag=" << urlEncode(join(f.getAcceptTags(), ";"));
|
||||||
|
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ std::string fullEntryXML(const Book& book, const std::string& rootLocation, cons
|
||||||
{"title", book.getTitle()},
|
{"title", book.getTitle()},
|
||||||
{"description", book.getDescription()},
|
{"description", book.getDescription()},
|
||||||
{"language", book.getLanguage()},
|
{"language", book.getLanguage()},
|
||||||
{"content_id", urlEncode(contentId, true)},
|
{"content_id", urlEncode(contentId)},
|
||||||
{"updated", bookDate}, // XXX: this should be the entry update datetime
|
{"updated", bookDate}, // XXX: this should be the entry update datetime
|
||||||
{"book_date", bookDate},
|
{"book_date", bookDate},
|
||||||
{"category", book.getCategory()},
|
{"category", book.getCategory()},
|
||||||
|
@ -241,7 +241,7 @@ std::string OPDSDumper::categoriesOPDSFeed() const
|
||||||
const auto now = gen_date_str();
|
const auto now = gen_date_str();
|
||||||
kainjow::mustache::list categoryData;
|
kainjow::mustache::list categoryData;
|
||||||
for ( const auto& category : library->getBooksCategories() ) {
|
for ( const auto& category : library->getBooksCategories() ) {
|
||||||
const auto urlencodedCategoryName = urlEncode(category, true);
|
const auto urlencodedCategoryName = urlEncode(category);
|
||||||
categoryData.push_back(kainjow::mustache::object{
|
categoryData.push_back(kainjow::mustache::object{
|
||||||
{"name", category},
|
{"name", category},
|
||||||
{"urlencoded_name", urlencodedCategoryName},
|
{"urlencoded_name", urlencodedCategoryName},
|
||||||
|
|
|
@ -94,7 +94,7 @@ kainjow::mustache::data buildQueryData
|
||||||
kainjow::mustache::data query;
|
kainjow::mustache::data query;
|
||||||
query.set("pattern", kiwix::encodeDiples(pattern));
|
query.set("pattern", kiwix::encodeDiples(pattern));
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
ss << searchProtocolPrefix << "?pattern=" << urlEncode(pattern, true);
|
ss << searchProtocolPrefix << "?pattern=" << urlEncode(pattern);
|
||||||
ss << "&" << bookQuery;
|
ss << "&" << bookQuery;
|
||||||
query.set("unpaginatedQuery", ss.str());
|
query.set("unpaginatedQuery", ss.str());
|
||||||
auto lang = extractValueFromQuery(bookQuery, "books.filter.lang");
|
auto lang = extractValueFromQuery(bookQuery, "books.filter.lang");
|
||||||
|
@ -174,7 +174,7 @@ std::string SearchRenderer::renderTemplate(const std::string& tmpl_str)
|
||||||
const std::string zim_id(it.getZimId());
|
const std::string zim_id(it.getZimId());
|
||||||
const auto path = mp_nameMapper->getNameForId(zim_id) + "/" + it.getPath();
|
const auto path = mp_nameMapper->getNameForId(zim_id) + "/" + it.getPath();
|
||||||
result.set("title", it.getTitle());
|
result.set("title", it.getTitle());
|
||||||
result.set("absolutePath", absPathPrefix + urlEncode(path, true));
|
result.set("absolutePath", absPathPrefix + urlEncode(path));
|
||||||
result.set("snippet", it.getSnippet());
|
result.set("snippet", it.getSnippet());
|
||||||
if (mp_library) {
|
if (mp_library) {
|
||||||
result.set("bookTitle", mp_library->getBookById(zim_id).getTitle());
|
result.set("bookTitle", mp_library->getBookById(zim_id).getTitle());
|
||||||
|
|
|
@ -1030,7 +1030,7 @@ ParameterizedMessage suggestSearchMsg(const std::string& searchURL, const std::s
|
||||||
std::unique_ptr<Response>
|
std::unique_ptr<Response>
|
||||||
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
|
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
|
||||||
{
|
{
|
||||||
const auto path = kiwix::urlEncode(item.getPath(), true);
|
const auto path = kiwix::urlEncode(item.getPath());
|
||||||
const auto redirectUrl = m_root + "/content/" + bookName + "/" + path;
|
const auto redirectUrl = m_root + "/content/" + bookName + "/" + path;
|
||||||
return Response::build_redirect(*this, redirectUrl);
|
return Response::build_redirect(*this, redirectUrl);
|
||||||
}
|
}
|
||||||
|
@ -1055,7 +1055,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
|
||||||
} catch (const std::out_of_range& e) {}
|
} catch (const std::out_of_range& e) {}
|
||||||
|
|
||||||
if (archive == nullptr) {
|
if (archive == nullptr) {
|
||||||
const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true);
|
const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern);
|
||||||
return HTTP404Response(*this, request)
|
return HTTP404Response(*this, request)
|
||||||
+ urlNotFoundMsg
|
+ urlNotFoundMsg
|
||||||
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
||||||
|
@ -1096,7 +1096,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
|
||||||
if (m_verbose.load())
|
if (m_verbose.load())
|
||||||
printf("Failed to find %s\n", urlStr.c_str());
|
printf("Failed to find %s\n", urlStr.c_str());
|
||||||
|
|
||||||
std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true);
|
std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern);
|
||||||
return HTTP404Response(*this, request)
|
return HTTP404Response(*this, request)
|
||||||
+ urlNotFoundMsg
|
+ urlNotFoundMsg
|
||||||
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
||||||
|
|
|
@ -116,10 +116,10 @@ MHD_Result RequestContext::fill_argument(void *__this, enum MHD_ValueKind kind,
|
||||||
if ( ! _this->queryString.empty() ) {
|
if ( ! _this->queryString.empty() ) {
|
||||||
_this->queryString += "&";
|
_this->queryString += "&";
|
||||||
}
|
}
|
||||||
_this->queryString += urlEncode(key, true);
|
_this->queryString += urlEncode(key);
|
||||||
if ( value ) {
|
if ( value ) {
|
||||||
_this->queryString += "=";
|
_this->queryString += "=";
|
||||||
_this->queryString += urlEncode(value, true);
|
_this->queryString += urlEncode(value);
|
||||||
}
|
}
|
||||||
return MHD_YES;
|
return MHD_YES;
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,7 +99,7 @@ class RequestContext {
|
||||||
std::string get_query(F filter, bool mustEncode) const {
|
std::string get_query(F filter, bool mustEncode) const {
|
||||||
std::string q;
|
std::string q;
|
||||||
const char* sep = "";
|
const char* sep = "";
|
||||||
auto encode = [=](const std::string& value) { return mustEncode?urlEncode(value, true):value; };
|
auto encode = [=](const std::string& value) { return mustEncode?urlEncode(value):value; };
|
||||||
for ( const auto& a : arguments ) {
|
for ( const auto& a : arguments ) {
|
||||||
if (!filter(a.first)) {
|
if (!filter(a.first)) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -164,9 +164,6 @@ std::string kiwix::encodeDiples(const std::string& str)
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
/* urlEncode() based on javascript encodeURI() &
|
|
||||||
encodeURIComponent(). Mostly code from rstudio/httpuv (GPLv3) */
|
|
||||||
|
|
||||||
bool isReservedUrlChar(char c)
|
bool isReservedUrlChar(char c)
|
||||||
{
|
{
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
@ -246,7 +243,7 @@ int hexToInt(char c) {
|
||||||
|
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
|
|
||||||
std::string kiwix::urlEncode(const std::string& value, bool encodeReserved)
|
std::string kiwix::urlEncode(const std::string& value)
|
||||||
{
|
{
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << std::hex << std::uppercase;
|
os << std::hex << std::uppercase;
|
||||||
|
@ -254,7 +251,7 @@ std::string kiwix::urlEncode(const std::string& value, bool encodeReserved)
|
||||||
it != value.end();
|
it != value.end();
|
||||||
it++) {
|
it++) {
|
||||||
|
|
||||||
if (!needsEscape(*it, encodeReserved)) {
|
if (!needsEscape(*it, true)) {
|
||||||
os << *it;
|
os << *it;
|
||||||
} else {
|
} else {
|
||||||
const unsigned int charVal = static_cast<unsigned char>(*it);
|
const unsigned int charVal = static_cast<unsigned char>(*it);
|
||||||
|
|
|
@ -55,7 +55,9 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
std::string urlEncode(const std::string& value, bool encodeReserved);
|
/* urlEncode() is the equivalent of JS encodeURIComponent(), with the only
|
||||||
|
* difference that the slash (/) symbol is NOT encoded. */
|
||||||
|
std::string urlEncode(const std::string& value);
|
||||||
std::string urlDecode(const std::string& value, bool component = false);
|
std::string urlDecode(const std::string& value, bool component = false);
|
||||||
|
|
||||||
std::string join(const std::vector<std::string>& list, const std::string& sep);
|
std::string join(const std::vector<std::string>& list, const std::string& sep);
|
||||||
|
|
|
@ -125,26 +125,19 @@ TEST(stringTools, urlEncode)
|
||||||
{
|
{
|
||||||
using namespace URLEncoding;
|
using namespace URLEncoding;
|
||||||
|
|
||||||
EXPECT_EQ(urlEncode(letters,false), letters);
|
EXPECT_EQ(urlEncode(letters), letters);
|
||||||
EXPECT_EQ(urlEncode(letters, true), letters);
|
|
||||||
|
|
||||||
EXPECT_EQ(urlEncode(digits,false), digits);
|
EXPECT_EQ(urlEncode(digits), digits);
|
||||||
EXPECT_EQ(urlEncode(digits, true), digits);
|
|
||||||
|
|
||||||
EXPECT_EQ(urlEncode(nonEncodableSymbols,false), nonEncodableSymbols);
|
EXPECT_EQ(urlEncode(nonEncodableSymbols), nonEncodableSymbols);
|
||||||
EXPECT_EQ(urlEncode(nonEncodableSymbols, true), nonEncodableSymbols);
|
|
||||||
|
|
||||||
EXPECT_EQ(urlEncode(uriDelimSymbols,false), uriDelimSymbols);
|
EXPECT_EQ(urlEncode(uriDelimSymbols), "%3A%40%3F%3D%2B%26%23%24%3B%2C");
|
||||||
EXPECT_EQ(urlEncode(uriDelimSymbols, true), "%3A%40%3F%3D%2B%26%23%24%3B%2C");
|
|
||||||
|
|
||||||
EXPECT_EQ(urlEncode(otherSymbols,false), "%60%25%5E%5B%5D%7B%7D%5C%7C%22%3C%3E");
|
EXPECT_EQ(urlEncode(otherSymbols), "%60%25%5E%5B%5D%7B%7D%5C%7C%22%3C%3E");
|
||||||
EXPECT_EQ(urlEncode(otherSymbols,false), urlEncode(otherSymbols, true));
|
|
||||||
|
|
||||||
EXPECT_EQ(urlEncode(whitespace,false), "%20%0A%09%0D");
|
EXPECT_EQ(urlEncode(whitespace), "%20%0A%09%0D");
|
||||||
EXPECT_EQ(urlEncode(whitespace,false), urlEncode(whitespace, true));
|
|
||||||
|
|
||||||
EXPECT_EQ(urlEncode(someNonASCIIChars,false), "%CE%A3%E2%99%82%E2%99%80%E3%83%84");
|
EXPECT_EQ(urlEncode(someNonASCIIChars), "%CE%A3%E2%99%82%E2%99%80%E3%83%84");
|
||||||
EXPECT_EQ(urlEncode(someNonASCIIChars,false), urlEncode(someNonASCIIChars, true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(stringTools, urlDecode)
|
TEST(stringTools, urlDecode)
|
||||||
|
@ -161,13 +154,13 @@ TEST(stringTools, urlDecode)
|
||||||
|
|
||||||
for ( const char c : allTestChars ) {
|
for ( const char c : allTestChars ) {
|
||||||
const std::string str(1, c);
|
const std::string str(1, c);
|
||||||
EXPECT_EQ(urlDecode(urlEncode(str,false),false), str);
|
EXPECT_EQ(urlDecode(urlEncode(str), true), str);
|
||||||
EXPECT_EQ(urlDecode(urlEncode(str, true), true), str);
|
|
||||||
EXPECT_EQ(urlDecode(urlEncode(str,false)), str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EXPECT_EQ(urlDecode(urlEncode(allTestChars,false),false), allTestChars);
|
EXPECT_EQ(urlDecode(urlEncode(allTestChars), true), allTestChars);
|
||||||
EXPECT_EQ(urlDecode(urlEncode(allTestChars, true), true), allTestChars);
|
|
||||||
|
const std::string encodedUriDelimSymbols = urlEncode(uriDelimSymbols);
|
||||||
|
EXPECT_EQ(urlDecode(encodedUriDelimSymbols, false), encodedUriDelimSymbols);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue