Mercurial > hg > orthanc
annotate Core/HttpServer/HttpContentNegociation.cpp @ 2919:2ca9cd064b15
PamReader::ReadFromMemory
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 06 Nov 2018 15:41:21 +0100 |
parents | 878b59270859 |
children | d924f9bb61cc |
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 |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
5 * Copyright (C) 2017-2018 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 { | |
162 LOG(ERROR) << "Quality parameter out of range in a HTTP request (must be between 0 and 1): " << value; | |
163 throw OrthancException(ErrorCode_BadRequest); | |
164 } | |
165 } | |
166 } | |
167 | |
168 return 1.0f; // Default quality | |
169 } | |
170 | |
171 | |
172 void HttpContentNegociation::SelectBestMatch(std::auto_ptr<Reference>& best, | |
173 const Handler& handler, | |
174 const std::string& type, | |
175 const std::string& subtype, | |
176 float quality) | |
177 { | |
178 std::auto_ptr<Reference> match(new Reference(handler, type, subtype, quality)); | |
179 | |
180 if (best.get() == NULL || | |
181 *best < *match) | |
182 { | |
183 best = match; | |
184 } | |
185 } | |
186 | |
187 | |
188 void HttpContentNegociation::Register(const std::string& mime, | |
189 IHandler& handler) | |
190 { | |
191 std::string type, subtype; | |
192 | |
193 if (SplitPair(type, subtype, mime, '/') && | |
194 type != "*" && | |
195 subtype != "*") | |
196 { | |
197 handlers_.push_back(Handler(type, subtype, handler)); | |
198 } | |
199 else | |
200 { | |
201 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
202 } | |
203 } | |
204 | |
205 | |
206 bool HttpContentNegociation::Apply(const HttpHeaders& headers) | |
207 { | |
208 HttpHeaders::const_iterator accept = headers.find("accept"); | |
209 if (accept != headers.end()) | |
210 { | |
211 return Apply(accept->second); | |
212 } | |
213 else | |
214 { | |
215 return Apply("*/*"); | |
216 } | |
217 } | |
218 | |
219 | |
220 bool HttpContentNegociation::Apply(const std::string& accept) | |
221 { | |
222 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 | |
223 // https://en.wikipedia.org/wiki/Content_negotiation | |
224 // http://www.newmediacampaigns.com/blog/browser-rest-http-accept-headers | |
225 | |
226 Tokens mediaRanges; | |
227 Toolbox::TokenizeString(mediaRanges, accept, ','); | |
228 | |
229 std::auto_ptr<Reference> bestMatch; | |
230 | |
231 for (Tokens::const_iterator it = mediaRanges.begin(); | |
232 it != mediaRanges.end(); ++it) | |
233 { | |
234 Tokens parameters; | |
235 Toolbox::TokenizeString(parameters, *it, ';'); | |
236 | |
237 if (parameters.size() > 0) | |
238 { | |
239 float quality = GetQuality(parameters); | |
240 | |
241 std::string type, subtype; | |
242 if (SplitPair(type, subtype, parameters[0], '/')) | |
243 { | |
244 for (Handlers::const_iterator it2 = handlers_.begin(); | |
245 it2 != handlers_.end(); ++it2) | |
246 { | |
247 if (it2->IsMatch(type, subtype)) | |
248 { | |
249 SelectBestMatch(bestMatch, *it2, type, subtype, quality); | |
250 } | |
251 } | |
252 } | |
253 } | |
254 } | |
255 | |
256 if (bestMatch.get() == NULL) // No match was found | |
257 { | |
258 return false; | |
259 } | |
260 else | |
261 { | |
262 bestMatch->handler_.Call(); | |
263 return true; | |
264 } | |
265 } | |
266 } |