@@ -21,25 +21,34 @@ Router::Router(const std::map<std::string, RouteConfig>& routes)
2121{
2222}
2323
24- // Returns the length of the matching prefix between request_path and route_path
25- static size_t prefix_length (const std::string& request_path, const std::string& route_path)
24+ static bool route_matches (const std::string& request_path, const std::string& route)
2625{
27- if (route_path == " /" )
28- return 1 ;
26+ if (route == " /" )
27+ return true ;
2928
30- if (request_path.size () < route_path.size ())
31- return 0 ;
29+ if (request_path.size () < route.size ())
30+ return false ;
31+
32+ // Check if prefix matches
33+ if (request_path.compare (0 , route.size (), route) != 0 )
34+ return false ;
3235
33- if (request_path.compare (0 , route_path.size (), route_path) != 0 )
34- return 0 ;
36+ // Check for exact match
37+ if (request_path.size () == route.size ())
38+ return true ;
3539
36- if (request_path. size () == route_path. size ())
37- return route_path. size ( );
40+ assert (route[ 0 ] == ' / ' );
41+ assert (route. size () > 1 && " At this point we must have matched more than just '/' " );
3842
39- if (request_path[route_path.size ()] == ' /' )
40- return route_path.size ();
43+ // Directory match
44+ if (route[route.size () - 1 ] == ' /' )
45+ return true ;
4146
42- return 0 ;
47+ // Endpoint match, ensure boundary
48+ if (request_path[route.size ()] == ' /' )
49+ return true ;
50+
51+ return false ;
4352}
4453
4554// for the moment it takes some static input
@@ -54,7 +63,9 @@ const RouteConfig& Router::find_best_route(const std::string& request_path) cons
5463 for (std::map<std::string, RouteConfig>::const_iterator it = routes_.begin ();
5564 it != routes_.end ();
5665 ++it) {
57- size_t match_len = prefix_length (request_path, it->first );
66+
67+ std::string route_path = it->second .path ;
68+ size_t match_len = route_matches (request_path, route_path) ? route_path.size () : 0 ;
5869 if (match_len > best_len) {
5970 best_len = match_len;
6071 best = &it->second ;
@@ -68,15 +79,13 @@ const RouteConfig& Router::find_best_route(const std::string& request_path) cons
6879// This seems to be valid in some cases but we ignore it right now.
6980static bool is_cgi_request (const HttpRequest& request, const RouteConfig& route)
7081{
71- LOG (DEBUG) << " shared.ext" << route.shared .cgi .extension ;
7282 if (route.shared .cgi .extension .empty ())
7383 return false ;
7484
7585 size_t dot = request.path .find_last_of (" ." );
7686 if (dot == std::string::npos)
7787 return false ;
78- std::string ext = request.path .substr (dot);
79- LOG (DEBUG) << " ext = " << ext;
88+ std::string ext = request.path .substr (dot);
8089 return request.path .substr (dot) == route.shared .cgi .extension ;
8190}
8291
0 commit comments