From 09aeec115bfbd51cc5f85965423399585931ec59 Mon Sep 17 00:00:00 2001 From: Windoze Date: Wed, 27 May 2015 22:12:20 +0800 Subject: [PATCH] Move path_ implementation out of header --- include/fibio/http/server/routing.hpp | 53 +------------------------- src/http/server.cpp | 54 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/include/fibio/http/server/routing.hpp b/include/fibio/http/server/routing.hpp index ea02648..15b0dc0 100644 --- a/include/fibio/http/server/routing.hpp +++ b/include/fibio/http/server/routing.hpp @@ -186,58 +186,7 @@ namespace fibio { namespace http { { return [=](server::request &req)->bool { return detail::find_and_test(req.params, p, pred); }; } // Match path pattern and extract parameters into match_info - inline match path_(const std::string &tmpl) { - typedef std::list components_type; - typedef components_type::const_iterator component_iterator; - struct matcher { - bool operator()(server::request &req) { - std::map m; - parse_url(req.url, req.parsed_url); - component_iterator p=pattern.cbegin(); - for (auto &i : req.parsed_url.path_components) { - // Skip empty component - if (i.empty()) continue; - if (p==pattern.cend()) { - // End of pattern - return false; - } else if ((*p)[0]==':') { - // This pattern component is a parameter - m.insert({std::string(p->begin()+1, p->end()), i}); - } else if ((*p)[0]=='*') { - // Ignore anything remains if the wildcard doesn't have a name - if (p->length()==1) return true; - std::string param_name(p->begin()+1, p->end()); - //auto mi=m.find(param_name); - auto mi=std::find_if(m.begin(), m.end(), [&](const std::pair &e){return e.first==param_name;}); - if (mi==m.end()) { - // Not found - m.insert({param_name, i}); - } else { - // Concat this component to existing parameter - mi->second.push_back('/'); - mi->second.append(i); - } - // NOTE: Do not increment p - continue; - } else if (*p!=i) { - // Not match - return false; - } - ++p; - } - // Either pattern consumed or ended with a wildcard - bool ret=(p==pattern.end() || (*p)[0]=='*'); - if (ret) std::move(m.begin(), m.end(), std::inserter(req.params, req.params.end())); - return ret; - } - std::list pattern; - common::iequal eq; - }; - matcher m; - std::vector c; - common::parse_path_components(tmpl, m.pattern); - return std::move(m); - } + match path_(const std::string &tmpl); // Convenience inline match get_(const std::string &pattern) diff --git a/src/http/server.cpp b/src/http/server.cpp index c311599..dee789b 100644 --- a/src/http/server.cpp +++ b/src/http/server.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include namespace fibio { namespace http { @@ -489,4 +490,57 @@ namespace fibio { namespace http { } return boost::system::error_code(); } + + std::function path_(const std::string &tmpl) { + typedef std::list components_type; + typedef components_type::const_iterator component_iterator; + struct matcher { + bool operator()(server::request &req) { + std::map m; + parse_url(req.url, req.parsed_url); + component_iterator p=pattern.cbegin(); + for (auto &i : req.parsed_url.path_components) { + // Skip empty component + if (i.empty()) continue; + if (p==pattern.cend()) { + // End of pattern + return false; + } else if ((*p)[0]==':') { + // This pattern component is a parameter + m.insert({std::string(p->begin()+1, p->end()), i}); + } else if ((*p)[0]=='*') { + // Ignore anything remains if the wildcard doesn't have a name + if (p->length()==1) return true; + std::string param_name(p->begin()+1, p->end()); + //auto mi=m.find(param_name); + auto mi=std::find_if(m.begin(), m.end(), [&](const std::pair &e){return e.first==param_name;}); + if (mi==m.end()) { + // Not found + m.insert({param_name, i}); + } else { + // Concat this component to existing parameter + mi->second.push_back('/'); + mi->second.append(i); + } + // NOTE: Do not increment p + continue; + } else if (*p!=i) { + // Not match + return false; + } + ++p; + } + // Either pattern consumed or ended with a wildcard + bool ret=(p==pattern.end() || (*p)[0]=='*'); + if (ret) std::move(m.begin(), m.end(), std::inserter(req.params, req.params.end())); + return ret; + } + std::list pattern; + common::iequal eq; + }; + matcher m; + std::vector c; + common::parse_path_components(tmpl, m.pattern); + return std::move(m); + } }} // End of namespace fibio::http