Mercurial > hg > orthanc
annotate Core/HttpServer/HttpToolbox.cpp @ 2018:300599489cab
USE_BOOST_LOCALE_BACKENDS
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 14 Jun 2016 15:51:00 +0200 |
parents | b1291df2f780 |
children | 65b1ce7cb84f |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
912
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
0 | 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. | |
136 | 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. | |
0 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
34 #include "HttpToolbox.h" |
0 | 35 |
1444
b2b09a3dbd8e
refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
36 #include <stdio.h> |
0 | 37 #include <string.h> |
912
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
38 #include <iostream> |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
39 |
1437
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
40 #include "HttpOutput.h" |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
41 #include "StringHttpOutput.h" |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
42 |
0 | 43 |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
44 static const char* LOCALHOST = "localhost"; |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
45 |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
46 |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
47 |
59 | 48 namespace Orthanc |
0 | 49 { |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
50 static void SplitGETNameValue(IHttpHandler::GetArguments& result, |
0 | 51 const char* start, |
52 const char* end) | |
53 { | |
334 | 54 std::string name, value; |
55 | |
0 | 56 const char* equal = strchr(start, '='); |
57 if (equal == NULL || equal >= end) | |
58 { | |
334 | 59 name = std::string(start, end - start); |
60 //value = ""; | |
0 | 61 } |
62 else | |
63 { | |
334 | 64 name = std::string(start, equal - start); |
65 value = std::string(equal + 1, end); | |
0 | 66 } |
334 | 67 |
336 | 68 Toolbox::UrlDecode(name); |
69 Toolbox::UrlDecode(value); | |
70 | |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
71 result.push_back(std::make_pair(name, value)); |
0 | 72 } |
73 | |
74 | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
75 void HttpToolbox::ParseGetArguments(IHttpHandler::GetArguments& result, |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
76 const char* query) |
0 | 77 { |
78 const char* pos = query; | |
79 | |
80 while (pos != NULL) | |
81 { | |
82 const char* ampersand = strchr(pos, '&'); | |
83 if (ampersand) | |
84 { | |
85 SplitGETNameValue(result, pos, ampersand); | |
86 pos = ampersand + 1; | |
87 } | |
88 else | |
89 { | |
90 // No more ampersand, this is the last argument | |
91 SplitGETNameValue(result, pos, pos + strlen(pos)); | |
92 pos = NULL; | |
93 } | |
94 } | |
95 } | |
96 | |
97 | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
98 void HttpToolbox::ParseGetQuery(UriComponents& uri, |
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
99 IHttpHandler::GetArguments& getArguments, |
912
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
100 const char* query) |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
101 { |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
102 const char *questionMark = ::strchr(query, '?'); |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
103 if (questionMark == NULL) |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
104 { |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
105 // No question mark in the string |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
106 Toolbox::SplitUriComponents(uri, query); |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
107 getArguments.clear(); |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
108 } |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
109 else |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
110 { |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
111 Toolbox::SplitUriComponents(uri, std::string(query, questionMark)); |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
112 HttpToolbox::ParseGetArguments(getArguments, questionMark + 1); |
912
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
113 } |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
114 } |
dcb2469f00f4
PluginsHttpHandler::RestApiGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
115 |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
116 |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
117 std::string HttpToolbox::GetArgument(const IHttpHandler::Arguments& getArguments, |
0 | 118 const std::string& name, |
119 const std::string& defaultValue) | |
120 { | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
121 IHttpHandler::Arguments::const_iterator it = getArguments.find(name); |
207 | 122 if (it == getArguments.end()) |
0 | 123 { |
124 return defaultValue; | |
125 } | |
126 else | |
127 { | |
128 return it->second; | |
129 } | |
130 } | |
330 | 131 |
132 | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
133 std::string HttpToolbox::GetArgument(const IHttpHandler::GetArguments& getArguments, |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
134 const std::string& name, |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
135 const std::string& defaultValue) |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
136 { |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
137 for (size_t i = 0; i < getArguments.size(); i++) |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
138 { |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
139 if (getArguments[i].first == name) |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
140 { |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
141 return getArguments[i].second; |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
142 } |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
143 } |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
144 |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
145 return defaultValue; |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
146 } |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
147 |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
148 |
330 | 149 |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
150 void HttpToolbox::ParseCookies(IHttpHandler::Arguments& result, |
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
151 const IHttpHandler::Arguments& httpHeaders) |
330 | 152 { |
153 result.clear(); | |
154 | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
155 IHttpHandler::Arguments::const_iterator it = httpHeaders.find("cookie"); |
330 | 156 if (it != httpHeaders.end()) |
157 { | |
158 const std::string& cookies = it->second; | |
159 | |
160 size_t pos = 0; | |
161 while (pos != std::string::npos) | |
162 { | |
163 size_t nextSemicolon = cookies.find(";", pos); | |
164 std::string cookie; | |
165 | |
166 if (nextSemicolon == std::string::npos) | |
167 { | |
168 cookie = cookies.substr(pos); | |
169 pos = std::string::npos; | |
170 } | |
171 else | |
172 { | |
173 cookie = cookies.substr(pos, nextSemicolon - pos); | |
174 pos = nextSemicolon + 1; | |
175 } | |
176 | |
177 size_t equal = cookie.find("="); | |
178 if (equal != std::string::npos) | |
179 { | |
180 std::string name = Toolbox::StripSpaces(cookie.substr(0, equal)); | |
181 std::string value = Toolbox::StripSpaces(cookie.substr(equal + 1)); | |
182 result[name] = value; | |
183 } | |
184 } | |
185 } | |
186 } | |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
187 |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
188 |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
189 void HttpToolbox::CompileGetArguments(IHttpHandler::Arguments& compiled, |
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
190 const IHttpHandler::GetArguments& source) |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
191 { |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
192 compiled.clear(); |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
193 |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
194 for (size_t i = 0; i < source.size(); i++) |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
195 { |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
196 compiled[source[i].first] = source[i].second; |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
197 } |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
198 } |
1437
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
199 |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
200 |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
201 bool HttpToolbox::SimpleGet(std::string& result, |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
202 IHttpHandler& handler, |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
203 RequestOrigin origin, |
1780
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
204 const std::string& uri, |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
205 const IHttpHandler::Arguments& httpHeaders) |
1437
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
206 { |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
207 UriComponents curi; |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
208 IHttpHandler::GetArguments getArguments; |
1437
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
209 ParseGetQuery(curi, getArguments, uri.c_str()); |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
210 |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
211 StringHttpOutput stream; |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
212 HttpOutput http(stream, false /* no keep alive */); |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
213 |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
214 if (handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Get, curi, |
1780
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
215 httpHeaders, getArguments, NULL /* no body for GET */, 0)) |
1437
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
216 { |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
217 stream.GetOutput(result); |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
218 return true; |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
219 } |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
220 else |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
221 { |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
222 return false; |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
223 } |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
224 } |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
225 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
226 |
1780
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
227 bool HttpToolbox::SimpleGet(std::string& result, |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
228 IHttpHandler& handler, |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
229 RequestOrigin origin, |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
230 const std::string& uri) |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
231 { |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
232 IHttpHandler::Arguments headers; // No HTTP header |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
233 return SimpleGet(result, handler, origin, uri, headers); |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
234 } |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
235 |
94990da8710e
OrthancPluginRestApiGet2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
236 |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
237 static bool SimplePostOrPut(std::string& result, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
238 IHttpHandler& handler, |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
239 RequestOrigin origin, |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
240 HttpMethod method, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
241 const std::string& uri, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
242 const char* bodyData, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
243 size_t bodySize) |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
244 { |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
245 IHttpHandler::Arguments headers; // No HTTP header |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
246 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
247 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
248 UriComponents curi; |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
249 Toolbox::SplitUriComponents(curi, uri); |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
250 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
251 StringHttpOutput stream; |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
252 HttpOutput http(stream, false /* no keep alive */); |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
253 |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
254 if (handler.Handle(http, origin, LOCALHOST, "", method, curi, |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
255 headers, getArguments, bodyData, bodySize)) |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
256 { |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
257 stream.GetOutput(result); |
1437
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
258 return true; |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
259 } |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
260 else |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
261 { |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
262 return false; |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
263 } |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
264 } |
02f5a3f5c0a0
access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
265 |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
266 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
267 bool HttpToolbox::SimplePost(std::string& result, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
268 IHttpHandler& handler, |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
269 RequestOrigin origin, |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
270 const std::string& uri, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
271 const char* bodyData, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
272 size_t bodySize) |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
273 { |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
274 return SimplePostOrPut(result, handler, origin, HttpMethod_Post, uri, bodyData, bodySize); |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
275 } |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
276 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
277 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
278 bool HttpToolbox::SimplePut(std::string& result, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
279 IHttpHandler& handler, |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
280 RequestOrigin origin, |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
281 const std::string& uri, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
282 const char* bodyData, |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
283 size_t bodySize) |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
284 { |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
285 return SimplePostOrPut(result, handler, origin, HttpMethod_Put, uri, bodyData, bodySize); |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
286 } |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
287 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
288 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
289 bool HttpToolbox::SimpleDelete(IHttpHandler& handler, |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
290 RequestOrigin origin, |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
291 const std::string& uri) |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
292 { |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
293 UriComponents curi; |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
294 Toolbox::SplitUriComponents(curi, uri); |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
295 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
296 IHttpHandler::Arguments headers; // No HTTP header |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
297 IHttpHandler::GetArguments getArguments; // No GET argument for DELETE |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
298 |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
299 StringHttpOutput stream; |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
300 HttpOutput http(stream, false /* no keep alive */); |
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
301 |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
302 return handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Delete, curi, |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1447
diff
changeset
|
303 headers, getArguments, NULL /* no body for DELETE */, 0); |
1447
5ba7471780ae
refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
304 } |
0 | 305 } |