From 71e02751ae753f2c8cc642844c2c10be1c3561d8 Mon Sep 17 00:00:00 2001 From: Jos Date: Sat, 1 Aug 2015 22:10:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9dispatch=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BB=A5=E4=BF=AE=E5=A4=8D=E8=B7=AF=E7=94=B1bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/cinatra/http_router.hpp | 36 ++++++++++++++++++++++++++------ include/cinatra/token_parser.hpp | 4 ++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/include/cinatra/http_router.hpp b/include/cinatra/http_router.hpp index e56b61cc..ab7a1eb7 100644 --- a/include/cinatra/http_router.hpp +++ b/include/cinatra/http_router.hpp @@ -5,7 +5,7 @@ #include #include #include -//#include "tuple_utils.hpp" +//#include #include #include #include @@ -50,7 +50,7 @@ namespace cinatra pos = name.find_first_of(':', nextpos); } - parser_.add(name, v); + parser_.add(funcName, v); return funcName; } @@ -89,19 +89,28 @@ namespace cinatra //处理hello/a/12 //先分离path,如果有参数key就按照key从query里取出相应的参数值. //如果没有则直接查找,需要逐步匹配,先匹配最长的,接着匹配次长的,直到查找完所有可能的path. + bool needbreak = false; size_t pos = func_name.rfind('/'); - while (pos != std::string::npos && pos != 0) + while (pos != std::string::npos &&!needbreak) { std::string name = func_name; if (pos != 0) name = func_name.substr(0, pos); + else + { + name = ""; + needbreak = true; + } std::string params = func_name.substr(pos); parser.parse(params); - bool r = handle(req, resp, name, parser, finish); - if (finish) - return r; + if (check(parser, name)) + { + bool r = handle(req, resp, name, parser, finish); + if (finish) + return r; + } pos = func_name.rfind('/', pos - 1); } @@ -110,6 +119,21 @@ namespace cinatra return false; } + bool check(token_parser& parser, const std::string& name) + { + auto kv = parser_.get_map(); + auto rg = kv.equal_range(name); + for (auto itr = rg.first; itr != rg.second; ++itr) + { + if (itr->second.size() == parser.size()) + { + return true; + } + } + + return false; + } + bool handle(Request& req, Response& resp, std::string& name, token_parser& parser, bool& finish) { bool r = false; diff --git a/include/cinatra/token_parser.hpp b/include/cinatra/token_parser.hpp index 59727abb..a26f6025 100644 --- a/include/cinatra/token_parser.hpp +++ b/include/cinatra/token_parser.hpp @@ -9,7 +9,7 @@ class token_parser { std::vector v_; //解析之后,v_的第一个元素为函数名,后面的元素均为参数. - std::map> map_; + std::multimap> map_; public: /* get("/hello/:name", (request, response) -> { @@ -29,7 +29,7 @@ class token_parser map_.emplace(path, std::move(v)); } - const std::map>& get_map() + const std::multimap>& get_map() { return map_; }