Compare commits

..

No commits in common. "e1163234ccef4e16b0907f1fd1ce4f5e9aa12a25" and "a0a8f56488ca8eba3faac41ccda49d450c70ba6c" have entirely different histories.

7 changed files with 34 additions and 40 deletions

View File

@ -416,7 +416,6 @@ class DROGON_EXPORT HttpRequest
/// Set the path of the request
virtual void setPath(const std::string &path) = 0;
virtual void setPath(std::string &&path) = 0;
/**
* @brief The default behavior is to encode the value of setPath

View File

@ -26,24 +26,11 @@ using RedirectorHandler =
std::function<bool(const drogon::HttpRequestPtr &,
std::string &, //"http://" or "https://"
std::string &, // host
bool &)>; // path changed or not
std::string &)>; // path
/**
* @brief This plugin is used to redirect requests to proper URLs. It is a
* helper plugin for other plugins, e.g. SlashRemover.
* Users can register a handler to this plugin to redirect requests. All
* handlers will be called in the order of registration.
* The json configuration is as follows:
*
* @code
{
"name": "drogon::plugin::Redirector",
"dependencies": [],
"config": {
}
}
@endcode
*
*/
class DROGON_EXPORT Redirector : public drogon::Plugin<Redirector>,
public std::enable_shared_from_this<Redirector>

View File

@ -67,11 +67,11 @@ class DROGON_EXPORT SecureSSLRedirector
bool redirectingAdvice(const HttpRequestPtr &,
std::string &,
std::string &,
bool &) const;
std::string &) const;
bool redirectToSSL(const HttpRequestPtr &,
std::string &,
std::string &,
bool &) const;
std::string &) const;
std::regex exemptPegex_;
bool regexFlag_{false};

View File

@ -159,11 +159,6 @@ class HttpRequestImpl : public HttpRequest
path_ = path;
}
void setPath(std::string &&path) override
{
path_ = std::move(path);
}
void setPathEncode(bool pathEncode) override
{
pathEncode_ = pathEncode;

View File

@ -28,16 +28,15 @@ void Redirector::initAndStart(const Json::Value &config)
{
return HttpResponsePtr{};
}
std::string protocol, host;
bool pathChanged{false};
std::string protocol, host, path;
for (auto &handler : thisPtr->handlers_)
{
if (!handler(req, protocol, host, pathChanged))
if (!handler(req, protocol, host, path))
{
return HttpResponse::newNotFoundResponse();
}
}
if (!protocol.empty() || !host.empty() || pathChanged)
if (!protocol.empty() || !host.empty() || !path.empty())
{
std::string url;
if (protocol.empty())
@ -48,7 +47,18 @@ void Redirector::initAndStart(const Json::Value &config)
: "http://";
url.append(host);
}
if (pathChanged)
if (!path.empty())
{
if (url.empty())
{
url = std::move(path);
}
else
{
url.append(path);
}
}
else
{
url.append(req->path());
}
@ -64,7 +74,11 @@ void Redirector::initAndStart(const Json::Value &config)
{
url.append(req->getHeader("host"));
}
if (pathChanged)
if (!path.empty())
{
url.append(path);
}
else
{
url.append(req->path());
}

View File

@ -52,13 +52,13 @@ void SecureSSLRedirector::initAndStart(const Json::Value &config)
redirector->registerHandler([weakPtr](const drogon::HttpRequestPtr &req,
std::string &protocol,
std::string &host,
bool &pathChanged) -> bool {
std::string &path) -> bool {
auto thisPtr = weakPtr.lock();
if (!thisPtr)
{
return false;
}
return thisPtr->redirectingAdvice(req, protocol, host, pathChanged);
return thisPtr->redirectingAdvice(req, protocol, host, path);
});
}
@ -70,7 +70,7 @@ void SecureSSLRedirector::shutdown()
bool SecureSSLRedirector::redirectingAdvice(const HttpRequestPtr &req,
std::string &protocol,
std::string &host,
bool &pathChanged) const
std::string &path) const
{
if (req->isOnSecureConnection() || protocol == "https://")
{
@ -85,21 +85,20 @@ bool SecureSSLRedirector::redirectingAdvice(const HttpRequestPtr &req,
}
else
{
return redirectToSSL(req, protocol, host, pathChanged);
return redirectToSSL(req, protocol, host, path);
}
}
else
{
return redirectToSSL(req, protocol, host, pathChanged);
return redirectToSSL(req, protocol, host, path);
}
}
bool SecureSSLRedirector::redirectToSSL(const HttpRequestPtr &req,
std::string &protocol,
std::string &host,
bool &pathChanged) const
std::string &path) const
{
(void)pathChanged;
if (!secureHost_.empty())
{
host = secureHost_;

View File

@ -85,7 +85,7 @@ void SlashRemover::initAndStart(const Json::Value& config)
[removeMode, redirect = redirect_](const HttpRequestPtr& req,
std::string& protocol,
std::string& host,
bool& pathChanged) -> bool {
std::string& path) -> bool {
static const std::regex regex(regexes[removeMode - 1]);
if (std::regex_match(req->path(), regex))
{
@ -103,10 +103,10 @@ void SlashRemover::initAndStart(const Json::Value& config)
removeExcessiveSlashes(newPath);
break;
}
req->setPath(std::move(newPath));
req->setPath(newPath);
if (redirect)
{
pathChanged = true;
path = std::move(newPath);
}
}
return true;