HttpRequest: add feature to avoid url encoding of path (#730)

This commit is contained in:
JuergenGleiss 2021-02-28 16:42:15 +01:00 committed by GitHub
parent ef51951785
commit af2bd6ba69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -326,6 +326,16 @@ class HttpRequest
/// Set the path of the request
virtual void setPath(const std::string &path) = 0;
/**
* @brief The default behavior is to encode the value of setPath
* using urlEncode. Setting the path encode to false avoid the
* value of path will be changed by the library
*
* @param bool true --> the path will be url encoded
* false --> using value of path as it is set
*/
virtual void setPathEncode(bool) = 0;
/// Set the parameter of the request
virtual void setParameter(const std::string &key,
const std::string &value) = 0;

View File

@ -193,10 +193,17 @@ void HttpRequestImpl::appendToBuffer(trantor::MsgBuffer *output) const
}
if (!path_.empty())
{
if (pathEncode_)
{
output->append(utils::urlEncode(path_));
}
else
{
output->append(path_);
}
}
else
{
output->append("/");
}
@ -508,6 +515,7 @@ void HttpRequestImpl::swap(HttpRequestImpl &that) noexcept
swap(flagForParsingParameters_, that.flagForParsingParameters_);
swap(matchedPathPattern_, that.matchedPathPattern_);
swap(path_, that.path_);
swap(pathEncode_, that.pathEncode_);
swap(query_, that.query_);
swap(headers_, that.headers_);
swap(cookies_, that.cookies_);

View File

@ -51,6 +51,7 @@ class HttpRequestImpl : public HttpRequest
cookies_.clear();
flagForParsingParameters_ = false;
path_.clear();
pathEncode_ = true;
matchedPathPattern_ = "";
query_.clear();
parameters_.clear();
@ -121,6 +122,11 @@ class HttpRequestImpl : public HttpRequest
path_ = path;
}
void setPathEncode(bool pathEncode) override
{
pathEncode_ = pathEncode;
}
virtual const std::unordered_map<std::string, std::string> &parameters()
const override
{
@ -488,6 +494,7 @@ class HttpRequestImpl : public HttpRequest
HttpMethod method_{Invalid};
Version version_{Version::kUnknown};
std::string path_;
bool pathEncode_{true};
string_view matchedPathPattern_{""};
std::string query_;
std::unordered_map<std::string, std::string> headers_;