Skip to content

Commit 8b0eb99

Browse files
committed
Added Date header to all responses, and Connection:close to final responses
1 parent 398175d commit 8b0eb99

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

HTTPServer.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,22 @@ HTTPResponse* HTTPServer::handleHead(Client *cl, HTTPRequest *req) {
355355
* @param disconnect Should the server disconnect the client after sending (Optional, default = false)
356356
*/
357357
void HTTPServer::sendResponse(Client* cl, HTTPResponse* res, bool disconnect) {
358+
// Time stamp the response with the Date header
359+
string tstr;
360+
char tbuf[36];
361+
time_t rawtime;
362+
struct tm* ptm;
363+
time(&rawtime);
364+
ptm = gmtime(&rawtime);
365+
// Ex: Fri, 31 Dec 1999 23:59:59 GMT
366+
strftime(tbuf, 36, "%a, %d %b %Y %H:%M:%S GMT", ptm);
367+
tstr = tbuf;
368+
res->addHeader("Date", tstr);
369+
370+
// Include a Connection: close header if this is the final response sent by the server
371+
if(disconnect)
372+
res->addHeader("Connection", "close");
373+
358374
// Get raw data by creating the response (pData will be cleaned up by the response obj)
359375
byte* pData = res->create();
360376

@@ -378,6 +394,8 @@ void HTTPServer::sendResponse(Client* cl, HTTPResponse* res, bool disconnect) {
378394
bytesLeft -= n;
379395
}
380396

397+
cout << "[" << cl->getClientIP() << "] was sent " << totalSent << " bytes" << endl;
398+
381399
if(disconnect)
382400
disconnectClient(cl);
383401
}

HTTPServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <map>
55
#include <string>
66

7+
#include <time.h>
78
#include <unistd.h>
89
#include <sys/types.h>
910
#include <sys/socket.h>

0 commit comments

Comments
 (0)