Browse Source

* HTTPServer.{cpp,h}: throw away request/reply/url, use new impl

pull/489/head
hagen 9 years ago
parent
commit
b1c85dcb74
  1. 65
      HTTPServer.cpp
  2. 30
      HTTPServer.h

65
HTTPServer.cpp

@ -15,6 +15,7 @@
#include "TransitTunnel.h" #include "TransitTunnel.h"
#include "Transports.h" #include "Transports.h"
#include "NetDb.h" #include "NetDb.h"
#include "HTTP.h"
#include "LeaseSet.h" #include "LeaseSet.h"
#include "I2PEndian.h" #include "I2PEndian.h"
#include "Streaming.h" #include "Streaming.h"
@ -209,42 +210,6 @@ namespace http {
const char HTTP_COMMAND_JUMPSERVICES[] = "jumpservices="; const char HTTP_COMMAND_JUMPSERVICES[] = "jumpservices=";
const char HTTP_PARAM_ADDRESS[] = "address"; 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 () void HTTPConnection::Terminate ()
{ {
if (!m_Stream) return; if (!m_Stream) return;
@ -895,23 +860,23 @@ namespace http {
void HTTPConnection::SendReply (const std::string& content, int status) 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); std::time_t time_now = std::time(nullptr);
char time_buff[128]; char time_buff[128];
if (std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now))) std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now));
{ HTTPRes reply;
m_Reply.headers[0].name = "Date"; reply.code = status;
m_Reply.headers[0].value = std::string(time_buff); reply.status = HTTPCodeToStatus(status);
m_Reply.headers[1].name = "Content-Length"; reply.headers.insert(std::pair<std::string, std::string>("Date", time_buff));
m_Reply.headers[1].value = std::to_string(m_Reply.content.size()); reply.headers.insert(std::pair<std::string, std::string>("Content-Type", "text/html"));
m_Reply.headers[2].name = "Content-Type"; reply.headers.insert(std::pair<std::string, std::string>("Content-Length", std::to_string(content.size())));
m_Reply.headers[2].value = "text/html";
} std::string res = reply.to_string();
std::string res = m_Reply.to_string(status); std::vector<boost::asio::const_buffer> 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)); std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1));
} }

30
HTTPServer.h

@ -7,34 +7,9 @@ namespace http {
extern const char *itoopieFavicon; extern const char *itoopieFavicon;
const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192; const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192;
const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds
class HTTPConnection: public std::enable_shared_from_this<HTTPConnection>
{
protected:
struct header class HTTPConnection: public std::enable_shared_from_this<HTTPConnection>
{
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<header> headers;
};
struct reply
{ {
std::vector<header> headers;
std::string status;
std::string to_string (int code);
};
public: public:
HTTPConnection (std::shared_ptr<boost::asio::ip::tcp::socket> socket): HTTPConnection (std::shared_ptr<boost::asio::ip::tcp::socket> socket):
@ -71,7 +46,6 @@ namespace http {
std::string ExtractAddress (); std::string ExtractAddress ();
void ExtractParams (const std::string& str, std::map<std::string, std::string>& params); void ExtractParams (const std::string& str, std::map<std::string, std::string>& params);
protected: protected:
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket; std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
@ -79,8 +53,6 @@ namespace http {
std::shared_ptr<i2p::stream::Stream> m_Stream; std::shared_ptr<i2p::stream::Stream> m_Stream;
char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1]; char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1];
size_t m_BufferLen; size_t m_BufferLen;
request m_Request;
reply m_Reply;
protected: protected:

Loading…
Cancel
Save