Mercurial > hg > orthanc
comparison Core/HttpServer/MongooseServer.cpp @ 1645:1558b3226b18
IHttpExceptionFormatter
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 24 Sep 2015 15:55:17 +0200 |
parents | 939b921b2c81 |
children | 8040d56cb0b3 |
comparison
equal
deleted
inserted
replaced
1644:939b921b2c81 | 1645:1558b3226b18 |
---|---|
34 | 34 |
35 #include "../PrecompiledHeaders.h" | 35 #include "../PrecompiledHeaders.h" |
36 #include "MongooseServer.h" | 36 #include "MongooseServer.h" |
37 | 37 |
38 #include "../Logging.h" | 38 #include "../Logging.h" |
39 #include "../OrthancException.h" | |
40 #include "../ChunkedBuffer.h" | 39 #include "../ChunkedBuffer.h" |
41 #include "HttpToolbox.h" | 40 #include "HttpToolbox.h" |
42 #include "mongoose.h" | 41 #include "mongoose.h" |
43 | 42 |
44 #include <algorithm> | 43 #include <algorithm> |
581 { | 580 { |
582 MongooseServer* that = reinterpret_cast<MongooseServer*>(request->user_data); | 581 MongooseServer* that = reinterpret_cast<MongooseServer*>(request->user_data); |
583 | 582 |
584 MongooseOutputStream stream(connection); | 583 MongooseOutputStream stream(connection); |
585 HttpOutput output(stream, that->IsKeepAliveEnabled()); | 584 HttpOutput output(stream, that->IsKeepAliveEnabled()); |
586 output.SetDescribeErrorsEnabled(that->IsDescribeErrorsEnabled()); | |
587 | 585 |
588 // Check remote calls | 586 // Check remote calls |
589 if (!that->IsRemoteAccessAllowed() && | 587 if (!that->IsRemoteAccessAllowed() && |
590 request->remote_ip != LOCALHOST) | 588 request->remote_ip != LOCALHOST) |
591 { | 589 { |
733 method, uri, headers, argumentsGET, body.c_str(), body.size()); | 731 method, uri, headers, argumentsGET, body.c_str(), body.size()); |
734 } | 732 } |
735 } | 733 } |
736 catch (boost::bad_lexical_cast&) | 734 catch (boost::bad_lexical_cast&) |
737 { | 735 { |
738 throw OrthancException(ErrorCode_BadParameterType, HttpStatus_400_BadRequest); | 736 throw OrthancException(ErrorCode_BadParameterType); |
739 } | 737 } |
740 catch (std::runtime_error&) | 738 catch (std::runtime_error&) |
741 { | 739 { |
742 // Presumably an error while parsing the JSON body | 740 // Presumably an error while parsing the JSON body |
743 throw OrthancException(ErrorCode_BadRequest, HttpStatus_400_BadRequest); | 741 throw OrthancException(ErrorCode_BadRequest); |
744 } | 742 } |
745 | 743 |
746 if (!found) | 744 if (!found) |
747 { | 745 { |
748 throw OrthancException(ErrorCode_UnknownResource); | 746 throw OrthancException(ErrorCode_UnknownResource); |
751 catch (OrthancException& e) | 749 catch (OrthancException& e) |
752 { | 750 { |
753 // Using this candidate handler results in an exception | 751 // Using this candidate handler results in an exception |
754 LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); | 752 LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); |
755 | 753 |
756 Json::Value message = Json::objectValue; | |
757 message["Method"] = EnumerationToString(method); | |
758 message["Uri"] = request->uri; | |
759 | |
760 // TODO | |
761 message["HttpError"] = EnumerationToString(e.GetHttpStatus()); | |
762 message["HttpStatus"] = e.GetHttpStatus(); | |
763 message["Message"] = e.What(); | |
764 message["OrthancError"] = EnumerationToString(e.GetErrorCode()); | |
765 message["OrthancStatus"] = e.GetErrorCode(); | |
766 | |
767 std::string info = message.toStyledString(); | |
768 | |
769 try | 754 try |
770 { | 755 { |
771 output.SendStatus(e.GetHttpStatus(), info); | 756 if (that->GetExceptionFormatter() == NULL) |
757 { | |
758 output.SendStatus(e.GetHttpStatus()); | |
759 } | |
760 else | |
761 { | |
762 that->GetExceptionFormatter()->Format(output, e, method, request->uri); | |
763 } | |
772 } | 764 } |
773 catch (OrthancException&) | 765 catch (OrthancException&) |
774 { | 766 { |
775 // An exception here reflects the fact that the status code | 767 // An exception here reflects the fact that the status code |
776 // was already set by the HTTP handler. | 768 // was already set by the HTTP handler. |
832 ssl_ = false; | 824 ssl_ = false; |
833 port_ = 8000; | 825 port_ = 8000; |
834 filter_ = NULL; | 826 filter_ = NULL; |
835 keepAlive_ = false; | 827 keepAlive_ = false; |
836 httpCompression_ = true; | 828 httpCompression_ = true; |
837 describeErrors_ = true; | 829 exceptionFormatter_ = NULL; |
838 | 830 |
839 #if ORTHANC_SSL_ENABLED == 1 | 831 #if ORTHANC_SSL_ENABLED == 1 |
840 // Check for the Heartbleed exploit | 832 // Check for the Heartbleed exploit |
841 // https://en.wikipedia.org/wiki/OpenSSL#Heartbleed_bug | 833 // https://en.wikipedia.org/wiki/OpenSSL#Heartbleed_bug |
842 if (OPENSSL_VERSION_NUMBER < 0x1000107fL /* openssl-1.0.1g */ && | 834 if (OPENSSL_VERSION_NUMBER < 0x1000107fL /* openssl-1.0.1g */ && |
983 Stop(); | 975 Stop(); |
984 httpCompression_ = enabled; | 976 httpCompression_ = enabled; |
985 LOG(WARNING) << "HTTP compression is " << (enabled ? "enabled" : "disabled"); | 977 LOG(WARNING) << "HTTP compression is " << (enabled ? "enabled" : "disabled"); |
986 } | 978 } |
987 | 979 |
988 void MongooseServer::SetDescribeErrorsEnabled(bool enabled) | |
989 { | |
990 describeErrors_ = enabled; | |
991 LOG(INFO) << "Description of the errors in the HTTP answers is " << (enabled ? "enabled" : "disabled"); | |
992 } | |
993 | |
994 void MongooseServer::SetIncomingHttpRequestFilter(IIncomingHttpRequestFilter& filter) | 980 void MongooseServer::SetIncomingHttpRequestFilter(IIncomingHttpRequestFilter& filter) |
995 { | 981 { |
996 Stop(); | 982 Stop(); |
997 filter_ = &filter; | 983 filter_ = &filter; |
998 } | 984 } |
999 | 985 |
986 | |
987 void MongooseServer::SetHttpExceptionFormatter(IHttpExceptionFormatter& formatter) | |
988 { | |
989 Stop(); | |
990 exceptionFormatter_ = &formatter; | |
991 } | |
992 | |
993 | |
1000 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const | 994 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const |
1001 { | 995 { |
1002 return registeredUsers_.find(basic) != registeredUsers_.end(); | 996 return registeredUsers_.find(basic) != registeredUsers_.end(); |
1003 } | 997 } |
1004 | 998 |