mirror of
https://github.com/drogonframework/drogon.git
synced 2025-07-18 00:00:46 -04:00
Compare commits
No commits in common. "e1163234ccef4e16b0907f1fd1ce4f5e9aa12a25" and "a0a8f56488ca8eba3faac41ccda49d450c70ba6c" have entirely different histories.
e1163234cc
...
a0a8f56488
@ -416,7 +416,6 @@ class DROGON_EXPORT HttpRequest
|
|||||||
|
|
||||||
/// Set the path of the request
|
/// Set the path of the request
|
||||||
virtual void setPath(const std::string &path) = 0;
|
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
|
* @brief The default behavior is to encode the value of setPath
|
||||||
|
@ -24,26 +24,13 @@ namespace plugin
|
|||||||
{
|
{
|
||||||
using RedirectorHandler =
|
using RedirectorHandler =
|
||||||
std::function<bool(const drogon::HttpRequestPtr &,
|
std::function<bool(const drogon::HttpRequestPtr &,
|
||||||
std::string &, //"http://" or "https://"
|
std::string &, //"http://" or "https://"
|
||||||
std::string &, // host
|
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
|
* @brief This plugin is used to redirect requests to proper URLs. It is a
|
||||||
* helper plugin for other plugins, e.g. SlashRemover.
|
* 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>,
|
class DROGON_EXPORT Redirector : public drogon::Plugin<Redirector>,
|
||||||
public std::enable_shared_from_this<Redirector>
|
public std::enable_shared_from_this<Redirector>
|
||||||
|
@ -67,11 +67,11 @@ class DROGON_EXPORT SecureSSLRedirector
|
|||||||
bool redirectingAdvice(const HttpRequestPtr &,
|
bool redirectingAdvice(const HttpRequestPtr &,
|
||||||
std::string &,
|
std::string &,
|
||||||
std::string &,
|
std::string &,
|
||||||
bool &) const;
|
std::string &) const;
|
||||||
bool redirectToSSL(const HttpRequestPtr &,
|
bool redirectToSSL(const HttpRequestPtr &,
|
||||||
std::string &,
|
std::string &,
|
||||||
std::string &,
|
std::string &,
|
||||||
bool &) const;
|
std::string &) const;
|
||||||
|
|
||||||
std::regex exemptPegex_;
|
std::regex exemptPegex_;
|
||||||
bool regexFlag_{false};
|
bool regexFlag_{false};
|
||||||
|
@ -159,11 +159,6 @@ class HttpRequestImpl : public HttpRequest
|
|||||||
path_ = path;
|
path_ = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPath(std::string &&path) override
|
|
||||||
{
|
|
||||||
path_ = std::move(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setPathEncode(bool pathEncode) override
|
void setPathEncode(bool pathEncode) override
|
||||||
{
|
{
|
||||||
pathEncode_ = pathEncode;
|
pathEncode_ = pathEncode;
|
||||||
|
@ -28,16 +28,15 @@ void Redirector::initAndStart(const Json::Value &config)
|
|||||||
{
|
{
|
||||||
return HttpResponsePtr{};
|
return HttpResponsePtr{};
|
||||||
}
|
}
|
||||||
std::string protocol, host;
|
std::string protocol, host, path;
|
||||||
bool pathChanged{false};
|
|
||||||
for (auto &handler : thisPtr->handlers_)
|
for (auto &handler : thisPtr->handlers_)
|
||||||
{
|
{
|
||||||
if (!handler(req, protocol, host, pathChanged))
|
if (!handler(req, protocol, host, path))
|
||||||
{
|
{
|
||||||
return HttpResponse::newNotFoundResponse();
|
return HttpResponse::newNotFoundResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!protocol.empty() || !host.empty() || pathChanged)
|
if (!protocol.empty() || !host.empty() || !path.empty())
|
||||||
{
|
{
|
||||||
std::string url;
|
std::string url;
|
||||||
if (protocol.empty())
|
if (protocol.empty())
|
||||||
@ -48,7 +47,18 @@ void Redirector::initAndStart(const Json::Value &config)
|
|||||||
: "http://";
|
: "http://";
|
||||||
url.append(host);
|
url.append(host);
|
||||||
}
|
}
|
||||||
if (pathChanged)
|
if (!path.empty())
|
||||||
|
{
|
||||||
|
if (url.empty())
|
||||||
|
{
|
||||||
|
url = std::move(path);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
url.append(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
url.append(req->path());
|
url.append(req->path());
|
||||||
}
|
}
|
||||||
@ -64,7 +74,11 @@ void Redirector::initAndStart(const Json::Value &config)
|
|||||||
{
|
{
|
||||||
url.append(req->getHeader("host"));
|
url.append(req->getHeader("host"));
|
||||||
}
|
}
|
||||||
if (pathChanged)
|
if (!path.empty())
|
||||||
|
{
|
||||||
|
url.append(path);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
url.append(req->path());
|
url.append(req->path());
|
||||||
}
|
}
|
||||||
|
@ -52,13 +52,13 @@ void SecureSSLRedirector::initAndStart(const Json::Value &config)
|
|||||||
redirector->registerHandler([weakPtr](const drogon::HttpRequestPtr &req,
|
redirector->registerHandler([weakPtr](const drogon::HttpRequestPtr &req,
|
||||||
std::string &protocol,
|
std::string &protocol,
|
||||||
std::string &host,
|
std::string &host,
|
||||||
bool &pathChanged) -> bool {
|
std::string &path) -> bool {
|
||||||
auto thisPtr = weakPtr.lock();
|
auto thisPtr = weakPtr.lock();
|
||||||
if (!thisPtr)
|
if (!thisPtr)
|
||||||
{
|
{
|
||||||
return false;
|
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,
|
bool SecureSSLRedirector::redirectingAdvice(const HttpRequestPtr &req,
|
||||||
std::string &protocol,
|
std::string &protocol,
|
||||||
std::string &host,
|
std::string &host,
|
||||||
bool &pathChanged) const
|
std::string &path) const
|
||||||
{
|
{
|
||||||
if (req->isOnSecureConnection() || protocol == "https://")
|
if (req->isOnSecureConnection() || protocol == "https://")
|
||||||
{
|
{
|
||||||
@ -85,21 +85,20 @@ bool SecureSSLRedirector::redirectingAdvice(const HttpRequestPtr &req,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return redirectToSSL(req, protocol, host, pathChanged);
|
return redirectToSSL(req, protocol, host, path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return redirectToSSL(req, protocol, host, pathChanged);
|
return redirectToSSL(req, protocol, host, path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SecureSSLRedirector::redirectToSSL(const HttpRequestPtr &req,
|
bool SecureSSLRedirector::redirectToSSL(const HttpRequestPtr &req,
|
||||||
std::string &protocol,
|
std::string &protocol,
|
||||||
std::string &host,
|
std::string &host,
|
||||||
bool &pathChanged) const
|
std::string &path) const
|
||||||
{
|
{
|
||||||
(void)pathChanged;
|
|
||||||
if (!secureHost_.empty())
|
if (!secureHost_.empty())
|
||||||
{
|
{
|
||||||
host = secureHost_;
|
host = secureHost_;
|
||||||
|
@ -85,7 +85,7 @@ void SlashRemover::initAndStart(const Json::Value& config)
|
|||||||
[removeMode, redirect = redirect_](const HttpRequestPtr& req,
|
[removeMode, redirect = redirect_](const HttpRequestPtr& req,
|
||||||
std::string& protocol,
|
std::string& protocol,
|
||||||
std::string& host,
|
std::string& host,
|
||||||
bool& pathChanged) -> bool {
|
std::string& path) -> bool {
|
||||||
static const std::regex regex(regexes[removeMode - 1]);
|
static const std::regex regex(regexes[removeMode - 1]);
|
||||||
if (std::regex_match(req->path(), regex))
|
if (std::regex_match(req->path(), regex))
|
||||||
{
|
{
|
||||||
@ -103,10 +103,10 @@ void SlashRemover::initAndStart(const Json::Value& config)
|
|||||||
removeExcessiveSlashes(newPath);
|
removeExcessiveSlashes(newPath);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
req->setPath(std::move(newPath));
|
req->setPath(newPath);
|
||||||
if (redirect)
|
if (redirect)
|
||||||
{
|
{
|
||||||
pathChanged = true;
|
path = std::move(newPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user