From 7933427691ec30afd63cde8927b9500237d5b2cb Mon Sep 17 00:00:00 2001 From: ipknHama Date: Tue, 15 Apr 2014 22:46:28 +0900 Subject: [PATCH] add more parameter types --- .gitignore | 2 + routing.h | 113 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 108 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 82c6ff6ef..e86bfe647 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ covtest unittest.gcda unittest.gcno +http_parser.gcda +http_parser.gcno diff --git a/routing.h b/routing.h index 4515af3ed..fd8c475ab 100644 --- a/routing.h +++ b/routing.h @@ -35,14 +35,9 @@ namespace flask return *this; } - virtual void validate() - { - } + virtual void validate() = 0; - virtual response handle(const request&, const routing_params&) - { - return response(400); - } + virtual response handle(const request&, const routing_params&) = 0; protected: std::string rule_; @@ -122,6 +117,39 @@ namespace flask } }; + template + struct call, black_magic::S> + { + response operator()(F& handler, const routing_params& params) + { + using pushed = typename black_magic::S::template push_back>; + return call, pushed>()(handler, params); + } + }; + + template + struct call, black_magic::S> + { + response operator()(F& handler, const routing_params& params) + { + using pushed = typename black_magic::S::template push_back>; + return call, pushed>()(handler, params); + } + }; + + template + struct call, black_magic::S> + { + response operator()(F& handler, const routing_params& params) + { + using pushed = typename black_magic::S::template push_back>; + return call, pushed>()(handler, params); + } + }; + template struct call, black_magic::S> { @@ -145,6 +173,10 @@ namespace flask return *this; } + void validate() + { + } + template void operator()(Func&& f) { @@ -272,6 +304,73 @@ public: } } + if (node->param_childrens[(int)ParamType::UINT]) + { + char c = req.url[pos]; + if ((c >= '0' && c <= '9') || c == '+') + { + char* eptr; + errno = 0; + unsigned long long int value = strtoull(req.url.data()+pos, &eptr, 10); + if (errno != ERANGE && eptr != req.url.data()+pos) + { + params->uint_params.push_back(value); + auto ret = find(req, &nodes_[node->param_childrens[(int)ParamType::UINT]], eptr - req.url.data(), params); + update_found(ret); + params->uint_params.pop_back(); + } + } + } + + if (node->param_childrens[(int)ParamType::DOUBLE]) + { + char c = req.url[pos]; + if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.') + { + char* eptr; + errno = 0; + double value = strtod(req.url.data()+pos, &eptr); + if (errno != ERANGE && eptr != req.url.data()+pos) + { + params->double_params.push_back(value); + auto ret = find(req, &nodes_[node->param_childrens[(int)ParamType::DOUBLE]], eptr - req.url.data(), params); + update_found(ret); + params->double_params.pop_back(); + } + } + } + + if (node->param_childrens[(int)ParamType::STRING]) + { + size_t epos = pos; + for(; epos < req.url.size(); epos ++) + { + if (req.url[epos] == '/') + break; + } + + if (epos != pos) + { + params->string_params.push_back(req.url.substr(pos, epos-pos)); + auto ret = find(req, &nodes_[node->param_childrens[(int)ParamType::STRING]], epos, params); + update_found(ret); + params->string_params.pop_back(); + } + } + + if (node->param_childrens[(int)ParamType::PATH]) + { + size_t epos = req.url.size(); + + if (epos != pos) + { + params->string_params.push_back(req.url.substr(pos, epos-pos)); + auto ret = find(req, &nodes_[node->param_childrens[(int)ParamType::PATH]], epos, params); + update_found(ret); + params->string_params.pop_back(); + } + } + for(auto& kv : node->children) { const std::string& fragment = kv.first;