EinMByte 9 years ago
parent
commit
3922a203b7
  1. 49
      HTTPServer.cpp

49
HTTPServer.cpp

@ -1,6 +1,8 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include <ctime>
#include <iomanip>
#include "util/base64.h" #include "util/base64.h"
#include "util/Log.h" #include "util/Log.h"
#include "tunnel/Tunnel.h" #include "tunnel/Tunnel.h"
@ -487,21 +489,24 @@ namespace util
std::vector<boost::asio::const_buffer> buffers; std::vector<boost::asio::const_buffer> buffers;
if (headers.size () > 0) if (headers.size () > 0)
{ {
buffers.push_back(boost::asio::buffer("HTTP/1.1 ", 9));
buffers.push_back(boost::asio::buffer(boost::lexical_cast<std::string>(status), 3));
buffers.push_back(boost::asio::buffer(" ", 1));
std::string status_string = "WTF";
switch (status) switch (status)
{ {
case 105: buffers.push_back(boost::asio::buffer("HTTP/1.1 105 Name Not Resolved\r\n")); break; case 105: status_string = "Name Not Resolved"; break;
case 200: buffers.push_back(boost::asio::buffer("HTTP/1.1 200 OK\r\n")); break; case 200: status_string = "OK"; break;
case 400: buffers.push_back(boost::asio::buffer("HTTP/1.1 400 Bad Request\r\n")); break; case 400: status_string = "Bad Request"; break;
case 404: buffers.push_back(boost::asio::buffer("HTTP/1.1 404 Not Found\r\n")); break; case 404: status_string = "Not Found"; break;
case 408: buffers.push_back(boost::asio::buffer("HTTP/1.1 408 Request Timeout\r\n")); break; case 408: status_string = "Request Timeout"; break;
case 500: buffers.push_back(boost::asio::buffer("HTTP/1.1 500 Internal Server Error\r\n")); break; case 500: status_string = "Internal Server Error"; break;
case 502: buffers.push_back(boost::asio::buffer("HTTP/1.1 502 Bad Gateway\r\n")); break; case 502: status_string = "Bad Gateway"; break;
case 503: buffers.push_back(boost::asio::buffer("HTTP/1.1 503 Not Implemented\r\n")); break; case 503: status_string = "Not Implemented"; break;
case 504: buffers.push_back(boost::asio::buffer("HTTP/1.1 504 Gateway Timeout\r\n")); break; case 504: status_string = "Gateway Timeout"; break;
default: }
buffers.push_back(boost::asio::buffer("HTTP/1.1 200 OK\r\n")); 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) for (std::size_t i = 0; i < headers.size(); ++i)
{ {
header& h = headers[i]; header& h = headers[i];
@ -913,12 +918,18 @@ namespace util
void HTTPConnection::SendReply (const std::string& content, int status) void HTTPConnection::SendReply (const std::string& content, int status)
{ {
m_Reply.content = content; m_Reply.content = content;
m_Reply.headers.resize(2); m_Reply.headers.resize(3);
m_Reply.headers[0].name = "Content-Length"; // we need the date header to be complaint with http 1.1
m_Reply.headers[0].value = boost::lexical_cast<std::string>(m_Reply.content.size()); std::time_t time_now = std::time(nullptr);
m_Reply.headers[1].name = "Content-Type"; char time_buff[128];
m_Reply.headers[1].value = "text/html"; 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<std::string>(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), boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status),
std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1)); std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1));
} }

Loading…
Cancel
Save