Browse Source

add http status code

pull/66/head
chertov 11 years ago
parent
commit
cdbcf0db76
  1. 32
      HTTPServer.cpp
  2. 4
      HTTPServer.h

32
HTTPServer.cpp

@ -118,12 +118,26 @@ namespace util
} // namespace misc_strings } // namespace misc_strings
std::vector<boost::asio::const_buffer> HTTPConnection::reply::to_buffers() std::vector<boost::asio::const_buffer> HTTPConnection::reply::to_buffers(int status)
{ {
std::vector<boost::asio::const_buffer> buffers; std::vector<boost::asio::const_buffer> buffers;
if (headers.size () > 0) if (headers.size () > 0)
{ {
buffers.push_back (boost::asio::buffer ("HTTP/1.0 200 OK\r\n")); // always OK switch (status)
{
case 105: buffers.push_back(boost::asio::buffer("HTTP/1.0 105 Name Not Resolved\r\n")); break;
case 200: buffers.push_back(boost::asio::buffer("HTTP/1.0 200 OK\r\n")); break;
case 400: buffers.push_back(boost::asio::buffer("HTTP/1.0 400 Bad Request\r\n")); break;
case 404: buffers.push_back(boost::asio::buffer("HTTP/1.0 404 Not Found\r\n")); break;
case 408: buffers.push_back(boost::asio::buffer("HTTP/1.0 408 Request Timeout\r\n")); break;
case 500: buffers.push_back(boost::asio::buffer("HTTP/1.0 500 Internal Server Error\r\n")); break;
case 502: buffers.push_back(boost::asio::buffer("HTTP/1.0 502 Bad Gateway\r\n")); break;
case 503: buffers.push_back(boost::asio::buffer("HTTP/1.0 503 Not Implemented\r\n")); break;
case 504: buffers.push_back(boost::asio::buffer("HTTP/1.0 504 Gateway Timeou\r\n")); break;
default:
buffers.push_back(boost::asio::buffer("HTTP/1.0 200 OK\r\n"));
}
for (std::size_t i = 0; i < headers.size(); ++i) for (std::size_t i = 0; i < headers.size(); ++i)
{ {
header& h = headers[i]; header& h = headers[i];
@ -316,7 +330,7 @@ namespace util
if (i2p::data::Base32ToByteStream(address.c_str(), address.length() - strlen(".b32.i2p"), (uint8_t *)destination, 32) != 32) if (i2p::data::Base32ToByteStream(address.c_str(), address.length() - strlen(".b32.i2p"), (uint8_t *)destination, 32) != 32)
{ {
LogPrint ("Invalid Base32 address ", address); LogPrint ("Invalid Base32 address ", address);
SendReply ("<html>" + itoopieImage + "<br>Invalid Base32 address"); SendReply ("<html>" + itoopieImage + "<br>Invalid Base32 address", 400);
return; return;
} }
fullAddress = address; fullAddress = address;
@ -329,7 +343,7 @@ namespace util
if (!addr) if (!addr)
{ {
LogPrint ("Unknown address ", address); LogPrint ("Unknown address ", address);
SendReply ("<html>" + itoopieImage + "<br>Unknown address " + address + "</html>"); SendReply ("<html>" + itoopieImage + "<br>Unknown address " + address + "</html>", 105);
return; return;
} }
destination = *addr; destination = *addr;
@ -340,7 +354,7 @@ namespace util
if (i2p::data::Base32ToByteStream(address.c_str(), address.length(), (uint8_t *)destination, 32) != 32) if (i2p::data::Base32ToByteStream(address.c_str(), address.length(), (uint8_t *)destination, 32) != 32)
{ {
LogPrint("Invalid Base32 address ", address); LogPrint("Invalid Base32 address ", address);
SendReply("<html>" + itoopieImage + "<br>Invalid Base32 address"); SendReply("<html>" + itoopieImage + "<br>Invalid Base32 address", 400);
return; return;
} }
fullAddress = address + ".b32.i2p"; fullAddress = address + ".b32.i2p";
@ -355,7 +369,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
{ {
SendReply (leaseSet ? "<html>" + itoopieImage + "<br>Leases expired</html>" : "<html>" + itoopieImage + "LeaseSet not found</html>"); SendReply (leaseSet ? "<html>" + itoopieImage + "<br>Leases expired</html>" : "<html>" + itoopieImage + "LeaseSet not found</html>", 504);
return; return;
} }
} }
@ -388,13 +402,13 @@ namespace util
else else
{ {
if (m_Stream && m_Stream->IsOpen ()) if (m_Stream && m_Stream->IsOpen ())
SendReply ("<html>" + itoopieImage + "<br>Not responding</html>"); SendReply ("<html>" + itoopieImage + "<br>Not responding</html>", 504);
else else
Terminate (); Terminate ();
} }
} }
void HTTPConnection::SendReply (const std::string& content) void HTTPConnection::SendReply (const std::string& content, int status)
{ {
m_Reply.content = content; m_Reply.content = content;
m_Reply.headers.resize(2); m_Reply.headers.resize(2);
@ -403,7 +417,7 @@ namespace util
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::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));
} }

4
HTTPServer.h

@ -36,7 +36,7 @@ namespace util
std::vector<header> headers; std::vector<header> headers;
std::string content; std::string content;
std::vector<boost::asio::const_buffer> to_buffers(); std::vector<boost::asio::const_buffer> to_buffers (int status);
}; };
public: public:
@ -53,7 +53,7 @@ namespace util
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); void SendReply (const std::string& content, int status = 200);
void HandleRequest (); void HandleRequest ();
void FillContent (std::stringstream& s); void FillContent (std::stringstream& s);

Loading…
Cancel
Save