#pragma once #include #include #include #include #include #include #include #include "crow/common.h" #include "crow/http_response.h" #include "crow/http_request.h" #include "crow/utility.h" #include "crow/logging.h" #include "crow/websocket.h" namespace crow { /// A base class for all rules. /// Used to provide a common interface for code dealing with different types of rules. /// A Rule provides a URL, allowed HTTP methods, and handlers. class BaseRule { public: BaseRule(std::string rule) : rule_(std::move(rule)) { } virtual ~BaseRule() { } virtual void validate() = 0; std::unique_ptr upgrade() { if (rule_to_upgrade_) return std::move(rule_to_upgrade_); return {}; } virtual void handle(const request&, response&, const routing_params&) = 0; virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&) { res = response(404); res.end(); } #ifdef CROW_ENABLE_SSL virtual void handle_upgrade(const request&, response& res, SSLAdaptor&&) { res = response(404); res.end(); } #endif uint32_t get_methods() { return methods_; } template void foreach_method(F f) { for(uint32_t method = 0, method_bit = 1; method < static_cast(HTTPMethod::InternalMethodCount); method++, method_bit<<=1) { if (methods_ & method_bit) f(method); } } const std::string& rule() { return rule_; } protected: uint32_t methods_{1<(HTTPMethod::Get)}; std::string rule_; std::string name_; std::unique_ptr rule_to_upgrade_; friend class Router; template friend struct RuleParameterTraits; }; namespace detail { namespace routing_handler_call_helper { template struct call_pair { using type = T; static const int pos = Pos; }; template struct call_params { H1& handler; const routing_params& params; const request& req; response& res; }; template struct call { }; template struct call, black_magic::S> { void operator()(F cparams) { using pushed = typename black_magic::S::template push_back>; call, pushed>()(cparams); } }; template struct call, black_magic::S> { void operator()(F cparams) { using pushed = typename black_magic::S::template push_back>; call, pushed>()(cparams); } }; template struct call, black_magic::S> { void operator()(F cparams) { using pushed = typename black_magic::S::template push_back>; call, pushed>()(cparams); } }; template struct call, black_magic::S> { void operator()(F cparams) { using pushed = typename black_magic::S::template push_back>; call, pushed>()(cparams); } }; template struct call, black_magic::S> { void operator()(F cparams) { cparams.handler( cparams.req, cparams.res, cparams.params.template get(Args1::pos)... ); } }; template struct Wrapped { template void set_(Func f, typename std::enable_if< !std::is_same>::type, const request&>::value , int>::type = 0) { handler_ = ( #ifdef CROW_CAN_USE_CPP14 [f = std::move(f)] #else [f] #endif (const request&, response& res, Args... args){ res = response(f(args...)); res.end(); }); } template struct req_handler_wrapper { req_handler_wrapper(Func f) : f(std::move(f)) { } void operator()(const request& req, response& res, Args... args) { res = response(f(req, args...)); res.end(); } Func f; }; template void set_(Func f, typename std::enable_if< std::is_same>::type, const request&>::value && !std::is_same>::type, response&>::value , int>::type = 0) { handler_ = req_handler_wrapper(std::move(f)); /*handler_ = ( [f = std::move(f)] (const request& req, response& res, Args... args){ res = response(f(req, args...)); res.end(); });*/ } template void set_(Func f, typename std::enable_if< std::is_same>::type, const request&>::value && std::is_same>::type, response&>::value , int>::type = 0) { handler_ = std::move(f); } template struct handler_type_helper { using type = std::function; using args_type = black_magic::S...>; }; template struct handler_type_helper { using type = std::function; using args_type = black_magic::S...>; }; template struct handler_type_helper { using type = std::function; using args_type = black_magic::S...>; }; typename handler_type_helper::type handler_; void operator()(const request& req, response& res, const routing_params& params) { detail::routing_handler_call_helper::call< detail::routing_handler_call_helper::call_params< decltype(handler_)>, 0, 0, 0, 0, typename handler_type_helper::args_type, black_magic::S<> >()( detail::routing_handler_call_helper::call_params< decltype(handler_)> {handler_, params, req, res} ); } }; } } /// A rule dealing with websockets. /// Provides the interface for the user to put in the necessary handlers for a websocket to work. /// class WebSocketRule : public BaseRule { using self_t = WebSocketRule; public: WebSocketRule(std::string rule) : BaseRule(std::move(rule)) { } void validate() override { } void handle(const request&, response& res, const routing_params&) override { res = response(404); res.end(); } void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override { new crow::websocket::Connection(req, std::move(adaptor), open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } #ifdef CROW_ENABLE_SSL void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override { new crow::websocket::Connection(req, std::move(adaptor), open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } #endif template self_t& onopen(Func f) { open_handler_ = f; return *this; } template self_t& onmessage(Func f) { message_handler_ = f; return *this; } template self_t& onclose(Func f) { close_handler_ = f; return *this; } template self_t& onerror(Func f) { error_handler_ = f; return *this; } template self_t& onaccept(Func f) { accept_handler_ = f; return *this; } protected: std::function open_handler_; std::function message_handler_; std::function close_handler_; std::function error_handler_; std::function accept_handler_; }; /// Allows the user to assign parameters using functions. /// /// `rule.name("name").methods(HTTPMethod::POST)` template struct RuleParameterTraits { using self_t = T; WebSocketRule& websocket() { auto p =new WebSocketRule(static_cast(this)->rule_); static_cast(this)->rule_to_upgrade_.reset(p); return *p; } self_t& name(std::string name) noexcept { static_cast(this)->name_ = std::move(name); return static_cast(*this); } self_t& methods(HTTPMethod method) { static_cast(this)->methods_ = 1 << static_cast(method); return static_cast(*this); } template self_t& methods(HTTPMethod method, MethodArgs ... args_method) { methods(args_method...); static_cast(this)->methods_ |= 1 << static_cast(method); return static_cast(*this); } }; /// A rule that can change its parameters during runtime. class DynamicRule : public BaseRule, public RuleParameterTraits { public: DynamicRule(std::string rule) : BaseRule(std::move(rule)) { } void validate() override { if (!erased_handler_) { throw std::runtime_error(name_ + (!name_.empty() ? ": " : "") + "no handler for url " + rule_); } } void handle(const request& req, response& res, const routing_params& params) override { erased_handler_(req, res, params); } template void operator()(Func f) { #ifdef CROW_MSVC_WORKAROUND using function_t = utility::function_traits; #else using function_t = utility::function_traits; #endif erased_handler_ = wrap(std::move(f), black_magic::gen_seq()); } // enable_if Arg1 == request && Arg2 == response // enable_if Arg1 == request && Arg2 != resposne // enable_if Arg1 != request #ifdef CROW_MSVC_WORKAROUND template #else template #endif std::function wrap(Func f, black_magic::seq) { #ifdef CROW_MSVC_WORKAROUND using function_t = utility::function_traits; #else using function_t = utility::function_traits; #endif if (!black_magic::is_parameter_tag_compatible( black_magic::get_parameter_tag_runtime(rule_.c_str()), black_magic::compute_parameter_tag_from_args_list< typename function_t::template arg...>::value)) { throw std::runtime_error("route_dynamic: Handler type is mismatched with URL parameters: " + rule_); } auto ret = detail::routing_handler_call_helper::Wrapped...>(); ret.template set_< typename function_t::template arg... >(std::move(f)); return ret; } template void operator()(std::string name, Func&& f) { name_ = std::move(name); (*this).template operator()(std::forward(f)); } private: std::function erased_handler_; }; /// Default rule created when CROW_ROUTE is called. template class TaggedRule : public BaseRule, public RuleParameterTraits> { public: using self_t = TaggedRule; TaggedRule(std::string rule) : BaseRule(std::move(rule)) { } void validate() override { if (!handler_) { throw std::runtime_error(name_ + (!name_.empty() ? ": " : "") + "no handler for url " + rule_); } } template typename std::enable_if>::value, void>::type operator()(Func&& f) { static_assert(black_magic::CallHelper>::value || black_magic::CallHelper>::value , "Handler type is mismatched with URL parameters"); static_assert(!std::is_same()...))>::value, "Handler function cannot have void return type; valid return types: string, int, crow::resposne, crow::json::wvalue"); handler_ = ( #ifdef CROW_CAN_USE_CPP14 [f = std::move(f)] #else [f] #endif (const request&, response& res, Args ... args){ res = response(f(args...)); res.end(); }); } template typename std::enable_if< !black_magic::CallHelper>::value && black_magic::CallHelper>::value, void>::type operator()(Func&& f) { static_assert(black_magic::CallHelper>::value || black_magic::CallHelper>::value, "Handler type is mismatched with URL parameters"); static_assert(!std::is_same(), std::declval()...))>::value, "Handler function cannot have void return type; valid return types: string, int, crow::resposne, crow::json::wvalue"); handler_ = ( #ifdef CROW_CAN_USE_CPP14 [f = std::move(f)] #else [f] #endif (const crow::request& req, crow::response& res, Args ... args){ res = response(f(req, args...)); res.end(); }); } template typename std::enable_if< !black_magic::CallHelper>::value && !black_magic::CallHelper>::value && black_magic::CallHelper>::value, void>::type operator()(Func&& f) { static_assert(black_magic::CallHelper>::value || black_magic::CallHelper>::value , "Handler type is mismatched with URL parameters"); static_assert(std::is_same(), std::declval()...))>::value, "Handler function with response argument should have void return type"); handler_ = ( #ifdef CROW_CAN_USE_CPP14 [f = std::move(f)] #else [f] #endif (const crow::request&, crow::response& res, Args ... args){ f(res, args...); }); } template typename std::enable_if< !black_magic::CallHelper>::value && !black_magic::CallHelper>::value && !black_magic::CallHelper>::value, void>::type operator()(Func&& f) { static_assert(black_magic::CallHelper>::value || black_magic::CallHelper>::value || black_magic::CallHelper>::value , "Handler type is mismatched with URL parameters"); static_assert(std::is_same(), std::declval(), std::declval()...))>::value, "Handler function with response argument should have void return type"); handler_ = std::move(f); } template void operator()(std::string name, Func&& f) { name_ = std::move(name); (*this).template operator()(std::forward(f)); } void handle(const request& req, response& res, const routing_params& params) override { detail::routing_handler_call_helper::call< detail::routing_handler_call_helper::call_params< decltype(handler_)>, 0, 0, 0, 0, black_magic::S, black_magic::S<> >()( detail::routing_handler_call_helper::call_params< decltype(handler_)> {handler_, params, req, res} ); } private: std::function handler_; }; const int RULE_SPECIAL_REDIRECT_SLASH = 1; /// A search tree. class Trie { public: struct Node { unsigned rule_index{}; std::array(ParamType::MAX)> param_childrens{}; std::unordered_map children; bool IsSimpleNode() const { return !rule_index && std::all_of( std::begin(param_childrens), std::end(param_childrens), [](unsigned x){ return !x; }); } }; Trie() : nodes_(1) { } private: void optimizeNode(Node* node) { for(auto x : node->param_childrens) { if (!x) continue; Node* child = &nodes_[x]; optimizeNode(child); } if (node->children.empty()) return; bool mergeWithChild = true; for(auto& kv : node->children) { Node* child = &nodes_[kv.second]; if (!child->IsSimpleNode()) { mergeWithChild = false; break; } } if (mergeWithChild) { decltype(node->children) merged; for(auto& kv : node->children) { Node* child = &nodes_[kv.second]; for(auto& child_kv : child->children) { merged[kv.first + child_kv.first] = child_kv.second; } } node->children = std::move(merged); optimizeNode(node); } else { for(auto& kv : node->children) { Node* child = &nodes_[kv.second]; optimizeNode(child); } } } void optimize() { optimizeNode(head()); } public: void validate() { if (!head()->IsSimpleNode()) throw std::runtime_error("Internal error: Trie header should be simple!"); optimize(); } std::pair find(const std::string& req_url, const Node* node = nullptr, unsigned pos = 0, routing_params* params = nullptr) const { routing_params empty; if (params == nullptr) params = ∅ unsigned found{}; routing_params match_params; if (node == nullptr) node = head(); if (pos == req_url.size()) return {node->rule_index, *params}; auto update_found = [&found, &match_params](std::pair& ret) { if (ret.first && (!found || found > ret.first)) { found = ret.first; match_params = std::move(ret.second); } }; if (node->param_childrens[static_cast(ParamType::INT)]) { char c = req_url[pos]; if ((c >= '0' && c <= '9') || c == '+' || c == '-') { char* eptr; errno = 0; long long int value = strtoll(req_url.data()+pos, &eptr, 10); if (errno != ERANGE && eptr != req_url.data()+pos) { params->int_params.push_back(value); auto ret = find(req_url, &nodes_[node->param_childrens[static_cast(ParamType::INT)]], eptr - req_url.data(), params); update_found(ret); params->int_params.pop_back(); } } } if (node->param_childrens[static_cast(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_url, &nodes_[node->param_childrens[static_cast(ParamType::UINT)]], eptr - req_url.data(), params); update_found(ret); params->uint_params.pop_back(); } } } if (node->param_childrens[static_cast(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_url, &nodes_[node->param_childrens[static_cast(ParamType::DOUBLE)]], eptr - req_url.data(), params); update_found(ret); params->double_params.pop_back(); } } } if (node->param_childrens[static_cast(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_url, &nodes_[node->param_childrens[static_cast(ParamType::STRING)]], epos, params); update_found(ret); params->string_params.pop_back(); } } if (node->param_childrens[static_cast(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_url, &nodes_[node->param_childrens[static_cast(ParamType::PATH)]], epos, params); update_found(ret); params->string_params.pop_back(); } } for(auto& kv : node->children) { const std::string& fragment = kv.first; const Node* child = &nodes_[kv.second]; if (req_url.compare(pos, fragment.size(), fragment) == 0) { auto ret = find(req_url, child, pos + fragment.size(), params); update_found(ret); } } return {found, match_params}; } void add(const std::string& url, unsigned rule_index) { unsigned idx{0}; for(unsigned i = 0; i < url.size(); i ++) { char c = url[i]; if (c == '<') { static struct ParamTraits { ParamType type; std::string name; } paramTraits[] = { { ParamType::INT, "" }, { ParamType::UINT, "" }, { ParamType::DOUBLE, "" }, { ParamType::DOUBLE, "" }, { ParamType::STRING, "" }, { ParamType::STRING, "" }, { ParamType::PATH, "" }, }; for(auto& x:paramTraits) { if (url.compare(i, x.name.size(), x.name) == 0) { if (!nodes_[idx].param_childrens[static_cast(x.type)]) { auto new_node_idx = new_node(); nodes_[idx].param_childrens[static_cast(x.type)] = new_node_idx; } idx = nodes_[idx].param_childrens[static_cast(x.type)]; i += x.name.size(); break; } } i --; } else { std::string piece(&c, 1); if (!nodes_[idx].children.count(piece)) { auto new_node_idx = new_node(); nodes_[idx].children.emplace(piece, new_node_idx); } idx = nodes_[idx].children[piece]; } } if (nodes_[idx].rule_index) throw std::runtime_error("handler already exists for " + url); nodes_[idx].rule_index = rule_index; } private: void debug_node_print(Node* n, int level) { for(int i = 0; i < static_cast(ParamType::MAX); i ++) { if (n->param_childrens[i]) { CROW_LOG_DEBUG << std::string(2*level, ' ') /*<< "("<param_childrens[i]<<") "*/; switch(static_cast(i)) { case ParamType::INT: CROW_LOG_DEBUG << ""; break; case ParamType::UINT: CROW_LOG_DEBUG << ""; break; case ParamType::DOUBLE: CROW_LOG_DEBUG << ""; break; case ParamType::STRING: CROW_LOG_DEBUG << ""; break; case ParamType::PATH: CROW_LOG_DEBUG << ""; break; default: CROW_LOG_DEBUG << ""; break; } debug_node_print(&nodes_[n->param_childrens[i]], level+1); } } for(auto& kv : n->children) { CROW_LOG_DEBUG << std::string(2*level, ' ') /*<< "(" << kv.second << ") "*/ << kv.first; debug_node_print(&nodes_[kv.second], level+1); } } public: void debug_print() { debug_node_print(head(), 0); } private: const Node* head() const { return &nodes_.front(); } Node* head() { return &nodes_.front(); } unsigned new_node() { nodes_.resize(nodes_.size()+1); return nodes_.size() - 1; } std::vector nodes_; }; /// Handles matching requests to existing rules and upgrade requests. class Router { public: Router() { } DynamicRule& new_rule_dynamic(const std::string& rule) { auto ruleObject = new DynamicRule(rule); all_rules_.emplace_back(ruleObject); return *ruleObject; } template typename black_magic::arguments::type::template rebind& new_rule_tagged(const std::string& rule) { using RuleT = typename black_magic::arguments::type::template rebind; auto ruleObject = new RuleT(rule); all_rules_.emplace_back(ruleObject); return *ruleObject; } void internal_add_rule_object(const std::string& rule, BaseRule* ruleObject) { bool has_trailing_slash = false; std::string rule_without_trailing_slash; if (rule.size() > 1 && rule.back() == '/') { has_trailing_slash = true; rule_without_trailing_slash = rule; rule_without_trailing_slash.pop_back(); } ruleObject->foreach_method([&](int method) { per_methods_[method].rules.emplace_back(ruleObject); per_methods_[method].trie.add(rule, per_methods_[method].rules.size() - 1); // directory case: // request to '/about' url matches '/about/' rule if (has_trailing_slash) { per_methods_[method].trie.add(rule_without_trailing_slash, RULE_SPECIAL_REDIRECT_SLASH); } }); } void validate() { for(auto& rule:all_rules_) { if (rule) { auto upgraded = rule->upgrade(); if (upgraded) rule = std::move(upgraded); rule->validate(); internal_add_rule_object(rule->rule(), rule.get()); } } for(auto& per_method:per_methods_) { per_method.trie.validate(); } } template void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) { if (req.method >= HTTPMethod::InternalMethodCount) return; auto& per_method = per_methods_[static_cast(req.method)]; auto& rules = per_method.rules; unsigned rule_index = per_method.trie.find(req.url).first; if (!rule_index) { for (auto& per_method: per_methods_) { if (per_method.trie.find(req.url).first) { CROW_LOG_DEBUG << "Cannot match method " << req.url << " " << method_name(req.method); res = response(405); res.end(); return; } } CROW_LOG_INFO << "Cannot match rules " << req.url; res = response(404); res.end(); return; } if (rule_index >= rules.size()) throw std::runtime_error("Trie internal structure corrupted!"); if (rule_index == RULE_SPECIAL_REDIRECT_SLASH) { CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url; res = response(301); // TODO absolute url building if (req.get_header_value("Host").empty()) { res.add_header("Location", req.url + "/"); } else { res.add_header("Location", "http://" + req.get_header_value("Host") + req.url + "/"); } res.end(); return; } CROW_LOG_DEBUG << "Matched rule (upgrade) '" << rules[rule_index]->rule_ << "' " << static_cast(req.method) << " / " << rules[rule_index]->get_methods(); // any uncaught exceptions become 500s try { rules[rule_index]->handle_upgrade(req, res, std::move(adaptor)); } catch(std::exception& e) { CROW_LOG_ERROR << "An uncaught exception occurred: " << e.what(); res = response(500); res.end(); return; } catch(...) { CROW_LOG_ERROR << "An uncaught exception occurred. The type was unknown so no information was available."; res = response(500); res.end(); return; } } void handle(const request& req, response& res) { if (req.method >= HTTPMethod::InternalMethodCount) return; auto& per_method = per_methods_[static_cast(req.method)]; auto& trie = per_method.trie; auto& rules = per_method.rules; auto found = trie.find(req.url); unsigned rule_index = found.first; if (!rule_index) { for (auto& per_method: per_methods_) { if (per_method.trie.find(req.url).first) { CROW_LOG_DEBUG << "Cannot match method " << req.url << " " << method_name(req.method); res = response(405); res.end(); return; } } CROW_LOG_DEBUG << "Cannot match rules " << req.url; res = response(404); res.end(); return; } if (rule_index >= rules.size()) throw std::runtime_error("Trie internal structure corrupted!"); if (rule_index == RULE_SPECIAL_REDIRECT_SLASH) { CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url; res = response(301); // TODO absolute url building if (req.get_header_value("Host").empty()) { res.add_header("Location", req.url + "/"); } else { res.add_header("Location", "http://" + req.get_header_value("Host") + req.url + "/"); } res.end(); return; } CROW_LOG_DEBUG << "Matched rule '" << rules[rule_index]->rule_ << "' " << static_cast(req.method) << " / " << rules[rule_index]->get_methods(); // any uncaught exceptions become 500s try { rules[rule_index]->handle(req, res, found.second); } catch(std::exception& e) { CROW_LOG_ERROR << "An uncaught exception occurred: " << e.what(); res = response(500); res.end(); return; } catch(...) { CROW_LOG_ERROR << "An uncaught exception occurred. The type was unknown so no information was available."; res = response(500); res.end(); return; } } void debug_print() { for(int i = 0; i < static_cast(HTTPMethod::InternalMethodCount); i ++) { CROW_LOG_DEBUG << method_name(static_cast(i)); per_methods_[i].trie.debug_print(); } } private: struct PerMethod { std::vector rules; Trie trie; // rule index 0, 1 has special meaning; preallocate it to avoid duplication. PerMethod() : rules(2) {} }; std::array(HTTPMethod::InternalMethodCount)> per_methods_; std::vector> all_rules_; }; }