From 87dd890eb054aabee7e6082d9dc50c91180b95d0 Mon Sep 17 00:00:00 2001 From: hagen Date: Thu, 14 Apr 2016 00:00:00 +0000 Subject: [PATCH] * HTTPConnection::reply : to_buffers() -> to_string() --- HTTPServer.cpp | 49 ++++++++++++++++++++----------------------------- HTTPServer.h | 2 +- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 2d69ae7a..4b426aa9 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -206,42 +206,33 @@ namespace util const char HTTP_HEADER_KV_SEP[] = ": "; const char HTTP_CRLF[] = "\r\n"; - std::vector HTTPConnection::reply::to_buffers(int status) + std::string HTTPConnection::reply::to_string(int code) { - std::vector buffers; + std::stringstream ss(""); if (headers.size () > 0) { - status_string = "HTTP/1.1 "; - status_string += std::to_string (status); - status_string += " "; - switch (status) + const char *status; + switch (code) { - case 105: status_string += "Name Not Resolved"; break; - case 200: status_string += "OK"; break; - case 400: status_string += "Bad Request"; break; - case 404: status_string += "Not Found"; break; - case 408: status_string += "Request Timeout"; break; - case 500: status_string += "Internal Server Error"; break; - case 502: status_string += "Bad Gateway"; break; - case 503: status_string += "Not Implemented"; break; - case 504: status_string += "Gateway Timeout"; break; - default: status_string += "WTF"; + case 105: status = "Name Not Resolved"; break; + case 200: status = "OK"; break; + case 400: status = "Bad Request"; break; + case 404: status = "Not Found"; break; + case 408: status = "Request Timeout"; break; + case 500: status = "Internal Server Error"; break; + case 502: status = "Bad Gateway"; break; + case 503: status = "Not Implemented"; break; + case 504: status = "Gateway Timeout"; break; + default: status = "WTF"; } - buffers.push_back(boost::asio::buffer(status_string, status_string.size())); - buffers.push_back(boost::asio::buffer(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(HTTP_HEADER_KV_SEP)); - buffers.push_back(boost::asio::buffer(h.value)); - buffers.push_back(boost::asio::buffer(HTTP_CRLF)); + ss << "HTTP/1.1 " << code << "" << status << HTTP_CRLF; + for (header & h : headers) { + ss << h.name << HTTP_HEADER_KV_SEP << h.value << HTTP_CRLF; } - buffers.push_back(boost::asio::buffer(HTTP_CRLF)); + ss << HTTP_CRLF; /* end of headers */ } - buffers.push_back(boost::asio::buffer(content)); - return buffers; + ss << content; + return ss.str(); } void HTTPConnection::Terminate () diff --git a/HTTPServer.h b/HTTPServer.h index f70e27dc..8dd40dea 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -40,7 +40,7 @@ namespace util { std::vector
headers; std::string status_string, content; - std::vector to_buffers (int status); + std::string to_string (int status); }; public: