changeset 1962:22ddb22fce83

sample plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 Apr 2016 17:42:20 +0200
parents ef1e9856c26f
children af0c90ae0915
files OrthancServer/main.cpp Plugins/Engine/OrthancPlugins.cpp Plugins/Include/orthanc/OrthancCPlugin.h Plugins/Samples/Basic/Plugin.c
diffstat 4 files changed, 49 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/main.cpp	Thu Apr 07 17:26:13 2016 +0200
+++ b/OrthancServer/main.cpp	Thu Apr 07 17:42:20 2016 +0200
@@ -278,9 +278,13 @@
 {
 private:
   ServerContext& context_;
+  OrthancPlugins*  plugins_;
 
 public:
-  MyIncomingHttpRequestFilter(ServerContext& context) : context_(context)
+  MyIncomingHttpRequestFilter(ServerContext& context,
+                              OrthancPlugins* plugins) : 
+    context_(context),
+    plugins_(plugins)
   {
   }
 
@@ -290,6 +294,12 @@
                          const char* username,
                          const IHttpHandler::Arguments& httpHeaders) const
   {
+    if (plugins_ != NULL &&
+        !plugins_->IsAllowed(method, uri, ip, username, httpHeaders))
+    {
+      return false;
+    }
+
     static const char* HTTP_FILTER = "IncomingHttpRequestFilter";
 
     LuaScripting::Locker locker(context_.GetLua());
@@ -679,7 +689,7 @@
   
 
   // HTTP server
-  MyIncomingHttpRequestFilter httpFilter(context);
+  MyIncomingHttpRequestFilter httpFilter(context, plugins);
   MongooseServer httpServer;
   httpServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("HttpPort", 8042));
   httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false));
--- a/Plugins/Engine/OrthancPlugins.cpp	Thu Apr 07 17:26:13 2016 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Thu Apr 07 17:42:20 2016 +0200
@@ -2431,18 +2431,15 @@
                                  const char* username,
                                  const IHttpHandler::Arguments& httpHeaders) const
   {
-    std::vector<const char*> httpKeys;
-    std::vector<const char*> httpValues;
-
-    httpKeys.reserve(httpHeaders.size());
-    httpValues.reserve(httpHeaders.size());
+    std::vector<const char*> httpKeys(httpHeaders.size());
+    std::vector<const char*> httpValues(httpHeaders.size());
 
     size_t pos = 0;
     for (IHttpHandler::Arguments::const_iterator
            it = httpHeaders.begin(); it != httpHeaders.end(); ++it, pos++)
     {
       httpKeys[pos] = it->first.c_str();
-      httpValues[pos] = it->first.c_str();
+      httpValues[pos] = it->second.c_str();
     }
 
     OrthancPluginHttpMethod cMethod = Plugins::Convert(method);
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Apr 07 17:26:13 2016 +0200
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Apr 07 17:42:20 2016 +0200
@@ -974,7 +974,7 @@
     OrthancPluginHttpMethod  method,
     const char*              uri,
     const char*              ip,
-    const uint32_t           headersCount,
+    uint32_t                 headersCount,
     const char* const*       headersKeys,
     const char* const*       headersValues);
 
--- a/Plugins/Samples/Basic/Plugin.c	Thu Apr 07 17:26:13 2016 +0200
+++ b/Plugins/Samples/Basic/Plugin.c	Thu Apr 07 17:42:20 2016 +0200
@@ -322,6 +322,38 @@
 }
 
 
+ORTHANC_PLUGINS_API int32_t FilterIncomingHttpRequest(OrthancPluginHttpMethod  method,
+                                                      const char*              uri,
+                                                      const char*              ip,
+                                                      uint32_t                 headersCount,
+                                                      const char* const*       headersKeys,
+                                                      const char* const*       headersValues)
+{
+  uint32_t i;
+
+  if (headersCount > 0)
+  {
+    OrthancPluginLogInfo(context, "HTTP headers of an incoming REST request:");
+    for (i = 0; i < headersCount; i++)
+    {
+      char info[1024];
+      sprintf(info, "  %s: %s", headersKeys[i], headersValues[i]);
+      OrthancPluginLogInfo(context, info);
+    }
+  }
+
+  if (method == OrthancPluginHttpMethod_Get ||
+      method == OrthancPluginHttpMethod_Post)
+  {
+    return 1;  /* Allowed */
+  }
+  else
+  {
+    return 0;  /* Only allow GET and POST requests */
+  }
+}
+
+
 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
 {
   OrthancPluginMemoryBuffer tmp;
@@ -384,8 +416,8 @@
   OrthancPluginRegisterRestCallback(context, "/plugin/create", CallbackCreateDicom);
 
   OrthancPluginRegisterOnStoredInstanceCallback(context, OnStoredCallback);
-
   OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);
+  OrthancPluginRegisterIncomingHttpRequestFilter(context, FilterIncomingHttpRequest);
 
   /* Declare several properties of the plugin */
   OrthancPluginSetRootUri(context, "/plugin/hello");