diff options
-rw-r--r-- | http_connection.cpp | 55 |
1 files 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<std::string, std::string>::iterator it = args.begin(); it != args.end(); it++) { + std::cout << " " << it->first << " = " << it->second << std::endl; + } + + std::cout << "Headers: " << std::endl; + for(std::map<std::string, std::string>::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<std::string, std::string>::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<std::string, std::string>::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) { |