From 02a42b0e98cdc6499a852d01a1dfe8391c4fa321 Mon Sep 17 00:00:00 2001 From: Meeh Date: Sat, 29 Mar 2014 17:03:14 +0100 Subject: [PATCH 1/2] switch to AsyncReceive in HTTPProxy --- HTTPProxy.cpp | 99 +++++++++++++++++++++++++++++---------------------- HTTPProxy.h | 13 ++++--- 2 files changed, 66 insertions(+), 46 deletions(-) diff --git a/HTTPProxy.cpp b/HTTPProxy.cpp index 810e5a18..c9a763ce 100644 --- a/HTTPProxy.cpp +++ b/HTTPProxy.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "base64.h" #include "Log.h" @@ -67,11 +68,6 @@ namespace proxy LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second); HandleDestinationRequest (requestInfo.first, requestInfo.second); - - boost::asio::async_write (*m_Socket, m_Reply.to_buffers(), - boost::bind (&HTTPConnection::HandleWrite, this, - boost::asio::placeholders::error)); - //Receive (); } else if (ecode != boost::asio::error::operation_aborted) Terminate (); @@ -129,11 +125,19 @@ namespace proxy return std::make_pair ("",""); } - void HTTPConnection::HandleWrite (const boost::system::error_code& ecode) + void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) { Terminate (); } + void HTTPConnection::HandleWrite (const boost::system::error_code& ecode) + { + if (ecode || (m_Stream && !m_Stream->IsOpen ())) + Terminate (); + else // data keeps coming + AsyncStreamReceive (); + } + void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri) { i2p::data::IdentHash destination; @@ -155,11 +159,12 @@ namespace proxy if (!addr) { LogPrint ("Unknown address ", address); + SendReply(""+ i2p::proxy::itoopieImage +"
Unknown address " + address + ""); return; } destination = *addr; fullAddress = address; - } + } auto leaseSet = i2p::data::netdb.FindLeaseSet (destination); if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) @@ -169,47 +174,57 @@ namespace proxy leaseSet = i2p::data::netdb.FindLeaseSet (destination); if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet { - m_Reply.content = leaseSet ? ""+ i2p::proxy::itoopieImage +"
Leases expired" : ""+ i2p::proxy::itoopieImage +"LeaseSet not found"; - m_Reply.headers.resize(2); - m_Reply.headers[0].name = "Content-Length"; - m_Reply.headers[0].value = boost::lexical_cast(m_Reply.content.size()); - m_Reply.headers[1].name = "Content-Type"; - m_Reply.headers[1].value = "text/html"; + SendReply(leaseSet ? ""+ i2p::proxy::itoopieImage +"
Leases expired" : ""+ i2p::proxy::itoopieImage +"LeaseSet not found"); return; } } - auto s = i2p::stream::CreateStream (*leaseSet); - if (s) + if (!m_Stream) + m_Stream = i2p::stream::CreateStream (*leaseSet); + if (m_Stream) { std::string request = "GET " + uri + " HTTP/1.1\n Host:" + fullAddress + "\n"; - s->Send ((uint8_t *)request.c_str (), request.length (), 10); - std::stringstream ss; - uint8_t buf[8192]; - size_t r = s->Receive (buf, 8192, 30); // 30 seconds - if (!r && s->IsEstablished ()) // nothing received but connection is established - r = s->Receive (buf, 8192, 30); // wait for another 30 secondd - if (r) // we recieved data - { - ss << std::string ((char *)buf, r); - while (s->IsOpen () && (r = s->Receive (buf, 8192, 30)) > 0) - ss << std::string ((char *)buf,r); - - m_Reply.content = ss.str (); // send "as is" - m_Reply.headers.resize(0); // no headers - return; - } - else // nothing received - ss << ""+ i2p::proxy::itoopieImage +"
Not responding"; - s->Close (); - DeleteStream (s); - - m_Reply.content = ss.str (); - m_Reply.headers.resize(2); - m_Reply.headers[0].name = "Content-Length"; - m_Reply.headers[0].value = boost::lexical_cast(m_Reply.content.size()); - m_Reply.headers[1].name = "Content-Type"; - m_Reply.headers[1].value = "text/html"; + m_Stream->Send ((uint8_t *)request.c_str (), request.length (), 10); + AsyncStreamReceive (); + } + } + + void HTTPConnection::AsyncStreamReceive () + { + if (m_Stream) + m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192), + boost::protect (boost::bind (&HTTPConnection::HandleStreamReceive, this, + boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)), + 45); // 45 seconds timeout + } + + void HTTPConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred) + { + if (bytes_transferred) + { + boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred), + boost::bind (&HTTPConnection::HandleWrite, this, boost::asio::placeholders::error)); + } + else + { + if (m_Stream && m_Stream->IsOpen ()) + SendReply (""+ i2p::proxy::itoopieImage +"
Not responding"); + else + Terminate (); } + } + + void HTTPConnection::SendReply (const std::string& content) + { + m_Reply.content = content; + m_Reply.headers.resize(2); + m_Reply.headers[0].name = "Content-Length"; + m_Reply.headers[0].value = boost::lexical_cast(m_Reply.content.size()); + m_Reply.headers[1].name = "Content-Type"; + m_Reply.headers[1].value = "text/html"; + + boost::asio::async_write (*m_Socket, m_Reply.to_buffers(), + boost::bind (&HTTPConnection::HandleWriteReply, this, + boost::asio::placeholders::error)); } diff --git a/HTTPProxy.h b/HTTPProxy.h index d607d917..2b172542 100644 --- a/HTTPProxy.h +++ b/HTTPProxy.h @@ -39,15 +39,19 @@ namespace proxy public: - HTTPConnection (boost::asio::ip::tcp::socket * socket): m_Socket (socket) { Receive (); }; + HTTPConnection (boost::asio::ip::tcp::socket * socket): m_Socket (socket), m_Stream (nullptr) { Receive (); }; ~HTTPConnection () { delete m_Socket; } private: void Terminate (); void Receive (); - void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); - void HandleWrite(const boost::system::error_code& ecode); + void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); + void AsyncStreamReceive (); + void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); + void HandleWriteReply(const boost::system::error_code& ecode); + void HandleWrite (const boost::system::error_code& ecode); + void SendReply (const std::string& content); void HandleDestinationRequest (const std::string& address, const std::string& uri); std::pair ExtractRequest (); @@ -55,8 +59,9 @@ namespace proxy private: + i2p::stream::Stream * m_Stream; boost::asio::ip::tcp::socket * m_Socket; - char m_Buffer[8192]; + char m_Buffer[8192], m_StreamBuffer[8192]; request m_Request; reply m_Reply; }; From 5fa7671ef77b61766c04c42c1023b0356889eaab Mon Sep 17 00:00:00 2001 From: Meeh Date: Sat, 29 Mar 2014 18:28:19 +0100 Subject: [PATCH 2/2] Rewriting url parser --- HTTPProxy.cpp | 53 +++++++++++++++++++++++++-------------------------- HTTPProxy.h | 3 ++- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/HTTPProxy.cpp b/HTTPProxy.cpp index c9a763ce..61264db2 100644 --- a/HTTPProxy.cpp +++ b/HTTPProxy.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "base64.h" #include "Log.h" @@ -62,12 +63,12 @@ namespace proxy { m_Buffer[bytes_transferred] = 0; - std::pair requestInfo = ExtractRequest (); request m_Request; + ExtractRequest (m_Request); parseHeaders (m_Buffer, m_Request.headers); - LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second); - HandleDestinationRequest (requestInfo.first, requestInfo.second); + LogPrint("Requesting ", m_Request.host, " with path ", m_Request.uri, " and method ", m_Request.method); + HandleDestinationRequest (m_Request.host, m_Request.uri); } else if (ecode != boost::asio::error::operation_aborted) Terminate (); @@ -96,33 +97,31 @@ namespace proxy } } - // TODO: Support other requests than GET. - std::pair HTTPConnection::ExtractRequest () + void HTTPConnection::ExtractRequest (request &m_Request) { - char * get = strstr (m_Buffer, "GET"); - if (get) - { - char * http = strstr (get, "HTTP"); - if (http) - { - std::string url (get + 4, http - get - 5); - size_t sp = url.find_first_of ('/', 7 /* skip http:// part */ ); - if (sp != std::string::npos) - { - std::string base_url (url.begin()+7, url.begin()+sp); - LogPrint ("Base URL is: ", base_url, "\n"); - if ( sp != std::string::npos ) - { - std::string query (url.begin ()+sp+1, url.end ()); - LogPrint ("Query is: ", "/" + query); - - return std::make_pair (base_url, "/" + query); - } - return std::make_pair (base_url, "/"); - } + std::string requestString = m_Buffer; + int idx=requestString.find(" "); + std::string method = requestString.substr(0,idx); + requestString = requestString.substr(idx+1); + idx=requestString.find(" "); + std::string requestUrl = requestString.substr(0,idx); + LogPrint("method is: ", method, "\nRequest is: ", requestUrl); + std::string server=""; + std::string port="80"; + boost::regex rHTTP("http://(.*?)(:(\\d+))?(/.*)"); + boost::smatch m; + std::string path; + if(boost::regex_search(requestUrl, m, rHTTP, boost::match_extra)) { + server=m[1].str(); + if(m[2].str() != "") { + port=m[3].str(); } + path=m[4].str(); } - return std::make_pair ("",""); + LogPrint("server is: ",server, "\n path is: ",path); + m_Request.uri = path; + m_Request.method = method; + m_Request.host = server; } void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) diff --git a/HTTPProxy.h b/HTTPProxy.h index 2b172542..c4370daa 100644 --- a/HTTPProxy.h +++ b/HTTPProxy.h @@ -24,6 +24,7 @@ namespace proxy { std::string method; std::string uri; + std::string host; int http_version_major; int http_version_minor; std::vector
headers; @@ -54,7 +55,7 @@ namespace proxy void SendReply (const std::string& content); void HandleDestinationRequest (const std::string& address, const std::string& uri); - std::pair ExtractRequest (); + void ExtractRequest (request& m_Request); void parseHeaders(const std::string& h, std::vector
& hm); private: