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