# HG changeset patch # User Sebastien Jodogne # Date 1435743019 -7200 # Node ID 895ab369d63c5ebd9f371759b24920c23ddd7ffe # Parent 4ff8dd753d79958de148f6caecde5c660b698bc3 refactoring: OrthancHttpHandler diff -r 4ff8dd753d79 -r 895ab369d63c Core/HttpServer/MongooseServer.cpp --- a/Core/HttpServer/MongooseServer.cpp Wed Jul 01 11:08:23 2015 +0200 +++ b/Core/HttpServer/MongooseServer.cpp Wed Jul 01 11:30:19 2015 +0200 @@ -552,6 +552,7 @@ const struct mg_request_info *request) { MongooseServer* that = reinterpret_cast(request->user_data); + MongooseOutputStream stream(connection); HttpOutput output(stream, that->IsKeepAliveEnabled()); @@ -680,61 +681,60 @@ } - // Loop over the candidate handlers for this URI LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); + bool found = false; - for (MongooseServer::Handlers::const_iterator it = - that->GetHandlers().begin(); it != that->GetHandlers().end() && !found; ++it) + try { + if (that->HasHandler()) + { + found = that->GetHandler().Handle(output, method, uri, headers, argumentsGET, body); + } + } + catch (OrthancException& e) + { + // Using this candidate handler results in an exception + LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); + try { - found = (*it)->Handle(output, method, uri, headers, argumentsGET, body); - } - catch (OrthancException& e) - { - // Using this candidate handler results in an exception - LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); - - try + switch (e.GetErrorCode()) { - switch (e.GetErrorCode()) - { - case ErrorCode_InexistentFile: - case ErrorCode_InexistentItem: - case ErrorCode_UnknownResource: - output.SendStatus(HttpStatus_404_NotFound); - break; + case ErrorCode_InexistentFile: + case ErrorCode_InexistentItem: + case ErrorCode_UnknownResource: + output.SendStatus(HttpStatus_404_NotFound); + break; - case ErrorCode_BadRequest: - case ErrorCode_UriSyntax: - output.SendStatus(HttpStatus_400_BadRequest); - break; + case ErrorCode_BadRequest: + case ErrorCode_UriSyntax: + output.SendStatus(HttpStatus_400_BadRequest); + break; - default: - output.SendStatus(HttpStatus_500_InternalServerError); - } + default: + output.SendStatus(HttpStatus_500_InternalServerError); } - catch (OrthancException&) - { - // An exception here reflects the fact that an exception was - // triggered after the status code was sent by the HTTP handler. - } - - return; + } + catch (OrthancException&) + { + // An exception here reflects the fact that an exception was + // triggered after the status code was sent by the HTTP handler. } - catch (boost::bad_lexical_cast&) - { - LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast"; - output.SendStatus(HttpStatus_400_BadRequest); - return; - } - catch (std::runtime_error&) - { - LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request"; - output.SendStatus(HttpStatus_400_BadRequest); - return; - } + + return; + } + catch (boost::bad_lexical_cast&) + { + LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast"; + output.SendStatus(HttpStatus_400_BadRequest); + return; + } + catch (std::runtime_error&) + { + LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request"; + output.SendStatus(HttpStatus_400_BadRequest); + return; } if (!found) @@ -789,6 +789,7 @@ MongooseServer::MongooseServer() : pimpl_(new PImpl) { pimpl_->context_ = NULL; + handler_ = NULL; remoteAllowed_ = false; authentication_ = false; ssl_ = false; @@ -811,7 +812,6 @@ MongooseServer::~MongooseServer() { Stop(); - ClearHandlers(); } @@ -876,20 +876,6 @@ } - void MongooseServer::RegisterHandler(IHttpHandler& handler) - { - Stop(); - - handlers_.push_back(&handler); - } - - - void MongooseServer::ClearHandlers() - { - Stop(); - } - - void MongooseServer::ClearUsers() { Stop(); @@ -963,4 +949,22 @@ { return registeredUsers_.find(basic) != registeredUsers_.end(); } + + + void MongooseServer::Register(IHttpHandler& handler) + { + Stop(); + handler_ = &handler; + } + + + IHttpHandler& MongooseServer::GetHandler() const + { + if (handler_ == NULL) + { + throw OrthancException(ErrorCode_InternalError); + } + + return *handler_; + } } diff -r 4ff8dd753d79 -r 895ab369d63c Core/HttpServer/MongooseServer.h --- a/Core/HttpServer/MongooseServer.h Wed Jul 01 11:08:23 2015 +0200 +++ b/Core/HttpServer/MongooseServer.h Wed Jul 01 11:30:19 2015 +0200 @@ -59,15 +59,12 @@ class MongooseServer { - public: - typedef std::list Handlers; - private: // http://stackoverflow.com/questions/311166/stdauto-ptr-or-boostshared-ptr-for-pimpl-idiom struct PImpl; boost::shared_ptr pimpl_; - Handlers handlers_; + IHttpHandler *handler_; typedef std::set RegisteredUsers; RegisteredUsers registeredUsers_; @@ -103,8 +100,6 @@ void RegisterUser(const char* username, const char* password); - void RegisterHandler(IHttpHandler& handler); - bool IsAuthenticationEnabled() const { return authentication_; @@ -147,15 +142,17 @@ void SetIncomingHttpRequestFilter(IIncomingHttpRequestFilter& filter); - void ClearHandlers(); - ChunkStore& GetChunkStore(); bool IsValidBasicHttpAuthentication(const std::string& basic) const; - const Handlers& GetHandlers() const + void Register(IHttpHandler& handler); + + bool HasHandler() const { - return handlers_; + return handler_ != NULL; } + + IHttpHandler& GetHandler() const; }; } diff -r 4ff8dd753d79 -r 895ab369d63c OrthancServer/OrthancHttpHandler.cpp --- a/OrthancServer/OrthancHttpHandler.cpp Wed Jul 01 11:08:23 2015 +0200 +++ b/OrthancServer/OrthancHttpHandler.cpp Wed Jul 01 11:30:19 2015 +0200 @@ -57,8 +57,8 @@ } - void OrthancHttpHandler::RegisterHandler(IHttpHandler& handler, - bool isOrthancRestApi) + void OrthancHttpHandler::Register(IHttpHandler& handler, + bool isOrthancRestApi) { handlers_.push_back(&handler); diff -r 4ff8dd753d79 -r 895ab369d63c OrthancServer/OrthancHttpHandler.h --- a/OrthancServer/OrthancHttpHandler.h Wed Jul 01 11:08:23 2015 +0200 +++ b/OrthancServer/OrthancHttpHandler.h Wed Jul 01 11:30:19 2015 +0200 @@ -56,8 +56,8 @@ const GetArguments& getArguments, const std::string& body); - void RegisterHandler(IHttpHandler& handler, - bool isOrthancRestApi); + void Register(IHttpHandler& handler, + bool isOrthancRestApi); bool HasOrthancRestApi() const { diff -r 4ff8dd753d79 -r 895ab369d63c OrthancServer/main.cpp --- a/OrthancServer/main.cpp Wed Jul 01 11:08:23 2015 +0200 +++ b/OrthancServer/main.cpp Wed Jul 01 11:30:19 2015 +0200 @@ -51,6 +51,7 @@ #include "ServerToolbox.h" #include "../Plugins/Engine/OrthancPlugins.h" #include "FromDcmtkBridge.h" +#include "OrthancHttpHandler.h" using namespace Orthanc; @@ -433,6 +434,27 @@ context->GetIndex().SetMaximumStorageSize(0); } + + OrthancHttpHandler httpHandler; + +#if ENABLE_PLUGINS == 1 + OrthancRestApi restApi(*context); + plugins.SetServerContext(*context); + httpHandler.Register(plugins, false); + context->SetPlugins(plugins); +#endif + +#if ORTHANC_STANDALONE == 1 + EmbeddedResourceHttpHandler staticResources("/app", EmbeddedResources::ORTHANC_EXPLORER); +#else + FilesystemHttpHandler staticResources("/app", ORTHANC_PATH "/OrthancExplorer"); +#endif + + httpHandler.Register(staticResources, false); + httpHandler.Register(restApi, true); + + + MyDicomServerFactory serverFactory(*context); bool isReset = false; @@ -471,22 +493,7 @@ httpServer.SetSslEnabled(false); } - OrthancRestApi restApi(*context); - -#if ORTHANC_STANDALONE == 1 - EmbeddedResourceHttpHandler staticResources("/app", EmbeddedResources::ORTHANC_EXPLORER); -#else - FilesystemHttpHandler staticResources("/app", ORTHANC_PATH "/OrthancExplorer"); -#endif - -#if ENABLE_PLUGINS == 1 - plugins.SetServerContext(*context); - httpServer.RegisterHandler(plugins); - context->SetPlugins(plugins); -#endif - - httpServer.RegisterHandler(staticResources); - httpServer.RegisterHandler(restApi); + httpServer.Register(httpHandler); #if ENABLE_PLUGINS == 1