@@ -93,8 +93,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
9393 rule_ (std::move(rule))
9494 {}
9595
96- virtual ~BaseRule ()
97- {}
96+ virtual ~BaseRule ()=default ;
9897
9998 virtual void validate () = 0;
10099
@@ -1254,8 +1253,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
12541253 };
12551254
12561255 // / Handles matching requests to existing rules and upgrade requests.
1257- class Router
1258- {
1256+ class Router {
12591257 public:
12601258 bool using_ssl;
12611259
@@ -1471,7 +1469,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
14711469 }
14721470 }
14731471
1474- void get_found_bp (std::vector<uint16_t >& bp_i, std::vector<Blueprint*>& blueprints, std::vector<Blueprint*>& found_bps, uint16_t index = 0 )
1472+ void get_found_bp (const std::vector<uint16_t >& bp_i, const std::vector<Blueprint*>& blueprints, std::vector<Blueprint*>& found_bps, uint16_t index = 0 )
14751473 {
14761474 // This statement makes 3 assertions:
14771475 // 1. The index is above 0.
@@ -1512,25 +1510,28 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15121510 }
15131511 }
15141512
1515- // / Is used to handle errors, you insert the error code, found route, request, and response. and it'll either call the appropriate catchall route (considering the blueprint system) and send you a status string (which is mainly used for debug messages), or just set the response code to the proper error code.
1516- std::string get_error (unsigned short code, routing_handle_result& found, const request& req, response& res)
1513+ CatchallRule& get_catch_all (const routing_handle_result& found) {
1514+ std::vector<Blueprint*> bps_found;
1515+ get_found_bp (found.blueprint_indices , blueprints_, bps_found);
1516+ for (int i = bps_found.size () - 1 ; i > 0 ; i--)
1517+ {
1518+ std::vector<uint16_t > bpi = found.blueprint_indices ;
1519+ if (bps_found[i]->catchall_rule ().has_handler ()) {
1520+ return bps_found[i]->catchall_rule ();
1521+ }
1522+ }
1523+ return catchall_rule_;
1524+ }
1525+
1526+ std::string get_error (const routing_handle_result& found)
15171527 {
1518- res.code = code;
15191528 std::vector<Blueprint*> bps_found;
15201529 get_found_bp (found.blueprint_indices , blueprints_, bps_found);
15211530 for (int i = bps_found.size () - 1 ; i > 0 ; i--)
15221531 {
15231532 std::vector<uint16_t > bpi = found.blueprint_indices ;
15241533 if (bps_found[i]->catchall_rule ().has_handler ())
15251534 {
1526- try
1527- {
1528- bps_found[i]->catchall_rule ().handler_ (req, res);
1529- }
1530- catch (...)
1531- {
1532- exception_handler_ (res);
1533- }
15341535#ifdef CROW_ENABLE_DEBUG
15351536 return std::string (" Redirected to Blueprint \" " + bps_found[i]->prefix () + " \" Catchall rule" );
15361537#else
@@ -1540,14 +1541,6 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15401541 }
15411542 if (catchall_rule_.has_handler ())
15421543 {
1543- try
1544- {
1545- catchall_rule_.handler_ (req, res);
1546- }
1547- catch (...)
1548- {
1549- exception_handler_ (res);
1550- }
15511544#ifdef CROW_ENABLE_DEBUG
15521545 return std::string (" Redirected to global Catchall rule" );
15531546#else
@@ -1667,17 +1660,45 @@ namespace crow // NOTE: Already documented in "crow/app.h"
16671660 {
16681661 if (per_method.trie .find (req.url ).rule_index ) // Route found, but in another method
16691662 {
1670- const std::string error_message (get_error (405 , *found, req, res));
1671- CROW_LOG_DEBUG << " Cannot match method " << req.url << " " << method_name (method_actual) << " . " << error_message;
1672- res.end ();
1663+ res.code = 405 ;
1664+ found->catch_all = true ;
1665+ // auto catch_all = get_catch_all(*found);
1666+ // if (catch_all.has_handler()) {
1667+ // try
1668+ // {
1669+ // catch_all.handler_(req, res);
1670+ // }
1671+ // catch (...)
1672+ // {
1673+ // exception_handler_(res);
1674+ // }
1675+ // }
1676+ const std::string error_message (get_error (*found));
1677+ // TODO handle
1678+ CROW_LOG_DEBUG << " Cannot match method " << req.url << " "
1679+ << method_name (method_actual) << " . " << error_message;
1680+ // res.end();
16731681 return found;
16741682 }
16751683 }
16761684 // Route does not exist anywhere
16771685
1678- const std::string error_message (get_error (404 , *found, req, res));
1686+ res.code = 404 ;
1687+ found->catch_all = true ;
1688+ // auto catch_all = get_catch_all(*found);
1689+ // if (catch_all.has_handler()) {
1690+ // try
1691+ // {
1692+ // catch_all.handler_(req, res);
1693+ // }
1694+ // catch (...)
1695+ // {
1696+ // exception_handler_(res);
1697+ // }
1698+ // }
1699+ const std::string error_message (get_error (*found));
16791700 CROW_LOG_DEBUG << " Cannot match rules " << req.url << " . " << error_message;
1680- res.end ();
1701+ // res.end();
16811702 return found;
16821703 }
16831704
@@ -1696,28 +1717,38 @@ namespace crow // NOTE: Already documented in "crow/app.h"
16961717 if (rule_index >= rules.size ())
16971718 throw std::runtime_error (" Trie internal structure corrupted!" );
16981719
1699- if (rule_index == RULE_SPECIAL_REDIRECT_SLASH)
1700- {
1720+ if (found.catch_all ) {
1721+ auto catch_all = get_catch_all (found);
1722+ if (catch_all.has_handler ()) {
1723+ try
1724+ {
1725+ catch_all.handler_ (req, res);
1726+ }
1727+ catch (...)
1728+ {
1729+ exception_handler_ (res);
1730+ }
1731+ }
1732+ res.end ();
1733+ } else if (rule_index == RULE_SPECIAL_REDIRECT_SLASH) {
17011734 CROW_LOG_INFO << " Redirecting to a url with trailing slash: " << req.url ;
17021735 res = response (301 );
17031736 res.add_header (" Location" , req.url + " /" );
17041737 res.end ();
1705- return ;
1706- }
1707-
1708- CROW_LOG_DEBUG << " Matched rule '" << rules[rule_index]->rule_ << " ' " << static_cast <uint32_t >(req.method ) << " / " << rules[rule_index]->get_methods ();
1738+ } else {
1739+ CROW_LOG_DEBUG << " Matched rule '" << rules[rule_index]->rule_ << " ' " << static_cast <uint32_t >(req.
1740+ method) << " / " << rules[rule_index]->get_methods ();
1741+
1742+ try {
1743+ BaseRule &rule = *rules[rule_index];
1744+ handle_rule<App>(rule, req, res, found.r_params );
1745+ } catch (...) {
1746+ exception_handler_ (res);
1747+ res.end ();
1748+ }
17091749
1710- try
1711- {
1712- BaseRule& rule = *rules[rule_index];
1713- handle_rule<App>(rule, req, res, found.r_params );
1714- }
1715- catch (...)
1716- {
1717- exception_handler_ (res);
1718- res.end ();
1719- return ;
17201750 }
1751+
17211752 }
17221753
17231754 template <typename App>
0 commit comments