# HG changeset patch # User Sebastien Jodogne # Date 1405599255 -7200 # Node ID 2c49b7dffcec384f905899806274e36c6d552001 # Parent d06186cdc502f6ebb8d2fd6ae621c1f8e24afb8b plugins have access to the HTTP headers diff -r d06186cdc502 -r 2c49b7dffcec Plugins/Engine/PluginsHttpHandler.cpp --- a/Plugins/Engine/PluginsHttpHandler.cpp Thu Jul 17 13:04:02 2014 +0200 +++ b/Plugins/Engine/PluginsHttpHandler.cpp Thu Jul 17 14:14:15 2014 +0200 @@ -103,11 +103,30 @@ for (PImpl::Callbacks::iterator it = pimpl_->callbacks_.begin(); it != pimpl_->callbacks_.end(); ++it) { + // Delete the regular expression associated with this callback delete it->first; } } + static void ArgumentsToPlugin(std::vector& keys, + std::vector& values, + const HttpHandler::Arguments& arguments) + { + keys.resize(arguments.size()); + values.resize(arguments.size()); + + size_t pos = 0; + for (HttpHandler::Arguments::const_iterator + it = arguments.begin(); it != arguments.end(); ++it) + { + keys[pos] = it->first.c_str(); + values[pos] = it->second.c_str(); + pos++; + } + } + + bool PluginsHttpHandler::Handle(HttpOutput& output, HttpMethod method, const UriComponents& uri, @@ -121,15 +140,19 @@ std::vector groups; std::vector cgroups; + // Loop over the callbacks registered by the plugins bool found = false; for (PImpl::Callbacks::const_iterator it = pimpl_->callbacks_.begin(); it != pimpl_->callbacks_.end() && !found; ++it) { + // Check whether the regular expression associated to this + // callback matches the URI boost::cmatch what; if (boost::regex_match(flatUri.c_str(), what, *(it->first))) { callback = it->second; + // Extract the value of the free parameters of the regular expression if (what.size() > 1) { groups.resize(what.size() - 1); @@ -152,28 +175,19 @@ LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri; - std::vector getKeys(getArguments.size()); - std::vector getValues(getArguments.size()); + std::vector getKeys, getValues, headersKeys, headersValues; OrthancPluginHttpRequest request; memset(&request, 0, sizeof(OrthancPluginHttpRequest)); + ArgumentsToPlugin(headersKeys, headersValues, headers); + switch (method) { case HttpMethod_Get: - { request.method = OrthancPluginHttpMethod_Get; - - size_t i = 0; - for (Arguments::const_iterator it = getArguments.begin(); - it != getArguments.end(); ++it, ++i) - { - getKeys[i] = it->first.c_str(); - getValues[i] = it->second.c_str(); - } - + ArgumentsToPlugin(getKeys, getValues, getArguments); break; - } case HttpMethod_Post: request.method = OrthancPluginHttpMethod_Post; @@ -197,12 +211,19 @@ request.getCount = getArguments.size(); request.body = (postData.size() ? &postData[0] : NULL); request.bodySize = postData.size(); - + request.headersCount = headers.size(); + if (getArguments.size() > 0) { request.getKeys = &getKeys[0]; request.getValues = &getValues[0]; } + + if (headers.size() > 0) + { + request.headersKeys = &headersKeys[0]; + request.headersValues = &headersValues[0]; + } assert(callback != NULL); int32_t error = callback(reinterpret_cast(&output), diff -r d06186cdc502 -r 2c49b7dffcec Plugins/OrthancCPlugin/OrthancCPlugin.h --- a/Plugins/OrthancCPlugin/OrthancCPlugin.h Thu Jul 17 13:04:02 2014 +0200 +++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h Thu Jul 17 14:14:15 2014 +0200 @@ -8,15 +8,18 @@ * * -# int32_t OrthancPluginInitialize(const OrthancPluginContext* context): * This function is invoked by Orthanc when it loads the plugin on startup. - * The plugin must store the context pointer so that it can use the plugin - * services of Orthanc. It must also register all its callbacks using - * ::OrthancPluginRegisterRestCallback(). + * The plugin must: + * - Check its compatibility with the Orthanc version using + * ::OrthancPluginCheckVersion(). + * - Store the context pointer so that it can use the plugin + * services of Orthanc. + * - Register all its REST callbacks using ::OrthancPluginRegisterRestCallback(). * -# void OrthancPluginFinalize(): * This function is invoked by Orthanc during its shutdown. The plugin * must free all its memory. * -# const char* OrthancPluginGetName(): * The plugin must return a short string to identify itself. - * -# const char* OrthancPluginGetVersion() + * -# const char* OrthancPluginGetVersion(): * The plugin must return a string containing its version number. * * The name and the version of a plugin is only used to prevent it @@ -202,8 +205,30 @@ * @brief For a PUT or POST request, the number of bytes of the body. **/ uint32_t bodySize; + + + /* -------------------------------------------------- + New in version 0.8.1 + -------------------------------------------------- */ + + /** + * @brief The number of HTTP headers. + **/ + uint32_t headersCount; + + /** + * @brief The keys of the HTTP headers (always converted to low-case). + **/ + const char* const* headersKeys; + + /** + * @brief The values of the HTTP headers. + **/ + const char* const* headersValues; + } OrthancPluginHttpRequest; + typedef enum { /* Generic services */ @@ -353,7 +378,7 @@ /** - * @brief Check the version of Orthanc. + * @brief Check the compatibility of the plugin wrt. the version of its hosting Orthanc. * * This function checks whether the version of this C header is * compatible with the current version of Orthanc. The result of diff -r d06186cdc502 -r 2c49b7dffcec Plugins/Samples/Basic/Plugin.c --- a/Plugins/Samples/Basic/Plugin.c Thu Jul 17 13:04:02 2014 +0200 +++ b/Plugins/Samples/Basic/Plugin.c Thu Jul 17 14:14:15 2014 +0200 @@ -40,23 +40,37 @@ char buffer[1024]; uint32_t i; - sprintf(buffer, "Callback on URL [%s] with body [%s]", url, request->body); - OrthancPluginLogInfo(context, buffer); + sprintf(buffer, "Callback on URL [%s] with body [%s]\n", url, request->body); + OrthancPluginLogWarning(context, buffer); OrthancPluginAnswerBuffer(context, output, buffer, strlen(buffer), "text/plain"); + OrthancPluginLogWarning(context, ""); + + for (i = 0; i < request->groupsCount; i++) + { + sprintf(buffer, " REGEX GROUP %d = [%s]", i, request->groups[i]); + OrthancPluginLogWarning(context, buffer); + } + + OrthancPluginLogWarning(context, ""); + for (i = 0; i < request->getCount; i++) { - sprintf(buffer, " [%s] = [%s]", request->getKeys[i], request->getValues[i]); - OrthancPluginLogInfo(context, buffer); + sprintf(buffer, " GET [%s] = [%s]", request->getKeys[i], request->getValues[i]); + OrthancPluginLogWarning(context, buffer); } - printf("** %d\n", request->groupsCount); - for (i = 0; i < request->groupsCount; i++) + OrthancPluginLogWarning(context, ""); + + for (i = 0; i < request->headersCount; i++) { - printf("** [%s]\n", request->groups[i]); + sprintf(buffer, " HEADERS [%s] = [%s]", request->headersKeys[i], request->headersValues[i]); + OrthancPluginLogWarning(context, buffer); } + OrthancPluginLogWarning(context, ""); + return 1; }