# HG changeset patch # User Sebastien Jodogne # Date 1402933678 -7200 # Node ID 7e8cde5905fd3d8d7e8943ae744d95e8b7edefdd # Parent 690aeb4cb89964922551896e90eed247596307c1 allow superposition of REST handlers diff -r 690aeb4cb899 -r 7e8cde5905fd Core/HttpServer/EmbeddedResourceHttpHandler.cpp --- a/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Mon Jun 16 17:47:58 2014 +0200 @@ -57,7 +57,7 @@ } - void EmbeddedResourceHttpHandler::Handle( + bool EmbeddedResourceHttpHandler::Handle( HttpOutput& output, HttpMethod method, const UriComponents& uri, @@ -68,7 +68,7 @@ if (method != HttpMethod_Get) { output.SendMethodNotAllowedError("GET"); - return; + return true; } std::string resourcePath = Toolbox::FlattenUri(uri, baseUri_.size()); @@ -85,5 +85,7 @@ LOG(WARNING) << "Unable to find HTTP resource: " << resourcePath; output.SendHeader(HttpStatus_404_NotFound); } + + return true; } } diff -r 690aeb4cb899 -r 7e8cde5905fd Core/HttpServer/EmbeddedResourceHttpHandler.h --- a/Core/HttpServer/EmbeddedResourceHttpHandler.h Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/HttpServer/EmbeddedResourceHttpHandler.h Mon Jun 16 17:47:58 2014 +0200 @@ -52,7 +52,7 @@ virtual bool IsServedUri(const UriComponents& uri); - virtual void Handle( + virtual bool Handle( HttpOutput& output, HttpMethod method, const UriComponents& uri, diff -r 690aeb4cb899 -r 7e8cde5905fd Core/HttpServer/FilesystemHttpHandler.cpp --- a/Core/HttpServer/FilesystemHttpHandler.cpp Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/HttpServer/FilesystemHttpHandler.cpp Mon Jun 16 17:47:58 2014 +0200 @@ -126,7 +126,7 @@ } - void FilesystemHttpHandler::Handle( + bool FilesystemHttpHandler::Handle( HttpOutput& output, HttpMethod method, const UriComponents& uri, @@ -137,7 +137,7 @@ if (method != HttpMethod_Get) { output.SendMethodNotAllowedError("GET"); - return; + return true; } namespace fs = boost::filesystem; @@ -164,5 +164,7 @@ { output.SendHeader(HttpStatus_404_NotFound); } + + return true; } } diff -r 690aeb4cb899 -r 7e8cde5905fd Core/HttpServer/FilesystemHttpHandler.h --- a/Core/HttpServer/FilesystemHttpHandler.h Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/HttpServer/FilesystemHttpHandler.h Mon Jun 16 17:47:58 2014 +0200 @@ -54,7 +54,7 @@ virtual bool IsServedUri(const UriComponents& uri); - virtual void Handle( + virtual bool Handle( HttpOutput& output, HttpMethod method, const UriComponents& uri, diff -r 690aeb4cb899 -r 7e8cde5905fd Core/HttpServer/HttpHandler.h --- a/Core/HttpServer/HttpHandler.h Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/HttpServer/HttpHandler.h Mon Jun 16 17:47:58 2014 +0200 @@ -52,7 +52,7 @@ virtual bool IsServedUri(const UriComponents& uri) = 0; - virtual void Handle(HttpOutput& output, + virtual bool Handle(HttpOutput& output, HttpMethod method, const UriComponents& uri, const Arguments& headers, diff -r 690aeb4cb899 -r 7e8cde5905fd Core/HttpServer/MongooseServer.cpp --- a/Core/HttpServer/MongooseServer.cpp Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/HttpServer/MongooseServer.cpp Mon Jun 16 17:47:58 2014 +0200 @@ -255,18 +255,17 @@ - HttpHandler* MongooseServer::FindHandler(const UriComponents& forUri) const + void MongooseServer::FindHandlers(std::list& result, + const UriComponents& forUri) const { for (Handlers::const_iterator it = handlers_.begin(); it != handlers_.end(); ++it) { if ((*it)->IsServedUri(forUri)) { - return *it; + result.push_back(*it); } } - - return NULL; } @@ -703,31 +702,40 @@ } - HttpHandler* handler = that->FindHandler(uri); - if (handler) + std::list handlers; + that->FindHandlers(handlers, uri); + + bool found = false; + + for (std::list::const_iterator + it = handlers.begin(); it != handlers.end() && !found; it++) { try { LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); - handler->Handle(output, method, uri, headers, argumentsGET, body); + found = (*it)->Handle(output, method, uri, headers, argumentsGET, body); } catch (OrthancException& e) { LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]"; - output.SendHeader(HttpStatus_500_InternalServerError); + output.SendHeader(HttpStatus_500_InternalServerError); + found = true; } catch (boost::bad_lexical_cast&) { LOG(ERROR) << "MongooseServer Exception: Bad lexical cast"; output.SendHeader(HttpStatus_400_BadRequest); + found = true; } catch (std::runtime_error&) { LOG(ERROR) << "MongooseServer Exception: Presumably a bad JSON request"; output.SendHeader(HttpStatus_400_BadRequest); + found = true; } } - else + + if (!found) { output.SendHeader(HttpStatus_404_NotFound); } diff -r 690aeb4cb899 -r 7e8cde5905fd Core/HttpServer/MongooseServer.h --- a/Core/HttpServer/MongooseServer.h Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/HttpServer/MongooseServer.h Mon Jun 16 17:47:58 2014 +0200 @@ -139,8 +139,8 @@ void ClearHandlers(); - // Can return NULL if no handler is associated to this URI - HttpHandler* FindHandler(const UriComponents& forUri) const; + void FindHandlers(std::list& result, + const UriComponents& forUri) const; ChunkStore& GetChunkStore(); diff -r 690aeb4cb899 -r 7e8cde5905fd Core/RestApi/RestApi.cpp --- a/Core/RestApi/RestApi.cpp Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/RestApi/RestApi.cpp Mon Jun 16 17:47:58 2014 +0200 @@ -180,7 +180,7 @@ IsDeleteAccepted(uri)); } - void RestApi::Handle(HttpOutput& output, + bool RestApi::Handle(HttpOutput& output, HttpMethod method, const UriComponents& uri, const Arguments& headers, @@ -255,6 +255,8 @@ << " not allowed on: " << Toolbox::FlattenUri(uri); output.SendMethodNotAllowedError(GetAcceptedMethods(uri)); } + + return true; } void RestApi::Register(const std::string& path, diff -r 690aeb4cb899 -r 7e8cde5905fd Core/RestApi/RestApi.h --- a/Core/RestApi/RestApi.h Mon Jun 16 17:31:09 2014 +0200 +++ b/Core/RestApi/RestApi.h Mon Jun 16 17:47:58 2014 +0200 @@ -273,7 +273,7 @@ virtual bool IsServedUri(const UriComponents& uri); - virtual void Handle(HttpOutput& output, + virtual bool Handle(HttpOutput& output, HttpMethod method, const UriComponents& uri, const Arguments& headers,