|
|
@ -1,5 +1,7 @@ |
|
|
|
#include <boost/bind.hpp> |
|
|
|
#include <boost/bind.hpp> |
|
|
|
#include <boost/lexical_cast.hpp> |
|
|
|
#include <boost/lexical_cast.hpp> |
|
|
|
|
|
|
|
#include <boost/bind/protect.hpp> |
|
|
|
|
|
|
|
#include <boost/regex.hpp> |
|
|
|
|
|
|
|
|
|
|
|
#include "base64.h" |
|
|
|
#include "base64.h" |
|
|
|
#include "Log.h" |
|
|
|
#include "Log.h" |
|
|
@ -61,17 +63,12 @@ namespace proxy |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Buffer[bytes_transferred] = 0; |
|
|
|
m_Buffer[bytes_transferred] = 0; |
|
|
|
|
|
|
|
|
|
|
|
std::pair<std::string,std::string> requestInfo = ExtractRequest (); |
|
|
|
|
|
|
|
request m_Request; |
|
|
|
request m_Request; |
|
|
|
|
|
|
|
ExtractRequest (m_Request); |
|
|
|
parseHeaders (m_Buffer, m_Request.headers); |
|
|
|
parseHeaders (m_Buffer, m_Request.headers); |
|
|
|
|
|
|
|
|
|
|
|
LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second); |
|
|
|
LogPrint("Requesting ", m_Request.host, " with path ", m_Request.uri, " and method ", m_Request.method); |
|
|
|
HandleDestinationRequest (requestInfo.first, requestInfo.second); |
|
|
|
HandleDestinationRequest (m_Request.host, m_Request.uri); |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
else if (ecode != boost::asio::error::operation_aborted) |
|
|
|
Terminate (); |
|
|
|
Terminate (); |
|
|
@ -100,38 +97,44 @@ namespace proxy |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO: Support other requests than GET.
|
|
|
|
void HTTPConnection::ExtractRequest (request &m_Request) |
|
|
|
std::pair<std::string, std::string> HTTPConnection::ExtractRequest () |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::string requestString = m_Buffer; |
|
|
|
char * get = strstr (m_Buffer, "GET"); |
|
|
|
int idx=requestString.find(" "); |
|
|
|
if (get) |
|
|
|
std::string method = requestString.substr(0,idx); |
|
|
|
{ |
|
|
|
requestString = requestString.substr(idx+1); |
|
|
|
char * http = strstr (get, "HTTP"); |
|
|
|
idx=requestString.find(" "); |
|
|
|
if (http) |
|
|
|
std::string requestUrl = requestString.substr(0,idx); |
|
|
|
{ |
|
|
|
LogPrint("method is: ", method, "\nRequest is: ", requestUrl); |
|
|
|
std::string url (get + 4, http - get - 5); |
|
|
|
std::string server=""; |
|
|
|
size_t sp = url.find_first_of ('/', 7 /* skip http:// part */ ); |
|
|
|
std::string port="80"; |
|
|
|
if (sp != std::string::npos) |
|
|
|
boost::regex rHTTP("http://(.*?)(:(\\d+))?(/.*)"); |
|
|
|
{ |
|
|
|
boost::smatch m; |
|
|
|
std::string base_url (url.begin()+7, url.begin()+sp); |
|
|
|
std::string path; |
|
|
|
LogPrint ("Base URL is: ", base_url, "\n"); |
|
|
|
if(boost::regex_search(requestUrl, m, rHTTP, boost::match_extra)) { |
|
|
|
if ( sp != std::string::npos ) |
|
|
|
server=m[1].str(); |
|
|
|
{ |
|
|
|
if(m[2].str() != "") { |
|
|
|
std::string query (url.begin ()+sp+1, url.end ()); |
|
|
|
port=m[3].str(); |
|
|
|
LogPrint ("Query is: ", "/" + query); |
|
|
|
} |
|
|
|
|
|
|
|
path=m[4].str(); |
|
|
|
return std::make_pair (base_url, "/" + query); |
|
|
|
} |
|
|
|
} |
|
|
|
LogPrint("server is: ",server, "\n path is: ",path); |
|
|
|
return std::make_pair (base_url, "/"); |
|
|
|
m_Request.uri = path; |
|
|
|
|
|
|
|
m_Request.method = method; |
|
|
|
|
|
|
|
m_Request.host = server; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) |
|
|
|
return std::make_pair ("",""); |
|
|
|
{ |
|
|
|
|
|
|
|
Terminate (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void HTTPConnection::HandleWrite (const boost::system::error_code& ecode) |
|
|
|
void HTTPConnection::HandleWrite (const boost::system::error_code& ecode) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if (ecode || (m_Stream && !m_Stream->IsOpen ())) |
|
|
|
Terminate (); |
|
|
|
Terminate (); |
|
|
|
|
|
|
|
else // data keeps coming
|
|
|
|
|
|
|
|
AsyncStreamReceive (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri) |
|
|
|
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri) |
|
|
@ -155,6 +158,7 @@ namespace proxy |
|
|
|
if (!addr) |
|
|
|
if (!addr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint ("Unknown address ", address); |
|
|
|
LogPrint ("Unknown address ", address); |
|
|
|
|
|
|
|
SendReply("<html>"+ i2p::proxy::itoopieImage +"<br>Unknown address " + address + "</html>"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
destination = *addr; |
|
|
|
destination = *addr; |
|
|
@ -169,47 +173,57 @@ namespace proxy |
|
|
|
leaseSet = i2p::data::netdb.FindLeaseSet (destination); |
|
|
|
leaseSet = i2p::data::netdb.FindLeaseSet (destination); |
|
|
|
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet
|
|
|
|
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet
|
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Reply.content = leaseSet ? "<html>"+ i2p::proxy::itoopieImage +"<br>Leases expired</html>" : "<html>"+ i2p::proxy::itoopieImage +"LeaseSet not found</html>"; |
|
|
|
SendReply(leaseSet ? "<html>"+ i2p::proxy::itoopieImage +"<br>Leases expired</html>" : "<html>"+ i2p::proxy::itoopieImage +"LeaseSet not found</html>"); |
|
|
|
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"; |
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
auto s = i2p::stream::CreateStream (*leaseSet); |
|
|
|
if (!m_Stream) |
|
|
|
if (s) |
|
|
|
m_Stream = i2p::stream::CreateStream (*leaseSet); |
|
|
|
|
|
|
|
if (m_Stream) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::string request = "GET " + uri + " HTTP/1.1\n Host:" + fullAddress + "\n"; |
|
|
|
std::string request = "GET " + uri + " HTTP/1.1\n Host:" + fullAddress + "\n"; |
|
|
|
s->Send ((uint8_t *)request.c_str (), request.length (), 10); |
|
|
|
m_Stream->Send ((uint8_t *)request.c_str (), request.length (), 10); |
|
|
|
std::stringstream ss; |
|
|
|
AsyncStreamReceive (); |
|
|
|
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"
|
|
|
|
void HTTPConnection::AsyncStreamReceive () |
|
|
|
m_Reply.headers.resize(0); // no headers
|
|
|
|
{ |
|
|
|
return; |
|
|
|
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
|
|
|
|
} |
|
|
|
} |
|
|
|
else // nothing received
|
|
|
|
|
|
|
|
ss << "<html>"+ i2p::proxy::itoopieImage +"<br>Not responding</html>"; |
|
|
|
|
|
|
|
s->Close (); |
|
|
|
|
|
|
|
DeleteStream (s); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_Reply.content = ss.str (); |
|
|
|
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 ("<html>"+ i2p::proxy::itoopieImage +"<br>Not responding</html>"); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
Terminate (); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void HTTPConnection::SendReply (const std::string& content) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Reply.content = content; |
|
|
|
m_Reply.headers.resize(2); |
|
|
|
m_Reply.headers.resize(2); |
|
|
|
m_Reply.headers[0].name = "Content-Length"; |
|
|
|
m_Reply.headers[0].name = "Content-Length"; |
|
|
|
m_Reply.headers[0].value = boost::lexical_cast<std::string>(m_Reply.content.size()); |
|
|
|
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].name = "Content-Type"; |
|
|
|
m_Reply.headers[1].value = "text/html"; |
|
|
|
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)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|