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