@@ -26,7 +26,7 @@ int Router::prefix_length(const std::string& req_location, const std::string& lo
2626 return 0 ;
2727 if (req_location.size () == location.size ())
2828 return location.size ();
29- if (req_location[location.size ()] == ' /' )
29+ if (req_location. size () > location. size () && req_location [location.size ()] == ' /' )
3030 return location.size ();
3131 return 0 ;
3232}
@@ -40,6 +40,7 @@ const Location* Router::routing(const std::string& req_location)
4040 for (size_t i = 0 ; i < config_.locations .size (); i++) {
4141 int len_match = prefix_length (req_location, config_.locations [i].path );
4242 if (len_match > longest_match) {
43+ longest_match = len_match;
4344 best_match = &config_.locations [i];
4445 }
4546 }
@@ -61,34 +62,41 @@ bool Router::is_valid_method(const std::string& method)
6162// TO DO (DHE) : In the config parser -> ensure that root has no trailing /
6263std::string Router::build_request_path (const HttpRequest& request, const Location* best_match)
6364{
64- std::string full_path = config_.root ;
65- std::cout << " config_.root = " << config_.root << std::endl;
65+ std::string root = config_.root ;
66+ // add trailing / to root if required
67+ if (root[root.size () - 1 ] != ' /' )
68+ root += ' /' ;
69+
6670 std::string folder = static_cast <std::string>(best_match->path );
67- std::string file =
68- request.path .substr (best_match->path .size (), request.path .size () - best_match->path .size ());
69- std::cout << " file = " << file << std::endl;
70- if (full_path[full_path.size () - 1 ] != ' /' )
71- full_path = full_path + " /" ; // root/
72- if (folder[0 ] == ' /' )
71+ // remove leading / from folder if required
72+ if (folder == " /" )
73+ folder.clear ();
74+
75+ if (!folder.empty () && folder[0 ] == ' /' )
7376 folder.erase (0 , 1 );
74- if (folder[folder.size () - 1 ] != ' /' )
75- folder = folder + " /" ;
76- if (file[0 ] == ' /' )
77- file.erase (0 , 1 );
78- LOG (DEBUG) << " root = " << full_path;
79- LOG (DEBUG) << " folder = " << folder;
80- LOG (DEBUG) << " file = " << file;
8177
82- if (!folder.empty () && folder != " /" ) {
83- LOG (DEBUG) << " full_path = full_path + folder + file" ;
84- full_path = full_path + folder + file;
78+ if (!folder.empty () && folder[folder.size () - 1 ] != ' /' )
79+ folder += ' /' ;
80+
81+ std::string file;
82+ if (best_match->path == " /" || best_match->path .empty ()) {
83+ file = request.path ;
8584 }
8685 else {
87- LOG (DEBUG) << " full_path = full_path + file " ;
88- full_path = full_path + file ;
86+ size_t prefix = best_match-> path . size () ;
87+ file = request. path . substr (prefix) ;
8988 }
90- if (full_path[full_path.size () - 1 ] == ' /' )
91- full_path = full_path + " index.html" ;
89+ if (!file.empty () && file[0 ] == ' /' )
90+ file.erase (0 , 1 );
91+ // LOG(DEBUG) << "root = " << root;
92+ // LOG(DEBUG) << "folder = " << folder;
93+ // LOG(DEBUG) << "file = " << file;
94+ std::string full_path = root;
95+ if (!folder.empty ())
96+ full_path += folder;
97+ full_path += file;
98+ if (!full_path.empty () && full_path[full_path.size () - 1 ] == ' /' )
99+ full_path += " index.html" ;
92100 return (full_path);
93101}
94102
0 commit comments