Compare commits

..

4 Commits

Author SHA1 Message Date
an-tao
e1163234cc Update 2023-09-14 15:49:44 +08:00
an-tao
01af32e447 Fix 2023-09-14 15:29:11 +08:00
an-tao
8049959290 Update comments 2023-09-14 15:23:01 +08:00
an-tao
b00e59756a opt 2023-09-14 15:17:54 +08:00
7 changed files with 40 additions and 34 deletions

View File

@ -416,6 +416,7 @@ 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

@ -24,13 +24,26 @@ namespace plugin
{
using RedirectorHandler =
std::function<bool(const drogon::HttpRequestPtr &,
std::string &, //"http://" or "https://"
std::string &, // host
std::string &)>; // path
std::string &, //"http://" or "https://"
std::string &, // host
bool &)>; // path changed or not
/**
* @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 &,
std::string &) const;
bool &) const;
bool redirectToSSL(const HttpRequestPtr &,
std::string &,
std::string &,
std::string &) const;
bool &) const;
std::regex exemptPegex_;
bool regexFlag_{false};

View File

@ -159,6 +159,11 @@ 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,15 +28,16 @@ void Redirector::initAndStart(const Json::Value &config)
{
return HttpResponsePtr{};
}
std::string protocol, host, path;
std::string protocol, host;
bool pathChanged{false};
for (auto &handler : thisPtr->handlers_)
{
if (!handler(req, protocol, host, path))
if (!handler(req, protocol, host, pathChanged))
{
return HttpResponse::newNotFoundResponse();
}
}
if (!protocol.empty() || !host.empty() || !path.empty())
if (!protocol.empty() || !host.empty() || pathChanged)
{
std::string url;
if (protocol.empty())
@ -47,18 +48,7 @@ void Redirector::initAndStart(const Json::Value &config)
: "http://";
url.append(host);
}
if (!path.empty())
{
if (url.empty())
{
url = std::move(path);
}
else
{
url.append(path);
}
}
else
if (pathChanged)
{
url.append(req->path());
}
@ -74,11 +64,7 @@ void Redirector::initAndStart(const Json::Value &config)
{
url.append(req->getHeader("host"));
}
if (!path.empty())
{
url.append(path);
}
else
if (pathChanged)
{
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,
std::string &path) -> bool {
bool &pathChanged) -> bool {
auto thisPtr = weakPtr.lock();
if (!thisPtr)
{
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,
std::string &protocol,
std::string &host,
std::string &path) const
bool &pathChanged) const
{
if (req->isOnSecureConnection() || protocol == "https://")
{
@ -85,20 +85,21 @@ bool SecureSSLRedirector::redirectingAdvice(const HttpRequestPtr &req,
}
else
{
return redirectToSSL(req, protocol, host, path);
return redirectToSSL(req, protocol, host, pathChanged);
}
}
else
{
return redirectToSSL(req, protocol, host, path);
return redirectToSSL(req, protocol, host, pathChanged);
}
}
bool SecureSSLRedirector::redirectToSSL(const HttpRequestPtr &req,
std::string &protocol,
std::string &host,
std::string &path) const
bool &pathChanged) 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,
std::string& path) -> bool {
bool& pathChanged) -> 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(newPath);
req->setPath(std::move(newPath));
if (redirect)
{
path = std::move(newPath);
pathChanged = true;
}
}
return true;