Skip to content

Commit 89f73f0

Browse files
committed
Optimization and safety
1 parent 9e2e6f6 commit 89f73f0

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

lib/inc/drogon/plugins/HostRedirector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class DROGON_EXPORT HostRedirector
102102

103103
void recursiveDelete(const RedirectGroup *group);
104104

105+
bool doHostLookup_{false};
105106
std::unordered_map<std::string_view, RedirectGroup> rulesFrom_;
106107
std::vector<RedirectLocation> rulesTo_;
107108
std::vector<RedirectFrom> rulesFromData_;

lib/src/HostRedirector.cc

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <drogon/plugins/Redirector.h>
33
#include <drogon/HttpAppFramework.h>
44
#include "drogon/utils/FunctionTraits.h"
5+
#include <cassert>
56
#include <cstddef>
67
#include <functional>
78
#include <memory>
@@ -21,10 +22,14 @@ bool HostRedirector::redirectingAdvice(const HttpRequestPtr& req,
2122
{
2223
const string& reqHost = host.empty() ? req->getHeader("host") : host;
2324
const string& reqPath = req->path();
24-
string newHost = reqHost, path = reqPath;
25+
string newHost, path = reqPath;
2526

26-
// Lookup host-specific rules first
27-
lookup(newHost, path);
27+
// Lookup host-specific rules first if they exist
28+
if (doHostLookup_ && !reqHost.empty())
29+
{
30+
newHost = reqHost;
31+
lookup(newHost, path);
32+
}
2833

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

37-
if (newHost != reqHost)
42+
if (!newHost.empty() && newHost != reqHost)
3843
host = std::move(newHost);
3944

4045
if (path != reqPath)
@@ -117,7 +122,8 @@ void HostRedirector::lookup(string& host, string& path) const
117122
if (to)
118123
{
119124
const string& toHost = to->host;
120-
if (!toHost.empty())
125+
bool hasToHost = !toHost.empty();
126+
if (hasToHost)
121127
host = toHost;
122128

123129
if (isWildcard)
@@ -135,6 +141,11 @@ void HostRedirector::lookup(string& host, string& path) const
135141
}
136142
else
137143
path = to->path;
144+
145+
if (!doHostLookup_ &&
146+
hasToHost) // If our maps don't contain hosts, no need to look
147+
// it up next iteration
148+
break;
138149
}
139150
else
140151
break;
@@ -143,12 +154,14 @@ void HostRedirector::lookup(string& host, string& path) const
143154

144155
void HostRedirector::initAndStart(const Json::Value& config)
145156
{
157+
doHostLookup_ = false;
146158
if (config.isMember("rules"))
147159
{
148160
const auto& rules = config["rules"];
149161
if (rules.isObject())
150162
{
151163
const auto& redirectToList = rules.getMemberNames();
164+
rulesTo_.reserve(redirectToList.size());
152165
for (const string redirectToStr : redirectToList)
153166
{
154167
string redirectToHost, redirectToPath;
@@ -175,8 +188,7 @@ void HostRedirector::initAndStart(const Json::Value& config)
175188
{
176189
for (const auto& redirectFrom : redirectFromValue)
177190
{
178-
if (!redirectFrom.isString())
179-
continue;
191+
assert(redirectFrom.isString());
180192

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

214+
const string& fromHost =
215+
redirectFromHost.empty() && pathIdx != 0
216+
? redirectFromStr
217+
: redirectFromHost;
218+
if (!fromHost.empty())
219+
doHostLookup_ =
220+
true; // We have hosts in lookup rules
202221
rulesFromData_.push_back({
203-
std::move(redirectFromHost.empty() && pathIdx != 0
204-
? redirectFromStr
205-
: redirectFromHost),
222+
std::move(fromHost),
206223
std::move(redirectFromPath),
207224
isWildcard,
208225
toIdx,

lib/src/SecureSSLRedirector.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ void SecureSSLRedirector::initAndStart(const Json::Value &config)
1616
{
1717
if (config.isMember("ssl_redirect_exempt"))
1818
{
19-
if (config["ssl_redirect_exempt"].isArray())
19+
const auto &exempts = config["ssl_redirect_exempt"];
20+
if (exempts.isArray())
2021
{
21-
const auto &exempts = config["ssl_redirect_exempt"];
2222
size_t exemptsCount = exempts.size();
2323
if (exemptsCount)
2424
{

0 commit comments

Comments
 (0)