From b1c85dcb74448d8dae94ca20f863562f8ff328b9 Mon Sep 17 00:00:00 2001 From: hagen Date: Wed, 27 Apr 2016 00:00:00 +0000 Subject: [PATCH] * HTTPServer.{cpp,h}: throw away request/reply/url, use new impl --- HTTPServer.cpp | 69 +++++++++++++------------------------------------- HTTPServer.h | 30 +--------------------- 2 files changed, 18 insertions(+), 81 deletions(-) diff --git a/HTTPServer.cpp b/HTTPServer.cpp index a0a82ecb..607b0254 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -15,6 +15,7 @@ #include "TransitTunnel.h" #include "Transports.h" #include "NetDb.h" +#include "HTTP.h" #include "LeaseSet.h" #include "I2PEndian.h" #include "Streaming.h" @@ -209,42 +210,6 @@ namespace http { const char HTTP_COMMAND_JUMPSERVICES[] = "jumpservices="; const char HTTP_PARAM_ADDRESS[] = "address"; - namespace misc_strings - { - - const char name_value_separator[] = { ':', ' ' }; - const char crlf[] = { '\r', '\n' }; - - } // namespace misc_strings - - std::string HTTPConnection::reply::to_string(int code) - { - std::stringstream ss(""); - if (headers.size () > 0) - { - const char *status; - switch (code) - { - case 105: status = "Name Not Resolved"; break; - case 200: status = "OK"; break; - case 400: status = "Bad Request"; break; - case 404: status = "Not Found"; break; - case 408: status = "Request Timeout"; break; - case 500: status = "Internal Server Error"; break; - case 502: status = "Bad Gateway"; break; - case 503: status = "Not Implemented"; break; - case 504: status = "Gateway Timeout"; break; - default: status = "WTF"; - } - ss << "HTTP/1.1 " << code << "" << status << HTTP_CRLF; - for (header & h : headers) { - ss << h.name << HTTP_HEADER_KV_SEP << h.value << HTTP_CRLF; - } - ss << HTTP_CRLF; /* end of headers */ - } - return ss.str(); - } - void HTTPConnection::Terminate () { if (!m_Stream) return; @@ -895,23 +860,23 @@ namespace http { void HTTPConnection::SendReply (const std::string& content, int status) { - m_Reply.content = content; - 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 = std::to_string(m_Reply.content.size()); - m_Reply.headers[2].name = "Content-Type"; - m_Reply.headers[2].value = "text/html"; - } - std::string res = m_Reply.to_string(status); + std::time_t time_now = std::time(nullptr); + char time_buff[128]; + std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now)); + HTTPRes reply; + reply.code = status; + reply.status = HTTPCodeToStatus(status); + reply.headers.insert(std::pair("Date", time_buff)); + reply.headers.insert(std::pair("Content-Type", "text/html")); + reply.headers.insert(std::pair("Content-Length", std::to_string(content.size()))); + + std::string res = reply.to_string(); + std::vector buffers; + + buffers.push_back(boost::asio::buffer(res)); + buffers.push_back(boost::asio::buffer(content)); - boost::asio::async_write (*m_Socket, res, + boost::asio::async_write (*m_Socket, buffers, std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1)); } diff --git a/HTTPServer.h b/HTTPServer.h index e53d95f2..1dc952c6 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -7,34 +7,9 @@ namespace http { extern const char *itoopieFavicon; const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192; const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds + class HTTPConnection: public std::enable_shared_from_this { - protected: - - struct header - { - std::string name; - std::string value; - }; - - struct request - { - std::string method; - std::string uri; - std::string host; - int port; - int http_version_major; - int http_version_minor; - std::vector
headers; - }; - - struct reply - { - std::vector
headers; - std::string status; - std::string to_string (int code); - }; - public: HTTPConnection (std::shared_ptr socket): @@ -71,7 +46,6 @@ namespace http { std::string ExtractAddress (); void ExtractParams (const std::string& str, std::map& params); - protected: std::shared_ptr m_Socket; @@ -79,8 +53,6 @@ namespace http { std::shared_ptr m_Stream; char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1]; size_t m_BufferLen; - request m_Request; - reply m_Reply; protected: