annotate Core/HttpServer/HttpContentNegociation.cpp @ 3873:ae5bdde751fd transcoding

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