changeset 1594:2bac60a4f584

OrthancPluginSendHttpStatus
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 27 Aug 2015 12:56:48 +0200
parents 235d89817b89
children e1e54a73ba8b
files NEWS Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/Include/orthanc/OrthancCPlugin.h
diffstat 4 files changed, 74 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Aug 27 12:32:09 2015 +0200
+++ b/NEWS	Thu Aug 27 12:56:48 2015 +0200
@@ -7,6 +7,7 @@
 * "limit" and "since" arguments while retrieving DICOM resources in the REST API
 * Support of "deflate" and "gzip" content-types in HTTP requests
 * Options to validate peers against CA certificates in HTTPS requests
+* New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
 
 Lua
 ---
@@ -23,15 +24,15 @@
 * New function "OrthancPluginReadFile()" to read files from the filesystem
 * New function "OrthancPluginWriteFile()" to write files to the filesystem
 * New function "OrthancPluginGetErrorDescription()" to convert error codes to strings
+* New function "OrthancPluginSendHttpStatus()" to send HTTP status with a body
 * Plugins have access to explicit error codes 
 
 Maintenance
 -----------
 
-* New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
-* Improved error codes
 * Many code refactorings
-* If error while calling the REST API, the answer body contains an error description
+* Improved error codes (no more custom descriptions in exceptions)
+* If error while calling the REST API, the answer body contains description of the error
   (this feature can be disabled with the "HttpDescribeErrors" option)
 * Upgrade to curl 7.44.0 for static and Windows builds
 * Upgrade to libcurl 1.0.2d for static and Windows builds
--- a/Plugins/Engine/OrthancPlugins.cpp	Thu Aug 27 12:32:09 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Thu Aug 27 12:56:48 2015 +0200
@@ -522,6 +522,25 @@
   }
 
 
+  void OrthancPlugins::SendHttpStatus(const void* parameters)
+  {
+    const _OrthancPluginSendHttpStatus& p = 
+      *reinterpret_cast<const _OrthancPluginSendHttpStatus*>(parameters);
+
+    HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(p.output);
+    HttpStatus status = static_cast<HttpStatus>(p.status);
+
+    if (p.bodySize > 0 && p.body != NULL)
+    {
+      translatedOutput->SendStatus(status, p.body, p.bodySize);
+    }
+    else
+    {
+      translatedOutput->SendStatus(status);
+    }
+  }
+
+
   void OrthancPlugins::SendUnauthorized(const void* parameters)
   {
     const _OrthancPluginOutputPlusArgument& p = 
@@ -1043,6 +1062,10 @@
         SendMethodNotAllowed(parameters);
         return true;
 
+      case _OrthancPluginService_SendHttpStatus:
+        SendHttpStatus(parameters);
+        return true;
+
       case _OrthancPluginService_SendHttpStatusCode:
         SendHttpStatusCode(parameters);
         return true;
--- a/Plugins/Engine/OrthancPlugins.h	Thu Aug 27 12:32:09 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.h	Thu Aug 27 12:56:48 2015 +0200
@@ -85,6 +85,8 @@
 
     void SendHttpStatusCode(const void* parameters);
 
+    void SendHttpStatus(const void* parameters);
+
     void SendUnauthorized(const void* parameters);
 
     void SendMethodNotAllowed(const void* parameters);
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Aug 27 12:32:09 2015 +0200
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Aug 27 12:56:48 2015 +0200
@@ -1467,6 +1467,7 @@
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param output The HTTP connection to the client application.
    * @param status The HTTP status code to be sent.
+   * @see OrthancPluginSendHttpStatus()
    **/
   ORTHANC_PLUGIN_INLINE void OrthancPluginSendHttpStatusCode(
     OrthancPluginContext*    context,
@@ -2493,6 +2494,50 @@
   }
 
 
+
+  typedef struct
+  {
+    OrthancPluginRestOutput* output;
+    uint16_t                 status;
+    const char*              body;
+    uint32_t                 bodySize;
+  } _OrthancPluginSendHttpStatus;
+
+  /**
+   * @brief Send a HTTP status, with a custom body.
+   *
+   * This function answers to a HTTP request by sending a HTTP status
+   * code (such as "400 - Bad Request"), together with a body
+   * describing the error. The body will only be returned if the
+   * configuration option "HttpDescribeErrors" of Orthanc is set to "true".
+   * 
+   * Note that:
+   * - Successful requests (status 200) must use ::OrthancPluginAnswerBuffer().
+   * - Redirections (status 301) must use ::OrthancPluginRedirect().
+   * - Unauthorized access (status 401) must use ::OrthancPluginSendUnauthorized().
+   * - Methods not allowed (status 405) must use ::OrthancPluginSendMethodNotAllowed().
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param output The HTTP connection to the client application.
+   * @param status The HTTP status code to be sent.
+   * @see OrthancPluginSendHttpStatusCode()
+   **/
+  ORTHANC_PLUGIN_INLINE void OrthancPluginSendHttpStatus(
+    OrthancPluginContext*    context,
+    OrthancPluginRestOutput* output,
+    uint16_t                 status,
+    const char*              body,
+    uint32_t                 bodySize)
+  {
+    _OrthancPluginSendHttpStatus params;
+    params.output = output;
+    params.status = status;
+    params.body = body;
+    params.bodySize = bodySize;
+    context->InvokeService(context, _OrthancPluginService_SendHttpStatus, &params);
+  }
+
+
 #ifdef  __cplusplus
 }
 #endif