mirror of
https://github.com/drogonframework/drogon.git
synced 2025-07-18 00:00:46 -04:00
Compare commits
4 Commits
a0a8f56488
...
e1163234cc
Author | SHA1 | Date | |
---|---|---|---|
|
e1163234cc | ||
|
01af32e447 | ||
|
8049959290 | ||
|
b00e59756a |
@ -416,6 +416,7 @@ 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
|
||||||
|
@ -26,11 +26,24 @@ 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
|
||||||
std::string &)>; // path
|
bool &)>; // path changed or not
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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 &,
|
||||||
std::string &) const;
|
bool &) const;
|
||||||
bool redirectToSSL(const HttpRequestPtr &,
|
bool redirectToSSL(const HttpRequestPtr &,
|
||||||
std::string &,
|
std::string &,
|
||||||
std::string &,
|
std::string &,
|
||||||
std::string &) const;
|
bool &) const;
|
||||||
|
|
||||||
std::regex exemptPegex_;
|
std::regex exemptPegex_;
|
||||||
bool regexFlag_{false};
|
bool regexFlag_{false};
|
||||||
|
@ -159,6 +159,11 @@ 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,15 +28,16 @@ void Redirector::initAndStart(const Json::Value &config)
|
|||||||
{
|
{
|
||||||
return HttpResponsePtr{};
|
return HttpResponsePtr{};
|
||||||
}
|
}
|
||||||
std::string protocol, host, path;
|
std::string protocol, host;
|
||||||
|
bool pathChanged{false};
|
||||||
for (auto &handler : thisPtr->handlers_)
|
for (auto &handler : thisPtr->handlers_)
|
||||||
{
|
{
|
||||||
if (!handler(req, protocol, host, path))
|
if (!handler(req, protocol, host, pathChanged))
|
||||||
{
|
{
|
||||||
return HttpResponse::newNotFoundResponse();
|
return HttpResponse::newNotFoundResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!protocol.empty() || !host.empty() || !path.empty())
|
if (!protocol.empty() || !host.empty() || pathChanged)
|
||||||
{
|
{
|
||||||
std::string url;
|
std::string url;
|
||||||
if (protocol.empty())
|
if (protocol.empty())
|
||||||
@ -47,18 +48,7 @@ void Redirector::initAndStart(const Json::Value &config)
|
|||||||
: "http://";
|
: "http://";
|
||||||
url.append(host);
|
url.append(host);
|
||||||
}
|
}
|
||||||
if (!path.empty())
|
if (pathChanged)
|
||||||
{
|
|
||||||
if (url.empty())
|
|
||||||
{
|
|
||||||
url = std::move(path);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
url.append(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
url.append(req->path());
|
url.append(req->path());
|
||||||
}
|
}
|
||||||
@ -74,11 +64,7 @@ void Redirector::initAndStart(const Json::Value &config)
|
|||||||
{
|
{
|
||||||
url.append(req->getHeader("host"));
|
url.append(req->getHeader("host"));
|
||||||
}
|
}
|
||||||
if (!path.empty())
|
if (pathChanged)
|
||||||
{
|
|
||||||
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,
|
||||||
std::string &path) -> bool {
|
bool &pathChanged) -> bool {
|
||||||
auto thisPtr = weakPtr.lock();
|
auto thisPtr = weakPtr.lock();
|
||||||
if (!thisPtr)
|
if (!thisPtr)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return thisPtr->redirectingAdvice(req, protocol, host, path);
|
return thisPtr->redirectingAdvice(req, protocol, host, pathChanged);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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,
|
||||||
std::string &path) const
|
bool &pathChanged) const
|
||||||
{
|
{
|
||||||
if (req->isOnSecureConnection() || protocol == "https://")
|
if (req->isOnSecureConnection() || protocol == "https://")
|
||||||
{
|
{
|
||||||
@ -85,20 +85,21 @@ bool SecureSSLRedirector::redirectingAdvice(const HttpRequestPtr &req,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return redirectToSSL(req, protocol, host, path);
|
return redirectToSSL(req, protocol, host, pathChanged);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return redirectToSSL(req, protocol, host, path);
|
return redirectToSSL(req, protocol, host, pathChanged);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SecureSSLRedirector::redirectToSSL(const HttpRequestPtr &req,
|
bool SecureSSLRedirector::redirectToSSL(const HttpRequestPtr &req,
|
||||||
std::string &protocol,
|
std::string &protocol,
|
||||||
std::string &host,
|
std::string &host,
|
||||||
std::string &path) const
|
bool &pathChanged) 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,
|
||||||
std::string& path) -> bool {
|
bool& pathChanged) -> 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(newPath);
|
req->setPath(std::move(newPath));
|
||||||
if (redirect)
|
if (redirect)
|
||||||
{
|
{
|
||||||
path = std::move(newPath);
|
pathChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user