Prepare support for POST/PUT

pull/92/head
Mikal Villa 10 years ago
parent 9ee049aa63
commit bff3d8f5c1

@ -13,7 +13,7 @@ namespace i2p
namespace util namespace util
{ {
const std::string HTTPConnection::itoopieImage = const std::string HTTPConnection::itoopieImage =
"<img alt=\"\" src=\"data:image/png;base64," "<img alt=\"\" src=\"data:image/png;base64,"
"iVBORw0KGgoAAAANSUhEUgAAADYAAABECAYAAAG9qPaBAAAACXBIWXMAAA7DAAAOwwHHb6h" "iVBORw0KGgoAAAANSUhEUgAAADYAAABECAYAAAG9qPaBAAAACXBIWXMAAA7DAAAOwwHHb6h"
"kAAAStElEQVR4nMU7CXRUVbL1ek0nTUI6BLNAICEIhESWQAAFgsH8D4g5IwSHg6PfEQ/E0T" "kAAAStElEQVR4nMU7CXRUVbL1ek0nTUI6BLNAICEIhESWQAAFgsH8D4g5IwSHg6PfEQ/E0T"
@ -108,8 +108,8 @@ namespace util
"13ZO0Q0SjbAu6DCkQs4FYg4EwKcOv4WsAFLePCb04Y/ZFCY2MjQ8uGE4cTx7A9f7/dk8r/B" "13ZO0Q0SjbAu6DCkQs4FYg4EwKcOv4WsAFLePCb04Y/ZFCY2MjQ8uGE4cTx7A9f7/dk8r/B"
"+U0vclvzH+PAAAAAElFTkSuQmCC" "+U0vclvzH+PAAAAAElFTkSuQmCC"
"\" />"; "\" />";
namespace misc_strings namespace misc_strings
{ {
const char name_value_separator[] = { ':', ' ' }; const char name_value_separator[] = { ':', ' ' };
@ -146,7 +146,7 @@ namespace util
buffers.push_back(boost::asio::buffer(misc_strings::crlf)); buffers.push_back(boost::asio::buffer(misc_strings::crlf));
} }
buffers.push_back(boost::asio::buffer(misc_strings::crlf)); buffers.push_back(boost::asio::buffer(misc_strings::crlf));
} }
buffers.push_back(boost::asio::buffer(content)); buffers.push_back(boost::asio::buffer(content));
return buffers; return buffers;
} }
@ -154,9 +154,9 @@ namespace util
void HTTPConnection::Terminate () void HTTPConnection::Terminate ()
{ {
if (m_Stream) if (m_Stream)
{ {
m_Stream->Close (); m_Stream->Close ();
DeleteStream (m_Stream); DeleteStream (m_Stream);
} }
m_Socket->close (); m_Socket->close ();
delete this; delete this;
@ -193,11 +193,11 @@ namespace util
{ {
b32 = address.substr (1, pos - 1); // excluding leading '/' to next '/' b32 = address.substr (1, pos - 1); // excluding leading '/' to next '/'
uri = address.substr (pos); // rest of line uri = address.substr (pos); // rest of line
} }
HandleDestinationRequest (b32, uri); HandleDestinationRequest (b32, uri);
} }
else else
HandleRequest (); HandleRequest ();
} }
@ -209,10 +209,10 @@ namespace util
char * http = strstr (get, "HTTP"); char * http = strstr (get, "HTTP");
if (http) if (http)
return std::string (get + 4, http - get - 5); return std::string (get + 4, http - get - 5);
} }
return ""; return "";
} }
void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode)
{ {
Terminate (); Terminate ();
@ -233,7 +233,7 @@ namespace util
FillContent (s); FillContent (s);
s << "</html>"; s << "</html>";
SendReply (s.str ()); SendReply (s.str ());
} }
void HTTPConnection::FillContent (std::stringstream& s) void HTTPConnection::FillContent (std::stringstream& s)
{ {
@ -241,7 +241,7 @@ namespace util
s << "Our external address:" << "<BR>"; s << "Our external address:" << "<BR>";
for (auto& address : i2p::context.GetRouterInfo().GetAddresses()) for (auto& address : i2p::context.GetRouterInfo().GetAddresses())
{ {
switch (address.transportStyle) switch (address.transportStyle)
{ {
case i2p::data::RouterInfo::eTransportNTCP: case i2p::data::RouterInfo::eTransportNTCP:
s << "NTCP&nbsp;&nbsp;"; s << "NTCP&nbsp;&nbsp;";
@ -257,31 +257,31 @@ namespace util
s << "<BR>Routers: " << i2p::data::netdb.GetNumRouters () << " "; s << "<BR>Routers: " << i2p::data::netdb.GetNumRouters () << " ";
s << "Floodfills: " << i2p::data::netdb.GetNumFloodfills () << " "; s << "Floodfills: " << i2p::data::netdb.GetNumFloodfills () << " ";
s << "LeaseSets: " << i2p::data::netdb.GetNumLeaseSets () << "<BR>"; s << "LeaseSets: " << i2p::data::netdb.GetNumLeaseSets () << "<BR>";
s << "<P>Tunnels</P>"; s << "<P>Tunnels</P>";
for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ()) for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ())
{ {
it->GetTunnelConfig ()->Print (s); it->GetTunnelConfig ()->Print (s);
if (it->GetTunnelPool () && !it->GetTunnelPool ()->IsExploratory ()) if (it->GetTunnelPool () && !it->GetTunnelPool ()->IsExploratory ())
s << " " << "Pool"; s << " " << "Pool";
if (it->IsFailed ()) if (it->IsFailed ())
s << " " << "Failed"; s << " " << "Failed";
s << " " << (int)it->GetNumSentBytes () << "<BR>"; s << " " << (int)it->GetNumSentBytes () << "<BR>";
} }
for (auto it: i2p::tunnel::tunnels.GetInboundTunnels ()) for (auto it: i2p::tunnel::tunnels.GetInboundTunnels ())
{ {
it.second->GetTunnelConfig ()->Print (s); it.second->GetTunnelConfig ()->Print (s);
if (it.second->GetTunnelPool () && !it.second->GetTunnelPool ()->IsExploratory ()) if (it.second->GetTunnelPool () && !it.second->GetTunnelPool ()->IsExploratory ())
s << " " << "Pool"; s << " " << "Pool";
if (it.second->IsFailed ()) if (it.second->IsFailed ())
s << " " << "Failed"; s << " " << "Failed";
s << " " << (int)it.second->GetNumReceivedBytes () << "<BR>"; s << " " << (int)it.second->GetNumReceivedBytes () << "<BR>";
} }
s << "<P>Transit tunnels</P>"; s << "<P>Transit tunnels</P>";
for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ()) for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ())
{ {
if (dynamic_cast<i2p::tunnel::TransitTunnelGateway *>(it.second)) if (dynamic_cast<i2p::tunnel::TransitTunnelGateway *>(it.second))
s << it.second->GetTunnelID () << "-->"; s << it.second->GetTunnelID () << "-->";
else if (dynamic_cast<i2p::tunnel::TransitTunnelEndpoint *>(it.second)) else if (dynamic_cast<i2p::tunnel::TransitTunnelEndpoint *>(it.second))
@ -289,23 +289,23 @@ namespace util
else else
s << "-->" << it.second->GetTunnelID () << "-->"; s << "-->" << it.second->GetTunnelID () << "-->";
s << " " << it.second->GetNumTransmittedBytes () << "<BR>"; s << " " << it.second->GetNumTransmittedBytes () << "<BR>";
} }
s << "<P>Transports</P>"; s << "<P>Transports</P>";
s << "NTCP<BR>"; s << "NTCP<BR>";
for (auto it: i2p::transports.GetNTCPSessions ()) for (auto it: i2p::transports.GetNTCPSessions ())
{ {
// RouterInfo of incoming connection doesn't have address // RouterInfo of incoming connection doesn't have address
bool outgoing = it.second->GetRemoteRouterInfo ().GetNTCPAddress (); bool outgoing = it.second->GetRemoteRouterInfo ().GetNTCPAddress ();
if (it.second->IsEstablished ()) if (it.second->IsEstablished ())
{ {
if (outgoing) s << "-->"; if (outgoing) s << "-->";
s << it.second->GetRemoteRouterInfo ().GetIdentHashAbbreviation () << ": " s << it.second->GetRemoteRouterInfo ().GetIdentHashAbbreviation () << ": "
<< it.second->GetSocket ().remote_endpoint().address ().to_string (); << it.second->GetSocket ().remote_endpoint().address ().to_string ();
if (!outgoing) s << "-->"; if (!outgoing) s << "-->";
s << "<BR>"; s << "<BR>";
} }
} }
auto ssuServer = i2p::transports.GetSSUServer (); auto ssuServer = i2p::transports.GetSSUServer ();
if (ssuServer) if (ssuServer)
{ {
@ -319,12 +319,16 @@ namespace util
s << endpoint.address ().to_string () << ":" << endpoint.port (); s << endpoint.address ().to_string () << ":" << endpoint.port ();
if (!outgoing) s << "-->"; if (!outgoing) s << "-->";
s << "<BR>"; s << "<BR>";
} }
} }
s << "<p><a href=\"zmw2cyw2vj7f6obx3msmdvdepdhnw2ctc4okza2zjxlukkdfckhq\">Flibusta</a></p>"; s << "<p><a href=\"zmw2cyw2vj7f6obx3msmdvdepdhnw2ctc4okza2zjxlukkdfckhq\">Flibusta</a></p>";
} }
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri) void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri)
{
HandleDestinationRequest(address, "GET", "", uri);
}
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& method, const std::string& data, const std::string& uri)
{ {
i2p::data::IdentHash destination; i2p::data::IdentHash destination;
std::string fullAddress; std::string fullAddress;
@ -363,7 +367,7 @@ namespace util
fullAddress = address + ".b32.i2p"; fullAddress = address + ".b32.i2p";
} }
} }
auto leaseSet = i2p::data::netdb.FindLeaseSet (destination); auto leaseSet = i2p::data::netdb.FindLeaseSet (destination);
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
{ {
@ -374,23 +378,29 @@ namespace util
{ {
SendReply (leaseSet ? "<html>" + itoopieImage + "<br>Leases expired</html>" : "<html>" + itoopieImage + "LeaseSet not found</html>", 504); SendReply (leaseSet ? "<html>" + itoopieImage + "<br>Leases expired</html>" : "<html>" + itoopieImage + "LeaseSet not found</html>", 504);
return; return;
} }
} }
if (!m_Stream) if (!m_Stream)
m_Stream = i2p::stream::CreateStream (*leaseSet); m_Stream = i2p::stream::CreateStream (*leaseSet);
if (m_Stream) if (m_Stream)
{ {
std::string request = "GET " + uri + " HTTP/1.1\n Host:" + fullAddress + "\n"; std::string request = method+" " + uri + " HTTP/1.1\n Host:" + fullAddress + "\r\n";
m_Stream->Send ((uint8_t *)request.c_str (), request.length (), 10); 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);
AsyncStreamReceive (); AsyncStreamReceive ();
} }
} }
void HTTPConnection::AsyncStreamReceive () void HTTPConnection::AsyncStreamReceive ()
{ {
if (m_Stream) if (m_Stream)
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192), m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
boost::bind (&HTTPConnection::HandleStreamReceive, this, boost::bind (&HTTPConnection::HandleStreamReceive, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred),
45); // 45 seconds timeout 45); // 45 seconds timeout
} }
@ -408,7 +418,7 @@ namespace util
SendReply ("<html>" + itoopieImage + "<br>Not responding</html>", 504); SendReply ("<html>" + itoopieImage + "<br>Not responding</html>", 504);
else else
Terminate (); Terminate ();
} }
} }
void HTTPConnection::SendReply (const std::string& content, int status) void HTTPConnection::SendReply (const std::string& content, int status)
@ -421,16 +431,16 @@ namespace util
m_Reply.headers[1].value = "text/html"; m_Reply.headers[1].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),
boost::bind (&HTTPConnection::HandleWriteReply, this, boost::bind (&HTTPConnection::HandleWriteReply, this,
boost::asio::placeholders::error)); boost::asio::placeholders::error));
} }
HTTPServer::HTTPServer (int port): HTTPServer::HTTPServer (int port):
m_Thread (nullptr), m_Work (m_Service), m_Thread (nullptr), m_Work (m_Service),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
m_NewSocket (nullptr) m_NewSocket (nullptr)
{ {
} }
HTTPServer::~HTTPServer () HTTPServer::~HTTPServer ()
@ -450,17 +460,17 @@ namespace util
m_Acceptor.close(); m_Acceptor.close();
m_Service.stop (); m_Service.stop ();
if (m_Thread) if (m_Thread)
{ {
m_Thread->join (); m_Thread->join ();
delete m_Thread; delete m_Thread;
m_Thread = nullptr; m_Thread = nullptr;
} }
} }
void HTTPServer::Run () void HTTPServer::Run ()
{ {
m_Service.run (); m_Service.run ();
} }
void HTTPServer::Accept () void HTTPServer::Accept ()
{ {
@ -476,7 +486,7 @@ namespace util
CreateConnection(m_NewSocket); // new HTTPConnection(m_NewSocket); CreateConnection(m_NewSocket); // new HTTPConnection(m_NewSocket);
Accept (); Accept ();
} }
} }
void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket) void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket)
{ {

@ -14,13 +14,13 @@ namespace util
class HTTPConnection class HTTPConnection
{ {
protected: protected:
struct header struct header
{ {
std::string name; std::string name;
std::string value; std::string value;
}; };
struct request struct request
{ {
std::string method; std::string method;
@ -38,7 +38,7 @@ namespace util
std::vector<boost::asio::const_buffer> to_buffers (int status); std::vector<boost::asio::const_buffer> to_buffers (int status);
}; };
public: public:
HTTPConnection (boost::asio::ip::tcp::socket * socket): m_Socket (socket), m_Stream (nullptr) { Receive (); }; HTTPConnection (boost::asio::ip::tcp::socket * socket): m_Socket (socket), m_Stream (nullptr) { Receive (); };
@ -48,9 +48,9 @@ namespace util
void Terminate (); void Terminate ();
void Receive (); void Receive ();
void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void AsyncStreamReceive (); void AsyncStreamReceive ();
void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleWriteReply(const boost::system::error_code& ecode); void HandleWriteReply(const boost::system::error_code& ecode);
void HandleWrite (const boost::system::error_code& ecode); void HandleWrite (const boost::system::error_code& ecode);
void SendReply (const std::string& content, int status = 200); void SendReply (const std::string& content, int status = 200);
@ -58,9 +58,9 @@ namespace util
void HandleRequest (); void HandleRequest ();
void FillContent (std::stringstream& s); void FillContent (std::stringstream& s);
std::string ExtractAddress (); std::string ExtractAddress ();
protected: protected:
boost::asio::ip::tcp::socket * m_Socket; boost::asio::ip::tcp::socket * m_Socket;
i2p::stream::Stream * m_Stream; i2p::stream::Stream * m_Stream;
char m_Buffer[8192], m_StreamBuffer[8192]; char m_Buffer[8192], m_StreamBuffer[8192];
@ -68,14 +68,16 @@ namespace util
reply m_Reply; reply m_Reply;
protected: protected:
virtual void HandleDestinationRequest(const std::string& address, const std::string& uri); virtual void HandleDestinationRequest(const std::string& address, const std::string& uri);
virtual void HandleDestinationRequest(const std::string& address, const std::string& method, const std::string& data, const std::string& uri);
virtual void RunRequest (); virtual void RunRequest ();
private: private:
static const std::string itoopieImage; static const std::string itoopieImage;
}; };
class HTTPServer class HTTPServer
{ {
@ -89,9 +91,9 @@ namespace util
private: private:
void Run (); void Run ();
void Accept (); void Accept ();
void HandleAccept(const boost::system::error_code& ecode); void HandleAccept(const boost::system::error_code& ecode);
private: private:
@ -103,7 +105,7 @@ namespace util
protected: protected:
virtual void CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket); virtual void CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket);
}; };
} }
} }

Loading…
Cancel
Save