diff options
Diffstat (limited to 'http_connection.cpp')
-rw-r--r-- | http_connection.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/http_connection.cpp b/http_connection.cpp index ff7e795..a16d237 100644 --- a/http_connection.cpp +++ b/http_connection.cpp @@ -5,8 +5,23 @@ #include <boost/bind.hpp> #include <boost/format.hpp> -void HTTP::Connection::send_error(int code, std::string name) { - boost::asio::write(socket, boost::asio::buffer(boost::str(boost::format("HTTP/1.1 %1$d %2$s\r\n\r\n<h1>%1$d %2$s</h1>") % code % name))); +namespace response_map_init { + typedef std::pair<int, std::string> P; + const P m[] = { + P(200, "OK"), + P(400, "Bad Request"), + P(404, "Not Found"), + P(500, "Internal Server Error"), + P(501, "Not Implemented") + }; + const P* begin = m; + const P* end = m + sizeof(m) / sizeof(P); +} + +const std::map<int, std::string> response_map(response_map_init::begin, response_map_init::end); + +void HTTP::Connection::send_error(int code) { + boost::asio::write(socket, boost::asio::buffer(boost::str(boost::format("HTTP/1.1 %1$d %2$s\r\n\r\n<h1>%1$d %2$s</h1>") % code % response_map.find(code)->second))); } void HTTP::Connection::add_header(std::string key, std::string value) { @@ -47,7 +62,7 @@ void HTTP::Connection::handle_read(const boost::system::error_code& error, size_ if(!parse_request(buf)) { // Request parse error. std::cout << "Request parse error." << std::endl; - send_error(400, "Bad Request"); + send_error(400); return; } |