diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-01-02 21:45:20 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-01-02 21:45:46 +0100 |
commit | b72175dab679c14be80b6e5db7129f8d3b518079 (patch) | |
tree | 88d82aaed69f83c0fa68a2831a029527eea4a413 | |
parent | b53ea34b123721f70ae6e4ff4eb96e90e0664ba4 (diff) |
Add HTTP::Connection::send_error.
-rw-r--r-- | http_connection.cpp | 8 | ||||
-rw-r--r-- | http_connection.h | 7 | ||||
-rw-r--r-- | httpd.cpp | 2 |
3 files changed, 13 insertions, 4 deletions
diff --git a/http_connection.cpp b/http_connection.cpp index 6567f81..a86d2ac 100644 --- a/http_connection.cpp +++ b/http_connection.cpp @@ -3,6 +3,7 @@ #include "http.h" #include <boost/bind.hpp> +#include <boost/format.hpp> HTTP::Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) { } @@ -14,7 +15,8 @@ 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; - //return; + send_error(400, "Bad Request"); + return; } std::cout << "Path: " << std::endl; @@ -54,3 +56,7 @@ std::string HTTP::Connection::pop_path_base() { return base_path.back(); } + +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))); +} diff --git a/http_connection.h b/http_connection.h index 23cadc6..5a01273 100644 --- a/http_connection.h +++ b/http_connection.h @@ -43,8 +43,11 @@ namespace HTTP { //! Request headers. std::map<std::string, std::string> headers; - - tcp::socket socket; + + //! Send error. + void send_error(int code, std::string name); + + tcp::socket socket; private: Connection(boost::asio::io_service& io_service); void handle_write(const boost::system::error_code& error, size_t bytes_transferred); @@ -29,6 +29,6 @@ void HTTP::Server::handle_request(Connection::p connection) { // Call handler. handlers[handler](connection); } else { - // Error 404. + connection->send_error(404, "Not Found"); } } |