asio: Fix path matching in server

This commit is contained in:
Tatsuhiro Tsujikawa 2015-03-06 23:32:28 +09:00
parent d7cfe464a2
commit 2cadd38b6b
1 changed files with 13 additions and 7 deletions

View File

@ -103,20 +103,26 @@ request_cb serve_mux::handler(request_impl &req) const {
return status_handler(404); return status_handler(404);
} }
namespace {
bool path_match(const std::string &pattern, const std::string &path) {
if (pattern.back() != '/') {
return pattern == path;
}
return util::startsWith(path, pattern);
}
} // namespace
request_cb serve_mux::match(const std::string &path) const { request_cb serve_mux::match(const std::string &path) const {
const handler_entry *ent = nullptr; const handler_entry *ent = nullptr;
size_t best = 0; size_t best = 0;
for (auto &kv : mux_) { for (auto &kv : mux_) {
auto &pattern = kv.first; auto &pattern = kv.first;
if (!util::startsWith(path, pattern)) { if (!path_match(pattern, path)) {
continue; continue;
} }
if (path.size() == pattern.size() || pattern.back() == '/' || if (!ent || best < pattern.size()) {
path[pattern.size()] == '/') { best = pattern.size();
if (!ent || best < pattern.size()) { ent = &kv.second;
best = pattern.size();
ent = &kv.second;
}
} }
} }
if (ent) { if (ent) {