From 413d1b49606db4b90534cbfe28fa034c4dfa6278 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 11 Aug 2015 16:55:55 -0400 Subject: [PATCH] make http server http/1.1 compliant --- HTTPServer.cpp | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 2419b386..fe103431 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "util/base64.h" #include "util/Log.h" #include "tunnel/Tunnel.h" @@ -487,21 +489,24 @@ namespace util std::vector buffers; if (headers.size () > 0) { + buffers.push_back(boost::asio::buffer("HTTP/1.1 ", 9)); + buffers.push_back(boost::asio::buffer(boost::lexical_cast(status), 3)); + buffers.push_back(boost::asio::buffer(" ", 1)); + std::string status_string = "WTF"; switch (status) { - case 105: buffers.push_back(boost::asio::buffer("HTTP/1.1 105 Name Not Resolved\r\n")); break; - case 200: buffers.push_back(boost::asio::buffer("HTTP/1.1 200 OK\r\n")); break; - case 400: buffers.push_back(boost::asio::buffer("HTTP/1.1 400 Bad Request\r\n")); break; - case 404: buffers.push_back(boost::asio::buffer("HTTP/1.1 404 Not Found\r\n")); break; - case 408: buffers.push_back(boost::asio::buffer("HTTP/1.1 408 Request Timeout\r\n")); break; - case 500: buffers.push_back(boost::asio::buffer("HTTP/1.1 500 Internal Server Error\r\n")); break; - case 502: buffers.push_back(boost::asio::buffer("HTTP/1.1 502 Bad Gateway\r\n")); break; - case 503: buffers.push_back(boost::asio::buffer("HTTP/1.1 503 Not Implemented\r\n")); break; - case 504: buffers.push_back(boost::asio::buffer("HTTP/1.1 504 Gateway Timeout\r\n")); break; - default: - buffers.push_back(boost::asio::buffer("HTTP/1.1 200 OK\r\n")); + case 105: status_string = "Name Not Resolved"; break; + case 200: status_string = "OK"; break; + case 400: status_string = "Bad Request"; break; + case 404: status_string = "Not Found"; break; + case 408: status_string = "Request Timeout"; break; + case 500: status_string = "Internal Server Error"; break; + case 502: status_string = "Bad Gateway"; break; + case 503: status_string = "Not Implemented"; break; + case 504: status_string = "Gateway Timeout"; break; } - + buffers.push_back(boost::asio::buffer(status_string, status_string.size())); + buffers.push_back(boost::asio::buffer(misc_strings::crlf)); for (std::size_t i = 0; i < headers.size(); ++i) { header& h = headers[i]; @@ -1032,12 +1037,18 @@ namespace util void HTTPConnection::SendReply (const std::string& content, int status) { m_Reply.content = content; - m_Reply.headers.resize(2); - m_Reply.headers[0].name = "Content-Length"; - m_Reply.headers[0].value = boost::lexical_cast(m_Reply.content.size()); - m_Reply.headers[1].name = "Content-Type"; - m_Reply.headers[1].value = "text/html"; - + m_Reply.headers.resize(3); + // we need the date header to be complaint with http 1.1 + std::time_t time_now = std::time(nullptr); + char time_buff[128]; + if ( std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now)) ) { + m_Reply.headers[0].name = "Date"; + m_Reply.headers[0].value = std::string(time_buff); + m_Reply.headers[1].name = "Content-Length"; + m_Reply.headers[1].value = boost::lexical_cast(m_Reply.content.size()); + m_Reply.headers[2].name = "Content-Type"; + m_Reply.headers[2].value = "text/html"; + } boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status), std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1)); }