2013-12-10 17:03:22 +04:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-01-13 06:41:25 +04:00
|
|
|
#include "base64.h"
|
2014-01-15 04:56:34 +04:00
|
|
|
#include "Log.h"
|
2013-12-10 17:03:22 +04:00
|
|
|
#include "Tunnel.h"
|
|
|
|
#include "TransitTunnel.h"
|
|
|
|
#include "Transports.h"
|
2014-01-13 06:41:25 +04:00
|
|
|
#include "NetDb.h"
|
2013-12-10 17:03:22 +04:00
|
|
|
#include "HTTPServer.h"
|
|
|
|
|
2014-08-17 15:20:57 +04:00
|
|
|
// For image and info
|
|
|
|
#include "version.h"
|
|
|
|
|
2013-12-10 17:03:22 +04:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2014-03-30 16:40:53 +04:00
|
|
|
|
2014-08-17 15:20:57 +04:00
|
|
|
const std::string HTTPConnection::itoopieImage = ICTOOPIE_128;
|
2014-07-16 20:41:40 +04:00
|
|
|
|
|
|
|
namespace misc_strings
|
2013-12-10 17:03:22 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
const char name_value_separator[] = { ':', ' ' };
|
|
|
|
const char crlf[] = { '\r', '\n' };
|
|
|
|
|
|
|
|
} // namespace misc_strings
|
|
|
|
|
2014-04-18 01:53:48 +04:00
|
|
|
std::vector<boost::asio::const_buffer> HTTPConnection::reply::to_buffers(int status)
|
2013-12-10 17:03:22 +04:00
|
|
|
{
|
|
|
|
std::vector<boost::asio::const_buffer> buffers;
|
2014-01-19 02:43:04 +04:00
|
|
|
if (headers.size () > 0)
|
2014-04-18 01:53:48 +04:00
|
|
|
{
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case 105: buffers.push_back(boost::asio::buffer("HTTP/1.0 105 Name Not Resolved\r\n")); break;
|
|
|
|
case 200: buffers.push_back(boost::asio::buffer("HTTP/1.0 200 OK\r\n")); break;
|
|
|
|
case 400: buffers.push_back(boost::asio::buffer("HTTP/1.0 400 Bad Request\r\n")); break;
|
|
|
|
case 404: buffers.push_back(boost::asio::buffer("HTTP/1.0 404 Not Found\r\n")); break;
|
|
|
|
case 408: buffers.push_back(boost::asio::buffer("HTTP/1.0 408 Request Timeout\r\n")); break;
|
|
|
|
case 500: buffers.push_back(boost::asio::buffer("HTTP/1.0 500 Internal Server Error\r\n")); break;
|
|
|
|
case 502: buffers.push_back(boost::asio::buffer("HTTP/1.0 502 Bad Gateway\r\n")); break;
|
|
|
|
case 503: buffers.push_back(boost::asio::buffer("HTTP/1.0 503 Not Implemented\r\n")); break;
|
2014-04-18 02:54:15 +04:00
|
|
|
case 504: buffers.push_back(boost::asio::buffer("HTTP/1.0 504 Gateway Timeout\r\n")); break;
|
2014-04-18 01:53:48 +04:00
|
|
|
default:
|
|
|
|
buffers.push_back(boost::asio::buffer("HTTP/1.0 200 OK\r\n"));
|
|
|
|
}
|
|
|
|
|
2014-01-19 02:43:04 +04:00
|
|
|
for (std::size_t i = 0; i < headers.size(); ++i)
|
|
|
|
{
|
|
|
|
header& h = headers[i];
|
|
|
|
buffers.push_back(boost::asio::buffer(h.name));
|
|
|
|
buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
|
|
|
|
buffers.push_back(boost::asio::buffer(h.value));
|
|
|
|
buffers.push_back(boost::asio::buffer(misc_strings::crlf));
|
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
buffers.push_back(boost::asio::buffer(misc_strings::crlf));
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
buffers.push_back(boost::asio::buffer(content));
|
|
|
|
return buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::Terminate ()
|
|
|
|
{
|
2014-03-27 23:42:23 +04:00
|
|
|
if (m_Stream)
|
2014-07-16 20:41:40 +04:00
|
|
|
{
|
2014-03-27 23:42:23 +04:00
|
|
|
m_Stream->Close ();
|
2014-07-16 20:41:40 +04:00
|
|
|
DeleteStream (m_Stream);
|
2014-03-27 23:42:23 +04:00
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
m_Socket->close ();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::Receive ()
|
|
|
|
{
|
2014-01-15 04:56:34 +04:00
|
|
|
m_Socket->async_read_some (boost::asio::buffer (m_Buffer, 8192),
|
2013-12-10 17:03:22 +04:00
|
|
|
boost::bind(&HTTPConnection::HandleReceive, this,
|
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
2014-01-15 04:56:34 +04:00
|
|
|
m_Buffer[bytes_transferred] = 0;
|
2014-03-30 02:16:23 +04:00
|
|
|
RunRequest();
|
2013-12-10 17:03:22 +04:00
|
|
|
}
|
|
|
|
else if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
|
2014-03-30 02:16:23 +04:00
|
|
|
void HTTPConnection::RunRequest ()
|
|
|
|
{
|
|
|
|
auto address = ExtractAddress ();
|
|
|
|
if (address.length () > 1) // not just '/'
|
|
|
|
{
|
|
|
|
std::string uri ("/"), b32;
|
|
|
|
size_t pos = address.find ('/', 1);
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
b32 = address.substr (1); // excluding leading '/' to end of line
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b32 = address.substr (1, pos - 1); // excluding leading '/' to next '/'
|
|
|
|
uri = address.substr (pos); // rest of line
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2014-03-30 02:16:23 +04:00
|
|
|
|
2014-07-16 20:41:40 +04:00
|
|
|
HandleDestinationRequest (b32, uri);
|
|
|
|
}
|
|
|
|
else
|
2014-03-30 02:16:23 +04:00
|
|
|
HandleRequest ();
|
|
|
|
}
|
|
|
|
|
2014-01-15 04:56:34 +04:00
|
|
|
std::string HTTPConnection::ExtractAddress ()
|
|
|
|
{
|
|
|
|
char * get = strstr (m_Buffer, "GET");
|
|
|
|
if (get)
|
|
|
|
{
|
|
|
|
char * http = strstr (get, "HTTP");
|
|
|
|
if (http)
|
2014-01-20 04:19:09 +04:00
|
|
|
return std::string (get + 4, http - get - 5);
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2014-01-15 04:56:34 +04:00
|
|
|
return "";
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
|
|
|
|
2014-03-27 23:42:23 +04:00
|
|
|
void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::HandleWrite (const boost::system::error_code& ecode)
|
2013-12-10 17:03:22 +04:00
|
|
|
{
|
2014-03-27 23:42:23 +04:00
|
|
|
if (ecode || (m_Stream && !m_Stream->IsOpen ()))
|
2014-03-27 21:24:23 +04:00
|
|
|
Terminate ();
|
2014-03-27 23:42:23 +04:00
|
|
|
else // data keeps coming
|
|
|
|
AsyncStreamReceive ();
|
2013-12-10 17:03:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::HandleRequest ()
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
2014-08-17 15:20:57 +04:00
|
|
|
// Html5 head start
|
|
|
|
s << "<!DOCTYPE html>\n<html lang=\"en\">"; // TODO: Add support for locale.
|
|
|
|
s << "<head><meta charset=\"utf-8\" />"; // TODO: Find something to parse html/template system. This is horrible.
|
|
|
|
s << "<link rel='shortcut icon' href='";
|
|
|
|
s << ICTOOPIE_64_FAVICON;
|
|
|
|
s << "' /><title>Purple I2Pd Webconsole</title></head>";
|
|
|
|
// Head end
|
2013-12-10 17:03:22 +04:00
|
|
|
FillContent (s);
|
|
|
|
s << "</html>";
|
2014-03-27 21:24:23 +04:00
|
|
|
SendReply (s.str ());
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
|
|
|
|
void HTTPConnection::FillContent (std::stringstream& s)
|
|
|
|
{
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<h2>Welcome to the Webconsole!</h2><br><br>";
|
|
|
|
s << "<b>Data path:</b> " << i2p::util::filesystem::GetDataDir().string() << "<br>" << "<br>";
|
|
|
|
s << "<b>Our external address:</b>" << "<br>";
|
2014-03-13 08:10:05 +04:00
|
|
|
for (auto& address : i2p::context.GetRouterInfo().GetAddresses())
|
|
|
|
{
|
2014-07-16 20:41:40 +04:00
|
|
|
switch (address.transportStyle)
|
2014-03-13 15:43:54 +04:00
|
|
|
{
|
|
|
|
case i2p::data::RouterInfo::eTransportNTCP:
|
|
|
|
s << "NTCP ";
|
2014-03-13 08:10:05 +04:00
|
|
|
break;
|
2014-03-13 15:43:54 +04:00
|
|
|
case i2p::data::RouterInfo::eTransportSSU:
|
|
|
|
s << "SSU ";
|
2014-03-13 08:10:05 +04:00
|
|
|
break;
|
2014-03-13 15:43:54 +04:00
|
|
|
default:
|
|
|
|
s << "Unknown ";
|
2014-03-13 08:10:05 +04:00
|
|
|
}
|
2014-08-17 15:20:57 +04:00
|
|
|
s << address.host.to_string() << ":" << address.port << "<br>";
|
2014-03-13 08:10:05 +04:00
|
|
|
}
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<br><b>Routers:</b> <i>" << i2p::data::netdb.GetNumRouters () << "</i> ";
|
|
|
|
s << "<b>Floodfills:</b> <i>" << i2p::data::netdb.GetNumFloodfills () << "</i> ";
|
|
|
|
s << "<b>LeaseSets:</b> <i>" << i2p::data::netdb.GetNumLeaseSets () << "</i><br>";
|
2014-07-16 20:41:40 +04:00
|
|
|
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<br><b>Tunnels</b><br>";
|
2013-12-10 17:03:22 +04:00
|
|
|
for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ())
|
2014-07-16 20:41:40 +04:00
|
|
|
{
|
2013-12-10 17:03:22 +04:00
|
|
|
it->GetTunnelConfig ()->Print (s);
|
2014-04-03 04:42:02 +04:00
|
|
|
if (it->GetTunnelPool () && !it->GetTunnelPool ()->IsExploratory ())
|
2014-03-17 00:03:20 +04:00
|
|
|
s << " " << "Pool";
|
2014-03-22 02:26:11 +04:00
|
|
|
if (it->IsFailed ())
|
|
|
|
s << " " << "Failed";
|
2014-08-17 15:20:57 +04:00
|
|
|
s << " " << (int)it->GetNumSentBytes () << "<br>";
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
|
|
|
|
for (auto it: i2p::tunnel::tunnels.GetInboundTunnels ())
|
2014-07-16 20:41:40 +04:00
|
|
|
{
|
2013-12-10 17:03:22 +04:00
|
|
|
it.second->GetTunnelConfig ()->Print (s);
|
2014-04-02 21:14:21 +04:00
|
|
|
if (it.second->GetTunnelPool () && !it.second->GetTunnelPool ()->IsExploratory ())
|
2014-03-15 04:51:51 +04:00
|
|
|
s << " " << "Pool";
|
2014-03-22 02:26:11 +04:00
|
|
|
if (it.second->IsFailed ())
|
|
|
|
s << " " << "Failed";
|
2014-08-17 15:20:57 +04:00
|
|
|
s << " " << (int)it.second->GetNumReceivedBytes () << "<br>";
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
|
|
|
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<br><b>Transit tunnels</b><br>";
|
2013-12-10 17:03:22 +04:00
|
|
|
for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ())
|
2014-07-16 20:41:40 +04:00
|
|
|
{
|
2013-12-10 17:03:22 +04:00
|
|
|
if (dynamic_cast<i2p::tunnel::TransitTunnelGateway *>(it.second))
|
|
|
|
s << it.second->GetTunnelID () << "-->";
|
|
|
|
else if (dynamic_cast<i2p::tunnel::TransitTunnelEndpoint *>(it.second))
|
|
|
|
s << "-->" << it.second->GetTunnelID ();
|
|
|
|
else
|
|
|
|
s << "-->" << it.second->GetTunnelID () << "-->";
|
2014-08-17 15:20:57 +04:00
|
|
|
s << " " << it.second->GetNumTransmittedBytes () << "<br>";
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<br><b>Transports</b><br>";
|
|
|
|
s << "NTCP<br>";
|
2013-12-10 17:03:22 +04:00
|
|
|
for (auto it: i2p::transports.GetNTCPSessions ())
|
2014-07-16 20:41:40 +04:00
|
|
|
{
|
2014-01-06 07:52:17 +04:00
|
|
|
// RouterInfo of incoming connection doesn't have address
|
|
|
|
bool outgoing = it.second->GetRemoteRouterInfo ().GetNTCPAddress ();
|
2013-12-10 17:03:22 +04:00
|
|
|
if (it.second->IsEstablished ())
|
2014-01-06 07:52:17 +04:00
|
|
|
{
|
|
|
|
if (outgoing) s << "-->";
|
2014-07-16 20:41:40 +04:00
|
|
|
s << it.second->GetRemoteRouterInfo ().GetIdentHashAbbreviation () << ": "
|
2014-01-06 07:52:17 +04:00
|
|
|
<< it.second->GetSocket ().remote_endpoint().address ().to_string ();
|
|
|
|
if (!outgoing) s << "-->";
|
2014-07-21 01:12:36 +04:00
|
|
|
s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<br>";
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 07:28:28 +04:00
|
|
|
auto ssuServer = i2p::transports.GetSSUServer ();
|
|
|
|
if (ssuServer)
|
|
|
|
{
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<br>SSU<br>";
|
2014-02-25 07:28:28 +04:00
|
|
|
for (auto it: ssuServer->GetSessions ())
|
|
|
|
{
|
2014-03-12 15:37:43 +04:00
|
|
|
// incoming connections don't have remote router
|
|
|
|
bool outgoing = it.second->GetRemoteRouter ();
|
2014-02-25 07:28:28 +04:00
|
|
|
auto endpoint = it.second->GetRemoteEndpoint ();
|
2014-03-12 15:37:43 +04:00
|
|
|
if (outgoing) s << "-->";
|
|
|
|
s << endpoint.address ().to_string () << ":" << endpoint.port ();
|
|
|
|
if (!outgoing) s << "-->";
|
2014-07-21 01:12:36 +04:00
|
|
|
s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
|
2014-08-17 15:20:57 +04:00
|
|
|
s << "<br>";
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 04:19:09 +04:00
|
|
|
s << "<p><a href=\"zmw2cyw2vj7f6obx3msmdvdepdhnw2ctc4okza2zjxlukkdfckhq\">Flibusta</a></p>";
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2014-03-17 05:18:23 +04:00
|
|
|
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri)
|
2014-07-16 20:41:40 +04:00
|
|
|
{
|
|
|
|
HandleDestinationRequest(address, "GET", "", uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& method, const std::string& data, const std::string& uri)
|
2014-01-13 06:41:25 +04:00
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
const i2p::data::LeaseSet * leaseSet = nullptr;
|
2014-03-17 05:18:23 +04:00
|
|
|
i2p::data::IdentHash destination;
|
|
|
|
std::string fullAddress;
|
2014-04-18 00:27:11 +04:00
|
|
|
if (address.find(".b32.i2p") != std::string::npos)
|
|
|
|
{
|
|
|
|
if (i2p::data::Base32ToByteStream(address.c_str(), address.length() - strlen(".b32.i2p"), (uint8_t *)destination, 32) != 32)
|
2014-03-17 05:18:23 +04:00
|
|
|
{
|
2014-04-18 00:27:11 +04:00
|
|
|
LogPrint ("Invalid Base32 address ", address);
|
2014-04-18 01:53:48 +04:00
|
|
|
SendReply ("<html>" + itoopieImage + "<br>Invalid Base32 address", 400);
|
2014-03-17 05:18:23 +04:00
|
|
|
return;
|
2014-04-18 00:27:11 +04:00
|
|
|
}
|
2014-03-17 05:18:23 +04:00
|
|
|
fullAddress = address;
|
|
|
|
}
|
|
|
|
else
|
2014-04-18 00:27:11 +04:00
|
|
|
{
|
|
|
|
if (address.find(".i2p") != std::string::npos)
|
2014-03-17 05:18:23 +04:00
|
|
|
{
|
2014-04-18 00:27:11 +04:00
|
|
|
auto addr = i2p::data::netdb.FindAddress(address);
|
|
|
|
if (!addr)
|
|
|
|
{
|
|
|
|
LogPrint ("Unknown address ", address);
|
2014-04-18 01:53:48 +04:00
|
|
|
SendReply ("<html>" + itoopieImage + "<br>Unknown address " + address + "</html>", 105);
|
2014-04-18 00:27:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
destination = *addr;
|
|
|
|
fullAddress = address;
|
2014-03-17 05:18:23 +04:00
|
|
|
}
|
2014-04-18 00:27:11 +04:00
|
|
|
else
|
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
if (address == "local")
|
2014-04-18 00:27:11 +04:00
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
// TODO: remove later
|
|
|
|
fullAddress = "local.i2p";
|
|
|
|
auto destination = i2p::stream::GetSharedLocalDestination ();
|
|
|
|
leaseSet = destination->GetLeaseSet ();
|
|
|
|
EepAccept (destination);
|
2014-04-18 00:27:11 +04:00
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
else
|
2014-08-17 15:20:57 +04:00
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
if (i2p::data::Base32ToByteStream(address.c_str(), address.length(), (uint8_t *)destination, 32) != 32)
|
|
|
|
{
|
|
|
|
LogPrint("Invalid Base32 address ", address);
|
|
|
|
SendReply("<html>" + itoopieImage + "<br>Invalid Base32 address", 400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fullAddress = address + ".b32.i2p";
|
2014-08-17 15:20:57 +04:00
|
|
|
}
|
2014-04-18 00:27:11 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-16 20:41:40 +04:00
|
|
|
|
2014-08-05 00:30:37 +04:00
|
|
|
if (!leaseSet)
|
|
|
|
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
2014-01-15 05:57:33 +04:00
|
|
|
if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
|
2014-01-13 06:41:25 +04:00
|
|
|
{
|
2014-02-15 01:10:25 +04:00
|
|
|
i2p::data::netdb.Subscribe(destination);
|
2014-01-13 06:41:25 +04:00
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(10)); // wait for 10 seconds
|
|
|
|
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
2014-01-15 05:57:33 +04:00
|
|
|
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet
|
2014-01-13 06:41:25 +04:00
|
|
|
{
|
2014-04-18 01:53:48 +04:00
|
|
|
SendReply (leaseSet ? "<html>" + itoopieImage + "<br>Leases expired</html>" : "<html>" + itoopieImage + "LeaseSet not found</html>", 504);
|
2014-01-13 06:41:25 +04:00
|
|
|
return;
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2014-03-27 23:42:23 +04:00
|
|
|
}
|
2014-07-16 20:41:40 +04:00
|
|
|
if (!m_Stream)
|
2014-03-27 23:42:23 +04:00
|
|
|
m_Stream = i2p::stream::CreateStream (*leaseSet);
|
|
|
|
if (m_Stream)
|
2014-01-13 06:41:25 +04:00
|
|
|
{
|
2014-07-16 20:41:40 +04:00
|
|
|
std::string request = method+" " + uri + " HTTP/1.1\n Host:" + fullAddress + "\r\n";
|
|
|
|
if (!strcmp(method.c_str(), "GET"))
|
|
|
|
{
|
|
|
|
// POST/PUT, apply body
|
|
|
|
request += "\r\n"+ data;
|
|
|
|
}
|
|
|
|
LogPrint("HTTP Client Request: ", request);
|
|
|
|
m_Stream->Send ((uint8_t *)request.c_str (), request.length (), 10);
|
2014-03-29 16:11:00 +04:00
|
|
|
AsyncStreamReceive ();
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 23:42:23 +04:00
|
|
|
void HTTPConnection::AsyncStreamReceive ()
|
|
|
|
{
|
|
|
|
if (m_Stream)
|
|
|
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
|
2014-07-16 20:41:40 +04:00
|
|
|
boost::bind (&HTTPConnection::HandleStreamReceive, this,
|
2014-04-23 21:44:08 +04:00
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred),
|
2014-03-29 16:11:00 +04:00
|
|
|
45); // 45 seconds timeout
|
2014-03-27 23:42:23 +04:00
|
|
|
}
|
|
|
|
|
2014-03-26 23:45:08 +04:00
|
|
|
void HTTPConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2014-03-27 21:24:23 +04:00
|
|
|
if (bytes_transferred)
|
|
|
|
{
|
2014-03-27 23:42:23 +04:00
|
|
|
boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
|
|
|
boost::bind (&HTTPConnection::HandleWrite, this, boost::asio::placeholders::error));
|
2014-03-27 21:24:23 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-21 22:28:17 +04:00
|
|
|
if (ecode == boost::asio::error::timed_out)
|
2014-04-18 01:53:48 +04:00
|
|
|
SendReply ("<html>" + itoopieImage + "<br>Not responding</html>", 504);
|
2014-08-17 15:20:57 +04:00
|
|
|
else
|
2014-03-27 23:42:23 +04:00
|
|
|
Terminate ();
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2014-03-27 21:24:23 +04:00
|
|
|
}
|
|
|
|
|
2014-04-18 01:53:48 +04:00
|
|
|
void HTTPConnection::SendReply (const std::string& content, int status)
|
2014-03-27 21:24:23 +04:00
|
|
|
{
|
|
|
|
m_Reply.content = content;
|
|
|
|
m_Reply.headers.resize(2);
|
|
|
|
m_Reply.headers[0].name = "Content-Length";
|
|
|
|
m_Reply.headers[0].value = boost::lexical_cast<std::string>(m_Reply.content.size());
|
|
|
|
m_Reply.headers[1].name = "Content-Type";
|
|
|
|
m_Reply.headers[1].value = "text/html";
|
|
|
|
|
2014-04-18 01:53:48 +04:00
|
|
|
boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status),
|
2014-07-16 20:41:40 +04:00
|
|
|
boost::bind (&HTTPConnection::HandleWriteReply, this,
|
2014-03-27 23:42:23 +04:00
|
|
|
boost::asio::placeholders::error));
|
2014-03-26 23:45:08 +04:00
|
|
|
}
|
2014-07-16 20:41:40 +04:00
|
|
|
|
|
|
|
HTTPServer::HTTPServer (int port):
|
|
|
|
m_Thread (nullptr), m_Work (m_Service),
|
2013-12-10 17:03:22 +04:00
|
|
|
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
|
|
|
|
m_NewSocket (nullptr)
|
|
|
|
{
|
2014-07-16 20:41:40 +04:00
|
|
|
|
2013-12-10 17:03:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTPServer::~HTTPServer ()
|
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::Start ()
|
|
|
|
{
|
|
|
|
m_Thread = new std::thread (std::bind (&HTTPServer::Run, this));
|
|
|
|
m_Acceptor.listen ();
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::Stop ()
|
|
|
|
{
|
|
|
|
m_Acceptor.close();
|
|
|
|
m_Service.stop ();
|
|
|
|
if (m_Thread)
|
2014-07-16 20:41:40 +04:00
|
|
|
{
|
|
|
|
m_Thread->join ();
|
2013-12-10 17:03:22 +04:00
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = nullptr;
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::Run ()
|
|
|
|
{
|
|
|
|
m_Service.run ();
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2013-12-10 17:03:22 +04:00
|
|
|
|
|
|
|
void HTTPServer::Accept ()
|
|
|
|
{
|
|
|
|
m_NewSocket = new boost::asio::ip::tcp::socket (m_Service);
|
|
|
|
m_Acceptor.async_accept (*m_NewSocket, boost::bind (&HTTPServer::HandleAccept, this,
|
|
|
|
boost::asio::placeholders::error));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::HandleAccept(const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
2014-03-30 02:16:23 +04:00
|
|
|
CreateConnection(m_NewSocket); // new HTTPConnection(m_NewSocket);
|
2013-12-10 17:03:22 +04:00
|
|
|
Accept ();
|
|
|
|
}
|
2014-07-16 20:41:40 +04:00
|
|
|
}
|
2014-03-30 02:16:23 +04:00
|
|
|
|
|
|
|
void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket)
|
|
|
|
{
|
|
|
|
new HTTPConnection (m_NewSocket);
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:30:37 +04:00
|
|
|
// eepSite. TODO: move away
|
2014-08-17 15:20:57 +04:00
|
|
|
|
2014-08-05 00:30:37 +04:00
|
|
|
void HTTPConnection::EepAccept (i2p::stream::StreamingDestination * destination)
|
|
|
|
{
|
|
|
|
if (destination)
|
|
|
|
destination->SetAcceptor (std::bind (&HTTPConnection::HandleEepAccept, this, std::placeholders::_1));
|
|
|
|
}
|
2014-08-17 15:20:57 +04:00
|
|
|
|
2014-08-05 00:30:37 +04:00
|
|
|
void HTTPConnection::HandleEepAccept (i2p::stream::Stream * stream)
|
|
|
|
{
|
|
|
|
if (stream)
|
2014-08-17 15:20:57 +04:00
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
auto conn = new EepSiteDummyConnection (stream);
|
|
|
|
conn->AsyncStreamReceive ();
|
2014-08-17 15:20:57 +04:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
|
|
|
|
void EepSiteDummyConnection::AsyncStreamReceive ()
|
|
|
|
{
|
|
|
|
if (m_Stream)
|
|
|
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
|
|
|
|
boost::bind (&EepSiteDummyConnection::HandleStreamReceive, this,
|
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred),
|
|
|
|
60); // 60 seconds timeout
|
2014-08-17 15:20:57 +04:00
|
|
|
}
|
|
|
|
|
2014-08-05 00:30:37 +04:00
|
|
|
void EepSiteDummyConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
2014-08-17 15:20:57 +04:00
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
LogPrint ("eepSite error: ", ecode.message ());
|
|
|
|
DeleteStream (m_Stream);
|
2014-08-17 15:20:57 +04:00
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
else
|
|
|
|
{
|
2014-08-11 20:10:41 +04:00
|
|
|
std::string content ("");
|
|
|
|
content += "<html>" + HTTPConnection::itoopieImage + "</html>";
|
2014-08-12 02:33:01 +04:00
|
|
|
std::string response ("HTTP/1.0 200 OK\r\n");
|
2014-08-11 20:10:41 +04:00
|
|
|
response += "Content-Length: " + boost::lexical_cast<std::string>(content.length()) + "\r\n";
|
2014-08-12 02:33:01 +04:00
|
|
|
response += "Content-Type: text/html\r\n\r\n";
|
2014-08-17 15:20:57 +04:00
|
|
|
response += content;
|
2014-08-05 00:30:37 +04:00
|
|
|
m_Stream->Send ((uint8_t *)response.c_str (), response.length (), 30);
|
2014-08-05 02:22:54 +04:00
|
|
|
m_Stream->Close ();
|
2014-08-17 15:20:57 +04:00
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
delete this;
|
2014-08-17 15:20:57 +04:00
|
|
|
}
|
|
|
|
|
2013-12-10 17:03:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|