changeset 895:7e8cde5905fd plugins

allow superposition of REST handlers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Jun 2014 17:47:58 +0200
parents 690aeb4cb899
children c4053ac5db04
files Core/HttpServer/EmbeddedResourceHttpHandler.cpp Core/HttpServer/EmbeddedResourceHttpHandler.h Core/HttpServer/FilesystemHttpHandler.cpp Core/HttpServer/FilesystemHttpHandler.h Core/HttpServer/HttpHandler.h Core/HttpServer/MongooseServer.cpp Core/HttpServer/MongooseServer.h Core/RestApi/RestApi.cpp Core/RestApi/RestApi.h
diffstat 9 files changed, 34 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- 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;
   } 
 }
--- 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,
--- 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;
   } 
 }
--- 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,
--- 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,
--- 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<HttpHandler*>& 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<HttpHandler*> handlers;
+      that->FindHandlers(handlers, uri);
+
+      bool found = false;
+
+      for (std::list<HttpHandler*>::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);
       }
--- 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<HttpHandler*>& result,
+                      const UriComponents& forUri) const;
 
     ChunkStore& GetChunkStore();
 
--- 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,
--- 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,