769 size_t blueprint_index{INVALID_BP_ID};
771 ParamType param = ParamType::MAX;
772 std::vector<Node> children;
774 bool IsSimpleNode()
const
776 return !rule_index &&
777 blueprint_index == INVALID_BP_ID &&
778 children.size() < 2 &&
779 param == ParamType::MAX &&
780 std::all_of(std::begin(children), std::end(children), [](
const Node& x) {
781 return x.param == ParamType::MAX;
785 Node& add_child_node()
787 children.emplace_back();
788 return children.back();
799 return head_.children.empty();
804 for (
auto& child : head_.children)
812 void optimizeNode(Node& node)
814 if (node.children.empty())
816 if (node.IsSimpleNode())
818 auto children_temp = std::move(node.children);
819 auto& child_temp = children_temp[0];
820 node.key += child_temp.key;
821 node.rule_index = child_temp.rule_index;
822 node.blueprint_index = child_temp.blueprint_index;
823 node.children = std::move(child_temp.children);
828 for (
auto& child : node.children)
835 void debug_node_print(
const Node& node,
size_t level)
837 if (node.param != ParamType::MAX)
842 CROW_LOG_DEBUG << std::string(3 * level,
' ') <<
"└➝ "
845 case ParamType::UINT:
846 CROW_LOG_DEBUG << std::string(3 * level,
' ') <<
"└➝ "
849 case ParamType::DOUBLE:
850 CROW_LOG_DEBUG << std::string(3 * level,
' ') <<
"└➝ "
853 case ParamType::STRING:
854 CROW_LOG_DEBUG << std::string(3 * level,
' ') <<
"└➝ "
857 case ParamType::PATH:
858 CROW_LOG_DEBUG << std::string(3 * level,
' ') <<
"└➝ "
862 CROW_LOG_DEBUG << std::string(3 * level,
' ') <<
"└➝ "
868 CROW_LOG_DEBUG << std::string(3 * level,
' ') <<
"└➝ " << node.key;
870 for (
const auto& child : node.children)
872 debug_node_print(child, level + 1);
879 CROW_LOG_DEBUG <<
"└➙ ROOT";
880 for (
const auto& child : head_.children)
881 debug_node_print(child, 1);
886 if (!head_.IsSimpleNode())
887 throw std::runtime_error(
"Internal error: Trie header should be simple!");
892 routing_handle_result find(
const std::string& req_url,
const Node& node,
size_t pos = 0, routing_params* params =
nullptr, std::vector<size_t>* blueprints =
nullptr)
const
895 routing_params empty;
896 if (params ==
nullptr)
899 std::vector<size_t> MT;
900 if (blueprints ==
nullptr)
904 std::vector<size_t> found_BP;
905 routing_params match_params;
907 auto update_found = [&found, &found_BP, &match_params](routing_handle_result& ret) {
908 found_BP = std::move(ret.blueprint_indices);
909 if (ret.rule_index && (!found || found > ret.rule_index))
911 found = ret.rule_index;
912 match_params = std::move(ret.r_params);
917 if (pos == req_url.size())
919 found_BP = std::move(*blueprints);
920 return routing_handle_result{node.rule_index, *blueprints, *params};
923 bool found_fragment =
false;
925 for (
const auto& child : node.children)
927 if (child.param != ParamType::MAX)
929 if (child.param == ParamType::INT)
931 char c = req_url[pos];
932 if ((c >=
'0' && c <=
'9') || c ==
'+' || c ==
'-')
936 long long int value = strtoll(req_url.data() + pos, &eptr, 10);
937 if (errno != ERANGE && eptr != req_url.data() + pos)
939 found_fragment =
true;
940 params->int_params.push_back(value);
941 if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index);
942 auto ret = find(req_url, child, eptr - req_url.data(), params, blueprints);
944 params->int_params.pop_back();
945 if (!blueprints->empty()) blueprints->pop_back();
950 else if (child.param == ParamType::UINT)
952 char c = req_url[pos];
953 if ((c >=
'0' && c <=
'9') || c ==
'+')
957 unsigned long long int value = strtoull(req_url.data() + pos, &eptr, 10);
958 if (errno != ERANGE && eptr != req_url.data() + pos)
960 found_fragment =
true;
961 params->uint_params.push_back(value);
962 if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index);
963 auto ret = find(req_url, child, eptr - req_url.data(), params, blueprints);
965 params->uint_params.pop_back();
966 if (!blueprints->empty()) blueprints->pop_back();
971 else if (child.param == ParamType::DOUBLE)
973 char c = req_url[pos];
974 if ((c >=
'0' && c <=
'9') || c ==
'+' || c ==
'-' || c ==
'.')
978 double value = strtod(req_url.data() + pos, &eptr);
979 if (errno != ERANGE && eptr != req_url.data() + pos)
981 found_fragment =
true;
982 params->double_params.push_back(value);
983 if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index);
984 auto ret = find(req_url, child, eptr - req_url.data(), params, blueprints);
986 params->double_params.pop_back();
987 if (!blueprints->empty()) blueprints->pop_back();
992 else if (child.param == ParamType::STRING)
995 for (; epos < req_url.size(); epos++)
997 if (req_url[epos] ==
'/')
1003 found_fragment =
true;
1004 params->string_params.push_back(req_url.substr(pos, epos - pos));
1005 if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index);
1006 auto ret = find(req_url, child, epos, params, blueprints);
1008 params->string_params.pop_back();
1009 if (!blueprints->empty()) blueprints->pop_back();
1013 else if (child.param == ParamType::PATH)
1015 size_t epos = req_url.size();
1019 found_fragment =
true;
1020 params->string_params.push_back(req_url.substr(pos, epos - pos));
1021 if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index);
1022 auto ret = find(req_url, child, epos, params, blueprints);
1024 params->string_params.pop_back();
1025 if (!blueprints->empty()) blueprints->pop_back();
1032 const std::string& fragment = child.key;
1033 if (req_url.compare(pos, fragment.size(), fragment) == 0)
1035 found_fragment =
true;
1036 if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index);
1037 auto ret = find(req_url, child, pos + fragment.size(), params, blueprints);
1039 if (!blueprints->empty()) blueprints->pop_back();
1044 if (!found_fragment)
1045 found_BP = std::move(*blueprints);
1047 return routing_handle_result{found, found_BP, match_params};
1050 routing_handle_result find(
const std::string& req_url)
const
1052 return find(req_url, head_);
1056 void add(
const std::string& url,
size_t rule_index,
unsigned bp_prefix_length = 0,
size_t blueprint_index = INVALID_BP_ID)
1060 bool has_blueprint = bp_prefix_length != 0 && blueprint_index != INVALID_BP_ID;
1062 for (
unsigned i = 0; i < url.size(); i++)
1067 static struct ParamTraits
1073 {ParamType::INT,
"<int>"},
1074 {ParamType::UINT,
"<uint>"},
1075 {ParamType::DOUBLE,
"<float>"},
1076 {ParamType::DOUBLE,
"<double>"},
1077 {ParamType::STRING,
"<str>"},
1078 {ParamType::STRING,
"<string>"},
1079 {ParamType::PATH,
"<path>"},
1082 for (
const auto& x : paramTraits)
1084 if (url.compare(i, x.name.size(), x.name) == 0)
1087 for (
auto& child : idx->children)
1089 if (child.param == x.type)
1100 auto new_node_idx = &idx->add_child_node();
1101 new_node_idx->param = x.type;
1113 bool piece_found =
false;
1114 for (
auto& child : idx->children)
1116 if (child.key[0] == c)
1125 auto new_node_idx = &idx->add_child_node();
1126 new_node_idx->key = c;
1128 if (has_blueprint && i == bp_prefix_length)
1129 new_node_idx->blueprint_index = blueprint_index;
1136 if (idx->rule_index)
1137 throw std::runtime_error(
"handler already exists for " + url);
1138 idx->rule_index = rule_index;
1309 Router() : using_ssl(
false)
1312 DynamicRule& new_rule_dynamic(
const std::string& rule)
1315 all_rules_.emplace_back(ruleObject);
1320 template<u
int64_t N>
1321 typename black_magic::arguments<N>::type::template rebind<TaggedRule>& new_rule_tagged(
const std::string& rule)
1323 using RuleT =
typename black_magic::arguments<N>::type::template rebind<TaggedRule>;
1325 return new_rule<RuleT>(rule);
1328 template<
typename RuleT=StaticRule>
1329 auto& new_rule(
const std::string& rule)
1331 auto ruleObject =
new RuleT(rule);
1332 all_rules_.emplace_back(ruleObject);
1339 return catchall_rule_;
1342 void internal_add_rule_object(
const std::string& rule,
BaseRule* ruleObject)
1344 internal_add_rule_object(rule, ruleObject, INVALID_BP_ID, blueprints_);
1347 void internal_add_rule_object(
const std::string& rule,
BaseRule* ruleObject,
const size_t& BP_index, std::vector<Blueprint*>& blueprints)
1349 bool has_trailing_slash =
false;
1350 std::string rule_without_trailing_slash;
1351 if (rule.size() > 1 && rule.back() ==
'/')
1353 has_trailing_slash =
true;
1354 rule_without_trailing_slash = rule;
1355 rule_without_trailing_slash.pop_back();
1358 ruleObject->mw_indices_.pack();
1360 ruleObject->foreach_method([&](
int method) {
1361 per_methods_[method].rules.emplace_back(ruleObject);
1362 per_methods_[method].trie.add(rule, per_methods_[method].rules.size() - 1,
1363 BP_index != INVALID_BP_ID ? blueprints[BP_index]->prefix().length() : 0,
1368 if (has_trailing_slash)
1370 per_methods_[method].trie.add(rule_without_trailing_slash, RULE_SPECIAL_REDIRECT_SLASH, BP_index != INVALID_BP_ID ? blueprints[BP_index]->prefix().length() : 0, BP_index);
1374 ruleObject->set_added();
1377 void register_blueprint(
Blueprint& blueprint)
1379 if (std::find(blueprints_.begin(), blueprints_.end(), &blueprint) == blueprints_.end())
1381 blueprints_.emplace_back(&blueprint);
1384 throw std::runtime_error(
"blueprint \"" + blueprint.prefix_ +
"\" already exists in router");
1387 void get_recursive_child_methods(
Blueprint* blueprint, std::vector<HTTPMethod>& methods)
1390 if (blueprint->static_dir_.empty() && blueprint->all_rules_.empty())
1392 for (
Blueprint* bp : blueprint->blueprints_)
1394 get_recursive_child_methods(bp, methods);
1397 else if (!blueprint->static_dir_.empty())
1398 methods.emplace_back(HTTPMethod::Get);
1399 for (
auto& rule : blueprint->all_rules_)
1401 rule->foreach_method([&methods](
unsigned method) {
1402 HTTPMethod method_final =
static_cast<HTTPMethod
>(method);
1403 if (std::find(methods.begin(), methods.end(), method_final) == methods.end())
1404 methods.emplace_back(method_final);
1413 validate_bp(blueprints_, blueprint_mw);
1418 for (
unsigned i = 0; i < blueprints.size(); i++)
1422 if (blueprint->is_added())
continue;
1424 if (blueprint->static_dir_ ==
"" && blueprint->all_rules_.empty())
1426 std::vector<HTTPMethod> methods;
1427 get_recursive_child_methods(blueprint, methods);
1428 for (HTTPMethod x : methods)
1430 int method_index =
static_cast<int>(x);
1431 per_methods_[method_index].trie.add(blueprint->prefix(), 0, blueprint->prefix().length(), method_index);
1435 current_mw.merge_back(blueprint->mw_indices_);
1436 for (
auto& rule : blueprint->all_rules_)
1438 if (rule && !rule->is_added())
1440 auto upgraded = rule->upgrade();
1442 rule = std::move(upgraded);
1444 rule->mw_indices_.merge_front(current_mw);
1445 internal_add_rule_object(rule->rule(), rule.get(), i, blueprints);
1448 validate_bp(blueprint->blueprints_, current_mw);
1449 current_mw.pop_back(blueprint->mw_indices_);
1450 blueprint->set_added();
1456 for (
auto& rule : all_rules_)
1458 if (rule && !rule->is_added())
1460 auto upgraded = rule->upgrade();
1462 rule = std::move(upgraded);
1464 internal_add_rule_object(rule->rule(), rule.get());
1467 for (
auto& per_method : per_methods_)
1469 per_method.trie.validate();
1474 template<
typename Adaptor>
1475 void handle_upgrade(
const request& req,
response& res, Adaptor&& adaptor)
1477 if (req.method >= HTTPMethod::InternalMethodCount)
1480 auto& per_method = per_methods_[
static_cast<int>(req.method)];
1481 auto& rules = per_method.rules;
1482 size_t rule_index = per_method.trie.find(req.
url).rule_index;
1486 for (
auto& method : per_methods_)
1488 if (method.trie.find(req.
url).rule_index)
1490 CROW_LOG_DEBUG <<
"Cannot match method " << req.
url <<
" " << method_name(req.method);
1497 CROW_LOG_INFO <<
"Cannot match rules " << req.
url;
1503 if (rule_index >= rules.size())
1504 throw std::runtime_error(
"Trie internal structure corrupted!");
1506 if (rule_index == RULE_SPECIAL_REDIRECT_SLASH)
1508 CROW_LOG_INFO <<
"Redirecting to a url with trailing slash: " << req.
url;
1515 CROW_LOG_DEBUG <<
"Matched rule (upgrade) '" << rules[rule_index]->rule_ <<
"' "
1516 <<
static_cast<uint64_t
>(req.method) <<
" / "
1517 << rules[rule_index]->get_methods();
1521 rules[rule_index]->handle_upgrade(req, res, std::move(adaptor));
1525 exception_handler_(res);
1531 void get_found_bp(
const std::vector<size_t>& bp_i,
const std::vector<Blueprint*>& blueprints, std::vector<Blueprint*>& found_bps,
size_t index = 0)
1541 auto verify_prefix = [&bp_i, &index, &blueprints, &found_bps]() {
1543 bp_i[index] < blueprints.size() &&
1544 blueprints[bp_i[index]]->prefix().substr(0, found_bps[index - 1]->prefix().length() + 1).compare(std::string(found_bps[index - 1]->prefix() +
'/')) == 0;
1546 if (index < bp_i.size())
1549 if (verify_prefix())
1551 found_bps.push_back(blueprints[bp_i[index]]);
1552 get_found_bp(bp_i, found_bps.back()->blueprints_, found_bps, ++index);
1556 if (found_bps.size() < 2)
1559 found_bps.push_back(blueprints_[bp_i[index]]);
1563 found_bps.pop_back();
1564 Blueprint* last_element = found_bps.back();
1565 found_bps.push_back(last_element->blueprints_[bp_i[index]]);
1567 get_found_bp(bp_i, found_bps.back()->blueprints_, found_bps, ++index);
1573 std::vector<Blueprint*> bps_found;
1574 get_found_bp(found.blueprint_indices, blueprints_, bps_found);
1575 if (!bps_found.empty()) {
1576 for (
size_t i = bps_found.size() - 1; i > 0; i--)
1578 if (bps_found[i]->catchall_rule().has_handler()) {
1579 return bps_found[i]->catchall_rule();
1583 return catchall_rule_;
1588 const std::string EMPTY;
1590 std::vector<Blueprint*> bps_found;
1591 get_found_bp(found.blueprint_indices, blueprints_, bps_found);
1592 if (!bps_found.empty()) {
1593 for (
size_t i = bps_found.size() - 1; i > 0; i--) {
1594 if (bps_found[i]->catchall_rule().has_handler()) {
1595#ifdef CROW_ENABLE_DEBUG
1596 return std::string(
"Redirected to Blueprint \"" + bps_found[i]->prefix() +
"\" Catchall rule");
1602 }
else if (catchall_rule_.has_handler()) {
1603#ifdef CROW_ENABLE_DEBUG
1604 return std::string(
"Redirected to global Catchall rule");
1612 std::unique_ptr<routing_handle_result> handle_initial(
request& req,
response& res)
1614 HTTPMethod method_actual = req.method;
1616 std::unique_ptr<routing_handle_result> found{
1619 std::vector<size_t>(),
1621 HTTPMethod::InternalMethodCount)};
1624 if (CROW_UNLIKELY(req.method >= HTTPMethod::InternalMethodCount))
1626 else if (req.method == HTTPMethod::Head)
1628 *found = per_methods_[
static_cast<int>(method_actual)].trie.find(req.
url);
1630 if (!found->rule_index)
1632 method_actual = HTTPMethod::Get;
1633 *found = per_methods_[
static_cast<int>(method_actual)].trie.find(req.
url);
1634 if (!found->rule_index)
1636 CROW_LOG_DEBUG <<
"Cannot match rules " << req.
url;
1644 found->method = method_actual;
1647 else if (req.method == HTTPMethod::Options)
1649 std::string allow =
"OPTIONS, HEAD";
1651 if (req.
url ==
"/*")
1653 for (
int i = 0; i < static_cast<int>(HTTPMethod::InternalMethodCount); i++)
1655 if (
static_cast<int>(HTTPMethod::Head) == i)
1658 if (!per_methods_[i].trie.is_empty())
1661 allow.append(method_name(
static_cast<HTTPMethod
>(i)));
1664#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
1667 res =
response(crow::status::NO_CONTENT);
1672 found->method = method_actual;
1677 bool rules_matched =
false;
1678 for (
int i = 0; i < static_cast<int>(HTTPMethod::InternalMethodCount); i++)
1680 if (per_methods_[i].trie.find(req.
url).rule_index)
1682 rules_matched =
true;
1684 if (
static_cast<int>(HTTPMethod::Head) == i)
1688 allow.append(method_name(
static_cast<HTTPMethod
>(i)));
1693#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
1696 res =
response(crow::status::NO_CONTENT);
1700 found->method = method_actual;
1705 CROW_LOG_DEBUG <<
"Cannot match rules " << req.
url;
1714 *found = per_methods_[
static_cast<int>(method_actual)].trie.find(req.
url);
1716 if (!found->rule_index)
1718 for (
auto& per_method : per_methods_)
1720 if (per_method.trie.find(req.
url).rule_index)
1723 found->catch_all =
true;
1724 CROW_LOG_DEBUG <<
"Cannot match method " << req.
url <<
" "
1725 << method_name(method_actual) <<
". " << get_error(*found);
1732 found->catch_all =
true;
1733 CROW_LOG_DEBUG <<
"Cannot match rules " << req.
url <<
". " << get_error(*found);
1737 found->method = method_actual;
1742 template<
typename App>
1745 if (found.catch_all) {
1746 auto catch_all = get_catch_all(found);
1747 if (catch_all.has_handler()) {
1750 catch_all.handler_(req, res);
1754 exception_handler_(res);
1759 HTTPMethod method_actual = found.method;
1760 const auto& rules = per_methods_[
static_cast<int>(method_actual)].rules;
1761 const size_t rule_index = found.rule_index;
1763 if (rule_index >= rules.size())
1764 throw std::runtime_error(
"Trie internal structure corrupted!");
1765 if (rule_index == RULE_SPECIAL_REDIRECT_SLASH) {
1766 CROW_LOG_INFO <<
"Redirecting to a url with trailing slash: " << req.
url;
1771 CROW_LOG_DEBUG <<
"Matched rule '" << rules[rule_index]->rule_ <<
"' " <<
static_cast<uint64_t
>(req.
1772 method) <<
" / " << rules[rule_index]->get_methods();
1775 BaseRule &rule = *rules[rule_index];
1776 handle_rule<App>(rule, req, res, found.r_params);
1778 exception_handler_(res);
1785 template<
typename App>
1786 typename std::enable_if<std::tuple_size<typename App::mw_container_t>::value != 0,
void>::type
1789 if (!rule.mw_indices_.empty())
1791 auto& ctx = *
reinterpret_cast<typename
App::context_t*
>(req.middleware_context);
1792 auto& container = *
reinterpret_cast<typename App::mw_container_t*
>(req.middleware_container);
1795 auto glob_completion_handler = std::move(res.complete_request_handler_);
1796 res.complete_request_handler_ = [] {};
1798 detail::middleware_call_helper<
decltype(crit_fwd),
1799 0,
typename App::context_t,
typename App::mw_container_t>(crit_fwd, container, req, res, ctx);
1803 glob_completion_handler();
1807 res.complete_request_handler_ = [&rule, &ctx, &container, &req, &res, glob_completion_handler] {
1810 detail::after_handlers_call_helper<
1812 std::tuple_size<typename App::mw_container_t>::value - 1,
1814 typename App::mw_container_t>(crit_bwd, container, ctx, req, res);
1815 glob_completion_handler();
1818 rule.handle(req, res, rp);
1821 template<
typename App>
1822 typename std::enable_if<std::tuple_size<typename App::mw_container_t>::value == 0,
void>::type
1825 rule.handle(req, res, rp);
1830 for (
int i = 0; i < static_cast<int>(HTTPMethod::InternalMethodCount); i++)
1832 Trie& trie_ = per_methods_[i].trie;
1835 CROW_LOG_DEBUG << method_name(static_cast<HTTPMethod>(i));
1836 trie_.debug_print();
1841 std::vector<Blueprint*>& blueprints()
1848 return exception_handler_;
1851 static void default_exception_handler(
response& res)
1863 res.
body = e.what();
1865 catch (
const std::exception& e)
1867 CROW_LOG_ERROR <<
"An uncaught exception occurred: " << e.what();
1871 CROW_LOG_ERROR <<
"An uncaught exception occurred. The type was unknown so no information was available.";
1880 std::vector<BaseRule*> rules;
1887 std::array<PerMethod, static_cast<int>(HTTPMethod::InternalMethodCount)> per_methods_;
1888 std::vector<std::unique_ptr<BaseRule>> all_rules_;
1889 std::vector<Blueprint*> blueprints_;
1890 std::function<void(
crow::response&)> exception_handler_ = &default_exception_handler;