diff --git a/HTTPServer.cpp b/HTTPServer.cpp index b92c786e..0900ecee 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -118,12 +118,26 @@ namespace util } // namespace misc_strings - std::vector HTTPConnection::reply::to_buffers() + std::vector HTTPConnection::reply::to_buffers(int status) { std::vector buffers; 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) { 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) { LogPrint ("Invalid Base32 address ", address); - SendReply ("" + itoopieImage + "
Invalid Base32 address"); + SendReply ("" + itoopieImage + "
Invalid Base32 address", 400); return; } fullAddress = address; @@ -329,7 +343,7 @@ namespace util if (!addr) { LogPrint ("Unknown address ", address); - SendReply ("" + itoopieImage + "
Unknown address " + address + ""); + SendReply ("" + itoopieImage + "
Unknown address " + address + "", 105); return; } destination = *addr; @@ -340,7 +354,7 @@ namespace util if (i2p::data::Base32ToByteStream(address.c_str(), address.length(), (uint8_t *)destination, 32) != 32) { LogPrint("Invalid Base32 address ", address); - SendReply("" + itoopieImage + "
Invalid Base32 address"); + SendReply("" + itoopieImage + "
Invalid Base32 address", 400); return; } fullAddress = address + ".b32.i2p"; @@ -355,7 +369,7 @@ namespace util leaseSet = i2p::data::netdb.FindLeaseSet (destination); if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet { - SendReply (leaseSet ? "" + itoopieImage + "
Leases expired" : "" + itoopieImage + "LeaseSet not found"); + SendReply (leaseSet ? "" + itoopieImage + "
Leases expired" : "" + itoopieImage + "LeaseSet not found", 504); return; } } @@ -388,13 +402,13 @@ namespace util else { if (m_Stream && m_Stream->IsOpen ()) - SendReply ("" + itoopieImage + "
Not responding"); + SendReply ("" + itoopieImage + "
Not responding", 504); else Terminate (); } } - void HTTPConnection::SendReply (const std::string& content) + void HTTPConnection::SendReply (const std::string& content, int status) { m_Reply.content = content; m_Reply.headers.resize(2); @@ -403,7 +417,7 @@ namespace util 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::asio::async_write (*m_Socket, m_Reply.to_buffers(status), boost::bind (&HTTPConnection::HandleWriteReply, this, boost::asio::placeholders::error)); } diff --git a/HTTPServer.h b/HTTPServer.h index df9ca4e7..cb594794 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -36,7 +36,7 @@ namespace util std::vector
headers; std::string content; - std::vector to_buffers(); + std::vector to_buffers (int status); }; public: @@ -53,7 +53,7 @@ namespace util 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 SendReply (const std::string& content, int status = 200); void HandleRequest (); void FillContent (std::stringstream& s);