Skip to content

Commit

Permalink
Optimization and safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Mis1eader-dev committed Oct 10, 2023
1 parent 9e2e6f6 commit 89f73f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/inc/drogon/plugins/HostRedirector.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class DROGON_EXPORT HostRedirector

void recursiveDelete(const RedirectGroup *group);

bool doHostLookup_{false};
std::unordered_map<std::string_view, RedirectGroup> rulesFrom_;
std::vector<RedirectLocation> rulesTo_;
std::vector<RedirectFrom> rulesFromData_;
Expand Down
37 changes: 27 additions & 10 deletions lib/src/HostRedirector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <drogon/plugins/Redirector.h>
#include <drogon/HttpAppFramework.h>
#include "drogon/utils/FunctionTraits.h"
#include <cassert>
#include <cstddef>
#include <functional>
#include <memory>
Expand All @@ -21,10 +22,14 @@ bool HostRedirector::redirectingAdvice(const HttpRequestPtr& req,
{
const string& reqHost = host.empty() ? req->getHeader("host") : host;
const string& reqPath = req->path();
string newHost = reqHost, path = reqPath;
string newHost, path = reqPath;

// Lookup host-specific rules first
lookup(newHost, path);
// Lookup host-specific rules first if they exist
if (doHostLookup_ && !reqHost.empty())
{
newHost = reqHost;
lookup(newHost, path);
}

// Lookup within non-host rules
{
Expand All @@ -34,7 +39,7 @@ bool HostRedirector::redirectingAdvice(const HttpRequestPtr& req,
newHost = std::move(temp);
}

if (newHost != reqHost)
if (!newHost.empty() && newHost != reqHost)
host = std::move(newHost);

if (path != reqPath)
Expand Down Expand Up @@ -117,7 +122,8 @@ void HostRedirector::lookup(string& host, string& path) const
if (to)
{
const string& toHost = to->host;
if (!toHost.empty())
bool hasToHost = !toHost.empty();
if (hasToHost)
host = toHost;

if (isWildcard)
Expand All @@ -135,6 +141,11 @@ void HostRedirector::lookup(string& host, string& path) const
}
else
path = to->path;

if (!doHostLookup_ &&
hasToHost) // If our maps don't contain hosts, no need to look
// it up next iteration
break;
}
else
break;
Expand All @@ -143,12 +154,14 @@ void HostRedirector::lookup(string& host, string& path) const

void HostRedirector::initAndStart(const Json::Value& config)
{
doHostLookup_ = false;
if (config.isMember("rules"))
{
const auto& rules = config["rules"];
if (rules.isObject())
{
const auto& redirectToList = rules.getMemberNames();
rulesTo_.reserve(redirectToList.size());
for (const string redirectToStr : redirectToList)
{
string redirectToHost, redirectToPath;
Expand All @@ -175,8 +188,7 @@ void HostRedirector::initAndStart(const Json::Value& config)
{
for (const auto& redirectFrom : redirectFromValue)
{
if (!redirectFrom.isString())
continue;
assert(redirectFrom.isString());

string redirectFromStr = redirectFrom.asString();
auto len = redirectFromStr.size();
Expand All @@ -199,10 +211,15 @@ void HostRedirector::initAndStart(const Json::Value& config)
else
redirectFromPath = '/';

const string& fromHost =
redirectFromHost.empty() && pathIdx != 0
? redirectFromStr
: redirectFromHost;
if (!fromHost.empty())
doHostLookup_ =
true; // We have hosts in lookup rules
rulesFromData_.push_back({
std::move(redirectFromHost.empty() && pathIdx != 0
? redirectFromStr
: redirectFromHost),
std::move(fromHost),
std::move(redirectFromPath),
isWildcard,
toIdx,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/SecureSSLRedirector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ void SecureSSLRedirector::initAndStart(const Json::Value &config)
{
if (config.isMember("ssl_redirect_exempt"))
{
if (config["ssl_redirect_exempt"].isArray())
const auto &exempts = config["ssl_redirect_exempt"];
if (exempts.isArray())
{
const auto &exempts = config["ssl_redirect_exempt"];
size_t exemptsCount = exempts.size();
if (exemptsCount)
{
Expand Down

0 comments on commit 89f73f0

Please sign in to comment.