From b72175dab679c14be80b6e5db7129f8d3b518079 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sun, 2 Jan 2011 21:45:20 +0100 Subject: Add HTTP::Connection::send_error. --- http_connection.cpp | 8 +++++++- http_connection.h | 7 +++++-- 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 +#include 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

%1$d %2$s

") % 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 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); diff --git a/httpd.cpp b/httpd.cpp index 4d6ed02..22ca999 100644 --- a/httpd.cpp +++ b/httpd.cpp @@ -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"); } } -- cgit v1.2.3