|
|
|
@ -15,6 +15,7 @@
@@ -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 {
@@ -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 {
@@ -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<std::string, std::string>("Date", time_buff)); |
|
|
|
|
reply.headers.insert(std::pair<std::string, std::string>("Content-Type", "text/html")); |
|
|
|
|
reply.headers.insert(std::pair<std::string, std::string>("Content-Length", std::to_string(content.size()))); |
|
|
|
|
|
|
|
|
|
std::string res = reply.to_string(); |
|
|
|
|
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)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|