nghttpx: Use StringRef for parameter in Router::match
This commit is contained in:
parent
2d273f8237
commit
93eabc642b
|
@ -2503,13 +2503,11 @@ int int_syslog_facility(const char *strfacility) {
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
size_t
|
size_t match_downstream_addr_group_host(
|
||||||
match_downstream_addr_group_host(const Router &router, const std::string &host,
|
const Router &router, const std::string &host, const StringRef &path,
|
||||||
const char *path, size_t pathlen,
|
const std::vector<DownstreamAddrGroup> &groups, size_t catch_all) {
|
||||||
const std::vector<DownstreamAddrGroup> &groups,
|
if (path.empty() || path[0] != '/') {
|
||||||
size_t catch_all) {
|
auto group = router.match(host, StringRef::from_lit("/"));
|
||||||
if (pathlen == 0 || *path != '/') {
|
|
||||||
auto group = router.match(host, "/", 1);
|
|
||||||
if (group != -1) {
|
if (group != -1) {
|
||||||
if (LOG_ENABLED(INFO)) {
|
if (LOG_ENABLED(INFO)) {
|
||||||
LOG(INFO) << "Found pattern with query " << host
|
LOG(INFO) << "Found pattern with query " << host
|
||||||
|
@ -2522,23 +2520,22 @@ match_downstream_addr_group_host(const Router &router, const std::string &host,
|
||||||
|
|
||||||
if (LOG_ENABLED(INFO)) {
|
if (LOG_ENABLED(INFO)) {
|
||||||
LOG(INFO) << "Perform mapping selection, using host=" << host
|
LOG(INFO) << "Perform mapping selection, using host=" << host
|
||||||
<< ", path=" << std::string(path, pathlen);
|
<< ", path=" << path;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto group = router.match(host, path, pathlen);
|
auto group = router.match(host, path);
|
||||||
if (group != -1) {
|
if (group != -1) {
|
||||||
if (LOG_ENABLED(INFO)) {
|
if (LOG_ENABLED(INFO)) {
|
||||||
LOG(INFO) << "Found pattern with query " << host
|
LOG(INFO) << "Found pattern with query " << host << path
|
||||||
<< std::string(path, pathlen)
|
|
||||||
<< ", matched pattern=" << groups[group].pattern;
|
<< ", matched pattern=" << groups[group].pattern;
|
||||||
}
|
}
|
||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
group = router.match("", path, pathlen);
|
group = router.match("", path);
|
||||||
if (group != -1) {
|
if (group != -1) {
|
||||||
if (LOG_ENABLED(INFO)) {
|
if (LOG_ENABLED(INFO)) {
|
||||||
LOG(INFO) << "Found pattern with query " << std::string(path, pathlen)
|
LOG(INFO) << "Found pattern with query " << path
|
||||||
<< ", matched pattern=" << groups[group].pattern;
|
<< ", matched pattern=" << groups[group].pattern;
|
||||||
}
|
}
|
||||||
return group;
|
return group;
|
||||||
|
@ -2565,12 +2562,11 @@ match_downstream_addr_group(const Router &router, const std::string &hostport,
|
||||||
|
|
||||||
auto fragment = std::find(std::begin(raw_path), std::end(raw_path), '#');
|
auto fragment = std::find(std::begin(raw_path), std::end(raw_path), '#');
|
||||||
auto query = std::find(std::begin(raw_path), fragment, '?');
|
auto query = std::find(std::begin(raw_path), fragment, '?');
|
||||||
auto path = raw_path.c_str();
|
auto path = StringRef{std::begin(raw_path), query};
|
||||||
auto pathlen = query - std::begin(raw_path);
|
|
||||||
|
|
||||||
if (hostport.empty()) {
|
if (hostport.empty()) {
|
||||||
return match_downstream_addr_group_host(router, hostport, path, pathlen,
|
return match_downstream_addr_group_host(router, hostport, path, groups,
|
||||||
groups, catch_all);
|
catch_all);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string host;
|
std::string host;
|
||||||
|
@ -2593,7 +2589,7 @@ match_downstream_addr_group(const Router &router, const std::string &hostport,
|
||||||
}
|
}
|
||||||
|
|
||||||
util::inp_strlower(host);
|
util::inp_strlower(host);
|
||||||
return match_downstream_addr_group_host(router, host, path, pathlen, groups,
|
return match_downstream_addr_group_host(router, host, path, groups,
|
||||||
catch_all);
|
catch_all);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -259,8 +259,7 @@ const RNode *match_partial(const RNode *node, size_t offset, const char *first,
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
ssize_t Router::match(const std::string &host, const char *path,
|
ssize_t Router::match(const std::string &host, const StringRef &path) const {
|
||||||
size_t pathlen) const {
|
|
||||||
const RNode *node;
|
const RNode *node;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
|
|
||||||
|
@ -270,7 +269,7 @@ ssize_t Router::match(const std::string &host, const char *path,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
node = match_partial(node, offset, path, path + pathlen);
|
node = match_partial(node, offset, std::begin(path), std::end(path));
|
||||||
if (node == nullptr || node == &root_) {
|
if (node == nullptr || node == &root_) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,8 +58,7 @@ public:
|
||||||
// Adds route |pattern| with its |index|.
|
// Adds route |pattern| with its |index|.
|
||||||
bool add_route(const StringRef &pattern, size_t index);
|
bool add_route(const StringRef &pattern, size_t index);
|
||||||
// Returns the matched index of pattern. -1 if there is no match.
|
// Returns the matched index of pattern. -1 if there is no match.
|
||||||
ssize_t match(const std::string &host, const char *path,
|
ssize_t match(const std::string &host, const StringRef &path) const;
|
||||||
size_t pathlen) const;
|
|
||||||
|
|
||||||
void add_node(RNode *node, const char *pattern, size_t patlen, size_t index);
|
void add_node(RNode *node, const char *pattern, size_t patlen, size_t index);
|
||||||
|
|
||||||
|
|
|
@ -397,6 +397,9 @@ public:
|
||||||
: base(reinterpret_cast<const char *>(s)), len(n) {}
|
: base(reinterpret_cast<const char *>(s)), len(n) {}
|
||||||
template <typename InputIt>
|
template <typename InputIt>
|
||||||
StringRef(InputIt first, InputIt last)
|
StringRef(InputIt first, InputIt last)
|
||||||
|
: base(&*first), len(std::distance(first, last)) {}
|
||||||
|
template <typename InputIt>
|
||||||
|
StringRef(InputIt *first, InputIt *last)
|
||||||
: base(first), len(std::distance(first, last)) {}
|
: base(first), len(std::distance(first, last)) {}
|
||||||
template <typename CharT, size_t N>
|
template <typename CharT, size_t N>
|
||||||
constexpr static StringRef from_lit(const CharT(&s)[N]) {
|
constexpr static StringRef from_lit(const CharT(&s)[N]) {
|
||||||
|
|
Loading…
Reference in New Issue