Mercurial > hg > orthanc
changeset 1571:3232f1c995a5
provide the origin of the requests to HTTP handlers
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 25 Aug 2015 11:04:19 +0200 |
parents | 2bd2c280f9b5 |
children | 904096e7367e |
files | Core/Enumerations.cpp Core/Enumerations.h Core/HttpServer/EmbeddedResourceHttpHandler.cpp Core/HttpServer/EmbeddedResourceHttpHandler.h Core/HttpServer/FilesystemHttpHandler.cpp Core/HttpServer/FilesystemHttpHandler.h Core/HttpServer/HttpToolbox.cpp Core/HttpServer/HttpToolbox.h Core/HttpServer/IHttpHandler.h Core/HttpServer/MongooseServer.cpp Core/RestApi/RestApi.cpp Core/RestApi/RestApi.h Core/RestApi/RestApiCall.h Core/RestApi/RestApiDeleteCall.h Core/RestApi/RestApiGetCall.h Core/RestApi/RestApiPostCall.h Core/RestApi/RestApiPutCall.h OrthancServer/LuaScripting.cpp OrthancServer/OrthancHttpHandler.cpp OrthancServer/OrthancHttpHandler.h Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h |
diffstat | 22 files changed, 172 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/Enumerations.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/Enumerations.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -370,6 +370,28 @@ } + const char* EnumerationToString(RequestOrigin origin) + { + switch (origin) + { + case RequestOrigin_DicomProtocol: + return "DicomProtocol"; + + case RequestOrigin_Http: + return "Http"; + + case RequestOrigin_Plugins: + return "Plugins"; + + case RequestOrigin_Lua: + return "Lua"; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + } + + const char* EnumerationToString(LogLevel level) { switch (level)
--- a/Core/Enumerations.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/Enumerations.h Tue Aug 25 11:04:19 2015 +0200 @@ -300,6 +300,14 @@ DicomModule_Image }; + enum RequestOrigin + { + RequestOrigin_DicomProtocol, + RequestOrigin_Http, + RequestOrigin_Plugins, + RequestOrigin_Lua + }; + /** * WARNING: Do not change the explicit values in the enumerations @@ -364,6 +372,8 @@ const char* EnumerationToString(LogLevel level); + const char* EnumerationToString(RequestOrigin origin); + Encoding StringToEncoding(const char* encoding); ResourceType StringToResourceType(const char* type);
--- a/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -53,6 +53,9 @@ bool EmbeddedResourceHttpHandler::Handle( HttpOutput& output, + RequestOrigin /*origin*/, + const char* /*remoteIp*/, + const char* /*username*/, HttpMethod method, const UriComponents& uri, const Arguments& headers,
--- a/Core/HttpServer/EmbeddedResourceHttpHandler.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/EmbeddedResourceHttpHandler.h Tue Aug 25 11:04:19 2015 +0200 @@ -52,6 +52,9 @@ virtual bool Handle( HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers,
--- a/Core/HttpServer/FilesystemHttpHandler.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/FilesystemHttpHandler.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -126,6 +126,9 @@ bool FilesystemHttpHandler::Handle( HttpOutput& output, + RequestOrigin /*origin*/, + const char* /*remoteIp*/, + const char* /*username*/, HttpMethod method, const UriComponents& uri, const Arguments& headers,
--- a/Core/HttpServer/FilesystemHttpHandler.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/FilesystemHttpHandler.h Tue Aug 25 11:04:19 2015 +0200 @@ -54,6 +54,9 @@ virtual bool Handle( HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers,
--- a/Core/HttpServer/HttpToolbox.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/HttpToolbox.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -41,6 +41,10 @@ #include "StringHttpOutput.h" +static const char* LOCALHOST = "localhost"; + + + namespace Orthanc { static void SplitGETNameValue(IHttpHandler::GetArguments& result, @@ -196,6 +200,7 @@ bool HttpToolbox::SimpleGet(std::string& result, IHttpHandler& handler, + RequestOrigin origin, const std::string& uri) { IHttpHandler::Arguments headers; // No HTTP header @@ -207,8 +212,8 @@ StringHttpOutput stream; HttpOutput http(stream, false /* no keep alive */); - if (handler.Handle(http, HttpMethod_Get, curi, headers, getArguments, - NULL /* no body for GET */, 0)) + if (handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Get, curi, + headers, getArguments, NULL /* no body for GET */, 0)) { stream.GetOutput(result); return true; @@ -222,6 +227,7 @@ static bool SimplePostOrPut(std::string& result, IHttpHandler& handler, + RequestOrigin origin, HttpMethod method, const std::string& uri, const char* bodyData, @@ -236,7 +242,8 @@ StringHttpOutput stream; HttpOutput http(stream, false /* no keep alive */); - if (handler.Handle(http, method, curi, headers, getArguments, bodyData, bodySize)) + if (handler.Handle(http, origin, LOCALHOST, "", method, curi, + headers, getArguments, bodyData, bodySize)) { stream.GetOutput(result); return true; @@ -250,25 +257,28 @@ bool HttpToolbox::SimplePost(std::string& result, IHttpHandler& handler, + RequestOrigin origin, const std::string& uri, const char* bodyData, size_t bodySize) { - return SimplePostOrPut(result, handler, HttpMethod_Post, uri, bodyData, bodySize); + return SimplePostOrPut(result, handler, origin, HttpMethod_Post, uri, bodyData, bodySize); } bool HttpToolbox::SimplePut(std::string& result, IHttpHandler& handler, + RequestOrigin origin, const std::string& uri, const char* bodyData, size_t bodySize) { - return SimplePostOrPut(result, handler, HttpMethod_Put, uri, bodyData, bodySize); + return SimplePostOrPut(result, handler, origin, HttpMethod_Put, uri, bodyData, bodySize); } bool HttpToolbox::SimpleDelete(IHttpHandler& handler, + RequestOrigin origin, const std::string& uri) { UriComponents curi; @@ -280,7 +290,7 @@ StringHttpOutput stream; HttpOutput http(stream, false /* no keep alive */); - return handler.Handle(http, HttpMethod_Delete, curi, headers, getArguments, - NULL /* no body for DELETE */, 0); + return handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Delete, curi, + headers, getArguments, NULL /* no body for DELETE */, 0); } }
--- a/Core/HttpServer/HttpToolbox.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/HttpToolbox.h Tue Aug 25 11:04:19 2015 +0200 @@ -62,21 +62,25 @@ static bool SimpleGet(std::string& result, IHttpHandler& handler, + RequestOrigin origin, const std::string& uri); static bool SimplePost(std::string& result, IHttpHandler& handler, + RequestOrigin origin, const std::string& uri, const char* bodyData, size_t bodySize); static bool SimplePut(std::string& result, IHttpHandler& handler, + RequestOrigin origin, const std::string& uri, const char* bodyData, size_t bodySize); static bool SimpleDelete(IHttpHandler& handler, + RequestOrigin origin, const std::string& uri); }; }
--- a/Core/HttpServer/IHttpHandler.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/IHttpHandler.h Tue Aug 25 11:04:19 2015 +0200 @@ -53,6 +53,9 @@ } virtual bool Handle(HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers,
--- a/Core/HttpServer/MongooseServer.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/HttpServer/MongooseServer.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -634,18 +634,18 @@ // Apply the filter, if it is installed + char remoteIp[24]; + sprintf(remoteIp, "%d.%d.%d.%d", + reinterpret_cast<const uint8_t*>(&request->remote_ip) [3], + reinterpret_cast<const uint8_t*>(&request->remote_ip) [2], + reinterpret_cast<const uint8_t*>(&request->remote_ip) [1], + reinterpret_cast<const uint8_t*>(&request->remote_ip) [0]); + + std::string username = GetAuthenticatedUsername(headers); + const IIncomingHttpRequestFilter *filter = that->GetIncomingHttpRequestFilter(); if (filter != NULL) { - std::string username = GetAuthenticatedUsername(headers); - - char remoteIp[24]; - sprintf(remoteIp, "%d.%d.%d.%d", - reinterpret_cast<const uint8_t*>(&request->remote_ip) [3], - reinterpret_cast<const uint8_t*>(&request->remote_ip) [2], - reinterpret_cast<const uint8_t*>(&request->remote_ip) [1], - reinterpret_cast<const uint8_t*>(&request->remote_ip) [0]); - if (!filter->IsAllowed(method, request->uri, remoteIp, username.c_str())) { output.SendUnauthorized(ORTHANC_REALM); @@ -728,7 +728,8 @@ { if (that->HasHandler()) { - found = that->GetHandler().Handle(output, method, uri, headers, argumentsGET, body.c_str(), body.size()); + found = that->GetHandler().Handle(output, RequestOrigin_Http, remoteIp, username.c_str(), + method, uri, headers, argumentsGET, body.c_str(), body.size()); } } catch (boost::bad_lexical_cast&)
--- a/Core/RestApi/RestApi.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/RestApi/RestApi.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -48,6 +48,9 @@ private: RestApi& api_; RestApiOutput& output_; + RequestOrigin origin_; + const char* remoteIp_; + const char* username_; HttpMethod method_; const IHttpHandler::Arguments& headers_; const IHttpHandler::Arguments& getArguments_; @@ -57,6 +60,9 @@ public: HttpHandlerVisitor(RestApi& api, RestApiOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const IHttpHandler::Arguments& headers, const IHttpHandler::Arguments& getArguments, @@ -64,6 +70,9 @@ size_t bodySize) : api_(api), output_(output), + origin_(origin), + remoteIp_(remoteIp), + username_(username), method_(method), headers_(headers), getArguments_(getArguments), @@ -83,28 +92,32 @@ { case HttpMethod_Get: { - RestApiGetCall call(output_, api_, headers_, components, trailing, uri, getArguments_); + RestApiGetCall call(output_, api_, origin_, remoteIp_, username_, + headers_, components, trailing, uri, getArguments_); resource.Handle(call); return true; } case HttpMethod_Post: { - RestApiPostCall call(output_, api_, headers_, components, trailing, uri, bodyData_, bodySize_); + RestApiPostCall call(output_, api_, origin_, remoteIp_, username_, + headers_, components, trailing, uri, bodyData_, bodySize_); resource.Handle(call); return true; } case HttpMethod_Delete: { - RestApiDeleteCall call(output_, api_, headers_, components, trailing, uri); + RestApiDeleteCall call(output_, api_, origin_, remoteIp_, username_, + headers_, components, trailing, uri); resource.Handle(call); return true; } case HttpMethod_Put: { - RestApiPutCall call(output_, api_, headers_, components, trailing, uri, bodyData_, bodySize_); + RestApiPutCall call(output_, api_, origin_, remoteIp_, username_, + headers_, components, trailing, uri, bodyData_, bodySize_); resource.Handle(call); return true; } @@ -160,6 +173,9 @@ bool RestApi::Handle(HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers, @@ -197,7 +213,8 @@ Arguments compiled; HttpToolbox::CompileGetArguments(compiled, getArguments); - HttpHandlerVisitor visitor(*this, wrappedOutput, method, headers, compiled, bodyData, bodySize); + HttpHandlerVisitor visitor(*this, wrappedOutput, origin, remoteIp, username, + method, headers, compiled, bodyData, bodySize); if (root_.LookupResource(uri, visitor)) {
--- a/Core/RestApi/RestApi.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/RestApi/RestApi.h Tue Aug 25 11:04:19 2015 +0200 @@ -47,6 +47,9 @@ static void AutoListChildren(RestApiGetCall& call); virtual bool Handle(HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers,
--- a/Core/RestApi/RestApiCall.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/RestApi/RestApiCall.h Tue Aug 25 11:04:19 2015 +0200 @@ -48,6 +48,9 @@ private: RestApiOutput& output_; RestApi& context_; + RequestOrigin origin_; + const char* remoteIp_; + const char* username_; const IHttpHandler::Arguments& httpHeaders_; const IHttpHandler::Arguments& uriComponents_; const UriComponents& trailing_; @@ -60,12 +63,18 @@ public: RestApiCall(RestApiOutput& output, RestApi& context, + RequestOrigin origin, + const char* remoteIp, + const char* username, const IHttpHandler::Arguments& httpHeaders, const IHttpHandler::Arguments& uriComponents, const UriComponents& trailing, const UriComponents& fullUri) : output_(output), context_(context), + origin_(origin), + remoteIp_(remoteIp), + username_(username), httpHeaders_(httpHeaders), uriComponents_(uriComponents), trailing_(trailing), @@ -117,6 +126,21 @@ std::string FlattenUri() const; + RequestOrigin GetRequestOrigin() const + { + return origin_; + } + + const char* GetRemoteIp() const + { + return remoteIp_; + } + + const char* GetUsername() const + { + return username_; + } + virtual bool ParseJsonRequest(Json::Value& result) const = 0; }; }
--- a/Core/RestApi/RestApiDeleteCall.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/RestApi/RestApiDeleteCall.h Tue Aug 25 11:04:19 2015 +0200 @@ -43,11 +43,15 @@ RestApiDeleteCall(RestApiOutput& output, RestApi& context, + RequestOrigin origin, + const char* remoteIp, + const char* username, const IHttpHandler::Arguments& httpHeaders, const IHttpHandler::Arguments& uriComponents, const UriComponents& trailing, const UriComponents& fullUri) : - RestApiCall(output, context, httpHeaders, uriComponents, trailing, fullUri) + RestApiCall(output, context, origin, remoteIp, username, + httpHeaders, uriComponents, trailing, fullUri) { }
--- a/Core/RestApi/RestApiGetCall.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/RestApi/RestApiGetCall.h Tue Aug 25 11:04:19 2015 +0200 @@ -46,12 +46,16 @@ RestApiGetCall(RestApiOutput& output, RestApi& context, + RequestOrigin origin, + const char* remoteIp, + const char* username, const IHttpHandler::Arguments& httpHeaders, const IHttpHandler::Arguments& uriComponents, const UriComponents& trailing, const UriComponents& fullUri, const IHttpHandler::Arguments& getArguments) : - RestApiCall(output, context, httpHeaders, uriComponents, trailing, fullUri), + RestApiCall(output, context, origin, remoteIp, username, + httpHeaders, uriComponents, trailing, fullUri), getArguments_(getArguments) { }
--- a/Core/RestApi/RestApiPostCall.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/RestApi/RestApiPostCall.h Tue Aug 25 11:04:19 2015 +0200 @@ -47,13 +47,17 @@ RestApiPostCall(RestApiOutput& output, RestApi& context, + RequestOrigin origin, + const char* remoteIp, + const char* username, const IHttpHandler::Arguments& httpHeaders, const IHttpHandler::Arguments& uriComponents, const UriComponents& trailing, const UriComponents& fullUri, const char* bodyData, size_t bodySize) : - RestApiCall(output, context, httpHeaders, uriComponents, trailing, fullUri), + RestApiCall(output, context, origin, remoteIp, username, + httpHeaders, uriComponents, trailing, fullUri), bodyData_(bodyData), bodySize_(bodySize) {
--- a/Core/RestApi/RestApiPutCall.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Core/RestApi/RestApiPutCall.h Tue Aug 25 11:04:19 2015 +0200 @@ -47,13 +47,17 @@ RestApiPutCall(RestApiOutput& output, RestApi& context, + RequestOrigin origin, + const char* remoteIp, + const char* username, const IHttpHandler::Arguments& httpHeaders, const IHttpHandler::Arguments& uriComponents, const UriComponents& trailing, const UriComponents& fullUri, const char* bodyData, size_t bodySize) : - RestApiCall(output, context, httpHeaders, uriComponents, trailing, fullUri), + RestApiCall(output, context, origin, remoteIp, username, + httpHeaders, uriComponents, trailing, fullUri), bodyData_(bodyData), bodySize_(bodySize) {
--- a/OrthancServer/LuaScripting.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/OrthancServer/LuaScripting.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -84,7 +84,8 @@ bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false); std::string result; - if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), uri)) + if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), + RequestOrigin_Lua, uri)) { lua_pushlstring(state, result.c_str(), result.size()); } @@ -129,9 +130,9 @@ std::string result; if (isPost ? HttpToolbox::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), - uri, bodyData, bodySize) : + RequestOrigin_Lua, uri, bodyData, bodySize) : HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), - uri, bodyData, bodySize)) + RequestOrigin_Lua, uri, bodyData, bodySize)) { lua_pushlstring(state, result.c_str(), result.size()); } @@ -184,7 +185,8 @@ const char* uri = lua_tostring(state, 1); bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false); - if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), uri)) + if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), + RequestOrigin_Lua, uri)) { lua_pushboolean(state, 1); }
--- a/OrthancServer/OrthancHttpHandler.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/OrthancServer/OrthancHttpHandler.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -39,6 +39,9 @@ namespace Orthanc { bool OrthancHttpHandler::Handle(HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers, @@ -51,7 +54,8 @@ for (Handlers::const_iterator it = handlers_.begin(); it != handlers_.end() && !found; ++it) { - found = (*it)->Handle(output, method, uri, headers, getArguments, bodyData, bodySize); + found = (*it)->Handle(output, origin, remoteIp, username, method, uri, + headers, getArguments, bodyData, bodySize); } return found;
--- a/OrthancServer/OrthancHttpHandler.h Sun Aug 23 11:13:03 2015 +0200 +++ b/OrthancServer/OrthancHttpHandler.h Tue Aug 25 11:04:19 2015 +0200 @@ -50,6 +50,9 @@ } virtual bool Handle(HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers,
--- a/Plugins/Engine/OrthancPlugins.cpp Sun Aug 23 11:13:03 2015 +0200 +++ b/Plugins/Engine/OrthancPlugins.cpp Tue Aug 25 11:04:19 2015 +0200 @@ -229,6 +229,9 @@ bool OrthancPlugins::Handle(HttpOutput& output, + RequestOrigin /*origin*/, + const char* /*remoteIp*/, + const char* /*username*/, HttpMethod method, const UriComponents& uri, const Arguments& headers, @@ -627,7 +630,7 @@ IHttpHandler& handler = pimpl_->context_->GetHttpHandler().RestrictToOrthancRestApi(!afterPlugins); std::string result; - if (HttpToolbox::SimpleGet(result, handler, p.uri)) + if (HttpToolbox::SimpleGet(result, handler, RequestOrigin_Plugins, p.uri)) { CopyToMemoryBuffer(*p.target, result); } @@ -653,8 +656,8 @@ std::string result; if (isPost ? - HttpToolbox::SimplePost(result, handler, p.uri, p.body, p.bodySize) : - HttpToolbox::SimplePut (result, handler, p.uri, p.body, p.bodySize)) + HttpToolbox::SimplePost(result, handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize) : + HttpToolbox::SimplePut (result, handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize)) { CopyToMemoryBuffer(*p.target, result); } @@ -675,7 +678,7 @@ CheckContextAvailable(); IHttpHandler& handler = pimpl_->context_->GetHttpHandler().RestrictToOrthancRestApi(!afterPlugins); - if (!HttpToolbox::SimpleDelete(handler, uri)) + if (!HttpToolbox::SimpleDelete(handler, RequestOrigin_Plugins, uri)) { throw OrthancException(ErrorCode_BadRequest); }
--- a/Plugins/Engine/OrthancPlugins.h Sun Aug 23 11:13:03 2015 +0200 +++ b/Plugins/Engine/OrthancPlugins.h Tue Aug 25 11:04:19 2015 +0200 @@ -103,6 +103,9 @@ void SetServerContext(ServerContext& context); virtual bool Handle(HttpOutput& output, + RequestOrigin origin, + const char* remoteIp, + const char* username, HttpMethod method, const UriComponents& uri, const Arguments& headers,