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,22 +103,28 @@ request_cb serve_mux::handler(request_impl &req) const {
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 {
const handler_entry *ent = nullptr;
size_t best = 0;
for (auto &kv : mux_) {
auto &pattern = kv.first;
if (!util::startsWith(path, pattern)) {
if (!path_match(pattern, path)) {
continue;
}
if (path.size() == pattern.size() || pattern.back() == '/' ||
path[pattern.size()] == '/') {
if (!ent || best < pattern.size()) {
best = pattern.size();
ent = &kv.second;
}
}
}
if (ent) {
return ent->cb;
}