mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-31 04:44:13 +00:00
switch to AsyncReceive in HTTPProxy
This commit is contained in:
parent
086b0d5418
commit
02a42b0e98
@ -1,5 +1,6 @@
|
|||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
|
#include <boost/bind/protect.hpp>
|
||||||
|
|
||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
@ -67,11 +68,6 @@ namespace proxy
|
|||||||
|
|
||||||
LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second);
|
LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second);
|
||||||
HandleDestinationRequest (requestInfo.first, 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)
|
else if (ecode != boost::asio::error::operation_aborted)
|
||||||
Terminate ();
|
Terminate ();
|
||||||
@ -129,11 +125,19 @@ namespace proxy
|
|||||||
return std::make_pair ("","");
|
return std::make_pair ("","");
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTTPConnection::HandleWrite (const boost::system::error_code& ecode)
|
void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode)
|
||||||
{
|
{
|
||||||
Terminate ();
|
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)
|
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri)
|
||||||
{
|
{
|
||||||
i2p::data::IdentHash destination;
|
i2p::data::IdentHash destination;
|
||||||
@ -155,11 +159,12 @@ 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;
|
||||||
fullAddress = address;
|
fullAddress = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
auto leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
||||||
if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
|
if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
|
||||||
@ -169,47 +174,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
|
void HTTPConnection::AsyncStreamReceive ()
|
||||||
if (r) // we recieved data
|
{
|
||||||
{
|
if (m_Stream)
|
||||||
ss << std::string ((char *)buf, r);
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
|
||||||
while (s->IsOpen () && (r = s->Receive (buf, 8192, 30)) > 0)
|
boost::protect (boost::bind (&HTTPConnection::HandleStreamReceive, this,
|
||||||
ss << std::string ((char *)buf,r);
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)),
|
||||||
|
45); // 45 seconds timeout
|
||||||
m_Reply.content = ss.str (); // send "as is"
|
}
|
||||||
m_Reply.headers.resize(0); // no headers
|
|
||||||
return;
|
void HTTPConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
}
|
{
|
||||||
else // nothing received
|
if (bytes_transferred)
|
||||||
ss << "<html>"+ i2p::proxy::itoopieImage +"<br>Not responding</html>";
|
{
|
||||||
s->Close ();
|
boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
||||||
DeleteStream (s);
|
boost::bind (&HTTPConnection::HandleWrite, this, boost::asio::placeholders::error));
|
||||||
|
}
|
||||||
m_Reply.content = ss.str ();
|
else
|
||||||
m_Reply.headers.resize(2);
|
{
|
||||||
m_Reply.headers[0].name = "Content-Length";
|
if (m_Stream && m_Stream->IsOpen ())
|
||||||
m_Reply.headers[0].value = boost::lexical_cast<std::string>(m_Reply.content.size());
|
SendReply ("<html>"+ i2p::proxy::itoopieImage +"<br>Not responding</html>");
|
||||||
m_Reply.headers[1].name = "Content-Type";
|
else
|
||||||
m_Reply.headers[1].value = "text/html";
|
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<std::string>(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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
13
HTTPProxy.h
13
HTTPProxy.h
@ -39,15 +39,19 @@ namespace proxy
|
|||||||
|
|
||||||
public:
|
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; }
|
~HTTPConnection () { delete m_Socket; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
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 HandleWrite(const boost::system::error_code& ecode);
|
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);
|
void HandleDestinationRequest (const std::string& address, const std::string& uri);
|
||||||
std::pair<std::string, std::string> ExtractRequest ();
|
std::pair<std::string, std::string> ExtractRequest ();
|
||||||
@ -55,8 +59,9 @@ namespace proxy
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
i2p::stream::Stream * m_Stream;
|
||||||
boost::asio::ip::tcp::socket * m_Socket;
|
boost::asio::ip::tcp::socket * m_Socket;
|
||||||
char m_Buffer[8192];
|
char m_Buffer[8192], m_StreamBuffer[8192];
|
||||||
request m_Request;
|
request m_Request;
|
||||||
reply m_Reply;
|
reply m_Reply;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user