Skip to content
This repository was archived by the owner on Nov 15, 2019. It is now read-only.

Commit 71e0275

Browse files
committed
修改dispatch的逻辑以修复路由bug
1 parent e54bb5d commit 71e0275

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

include/cinatra/http_router.hpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <functional>
66
#include <cinatra/function_traits.hpp>
77
#include <cinatra/string_utils.hpp>
8-
//#include "tuple_utils.hpp"
8+
//#include <cinatra/tuple_utils.hpp>
99
#include <cinatra/token_parser.hpp>
1010
#include <cinatra/request.hpp>
1111
#include <cinatra/response.hpp>
@@ -50,7 +50,7 @@ namespace cinatra
5050

5151
pos = name.find_first_of(':', nextpos);
5252
}
53-
parser_.add(name, v);
53+
parser_.add(funcName, v);
5454
return funcName;
5555
}
5656

@@ -89,19 +89,28 @@ namespace cinatra
8989
//处理hello/a/12
9090
//先分离path,如果有参数key就按照key从query里取出相应的参数值.
9191
//如果没有则直接查找,需要逐步匹配,先匹配最长的,接着匹配次长的,直到查找完所有可能的path.
92+
bool needbreak = false;
9293
size_t pos = func_name.rfind('/');
93-
while (pos != std::string::npos && pos != 0)
94+
while (pos != std::string::npos &&!needbreak)
9495
{
9596
std::string name = func_name;
9697
if (pos != 0)
9798
name = func_name.substr(0, pos);
99+
else
100+
{
101+
name = "";
102+
needbreak = true;
103+
}
98104

99105
std::string params = func_name.substr(pos);
100106
parser.parse(params);
101107

102-
bool r = handle(req, resp, name, parser, finish);
103-
if (finish)
104-
return r;
108+
if (check(parser, name))
109+
{
110+
bool r = handle(req, resp, name, parser, finish);
111+
if (finish)
112+
return r;
113+
}
105114

106115
pos = func_name.rfind('/', pos - 1);
107116
}
@@ -110,6 +119,21 @@ namespace cinatra
110119
return false;
111120
}
112121

122+
bool check(token_parser& parser, const std::string& name)
123+
{
124+
auto kv = parser_.get_map();
125+
auto rg = kv.equal_range(name);
126+
for (auto itr = rg.first; itr != rg.second; ++itr)
127+
{
128+
if (itr->second.size() == parser.size())
129+
{
130+
return true;
131+
}
132+
}
133+
134+
return false;
135+
}
136+
113137
bool handle(Request& req, Response& resp, std::string& name, token_parser& parser, bool& finish)
114138
{
115139
bool r = false;

include/cinatra/token_parser.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class token_parser
1010
{
1111
std::vector<std::string> v_; //解析之后,v_的第一个元素为函数名,后面的元素均为参数.
12-
std::map<std::string, std::vector<std::string>> map_;
12+
std::multimap<std::string, std::vector<std::string>> map_;
1313
public:
1414
/*
1515
get("/hello/:name", (request, response) -> {
@@ -29,7 +29,7 @@ class token_parser
2929
map_.emplace(path, std::move(v));
3030
}
3131

32-
const std::map<std::string, std::vector<std::string>>& get_map()
32+
const std::multimap<std::string, std::vector<std::string>>& get_map()
3333
{
3434
return map_;
3535
}

0 commit comments

Comments
 (0)