1783
|
1 /**
|
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
1900
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
|
1783
|
4 * Department, University Hospital of Liege, Belgium
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU General Public License as
|
|
8 * published by the Free Software Foundation, either version 3 of the
|
|
9 * License, or (at your option) any later version.
|
|
10 *
|
|
11 * In addition, as a special exception, the copyright holders of this
|
|
12 * program give permission to link the code of its release with the
|
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
|
|
14 * that use the same license as the "OpenSSL" library), and distribute
|
|
15 * the linked executables. You must obey the GNU General Public License
|
|
16 * in all respects for all of the code used other than "OpenSSL". If you
|
|
17 * modify file(s) with this exception, you may extend this exception to
|
|
18 * your version of the file(s), but you are not obligated to do so. If
|
|
19 * you do not wish to do so, delete this exception statement from your
|
|
20 * version. If you delete this exception statement from all source files
|
|
21 * in the program, then also delete it here.
|
|
22 *
|
|
23 * This program is distributed in the hope that it will be useful, but
|
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
26 * General Public License for more details.
|
|
27 *
|
|
28 * You should have received a copy of the GNU General Public License
|
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
30 **/
|
|
31
|
|
32
|
|
33 #include "../PrecompiledHeaders.h"
|
|
34 #include "HttpContentNegociation.h"
|
|
35
|
|
36 #include "../Logging.h"
|
|
37 #include "../OrthancException.h"
|
|
38 #include "../Toolbox.h"
|
|
39
|
|
40 #include <boost/lexical_cast.hpp>
|
|
41
|
|
42 namespace Orthanc
|
|
43 {
|
|
44 HttpContentNegociation::Handler::Handler(const std::string& type,
|
|
45 const std::string& subtype,
|
|
46 IHandler& handler) :
|
|
47 type_(type),
|
|
48 subtype_(subtype),
|
|
49 handler_(handler)
|
|
50 {
|
|
51 }
|
|
52
|
|
53
|
|
54 bool HttpContentNegociation::Handler::IsMatch(const std::string& type,
|
|
55 const std::string& subtype) const
|
|
56 {
|
|
57 if (type == "*" && subtype == "*")
|
|
58 {
|
|
59 return true;
|
|
60 }
|
|
61
|
|
62 if (subtype == "*" && type == type_)
|
|
63 {
|
|
64 return true;
|
|
65 }
|
|
66
|
|
67 return type == type_ && subtype == subtype_;
|
|
68 }
|
|
69
|
|
70
|
|
71 struct HttpContentNegociation::Reference : public boost::noncopyable
|
|
72 {
|
|
73 const Handler& handler_;
|
|
74 uint8_t level_;
|
|
75 float quality_;
|
|
76
|
|
77 Reference(const Handler& handler,
|
|
78 const std::string& type,
|
|
79 const std::string& subtype,
|
|
80 float quality) :
|
|
81 handler_(handler),
|
|
82 quality_(quality)
|
|
83 {
|
|
84 if (type == "*" && subtype == "*")
|
|
85 {
|
|
86 level_ = 0;
|
|
87 }
|
|
88 else if (subtype == "*")
|
|
89 {
|
|
90 level_ = 1;
|
|
91 }
|
|
92 else
|
|
93 {
|
|
94 level_ = 2;
|
|
95 }
|
|
96 }
|
|
97
|
|
98 bool operator< (const Reference& other) const
|
|
99 {
|
|
100 if (level_ < other.level_)
|
|
101 {
|
|
102 return true;
|
|
103 }
|
|
104
|
|
105 if (level_ > other.level_)
|
|
106 {
|
|
107 return false;
|
|
108 }
|
|
109
|
|
110 return quality_ < other.quality_;
|
|
111 }
|
|
112 };
|
|
113
|
|
114
|
|
115 bool HttpContentNegociation::SplitPair(std::string& first /* out */,
|
|
116 std::string& second /* out */,
|
|
117 const std::string& source,
|
|
118 char separator)
|
|
119 {
|
|
120 size_t pos = source.find(separator);
|
|
121
|
|
122 if (pos == std::string::npos)
|
|
123 {
|
|
124 return false;
|
|
125 }
|
|
126 else
|
|
127 {
|
|
128 first = Toolbox::StripSpaces(source.substr(0, pos));
|
|
129 second = Toolbox::StripSpaces(source.substr(pos + 1));
|
|
130 return true;
|
|
131 }
|
|
132 }
|
|
133
|
|
134
|
|
135 float HttpContentNegociation::GetQuality(const Tokens& parameters)
|
|
136 {
|
|
137 for (size_t i = 1; i < parameters.size(); i++)
|
|
138 {
|
|
139 std::string key, value;
|
|
140 if (SplitPair(key, value, parameters[i], '=') &&
|
|
141 key == "q")
|
|
142 {
|
|
143 float quality;
|
|
144 bool ok = false;
|
|
145
|
|
146 try
|
|
147 {
|
|
148 quality = boost::lexical_cast<float>(value);
|
|
149 ok = (quality >= 0.0f && quality <= 1.0f);
|
|
150 }
|
|
151 catch (boost::bad_lexical_cast&)
|
|
152 {
|
|
153 }
|
|
154
|
|
155 if (ok)
|
|
156 {
|
|
157 return quality;
|
|
158 }
|
|
159 else
|
|
160 {
|
|
161 LOG(ERROR) << "Quality parameter out of range in a HTTP request (must be between 0 and 1): " << value;
|
|
162 throw OrthancException(ErrorCode_BadRequest);
|
|
163 }
|
|
164 }
|
|
165 }
|
|
166
|
|
167 return 1.0f; // Default quality
|
|
168 }
|
|
169
|
|
170
|
|
171 void HttpContentNegociation::SelectBestMatch(std::auto_ptr<Reference>& best,
|
|
172 const Handler& handler,
|
|
173 const std::string& type,
|
|
174 const std::string& subtype,
|
|
175 float quality)
|
|
176 {
|
|
177 std::auto_ptr<Reference> match(new Reference(handler, type, subtype, quality));
|
|
178
|
|
179 if (best.get() == NULL ||
|
|
180 *best < *match)
|
|
181 {
|
|
182 best = match;
|
|
183 }
|
|
184 }
|
|
185
|
|
186
|
|
187 void HttpContentNegociation::Register(const std::string& mime,
|
|
188 IHandler& handler)
|
|
189 {
|
|
190 std::string type, subtype;
|
|
191
|
|
192 if (SplitPair(type, subtype, mime, '/') &&
|
|
193 type != "*" &&
|
|
194 subtype != "*")
|
|
195 {
|
|
196 handlers_.push_back(Handler(type, subtype, handler));
|
|
197 }
|
|
198 else
|
|
199 {
|
|
200 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
201 }
|
|
202 }
|
|
203
|
|
204
|
|
205 bool HttpContentNegociation::Apply(const HttpHeaders& headers)
|
|
206 {
|
|
207 HttpHeaders::const_iterator accept = headers.find("accept");
|
|
208 if (accept != headers.end())
|
|
209 {
|
|
210 return Apply(accept->second);
|
|
211 }
|
|
212 else
|
|
213 {
|
|
214 return Apply("*/*");
|
|
215 }
|
|
216 }
|
|
217
|
|
218
|
|
219 bool HttpContentNegociation::Apply(const std::string& accept)
|
|
220 {
|
|
221 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
|
222 // https://en.wikipedia.org/wiki/Content_negotiation
|
|
223 // http://www.newmediacampaigns.com/blog/browser-rest-http-accept-headers
|
|
224
|
|
225 Tokens mediaRanges;
|
|
226 Toolbox::TokenizeString(mediaRanges, accept, ',');
|
|
227
|
|
228 std::auto_ptr<Reference> bestMatch;
|
|
229
|
|
230 for (Tokens::const_iterator it = mediaRanges.begin();
|
|
231 it != mediaRanges.end(); ++it)
|
|
232 {
|
|
233 Tokens parameters;
|
|
234 Toolbox::TokenizeString(parameters, *it, ';');
|
|
235
|
|
236 if (parameters.size() > 0)
|
|
237 {
|
|
238 float quality = GetQuality(parameters);
|
|
239
|
|
240 std::string type, subtype;
|
|
241 if (SplitPair(type, subtype, parameters[0], '/'))
|
|
242 {
|
|
243 for (Handlers::const_iterator it2 = handlers_.begin();
|
|
244 it2 != handlers_.end(); ++it2)
|
|
245 {
|
|
246 if (it2->IsMatch(type, subtype))
|
|
247 {
|
|
248 SelectBestMatch(bestMatch, *it2, type, subtype, quality);
|
|
249 }
|
|
250 }
|
|
251 }
|
|
252 }
|
|
253 }
|
|
254
|
|
255 if (bestMatch.get() == NULL) // No match was found
|
|
256 {
|
|
257 return false;
|
|
258 }
|
|
259 else
|
|
260 {
|
|
261 bestMatch->handler_.Call();
|
|
262 return true;
|
|
263 }
|
|
264 }
|
|
265 }
|