Skip to content

Commit 73c7b5d

Browse files
committed
Proper Connection: close header support. Commented out checks for HTTP 1.1
1 parent 503ce80 commit 73c7b5d

File tree

11 files changed

+24
-21
lines changed

11 files changed

+24
-21
lines changed

src/Client.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
Client.cpp
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
*/
1818

1919
#include "Client.h"
20-
#include "HTTPServer.h"
2120

2221
Client::Client(SOCKET fd, sockaddr_in addr) {
2322
socketDesc = fd;

src/Client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
Client.h
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

src/HTTPRequest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ bool HTTPRequest::parse() {
133133
}
134134

135135
// Validate the HTTP version. If there is a mismatch, discontinue parsing
136-
if(strcmp(version.c_str(), HTTP_VERSION) != 0) {
136+
/*if(strcmp(version.c_str(), HTTP_VERSION) != 0) {
137137
parseErrorStr = "Supported HTTP version does not match";
138138
return false;
139-
}
139+
}*/
140140

141141
// Parse and populate the headers map using the parseHeaders helper
142142
parseHeaders();

src/HTTPResponse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ bool HTTPResponse::parse() {
137137
reason = getLine(); // Pull till \r\n termination
138138

139139
// Validate the HTTP version. If there is a mismatch, discontinue parsing
140-
if(strcmp(version.c_str(), HTTP_VERSION) != 0) {
140+
/*if(strcmp(version.c_str(), HTTP_VERSION) != 0) {
141141
parseErrorStr = "Supported HTTP version does not match";
142142
return false;
143-
}
143+
}*/
144144

145145
// Parse and populate the headers map using the parseHeaders helper
146146
parseHeaders();

src/HTTPServer.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
HTTPServer.cpp
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -394,11 +394,11 @@ void HTTPServer::handleRequest(Client *cl, HTTPRequest* req) {
394394
}
395395

396396
std::cout << "[" << cl->getClientIP() << "] " << req->methodIntToStr(req->getMethod()) << " " << req->getRequestUri() << std::endl;
397-
/*cout << "Headers:" << endl;
397+
/*std::cout << "Headers:" << std::endl;
398398
for(int i = 0; i < req->getNumHeaders(); i++) {
399-
cout << req->getHeaderStr(i) << endl;
399+
std::cout << req->getHeaderStr(i) << std::endl;
400400
}
401-
cout << endl;*/
401+
std::cout << std::endl;*/
402402

403403
// Retrieve the host specified in the request (Required for HTTP/1.1 compliance)
404404
std::string host = req->getHeaderValue("Host");
@@ -453,9 +453,13 @@ void HTTPServer::handleGet(Client* cl, HTTPRequest* req, ResourceHost* resHost)
453453
// Only send a message body if it's a GET request. Never send a body for HEAD
454454
if(req->getMethod() == Method(GET))
455455
resp->setData(r->getData(), r->getSize());
456-
457-
// Check if the client prefers to close the connection
458-
bool dc = req->getHeaderValue("Connection").compare("close") == 0;
456+
457+
bool dc = false;
458+
459+
// If Connection: close is specified, the connection should be terminated after the request is serviced
460+
std::string connection_val = req->getHeaderValue("Connection");
461+
if(connection_val.compare("close") == 0)
462+
dc = true;
459463

460464
sendResponse(cl, resp, dc);
461465
delete resp;

src/HTTPServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
HTTPServer.h
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ class HTTPServer {
6565
void readClient(Client* cl, int data_len);
6666
void writeClient(Client* cl, int avail_bytes);
6767
void sendStatusResponse(Client* cl, int status, std::string msg = "");
68-
void sendResponse(Client* cl, HTTPResponse* resp, bool disconnect = false);
68+
void sendResponse(Client* cl, HTTPResponse* resp, bool disconnect);
6969

7070
// Request handlers
7171
void handleRequest(Client* cl, HTTPRequest* req);

src/Resource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
Resource.cpp
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

src/Resource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
Resource.h
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

src/ResourceHost.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
ResourceHost.cpp
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

src/ResourceHost.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
httpserver
33
ResourceHost.h
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)