comparison OrthancFramework/Sources/HttpServer/HttpToolbox.cpp @ 4329:9dc0e42f868b

moving static methods from HttpToolbox to IHttpHandler
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Nov 2020 13:46:49 +0100
parents 9684a690ca63
children a01b1c9cbef4
comparison
equal deleted inserted replaced
4328:ce9284aebd40 4329:9dc0e42f868b
21 21
22 22
23 #include "../PrecompiledHeaders.h" 23 #include "../PrecompiledHeaders.h"
24 #include "HttpToolbox.h" 24 #include "HttpToolbox.h"
25 25
26 #include "HttpOutput.h"
27 #include "StringHttpOutput.h"
28
29 #include <stdio.h>
30 #include <string.h> 26 #include <string.h>
31 #include <iostream>
32
33
34 static const char* LOCALHOST = "127.0.0.1";
35 27
36 28
37 namespace Orthanc 29 namespace Orthanc
38 { 30 {
39 static void SplitGETNameValue(IHttpHandler::GetArguments& result, 31 static void SplitGETNameValue(IHttpHandler::GetArguments& result,
183 for (size_t i = 0; i < source.size(); i++) 175 for (size_t i = 0; i < source.size(); i++)
184 { 176 {
185 compiled[source[i].first] = source[i].second; 177 compiled[source[i].first] = source[i].second;
186 } 178 }
187 } 179 }
188
189
190 bool HttpToolbox::SimpleGet(std::string& result,
191 IHttpHandler& handler,
192 RequestOrigin origin,
193 const std::string& uri,
194 const IHttpHandler::Arguments& httpHeaders)
195 {
196 UriComponents curi;
197 IHttpHandler::GetArguments getArguments;
198 ParseGetQuery(curi, getArguments, uri.c_str());
199
200 StringHttpOutput stream;
201 HttpOutput http(stream, false /* no keep alive */);
202
203 if (handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Get, curi,
204 httpHeaders, getArguments, NULL /* no body for GET */, 0))
205 {
206 stream.GetOutput(result);
207 return true;
208 }
209 else
210 {
211 return false;
212 }
213 }
214
215
216 static bool SimplePostOrPut(std::string& result,
217 IHttpHandler& handler,
218 RequestOrigin origin,
219 HttpMethod method,
220 const std::string& uri,
221 const void* bodyData,
222 size_t bodySize,
223 const IHttpHandler::Arguments& httpHeaders)
224 {
225 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT
226
227 UriComponents curi;
228 Toolbox::SplitUriComponents(curi, uri);
229
230 StringHttpOutput stream;
231 HttpOutput http(stream, false /* no keep alive */);
232
233 if (handler.Handle(http, origin, LOCALHOST, "", method, curi,
234 httpHeaders, getArguments, bodyData, bodySize))
235 {
236 stream.GetOutput(result);
237 return true;
238 }
239 else
240 {
241 return false;
242 }
243 }
244
245
246 bool HttpToolbox::SimplePost(std::string& result,
247 IHttpHandler& handler,
248 RequestOrigin origin,
249 const std::string& uri,
250 const void* bodyData,
251 size_t bodySize,
252 const IHttpHandler::Arguments& httpHeaders)
253 {
254 return SimplePostOrPut(result, handler, origin, HttpMethod_Post, uri, bodyData, bodySize, httpHeaders);
255 }
256
257
258 bool HttpToolbox::SimplePut(std::string& result,
259 IHttpHandler& handler,
260 RequestOrigin origin,
261 const std::string& uri,
262 const void* bodyData,
263 size_t bodySize,
264 const IHttpHandler::Arguments& httpHeaders)
265 {
266 return SimplePostOrPut(result, handler, origin, HttpMethod_Put, uri, bodyData, bodySize, httpHeaders);
267 }
268
269
270 bool HttpToolbox::SimpleDelete(IHttpHandler& handler,
271 RequestOrigin origin,
272 const std::string& uri,
273 const IHttpHandler::Arguments& httpHeaders)
274 {
275 UriComponents curi;
276 Toolbox::SplitUriComponents(curi, uri);
277
278 IHttpHandler::GetArguments getArguments; // No GET argument for DELETE
279
280 StringHttpOutput stream;
281 HttpOutput http(stream, false /* no keep alive */);
282
283 return handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Delete, curi,
284 httpHeaders, getArguments, NULL /* no body for DELETE */, 0);
285 }
286 } 180 }