Browse Source

* HTTPConnection::reply : to_buffers() -> to_string()

pull/489/head
hagen 9 years ago
parent
commit
7cf171671d
  1. 51
      HTTPServer.cpp
  2. 4
      HTTPServer.h

51
HTTPServer.cpp

@ -212,42 +212,32 @@ namespace util
} // namespace misc_strings } // namespace misc_strings
std::vector<boost::asio::const_buffer> HTTPConnection::reply::to_buffers(int status) std::string HTTPConnection::reply::to_string(int code)
{ {
std::vector<boost::asio::const_buffer> buffers; std::stringstream ss("");
if (headers.size () > 0) if (headers.size () > 0)
{ {
status_string = "HTTP/1.1 "; const char *status;
status_string += std::to_string (status); switch (code)
status_string += " ";
switch (status)
{ {
case 105: status_string += "Name Not Resolved"; break; case 105: status = "Name Not Resolved"; break;
case 200: status_string += "OK"; break; case 200: status = "OK"; break;
case 400: status_string += "Bad Request"; break; case 400: status = "Bad Request"; break;
case 404: status_string += "Not Found"; break; case 404: status = "Not Found"; break;
case 408: status_string += "Request Timeout"; break; case 408: status = "Request Timeout"; break;
case 500: status_string += "Internal Server Error"; break; case 500: status = "Internal Server Error"; break;
case 502: status_string += "Bad Gateway"; break; case 502: status = "Bad Gateway"; break;
case 503: status_string += "Not Implemented"; break; case 503: status = "Not Implemented"; break;
case 504: status_string += "Gateway Timeout"; break; case 504: status = "Gateway Timeout"; break;
default: status_string += "WTF"; default: status = "WTF";
} }
buffers.push_back(boost::asio::buffer(status_string, status_string.size())); ss << "HTTP/1.1 " << code << "" << status << HTTP_CRLF;
buffers.push_back(boost::asio::buffer(misc_strings::crlf)); for (header & h : headers) {
ss << h.name << HTTP_HEADER_KV_SEP << h.value << HTTP_CRLF;
for (std::size_t i = 0; i < headers.size(); ++i)
{
header& h = headers[i];
buffers.push_back(boost::asio::buffer(h.name));
buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
buffers.push_back(boost::asio::buffer(h.value));
buffers.push_back(boost::asio::buffer(misc_strings::crlf));
} }
buffers.push_back(boost::asio::buffer(misc_strings::crlf)); ss << HTTP_CRLF; /* end of headers */
} }
buffers.push_back(boost::asio::buffer(content)); return ss.str();
return buffers;
} }
void HTTPConnection::Terminate () void HTTPConnection::Terminate ()
@ -914,8 +904,9 @@ namespace util
m_Reply.headers[2].name = "Content-Type"; m_Reply.headers[2].name = "Content-Type";
m_Reply.headers[2].value = "text/html"; m_Reply.headers[2].value = "text/html";
} }
std::string res = m_Reply.to_string(status);
boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status), boost::asio::async_write (*m_Socket, res,
std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1)); std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1));
} }

4
HTTPServer.h

@ -39,8 +39,8 @@ namespace util
struct reply struct reply
{ {
std::vector<header> headers; std::vector<header> headers;
std::string status_string, content; std::string status;
std::vector<boost::asio::const_buffer> to_buffers (int status); std::string to_string (int code);
}; };
public: public:

Loading…
Cancel
Save