# HG changeset patch # User Sebastien Jodogne # Date 1402991822 -7200 # Node ID c4053ac5db049e76b6f98096919ed97176bbb688 # Parent 7e8cde5905fd3d8d7e8943ae744d95e8b7edefdd better plugni api diff -r 7e8cde5905fd -r c4053ac5db04 Core/HttpServer/MongooseServer.cpp --- a/Core/HttpServer/MongooseServer.cpp Mon Jun 16 17:47:58 2014 +0200 +++ b/Core/HttpServer/MongooseServer.cpp Tue Jun 17 09:57:02 2014 +0200 @@ -689,7 +689,7 @@ } - // Call the proper handler for this URI + // Decompose the URI into its components UriComponents uri; try { @@ -702,44 +702,61 @@ } + // Locate the candidate handlers for this URI + LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); std::list handlers; that->FindHandlers(handlers, uri); bool found = false; + bool isError = false; + HttpStatus errorStatus; + std::string errorDescription; + + // Loop over the candidate handlers for this URI for (std::list::const_iterator it = handlers.begin(); it != handlers.end() && !found; it++) { try { - LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); found = (*it)->Handle(output, method, uri, headers, argumentsGET, body); } catch (OrthancException& e) { - LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]"; - output.SendHeader(HttpStatus_500_InternalServerError); - found = true; + // Using this candidate handler results in an exception, try + // another handler before failing + isError = true; + errorStatus = HttpStatus_500_InternalServerError; + errorDescription = e.What(); } catch (boost::bad_lexical_cast&) { - LOG(ERROR) << "MongooseServer Exception: Bad lexical cast"; - output.SendHeader(HttpStatus_400_BadRequest); - found = true; + isError = true; + errorStatus = HttpStatus_400_BadRequest; + errorDescription = "Bad lexical cast"; } catch (std::runtime_error&) { - LOG(ERROR) << "MongooseServer Exception: Presumably a bad JSON request"; - output.SendHeader(HttpStatus_400_BadRequest); - found = true; + isError = true; + errorStatus = HttpStatus_400_BadRequest; + errorDescription = "Presumably a bad JSON request"; } } if (!found) { - output.SendHeader(HttpStatus_404_NotFound); + if (isError) + { + LOG(ERROR) << "Exception in the HTTP handler: " << errorDescription; + output.SendHeader(errorStatus); + } + else + { + output.SendHeader(HttpStatus_404_NotFound); + } } + // Mark as processed return (void*) ""; } diff -r 7e8cde5905fd -r c4053ac5db04 Plugins/Engine/PluginsManager.cpp --- a/Plugins/Engine/PluginsManager.cpp Mon Jun 16 17:47:58 2014 +0200 +++ b/Plugins/Engine/PluginsManager.cpp Tue Jun 17 09:57:02 2014 +0200 @@ -147,50 +147,20 @@ } void PluginsManager::RegisterRestCallback(const OrthancPluginContext* context, - const char* path, - OrthancRestCallback callback) + const char* pathRegularExpression, + OrthancPluginRestCallback callback) { // TODO - LOG(INFO) << "Plugin has registered a REST callback on: " << path; + LOG(INFO) << "Plugin has registered a REST callback on: " << pathRegularExpression; PluginsManager* manager = reinterpret_cast(context->pimpl); manager->restCallbacks_.push_back(callback); - - const char* pp = "/hello/world"; - - UriComponents components; - Toolbox::SplitUriComponents(components, pp); - - OrthancRestUrl url; - url.path = pp; - - std::vector c(components.size()); - for (unsigned int i = 0; i < components.size(); i++) - { - c[i] = components[i].c_str(); - } - - if (components.size() == 0) - { - url.components = NULL; - url.componentsSize = 0; - } - else - { - url.components = &c[0]; - url.componentsSize = components.size(); - } - - // TODO - url.parameters = NULL; - url.parametersSize = 0; - - callback(NULL, OrthancHttpMethod_Get, &url, NULL, 0); + callback(NULL, OrthancPluginHttpMethod_Get, "/hello/world", NULL, 0); } - static void AnswerBuffer(OrthancRestOutput* output, + static void AnswerBuffer(OrthancPluginRestOutput* output, const char* answer, uint32_t answerSize, const char* mimeType) @@ -311,5 +281,4 @@ } } } - } diff -r 7e8cde5905fd -r c4053ac5db04 Plugins/Engine/PluginsManager.h --- a/Plugins/Engine/PluginsManager.h Mon Jun 16 17:47:58 2014 +0200 +++ b/Plugins/Engine/PluginsManager.h Tue Jun 17 09:57:02 2014 +0200 @@ -44,7 +44,7 @@ { private: typedef std::map Plugins; - typedef std::list RestCallbacks; + typedef std::list RestCallbacks; OrthancPluginContext context_; Plugins plugins_; @@ -52,7 +52,7 @@ static void RegisterRestCallback(const OrthancPluginContext* context, const char* path, - OrthancRestCallback callback); + OrthancPluginRestCallback callback); public: PluginsManager(); diff -r 7e8cde5905fd -r c4053ac5db04 Plugins/OrthancCPlugin/OrthancCPlugin.h --- a/Plugins/OrthancCPlugin/OrthancCPlugin.h Mon Jun 16 17:47:58 2014 +0200 +++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h Tue Jun 17 09:57:02 2014 +0200 @@ -55,34 +55,24 @@ { #endif - typedef struct OrthancRestOutput_t OrthancRestOutput; + typedef struct OrthancPluginRestOutput_t OrthancPluginRestOutput; typedef enum { - OrthancHttpMethod_Get = 1, - OrthancHttpMethod_Post = 2, - OrthancHttpMethod_Put = 3, - OrthancHttpMethod_Delete = 4 - } OrthancHttpMethod; - - typedef struct OrthancRestUrl_t - { - const char* path; - const char* const* components; - uint32_t componentsSize; - const char* const* parameters; - uint32_t parametersSize; - } OrthancRestUrl; - + OrthancPluginHttpMethod_Get = 1, + OrthancPluginHttpMethod_Post = 2, + OrthancPluginHttpMethod_Put = 3, + OrthancPluginHttpMethod_Delete = 4 + } OrthancPluginHttpMethod; typedef int32_t (*OrthancPluginService) (const char* serviceName, const void* serviceParameters); - typedef int32_t (*OrthancRestCallback) (OrthancRestOutput* output, - OrthancHttpMethod method, - const OrthancRestUrl* url, - const char* body, - uint32_t bodySize); + typedef int32_t (*OrthancPluginRestCallback) (OrthancPluginRestOutput* output, + OrthancPluginHttpMethod method, + const char* url, + const char* body, + uint32_t bodySize); typedef struct OrthancPluginContext_t { @@ -98,10 +88,10 @@ /* REST API */ void (*RegisterRestCallback) (const struct OrthancPluginContext_t* context, - const char* path, - OrthancRestCallback callback); + const char* pathRegularExpression, + OrthancPluginRestCallback callback); - void (*AnswerBuffer) (OrthancRestOutput* output, + void (*AnswerBuffer) (OrthancPluginRestOutput* output, const char* answer, uint32_t answerSize, const char* mimeType); @@ -116,7 +106,7 @@ - const char* OrthancPluginGetVersion(); nm -C -D --defined-only libPluginTest.so - **/ + **/ #ifdef __cplusplus } diff -r 7e8cde5905fd -r c4053ac5db04 Resources/Samples/Plugins/Basic/CMakeLists.txt --- a/Resources/Samples/Plugins/Basic/CMakeLists.txt Mon Jun 16 17:47:58 2014 +0200 +++ b/Resources/Samples/Plugins/Basic/CMakeLists.txt Tue Jun 17 09:57:02 2014 +0200 @@ -7,6 +7,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror") endif() +include_directories(${CMAKE_SOURCE_DIR}/../../../../Plugins/OrthancCPlugin/) add_library(PluginTest SHARED Plugin.c) if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") diff -r 7e8cde5905fd -r c4053ac5db04 Resources/Samples/Plugins/Basic/Plugin.c --- a/Resources/Samples/Plugins/Basic/Plugin.c Mon Jun 16 17:47:58 2014 +0200 +++ b/Resources/Samples/Plugins/Basic/Plugin.c Tue Jun 17 09:57:02 2014 +0200 @@ -25,7 +25,7 @@ **/ -#include "../../../../Plugins/OrthancCPlugin/OrthancCPlugin.h" +#include #include #include @@ -33,14 +33,14 @@ static const OrthancPluginContext* context = NULL; -ORTHANC_PLUGINS_API int32_t Callback(OrthancRestOutput* output, - OrthancHttpMethod method, - const OrthancRestUrl* url, +ORTHANC_PLUGINS_API int32_t Callback(OrthancPluginRestOutput* output, + OrthancPluginHttpMethod method, + const char* url, const char* body, uint32_t bodySize) { char buffer[1024]; - sprintf(buffer, "Callback on URL [%s]\n", url->path); + sprintf(buffer, "Callback on URL [%s]\n", url); context->LogInfo(buffer); context->AnswerBuffer(output, buffer, strlen(buffer), "text/plain"); return 1; @@ -57,7 +57,7 @@ sprintf(info, "The version of Orthanc is '%s'", context->orthancVersion); context->LogInfo(info); - context->RegisterRestCallback(c, "/plugin/hello", Callback); + context->RegisterRestCallback(c, "/plu.*/hello", Callback); return 0; }