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

Commit

Permalink
修改dispatch的逻辑以修复路由bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklove committed Aug 1, 2015
1 parent e54bb5d commit 71e0275
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
36 changes: 30 additions & 6 deletions include/cinatra/http_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <functional>
#include <cinatra/function_traits.hpp>
#include <cinatra/string_utils.hpp>
//#include "tuple_utils.hpp"
//#include <cinatra/tuple_utils.hpp>
#include <cinatra/token_parser.hpp>
#include <cinatra/request.hpp>
#include <cinatra/response.hpp>
Expand Down Expand Up @@ -50,7 +50,7 @@ namespace cinatra

pos = name.find_first_of(':', nextpos);
}
parser_.add(name, v);
parser_.add(funcName, v);
return funcName;
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions include/cinatra/token_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class token_parser
{
std::vector<std::string> v_; //解析之后,v_的第一个元素为函数名,后面的元素均为参数.
std::map<std::string, std::vector<std::string>> map_;
std::multimap<std::string, std::vector<std::string>> map_;
public:
/*
get("/hello/:name", (request, response) -> {
Expand All @@ -29,7 +29,7 @@ class token_parser
map_.emplace(path, std::move(v));
}

const std::map<std::string, std::vector<std::string>>& get_map()
const std::multimap<std::string, std::vector<std::string>>& get_map()
{
return map_;
}
Expand Down

0 comments on commit 71e0275

Please sign in to comment.