@@ -466,41 +466,11 @@ void HTTPServer::handleRequest(Client *cl, HTTPRequest* req) {
466466 }
467467 std::cout << std::endl;*/
468468
469- // Determine the appropriate vhost
470- ResourceHost* resHost = NULL ;
471- std::string host = " " ;
472-
473- // Retrieve the host specified in the request (Required for HTTP/1.1 compliance)
474- if (req->getVersion ().compare (HTTP_VERSION_11 ) == 0 ) {
475- host = req->getHeaderValue (" Host" );
476-
477- // All vhosts have the port appended, so need to append it to the host if it doesnt exist
478- if (host.find (" :" ) == std::string::npos) {
479- host.append (" :" + std::to_string (listenPort));
480- }
481-
482- std::unordered_map<std::string, ResourceHost*>::const_iterator it = vhosts.find (host);
483-
484- if (it != vhosts.end ())
485- resHost = it->second ;
486- } else {
487- // Temporary: HTTP/1.0 are given the first ResouceHost in the hostList
488- // TODO: Allow admin to specify a 'default resource host'
489- if (hostList.size () > 0 )
490- resHost = hostList[0 ];
491- }
492-
493- // ResourceHost couldnt be determined or the Host specified by the client was invalid
494- if (resHost == NULL ) {
495- sendStatusResponse (cl, Status (BAD_REQUEST ), " Invalid/No Host specified: " + host);
496- return ;
497- }
498-
499469 // Send the request to the correct handler function
500470 switch (req->getMethod ()) {
501471 case Method (HEAD ):
502472 case Method (GET ):
503- handleGet (cl, req, resHost );
473+ handleGet (cl, req);
504474 break ;
505475 case Method (OPTIONS ):
506476 handleOptions (cl, req);
@@ -521,12 +491,21 @@ void HTTPServer::handleRequest(Client *cl, HTTPRequest* req) {
521491 *
522492 * @param cl Client requesting the resource
523493 * @param req State of the request
524- * @param resHost Resource host to service the request
525494 */
526- void HTTPServer::handleGet (Client* cl, HTTPRequest* req, ResourceHost* resHost) {
495+ void HTTPServer::handleGet (Client* cl, HTTPRequest* req) {
496+ std::string uri;
497+ Resource* r = NULL ;
498+ ResourceHost* resHost = this ->getResourceHostForRequest (req);
499+
500+ // ResourceHost couldnt be determined or the Host specified by the client was invalid
501+ if (resHost == NULL ) {
502+ sendStatusResponse (cl, Status (BAD_REQUEST ), " Invalid/No Host specified" );
503+ return ;
504+ }
505+
527506 // Check if the requested resource exists
528- std::string uri = req->getRequestUri ();
529- Resource* r = resHost->getResource (uri);
507+ uri = req->getRequestUri ();
508+ r = resHost->getResource (uri);
530509
531510 if (r != NULL ) { // Exists
532511 std::cout << " [" << cl->getClientIP () << " ] " << " Sending file: " << uri << std::endl;
@@ -676,4 +655,36 @@ void HTTPServer::sendResponse(Client* cl, HTTPResponse* resp, bool disconnect) {
676655 cl->addToSendQueue (new SendQueueItem (pData, resp->size (), disconnect));
677656}
678657
658+ /* *
659+ * Get Resource Host
660+ * Retrieve the appropriate ResourceHost instance based on the requested path
661+ *
662+ * @param req State of the request
663+ */
664+ ResourceHost* HTTPServer::getResourceHostForRequest (HTTPRequest* req) {
665+ // Determine the appropriate vhost
666+ ResourceHost* resHost = NULL ;
667+ std::string host = " " ;
668+
669+ // Retrieve the host specified in the request (Required for HTTP/1.1 compliance)
670+ if (req->getVersion ().compare (HTTP_VERSION_11 ) == 0 ) {
671+ host = req->getHeaderValue (" Host" );
672+
673+ // All vhosts have the port appended, so need to append it to the host if it doesnt exist
674+ if (host.find (" :" ) == std::string::npos) {
675+ host.append (" :" + std::to_string (listenPort));
676+ }
679677
678+ std::unordered_map<std::string, ResourceHost*>::const_iterator it = vhosts.find (host);
679+
680+ if (it != vhosts.end ())
681+ resHost = it->second ;
682+ } else {
683+ // Temporary: HTTP/1.0 are given the first ResouceHost in the hostList
684+ // TODO: Allow admin to specify a 'default resource host'
685+ if (hostList.size () > 0 )
686+ resHost = hostList[0 ];
687+ }
688+
689+ return resHost;
690+ }
0 commit comments