Browse Source

send reply

pull/48/head
orignal 11 years ago
parent
commit
5eb67a08c9
  1. 56
      HTTPServer.cpp
  2. 3
      HTTPServer.h

56
HTTPServer.cpp

@ -1,4 +1,5 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/bind/protect.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include "base64.h" #include "base64.h"
#include "Log.h" #include "Log.h"
@ -76,10 +77,6 @@ namespace util
} }
else else
HandleRequest (); HandleRequest ();
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 ();
@ -97,9 +94,10 @@ namespace util
return ""; return "";
} }
void HTTPConnection::HandleWrite (const boost::system::error_code& ecode) void HTTPConnection::HandleWrite (const boost::system::error_code& ecode, bool terminate)
{ {
Terminate (); if (terminate)
Terminate ();
} }
void HTTPConnection::HandleRequest () void HTTPConnection::HandleRequest ()
@ -108,12 +106,7 @@ namespace util
s << "<html>"; s << "<html>";
FillContent (s); FillContent (s);
s << "</html>"; s << "</html>";
m_Reply.content = s.str (); SendReply (s.str ());
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";
} }
void HTTPConnection::FillContent (std::stringstream& s) void HTTPConnection::FillContent (std::stringstream& s)
@ -211,6 +204,7 @@ namespace util
if (!addr) if (!addr)
{ {
LogPrint ("Unknown address ", address); LogPrint ("Unknown address ", address);
SendReply ("Unknown address");
return; return;
} }
destination = *addr; destination = *addr;
@ -234,12 +228,7 @@ namespace util
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>Leases expired</html>" : "<html>LeaseSet not found</html>"; SendReply (leaseSet ? "<html>Leases expired</html>" : "<html>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;
} }
} }
@ -261,6 +250,9 @@ namespace util
m_Reply.content = ss.str (); // send "as is" m_Reply.content = ss.str (); // send "as is"
m_Reply.headers.resize(0); // no headers m_Reply.headers.resize(0); // no headers
boost::asio::async_write (*m_Socket, m_Reply.to_buffers(),
boost::bind (&HTTPConnection::HandleWrite, this,
boost::asio::placeholders::error, true));
return; return;
} }
else // nothing received else // nothing received
@ -268,17 +260,33 @@ namespace util
s->Close (); s->Close ();
DeleteStream (s); DeleteStream (s);
m_Reply.content = ss.str (); SendReply (ss.str ());
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";
} }
} }
void HTTPConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred) void HTTPConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{ {
if (bytes_transferred)
{
}
else
{
SendReply ("Not responding");
}
}
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::HandleWrite, this,
boost::asio::placeholders::error, true));
} }
HTTPServer::HTTPServer (int port): HTTPServer::HTTPServer (int port):

3
HTTPServer.h

@ -46,7 +46,8 @@ namespace util
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 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 HandleWrite(const boost::system::error_code& ecode); void HandleWrite(const boost::system::error_code& ecode, bool terminate);
void SendReply (const std::string& content);
void HandleRequest (); void HandleRequest ();
void HandleDestinationRequest (const std::string& address, const std::string& uri); void HandleDestinationRequest (const std::string& address, const std::string& uri);

Loading…
Cancel
Save