From 2dcf3091edfb316f4cf296fbeb1264aea633c09f Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 10 Jan 2011 08:44:27 +0100 Subject: Added response_map for http response codes. --- http_connection.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'http_connection.cpp') 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 #include -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

%1$d %2$s

") % code % name))); +namespace response_map_init { + typedef std::pair 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 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

%1$d %2$s

") % 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; } -- cgit v1.2.3