Skip to content

Commit 9853da8

Browse files
committed
Do not try to retrieve ResourceHost until a valid method (GET) is parsed
1 parent e5b6ac9 commit 9853da8

3 files changed

Lines changed: 49 additions & 37 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Ramsey Kant
44
https://github.com/RamseyK/httpserver
55

6-
A high performance, single threaded, HTTP server written in C++ to serve as a kqueue socket management and HTTP protocol learning tool on BSD systems
6+
A high performance, single threaded, HTTP/1.1 server written in C++ to serve as a kqueue socket management and HTTP/1.1 protocol learning tool on BSD systems
77

88
## Features
99
* Clean, documented code

src/HTTPServer.cpp

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/HTTPServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ class HTTPServer {
7373
void disconnectClient(Client* cl, bool mapErase = true);
7474
void readClient(Client* cl, int data_len); // Client read event
7575
bool writeClient(Client* cl, int avail_bytes); // Client write event
76+
ResourceHost* getResourceHostForRequest(HTTPRequest* req);
7677

7778
// Request handling
7879
void handleRequest(Client* cl, HTTPRequest* req);
79-
void handleGet(Client* cl, HTTPRequest* req, ResourceHost* resHost);
80+
void handleGet(Client* cl, HTTPRequest* req);
8081
void handleOptions(Client* cl, HTTPRequest* req);
8182
void handleTrace(Client* cl, HTTPRequest* req);
8283

0 commit comments

Comments
 (0)