Repository navigation
Expand file tree
/
Copy pathvirtual-host.cpp
More file actions
545 lines (482 loc) · 18.4 KB
/
Copy pathvirtual-host.cpp
File metadata and controls
545 lines (482 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/server/virtual-host.h"
#include <stdexcept>
#include "hphp/runtime/base/comparisons.h"
#include "hphp/runtime/base/config.h"
#include "hphp/runtime/base/execution-context.h"
#include "hphp/runtime/base/preg.h"
#include "hphp/runtime/base/runtime-option.h"
#include "hphp/runtime/base/string-util.h"
#include "hphp/runtime/base/type-string.h"
#include "hphp/runtime/base/variable-serializer.h"
#include "hphp/util/logger.h"
#include "hphp/util/text-util.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
bool VirtualHost::IsDefault(const IniSetting::Map &ini, const Hdf &vh,
const std::string& ini_key /* = "" */) {
if (vh.exists() && !vh.isEmpty()) {
return (vh.getName() == "default");
} else {
return (ini_key == "default");
}
}
VirtualHost &VirtualHost::GetDefault() {
// VirtualHost acquires global mutexes in its constructor, so we allocate
// s_default_vhost lazily to ensure that all of the global mutexes have
// been initialized before we enter the constructor.
static VirtualHost s_default_vhost;
return s_default_vhost;
}
void VirtualHost::SetCurrent(VirtualHost *vhost) {
g_context->setVirtualHost(vhost ? vhost : &VirtualHost::GetDefault());
UpdateSerializationSizeLimit();
}
const VirtualHost *VirtualHost::GetCurrent() {
const VirtualHost *ret = g_context.isNull() ?
nullptr : g_context->getVirtualHost();
if (!ret) ret = &VirtualHost::GetDefault();
return ret;
}
VirtualHost* VirtualHost::Resolve(const std::string& host) {
auto const hostString = String(host.c_str(), host.size(), CopyString);
for (auto vhost : RuntimeOption::VirtualHosts) {
if (vhost->match(hostString)) {
return vhost.get();
}
}
return nullptr;
}
int64_t VirtualHost::getMaxPostSize() const {
if (m_runtimeOption.maxPostSize != -1) {
return m_runtimeOption.maxPostSize;
}
return RuntimeOption::MaxPostSize;
}
int64_t VirtualHost::GetLowestMaxPostSize() {
auto lowest = RuntimeOption::MaxPostSize;
for (auto vhost : RuntimeOption::VirtualHosts) {
auto max = vhost->getMaxPostSize();
lowest = std::min(lowest, max);
}
return lowest;
}
int64_t VirtualHost::GetMaxPostSize() {
const VirtualHost *vh = GetCurrent();
assert(vh);
return vh->getMaxPostSize();
}
int64_t VirtualHost::GetUploadMaxFileSize() {
const VirtualHost *vh = GetCurrent();
assert(vh);
if (vh->m_runtimeOption.uploadMaxFileSize != -1) {
return vh->m_runtimeOption.uploadMaxFileSize;
}
return RuntimeOption::UploadMaxFileSize;
}
void VirtualHost::UpdateSerializationSizeLimit() {
const VirtualHost *vh = GetCurrent();
assert(vh);
if (vh->m_runtimeOption.serializationSizeLimit != StringData::MaxSize) {
VariableSerializer::serializationSizeLimit =
vh->m_runtimeOption.serializationSizeLimit;
}
}
bool VirtualHost::alwaysDecodePostData(const String& origPath) const {
if (!m_alwaysDecodePostData) return false;
if (m_decodePostDataBlackList.empty()) return true;
return !m_decodePostDataBlackList.count(origPath.toCppString());
}
const std::vector<std::string> &VirtualHost::GetAllowedDirectories() {
const VirtualHost *vh = GetCurrent();
assert(vh);
if (!vh->m_runtimeOption.allowedDirectories.empty()) {
return vh->m_runtimeOption.allowedDirectories;
}
return RuntimeOption::AllowedDirectories;
}
void VirtualHost::SortAllowedDirectories(std::vector<std::string>& dirs) {
/*
Make sure corresponding realpath's are also allowed
*/
for (unsigned int i = 0, s = dirs.size(); i < s; i++) {
std::string &directory = dirs[i];
char resolved_path[PATH_MAX];
if (realpath(directory.c_str(), resolved_path) &&
directory != resolved_path) {
dirs.push_back(resolved_path);
}
}
/*
sort so we can use upper_bound to find the right prefix,
rather than using a linear scan (and so we can remove
duplicates, etc below)
*/
std::sort(dirs.begin(), dirs.end());
/*
AllowedDirectories is a list of prefixes, so if x is a substring
of y, we dont need y (also remove any duplicates).
*/
dirs.erase(std::unique(dirs.begin(), dirs.end(),
[](const std::string &a, const std::string &b) {
if (a.size() < b.size()) {
return !b.compare(0, a.size(), a);
}
return !a.compare(0, b.size(), b);
}),
dirs.end());
}
///////////////////////////////////////////////////////////////////////////////
void VirtualHost::initRuntimeOption(const IniSetting::Map& ini, const Hdf& vh) {
int requestTimeoutSeconds =
Config::GetInt32(ini, vh, "overwrite.Server.RequestTimeoutSeconds", -1,
false);
int64_t maxPostSize =
Config::GetInt32(ini, vh, "overwrite.Server.MaxPostSize", -1, false);
if (maxPostSize != -1) maxPostSize *= (1LL << 20);
int64_t uploadMaxFileSize =
Config::GetInt32(ini, vh, "overwrite.Server.Upload.UploadMaxFileSize", -1,
false);
if (uploadMaxFileSize != -1) uploadMaxFileSize *= (1LL << 20);
int64_t serializationSizeLimit =
Config::GetInt32(
ini,
vh, "overwrite.ResourceLimit.SerializationSizeLimit",
StringData::MaxSize, false);
m_runtimeOption.allowedDirectories = Config::GetVector(
ini,
vh, "overwrite.Server.AllowedDirectories",
m_runtimeOption.allowedDirectories, false);
m_runtimeOption.requestTimeoutSeconds = requestTimeoutSeconds;
m_runtimeOption.maxPostSize = maxPostSize;
m_runtimeOption.uploadMaxFileSize = uploadMaxFileSize;
m_runtimeOption.serializationSizeLimit = serializationSizeLimit;
m_documentRoot = RuntimeOption::SourceRoot + m_pathTranslation;
if (m_documentRoot.length() > 1 &&
m_documentRoot.back() == '/') {
m_documentRoot.pop_back();
// Make sure we've not converted "/" to "" (which is why we're checking
// length() > 1 instead of !empty() above)
assert(!m_documentRoot.empty());
}
}
void VirtualHost::addAllowedDirectories(const std::vector<std::string>& dirs) {
if (!m_runtimeOption.allowedDirectories.empty()) {
m_runtimeOption.allowedDirectories.insert(
m_runtimeOption.allowedDirectories.end(),
dirs.begin(), dirs.end());
SortAllowedDirectories(m_runtimeOption.allowedDirectories);
}
}
int VirtualHost::getRequestTimeoutSeconds(int defaultTimeout) const {
return m_runtimeOption.requestTimeoutSeconds < 0 ?
defaultTimeout : m_runtimeOption.requestTimeoutSeconds;
}
VirtualHost::VirtualHost() : m_disabled(false) {
IniSetting::Map ini = IniSetting::Map::object;
Hdf empty;
// Pass empty config maps; the default values are being used
// for these runtime option values / overwrites.
initRuntimeOption(ini, empty);
}
VirtualHost::VirtualHost(const IniSetting::Map& ini, const Hdf& vh,
const std::string &ini_key /* = "" */
) : m_disabled(false) {
init(ini, vh, ini_key);
}
void VirtualHost::init(const IniSetting::Map& ini, const Hdf& vh,
const std::string& ini_key /* = "" */) {
m_name = vh.exists() && !vh.isEmpty() ? vh.getName() : ini_key;
m_prefix = Config::GetString(ini, vh, "Prefix", "", false);
auto pattern = format_pattern(Config::GetString(ini, vh, "Pattern", "",
false), false);
m_pathTranslation = Config::GetString(ini, vh, "PathTranslation", "",
false);
if (!pattern.empty()) {
pattern += "i"; // case-insensitive
m_pattern = makeStaticString(pattern);
}
if (!m_pathTranslation.empty() &&
m_pathTranslation[m_pathTranslation.length() - 1] != '/') {
m_pathTranslation += '/';
}
initRuntimeOption(ini, vh); // overwrites
m_disabled = Config::GetBool(ini, vh, "Disabled", false, false);
m_checkExistenceBeforeRewrite =
Config::GetBool(ini, vh, "CheckExistenceBeforeRewrite", true, false);
m_alwaysDecodePostData =
Config::GetBool(ini, vh, "AlwaysDecodePostData", true, false);
m_decodePostDataBlackList =
Config::GetSetC(ini, vh, "DecodePostDataBlackList");
auto rr_callback = [&] (const IniSetting::Map &ini_rr,
const Hdf &hdf_rr,
const std::string &ini_rr_key) {
RewriteRule dummy;
m_rewriteRules.push_back(dummy);
RewriteRule &rule = m_rewriteRules.back();
rule.pattern = makeStaticString(format_pattern(
Config::GetString(ini_rr, hdf_rr, "pattern", "", false),
true));
rule.to = Config::GetString(ini_rr, hdf_rr, "to", "", false);
rule.qsa = Config::GetBool(ini_rr, hdf_rr, "qsa", false, false);
rule.redirect = Config::GetInt16(ini_rr, hdf_rr, "redirect", 0, false);
rule.encode_backrefs = Config::GetBool(ini_rr, hdf_rr, "encode_backrefs",
false, false);
if (rule.pattern->empty() || rule.to.empty()) {
throw std::runtime_error("Invalid rewrite rule: (empty pattern or to)");
}
auto rc_callback = [&] (const IniSetting::Map &ini_rc,
const Hdf &hdf_rc,
const std::string &ini_rc_key) {
RewriteCond dummy;
rule.rewriteConds.push_back(dummy);
RewriteCond &cond = rule.rewriteConds.back();
cond.pattern = makeStaticString(format_pattern(
Config::GetString(ini_rc, hdf_rc, "pattern", "", false), true));
if (cond.pattern->empty()) {
throw std::runtime_error("Invalid rewrite rule: (empty cond pattern)");
}
std::string type = Config::GetString(ini_rc, hdf_rc, "type", "", false);
if (!type.empty()) {
if (strcasecmp(type.c_str(), "host") == 0) {
cond.type = RewriteCond::Type::Host;
} else if (strcasecmp(type.c_str(), "request") == 0) {
cond.type = RewriteCond::Type::Request;
} else {
throw std::runtime_error("Invalid rewrite rule: (invalid "
"cond type)");
}
} else {
cond.type = RewriteCond::Type::Request;
}
cond.negate = Config::GetBool(ini_rc, hdf_rc, "negate", false, false);
};
Config::Iterate(rc_callback, ini_rr, hdf_rr, "conditions", false);
};
Config::Iterate(rr_callback, ini, vh, "RewriteRules", false);
m_ipBlocks = std::make_shared<IpBlockMap>(ini, vh);
auto lf_callback = [&] (const IniSetting::Map &ini_lf,
const Hdf &hdf_lf,
const std::string &ini_lf_key) {
QueryStringFilter filter;
filter.urlPattern = format_pattern(Config::GetString(ini_lf, hdf_lf, "url",
"", false),
true);
filter.replaceWith = Config::GetString(ini_lf, hdf_lf, "value", "", false);
filter.replaceWith = "\\1=" + filter.replaceWith;
std::string namePattern = Config::GetString(ini_lf, hdf_lf, "pattern", "",
false);
std::vector<std::string> names;
names = Config::GetVector(ini_lf, hdf_lf, "params", names, false);
if (namePattern.empty()) {
for (unsigned int i = 0; i < names.size(); i++) {
if (namePattern.empty()) {
namePattern = "(?<=[&\?])(";
} else {
namePattern += "|";
}
namePattern += names[i];
}
if (!namePattern.empty()) {
namePattern += ")=.*?(?=(&|$))";
namePattern = format_pattern(namePattern, false);
}
} else if (!names.empty()) {
throw std::runtime_error("Invalid log filter: (cannot specify "
"both params and pattern)");
}
filter.namePattern = namePattern;
m_queryStringFilters.push_back(filter);
};
Config::Iterate(lf_callback, ini, vh, "LogFilters", false);
m_serverVars = Config::GetMap(ini, vh, "ServerVariables", m_serverVars,
false);
m_serverName = Config::GetString(ini, vh, "ServerName", m_serverName, false);
// We don't require a prefix or pattern for the default
if (m_name != "default" && !valid()) {
throw std::runtime_error("virtual host missing prefix or pattern");
}
}
bool VirtualHost::match(const String &host) const {
if (m_pattern) {
Variant ret = preg_match(m_pattern, host.get());
return ret.toInt64() > 0;
} else if (!m_prefix.empty()) {
return strncasecmp(host.c_str(), m_prefix.c_str(), m_prefix.size()) == 0;
}
return true;
}
static int get_backref(const char **s) {
assert('0' <= **s && **s <= '9');
int val = **s - '0';
*s += 1;
if ('0' <= **s && **s <= '9') {
val = val * 10 + **s - '0';
*s += 1;
}
return val;
}
bool VirtualHost::rewriteURL(const String& host, String &url, bool &qsa,
int &redirect) const {
String normalized = url;
if (normalized.empty() || normalized.charAt(0) != '/') {
normalized = String("/") + normalized;
}
Logger::Verbose("Matching host:%s url:%s using vhost:%s",
host.c_str(), url.c_str(), m_name.c_str());
for (unsigned int i = 0; i < m_rewriteRules.size(); i++) {
const RewriteRule &rule = m_rewriteRules[i];
bool passed = true;
for (auto it = rule.rewriteConds.begin();
it != rule.rewriteConds.end(); ++it) {
String subject;
if (it->type == RewriteCond::Type::Request) {
subject = normalized;
} else {
subject = host;
}
Variant ret = preg_match(it->pattern, subject.get());
if (!same(ret, it->negate ? 0 : 1)) {
passed = false;
break;
}
}
if (!passed) continue;
Variant matches;
int count = preg_match(rule.pattern,
normalized.get(),
&matches).toInt64();
if (count > 0) {
Logger::Verbose("Matched pattern %s", rule.pattern->data());
const char *s = rule.to.c_str();
StringBuffer ret;
while (*s) {
int backref = -1;
if (*s == '\\') {
if ('0' <= s[1] && s[1] <= '9') {
s++;
backref = get_backref(&s);
} else if (s[1] == '\\') {
s++;
}
} else if (*s == '$') {
if (s[1] == '{') {
const char *t = s+2;
if ('0' <= *t && *t <= '9') {
backref = get_backref(&t);
if (*t != '}') {
backref = -1;
} else {
s = t+1;
}
}
} else if ('0' <= s[1] && s[1] <= '9') {
s++;
backref = get_backref(&s);
}
}
if (backref >= 0) {
String br = matches.toArray()[backref].toString();
if (rule.encode_backrefs) {
br = StringUtil::UrlEncode(br);
}
ret.append(br);
} else {
ret.append(s, 1);
s++;
}
}
url = ret.detach();
qsa = rule.qsa;
redirect = rule.redirect;
return true;
} else {
Logger::Verbose("Did not match pattern %s", rule.pattern->data());
}
}
return false;
}
bool VirtualHost::isBlocking(const std::string &command,
const std::string &ip) const {
if (!m_ipBlocks) {
return RuntimeOption::IpBlocks->isBlocking(command, ip);
}
return m_ipBlocks->isBlocking(command, ip);
}
std::string VirtualHost::serverName(const std::string &host) const {
if (!m_serverName.empty()) {
return m_serverName;
}
if (!RuntimeOption::DefaultServerNameSuffix.empty()) {
if (m_pattern) {
Variant matches;
Variant ret = preg_match(m_pattern,
String(host.c_str(), host.size(),
CopyString).get(),
&matches);
if (ret.toInt64() > 0) {
String prefix = matches.toArray()[1].toString();
if (prefix.empty()) {
prefix = matches.toArray()[0].toString();
}
if (!prefix.empty()) {
return std::string(prefix.data()) +
RuntimeOption::DefaultServerNameSuffix;
}
}
} else if (!m_prefix.empty()) {
return m_prefix + RuntimeOption::DefaultServerNameSuffix;
}
}
return RuntimeOption::Host;
}
///////////////////////////////////////////////////////////////////////////////
// query string filter
std::string VirtualHost::filterUrl(const std::string &url) const {
assert(!m_queryStringFilters.empty());
for (unsigned int i = 0; i < m_queryStringFilters.size(); i++) {
const QueryStringFilter &filter = m_queryStringFilters[i];
bool match = true;
if (!filter.urlPattern.empty()) {
Variant ret = preg_match(String(filter.urlPattern.c_str(),
filter.urlPattern.size(),
CopyString),
String(url.c_str(), url.size(),
CopyString));
match = (ret.toInt64() > 0);
}
if (match) {
if (filter.namePattern.empty()) {
return "";
}
Variant ret;
int count = preg_replace(ret, filter.namePattern.c_str(),
String(filter.replaceWith.c_str(),
filter.replaceWith.size(),
CopyString),
String(url.c_str(), url.size(),
CopyString));
if (!same(ret, false) && count > 0) {
return ret.toString().data();
}
return url;
}
}
return url;
}
///////////////////////////////////////////////////////////////////////////////
}