From fcaf8ace27d3421a45df7842a8275ebe35a5dbe3 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 10 Jan 2011 09:26:12 +0100 Subject: Catch exceptions in HTTP::Connection::handle_read() and return status 500. --- http_connection.cpp | 55 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/http_connection.cpp b/http_connection.cpp index a16d237..f04dc28 100644 --- a/http_connection.cpp +++ b/http_connection.cpp @@ -59,29 +59,42 @@ void HTTP::Connection::handle_write(const boost::system::error_code& error, size } void HTTP::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred, Handler callback) { - if(!parse_request(buf)) { - // Request parse error. - std::cout << "Request parse error." << std::endl; - send_error(400); - return; - } - - std::cout << "Path: " << std::endl; - for(PathList::iterator it = path.begin(); it != path.end(); it++) { - std::cout << " " << *it << std::endl; - } + try { + if(!parse_request(buf)) { + // Request parse error. + std::cout << "Request parse error." << std::endl; + send_error(400); + return; + } + + std::cout << "Path: " << std::endl; + for(PathList::iterator it = path.begin(); it != path.end(); it++) { + std::cout << " " << *it << std::endl; + } + + std::cout << "Args: " << std::endl; + for(std::map::iterator it = args.begin(); it != args.end(); it++) { + std::cout << " " << it->first << " = " << it->second << std::endl; + } + + std::cout << "Headers: " << std::endl; + for(std::map::iterator it = headers.begin(); it != headers.end(); it++) { + std::cout << " " << it->first << " = " << it->second << std::endl; + } + + callback(shared_from_this()); - std::cout << "Args: " << std::endl; - for(std::map::iterator it = args.begin(); it != args.end(); it++) { - std::cout << " " << it->first << " = " << it->second << std::endl; + // Catch unhandled exceptions. + } catch(std::exception& e) { + std::cerr << "Unhandled exception while handling request: " << e.what() << std::endl; + + try { + send_error(500); + + } catch(...) { + // Ignored. + } } - - std::cout << "Headers: " << std::endl; - for(std::map::iterator it = headers.begin(); it != headers.end(); it++) { - std::cout << " " << it->first << " = " << it->second << std::endl; - } - - callback(shared_from_this()); } void HTTP::Connection::read_request(Handler callback) { -- cgit v1.2.3