@@ -127,11 +127,8 @@ void HTTPServer::stop() {
127127 */
128128void HTTPServer::closeSockets () {
129129 // Close all open connections and delete Client's from memory
130- std::unordered_map<int , Client*>::const_iterator it;
131- for (it = clientMap.begin (); it != clientMap.end (); ++it) {
132- Client *cl = it->second ;
133- disconnectClient (cl, false );
134- }
130+ for (auto & x : clientMap)
131+ disconnectClient (x.second , false );
135132
136133 // Clear the map
137134 clientMap.clear ();
@@ -319,13 +316,22 @@ void HTTPServer::handleRequest(Client *cl, HTTPRequest* req) {
319316 return ;
320317 }
321318
319+ // Retrieve the host specified in the request (Required for HTTP/1.1 compliance)
320+ std::string host = req->getHeaderValue (" Host" );
321+ std::unordered_map<std::string, ResourceHost*>::const_iterator it = vhosts.find (host);
322+ ResourceHost* resHost = it->second ;
323+
324+ // Invalid Host specified by client:
325+ if (it == vhosts.end () || resHost == NULL ) {
326+ sendStatusResponse (cl, Status (BAD_REQUEST), " Invalid/No Host specified: " + host);
327+ return ;
328+ }
329+
322330 // Send the request to the correct handler function
323331 switch (req->getMethod ()) {
324332 case Method (HEAD):
325- handleHead (cl, req);
326- break ;
327333 case Method (GET):
328- handleGet (cl, req);
334+ handleGet (cl, req, resHost );
329335 break ;
330336 case Method (OPTIONS):
331337 handleOptions (cl, req);
@@ -341,75 +347,38 @@ void HTTPServer::handleRequest(Client *cl, HTTPRequest* req) {
341347}
342348
343349/* *
344- * Handle Get
345- * Process a GET request to provide the client with an appropriate response
350+ * Handle Get or Head
351+ * Process a GET or HEAD request to provide the client with an appropriate response
346352 *
347353 * @param cl Client requesting the resource
348354 * @param req State of the request
355+ * @param resHost Resource host to service the request
349356 */
350- void HTTPServer::handleGet (Client * cl, HTTPRequest * req) {
357+ void HTTPServer::handleGet (Client* cl, HTTPRequest* req, ResourceHost* resHost ) {
351358 /* cout << "GET for: " << req->getRequestUri() << endl;
352359 cout << "Headers:" << endl;
353360 for(int i = 0; i < req->getNumHeaders(); i++) {
354361 cout << req->getHeaderStr(i) << endl;
355362 }
356363 cout << endl;*/
357364
358- // Retrieve the host specified in the request
359- std::string host = req->getHeaderValue (" Host" );
360- std::unordered_map<std::string, ResourceHost*>::const_iterator it = vhosts.find (host);
361- ResourceHost* resHost = it->second ;
362-
363- // Invalid Host specified by client:
364- if (it == vhosts.end () || resHost == NULL ) {
365- sendStatusResponse (cl, Status (BAD_REQUEST), " Invalid/No Host specified: " + host);
366- return ;
367- }
368-
369- // Check if the requested resource exists
370- std::string uri = req->getRequestUri ();
371- Resource* r = resHost->getResource (uri);
372- if (r != NULL ) { // Exists
373- HTTPResponse* res = new HTTPResponse ();
374- res->setStatus (Status (OK));
375- res->addHeader (" Content-Type" , " text/html" );
376- res->addHeader (" Content-Length" , r->getSize ());
377- res->setData (r->getData (), r->getSize ());
378- sendResponse (cl, res);
379- delete res;
380- } else { // Not found
381- sendStatusResponse (cl, Status (NOT_FOUND));
382- }
383- }
384-
385- /* *
386- * Handle Head
387- * Process a HEAD request
388- * HEAD: Return the corresponding headers for a resource, but not the acutal resource itself (in the body)
389- *
390- * @param cl Client requesting the resource
391- * @param req State of the request
392- */
393- void HTTPServer::handleHead (Client *cl, HTTPRequest *req) {
394- // Retrieve the host specified in the request
395- std::string host = req->getHeaderValue (" Host" );
396- ResourceHost* resHost = vhosts.find (host)->second ;
397- // Invalid Host specified by client:
398- if (resHost == NULL ) {
399- sendStatusResponse (cl, Status (BAD_REQUEST), " Invalid/No Host specified: " + host);
400- return ;
401- }
402-
403365 // Check if the requested resource exists
404366 std::string uri = req->getRequestUri ();
405367 Resource* r = resHost->getResource (uri);
406368 if (r != NULL ) { // Exists
407- // Only include headers associated with the file. NEVER contains a body
408369 HTTPResponse* res = new HTTPResponse ();
409370 res->setStatus (Status (OK));
410371 res->addHeader (" Content-Type" , " text/html" );
411372 res->addHeader (" Content-Length" , r->getSize ());
412- sendResponse (cl, res);
373+
374+ // Only send a message body if it's a GET request. Never send a body for HEAD
375+ if (req->getMethod () == Method (GET))
376+ res->setData (r->getData (), r->getSize ());
377+
378+ // Check if the client prefers to close the connection
379+ bool dc = req->getHeaderValue (" Connection" ).compare (" close" ) == 0 ;
380+
381+ sendResponse (cl, res, dc);
413382 delete res;
414383 } else { // Not found
415384 sendStatusResponse (cl, Status (NOT_FOUND));
0 commit comments