mirror of https://github.com/kiwix/libkiwix.git
Correctly url encode querystring
Fix tests with querystring needed url encoding (pattern=jazz&books.query.title=Ray%20Charles)
This commit is contained in:
parent
b857293cfd
commit
3bca43344f
|
@ -221,7 +221,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
|||
auto bookName = request.get_argument("content");
|
||||
try {
|
||||
const auto bookIds = Library::BookIdSet{mp_nameMapper->getIdForName(bookName)};
|
||||
const auto queryString = request.get_query([&](const std::string& key){return key == "content";});
|
||||
const auto queryString = request.get_query([&](const std::string& key){return key == "content";}, true);
|
||||
return {queryString, bookIds};
|
||||
} catch (const std::out_of_range&) {
|
||||
throw Error(noSuchBookErrorMsg(bookName));
|
||||
|
@ -238,7 +238,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
|||
throw Error(noValueForArgMsg("books.id"));
|
||||
}
|
||||
const auto bookIds = Library::BookIdSet(id_vec.begin(), id_vec.end());
|
||||
const auto queryString = request.get_query([&](const std::string& key){return key == "books.id";});
|
||||
const auto queryString = request.get_query([&](const std::string& key){return key == "books.id";}, true);
|
||||
return {queryString, bookIds};
|
||||
} catch(const std::out_of_range&) {}
|
||||
|
||||
|
@ -256,7 +256,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
|||
throw Error(noSuchBookErrorMsg(bookName));
|
||||
}
|
||||
}
|
||||
const auto queryString = request.get_query([&](const std::string& key){return key == "books.name";});
|
||||
const auto queryString = request.get_query([&](const std::string& key){return key == "books.name";}, true);
|
||||
return {queryString, bookIds};
|
||||
} catch(const std::out_of_range&) {}
|
||||
|
||||
|
@ -267,7 +267,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
|||
throw Error(nonParameterizedMessage("no-book-found"));
|
||||
}
|
||||
const auto bookIds = Library::BookIdSet(id_vec.begin(), id_vec.end());
|
||||
const auto queryString = request.get_query([&](const std::string& key){return startsWith(key, "books.filter.");});
|
||||
const auto queryString = request.get_query([&](const std::string& key){return startsWith(key, "books.filter.");}, true);
|
||||
return {queryString, bookIds};
|
||||
}
|
||||
|
||||
|
|
|
@ -92,20 +92,21 @@ class RequestContext {
|
|||
std::string get_url_part(int part) const;
|
||||
std::string get_full_url() const;
|
||||
|
||||
std::string get_query() const {
|
||||
return get_query([](const std::string& key) {return true;});
|
||||
std::string get_query(bool mustEncode = false) const {
|
||||
return get_query([](const std::string& key) {return true;}, mustEncode);
|
||||
}
|
||||
|
||||
template<class F>
|
||||
std::string get_query(F filter) const {
|
||||
std::string get_query(F filter, bool mustEncode) const {
|
||||
std::string q;
|
||||
const char* sep = "";
|
||||
auto encode = [=](const std::string& value) { return mustEncode?urlEncode(value, true):value; };
|
||||
for ( const auto& a : arguments ) {
|
||||
if (!filter(a.first)) {
|
||||
continue;
|
||||
}
|
||||
for (const auto& v: a.second) {
|
||||
q += sep + a.first + '=' + v;
|
||||
q += sep + encode(a.first) + '=' + encode(v);
|
||||
sep = "&";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue