1 /**
2  * Orthanc - A Lightweight, RESTful DICOM Store
3  * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4  * Department, University Hospital of Liege, Belgium
5  * Copyright (C) 2017-2021 Osimis S.A., Belgium
6  *
7  * This program is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program. If not, see
19  * <http://www.gnu.org/licenses/>.
20  **/
21 
22 
23 #pragma once
24 
25 #include "../HttpServer/HttpToolbox.h"
26 #include "RestApiCallDocumentation.h"
27 #include "RestApiPath.h"
28 #include "RestApiOutput.h"
29 
30 #include <boost/noncopyable.hpp>
31 #include <set>
32 
33 namespace Orthanc
34 {
35   class RestApi;
36 
37   class RestApiCall : public boost::noncopyable
38   {
39   private:
40     RestApiOutput& output_;
41     RestApi& context_;
42     RequestOrigin origin_;
43     const char* remoteIp_;
44     const char* username_;
45     const HttpToolbox::Arguments& httpHeaders_;
46     const HttpToolbox::Arguments& uriComponents_;
47     const UriComponents& trailing_;
48     const UriComponents& fullUri_;
49     HttpMethod method_;  // To create RestApiCallDocumentation on demand
50     std::unique_ptr<RestApiCallDocumentation>  documentation_;  // Lazy creation
51 
52   public:
RestApiCall(RestApiOutput & output,RestApi & context,RequestOrigin origin,const char * remoteIp,const char * username,HttpMethod method,const HttpToolbox::Arguments & httpHeaders,const HttpToolbox::Arguments & uriComponents,const UriComponents & trailing,const UriComponents & fullUri)53     RestApiCall(RestApiOutput& output,
54                 RestApi& context,
55                 RequestOrigin origin,
56                 const char* remoteIp,
57                 const char* username,
58                 HttpMethod method,
59                 const HttpToolbox::Arguments& httpHeaders,
60                 const HttpToolbox::Arguments& uriComponents,
61                 const UriComponents& trailing,
62                 const UriComponents& fullUri) :
63       output_(output),
64       context_(context),
65       origin_(origin),
66       remoteIp_(remoteIp),
67       username_(username),
68       httpHeaders_(httpHeaders),
69       uriComponents_(uriComponents),
70       trailing_(trailing),
71       fullUri_(fullUri),
72       method_(method)
73     {
74     }
75 
GetOutput()76     RestApiOutput& GetOutput()
77     {
78       return output_;
79     }
80 
GetContext()81     RestApi& GetContext()
82     {
83       return context_;
84     }
85 
GetFullUri()86     const UriComponents& GetFullUri() const
87     {
88       return fullUri_;
89     }
90 
GetTrailingUri()91     const UriComponents& GetTrailingUri() const
92     {
93       return trailing_;
94     }
95 
96     void GetUriComponentsNames(std::set<std::string>& components) const;
97 
HasUriComponent(const std::string & name)98     bool HasUriComponent(const std::string& name) const
99     {
100       return (uriComponents_.find(name) != uriComponents_.end());
101     }
102 
GetUriComponent(const std::string & name,const std::string & defaultValue)103     std::string GetUriComponent(const std::string& name,
104                                 const std::string& defaultValue) const
105     {
106       return HttpToolbox::GetArgument(uriComponents_, name, defaultValue);
107     }
108 
GetHttpHeader(const std::string & name,const std::string & defaultValue)109     std::string GetHttpHeader(const std::string& name,
110                               const std::string& defaultValue) const
111     {
112       return HttpToolbox::GetArgument(httpHeaders_, name, defaultValue);
113     }
114 
GetHttpHeaders()115     const HttpToolbox::Arguments& GetHttpHeaders() const
116     {
117       return httpHeaders_;
118     }
119 
ParseCookies(HttpToolbox::Arguments & result)120     void ParseCookies(HttpToolbox::Arguments& result) const
121     {
122       HttpToolbox::ParseCookies(result, httpHeaders_);
123     }
124 
125     std::string FlattenUri() const;
126 
GetRequestOrigin()127     RequestOrigin GetRequestOrigin() const
128     {
129       return origin_;
130     }
131 
GetRemoteIp()132     const char* GetRemoteIp() const
133     {
134       return remoteIp_;
135     }
136 
GetUsername()137     const char* GetUsername() const
138     {
139       return username_;
140     }
141 
142     virtual bool ParseJsonRequest(Json::Value& result) const = 0;
143 
144     RestApiCallDocumentation& GetDocumentation();
145 
GetMethod()146     HttpMethod GetMethod() const
147     {
148       return method_;
149     }
150 
IsDocumentation()151     bool IsDocumentation() const
152     {
153       return (origin_ == RequestOrigin_Documentation);
154     }
155 
156     static bool ParseBoolean(const std::string& value);
157   };
158 }
159