summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2011-01-10 09:26:12 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2011-01-10 09:26:25 +0100
commitfcaf8ace27d3421a45df7842a8275ebe35a5dbe3 (patch)
tree1425b8f762d6f9e5b3c08543caf328a1cb8f6b7b
parent2dcf3091edfb316f4cf296fbeb1264aea633c09f (diff)
Catch exceptions in HTTP::Connection::handle_read() and return status 500.
-rw-r--r--http_connection.cpp55
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) {