Skip to content

Commit 75c0137

Browse files
committed
Added Mime Type lookup based on static MimeTypes.inc file which is embedded at compile time. Improved Makefile to use new template that requires no modifications when adding new classes. misc bug fixes
1 parent 036036b commit 75c0137

File tree

9 files changed

+566
-142
lines changed

9 files changed

+566
-142
lines changed

Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Makefile for httpserver
2+
# (C) Ramsey Kant 2011-2012
3+
4+
CC := clang++
5+
SRCDIR := src
6+
BINDIR := bin
7+
BUILDDIR := build
8+
TARGET := httpserver
9+
10+
# Debug Flags
11+
DEBUGFLAGS := -g -O0 -Wall
12+
# Production Flags
13+
PRODFLAGS := -Wall -O3
14+
# Active Flags
15+
CFLAGS := -std=c++11 -stdlib=libc++ -Iinclude/ $(DEBUGFLAGS)
16+
LINK := -stdlib=libc++ $(DEBUGFLAGS)
17+
18+
SRCEXT := cpp
19+
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
20+
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
21+
22+
$(TARGET): $(OBJECTS)
23+
@echo " Linking..."; $(CC) $(LINK) $^ -o $(BINDIR)/$(TARGET)
24+
25+
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
26+
@mkdir -p $(BUILDDIR)
27+
@echo " CC $<"; $(CC) $(CFLAGS) -c -o $@ $<
28+
29+
clean:
30+
@echo " Cleaning..."; $(RM) -r $(BUILDDIR) $(BINDIR)/$(TARGET)
31+
32+
.PHONY: clean

src/HTTPServer.cpp

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,8 @@ void HTTPServer::stop() {
127127
*/
128128
void 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));

src/HTTPServer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class HTTPServer {
6868

6969
// Request handlers
7070
void handleRequest(Client* cl, HTTPRequest* req);
71-
void handleGet(Client* cl, HTTPRequest* req);
72-
void handleHead(Client* cl, HTTPRequest* req);
71+
void handleGet(Client* cl, HTTPRequest* req, ResourceHost* resHost);
7372
void handleOptions(Client* cl, HTTPRequest* req);
7473
void handleTrace(Client* cl, HTTPRequest* req);
7574

src/Makefile

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)