2
2
#include < drogon/plugins/Redirector.h>
3
3
#include < drogon/HttpAppFramework.h>
4
4
#include " drogon/utils/FunctionTraits.h"
5
+ #include < cassert>
5
6
#include < cstddef>
6
7
#include < functional>
7
8
#include < memory>
@@ -21,10 +22,14 @@ bool HostRedirector::redirectingAdvice(const HttpRequestPtr& req,
21
22
{
22
23
const string& reqHost = host.empty () ? req->getHeader (" host" ) : host;
23
24
const string& reqPath = req->path ();
24
- string newHost = reqHost , path = reqPath;
25
+ string newHost, path = reqPath;
25
26
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
+ }
28
33
29
34
// Lookup within non-host rules
30
35
{
@@ -34,7 +39,7 @@ bool HostRedirector::redirectingAdvice(const HttpRequestPtr& req,
34
39
newHost = std::move (temp);
35
40
}
36
41
37
- if (newHost != reqHost)
42
+ if (!newHost. empty () && newHost != reqHost)
38
43
host = std::move (newHost);
39
44
40
45
if (path != reqPath)
@@ -117,7 +122,8 @@ void HostRedirector::lookup(string& host, string& path) const
117
122
if (to)
118
123
{
119
124
const string& toHost = to->host ;
120
- if (!toHost.empty ())
125
+ bool hasToHost = !toHost.empty ();
126
+ if (hasToHost)
121
127
host = toHost;
122
128
123
129
if (isWildcard)
@@ -135,6 +141,11 @@ void HostRedirector::lookup(string& host, string& path) const
135
141
}
136
142
else
137
143
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 ;
138
149
}
139
150
else
140
151
break ;
@@ -143,12 +154,14 @@ void HostRedirector::lookup(string& host, string& path) const
143
154
144
155
void HostRedirector::initAndStart (const Json::Value& config)
145
156
{
157
+ doHostLookup_ = false ;
146
158
if (config.isMember (" rules" ))
147
159
{
148
160
const auto & rules = config[" rules" ];
149
161
if (rules.isObject ())
150
162
{
151
163
const auto & redirectToList = rules.getMemberNames ();
164
+ rulesTo_.reserve (redirectToList.size ());
152
165
for (const string redirectToStr : redirectToList)
153
166
{
154
167
string redirectToHost, redirectToPath;
@@ -175,8 +188,7 @@ void HostRedirector::initAndStart(const Json::Value& config)
175
188
{
176
189
for (const auto & redirectFrom : redirectFromValue)
177
190
{
178
- if (!redirectFrom.isString ())
179
- continue ;
191
+ assert (redirectFrom.isString ());
180
192
181
193
string redirectFromStr = redirectFrom.asString ();
182
194
auto len = redirectFromStr.size ();
@@ -199,10 +211,15 @@ void HostRedirector::initAndStart(const Json::Value& config)
199
211
else
200
212
redirectFromPath = ' /' ;
201
213
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
202
221
rulesFromData_.push_back ({
203
- std::move (redirectFromHost.empty () && pathIdx != 0
204
- ? redirectFromStr
205
- : redirectFromHost),
222
+ std::move (fromHost),
206
223
std::move (redirectFromPath),
207
224
isWildcard,
208
225
toIdx,
0 commit comments