URI-encoding of the root location part

Now the root location is URI-encoded too.

In order to properly test this change the root location in the tests was
changed from "/ROOT" to "/ROOT#?" (or "/ROOT%23%3F" in URI-encoded form),
which is why this commit is so big.
This commit is contained in:
Veloman Yunkan
2023-02-08 21:32:05 +01:00
parent 97f0314fe6
commit 05a66ead6e
9 changed files with 437 additions and 434 deletions

View File

@ -430,7 +430,9 @@ InternalServer::InternalServer(Library* library,
searchCache(getEnvVar<int>("KIWIX_SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)),
suggestionSearcherCache(getEnvVar<int>("KIWIX_SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))),
m_customizedResources(new CustomizedResources)
{}
{
m_root = urlEncode(m_root);
}
InternalServer::~InternalServer() = default;
@ -621,7 +623,7 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
if (isEndpointUrl(url, "catch"))
return handle_catch(request);
std::string contentUrl = urlEncode(m_root + "/content" + url);
std::string contentUrl = m_root + "/content" + urlEncode(url);
const std::string query = request.get_query();
if ( ! query.empty() )
contentUrl += "?" + query;
@ -1044,8 +1046,9 @@ ParameterizedMessage suggestSearchMsg(const std::string& searchURL, const std::s
std::unique_ptr<Response>
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
{
const auto absPath = m_root + "/content/" + bookName + "/" + item.getPath();
return Response::build_redirect(*this, kiwix::urlEncode(absPath));
const auto contentPath = "/content/" + bookName + "/" + item.getPath();
const auto url = m_root + kiwix::urlEncode(contentPath);
return Response::build_redirect(*this, url);
}
std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& request)

View File

@ -163,8 +163,8 @@ class InternalServer {
private: // data
std::string m_addr;
int m_port;
std::string m_root;
std::string m_rootWithSeparator;
std::string m_root; // URI-encoded
std::string m_rootWithSeparator; // URI-decoded
int m_nbThreads;
unsigned int m_multizimSearchLimit;
std::atomic_bool m_verbose;

View File

@ -52,12 +52,12 @@ RequestMethod str2RequestMethod(const std::string& method) {
} // unnamed namespace
RequestContext::RequestContext(struct MHD_Connection* connection,
const std::string& _rootLocation,
const std::string& unrootedUrl,
const std::string& _rootLocation, // URI-encoded
const std::string& unrootedUrl, // URI-decoded
const std::string& _method,
const std::string& version) :
rootLocation(_rootLocation),
full_url(_rootLocation + unrootedUrl),
full_url(_rootLocation + urlEncode(unrootedUrl)),
url(unrootedUrl),
method(str2RequestMethod(_method)),
version(version),

View File

@ -57,8 +57,8 @@ class IndexError: public std::runtime_error {};
class RequestContext {
public: // functions
RequestContext(struct MHD_Connection* connection,
const std::string& rootLocation,
const std::string& unrootedUrl,
const std::string& rootLocation, // URI-encoded
const std::string& unrootedUrl, // URI-decoded
const std::string& method,
const std::string& version);
~RequestContext();

View File

@ -200,7 +200,7 @@ HTTP404Response::HTTP404Response(const InternalServer& server,
HTTPErrorResponse& HTTP404Response::operator+(UrlNotFoundMsg /*unused*/)
{
const std::string requestUrl = m_request.get_full_url();
const std::string requestUrl = urlDecode(m_request.get_full_url(), false);
return *this + ParameterizedMessage("url-not-found", {{"url", requestUrl}});
}
@ -234,7 +234,7 @@ HTTP400Response::HTTP400Response(const InternalServer& server,
HTTPErrorResponse& HTTP400Response::operator+(InvalidUrlMsg /*unused*/)
{
std::string requestUrl = m_request.get_full_url();
std::string requestUrl = urlDecode(m_request.get_full_url(), false);
const auto query = m_request.get_query();
if (!query.empty()) {
requestUrl += "?" + encodeDiples(query);