Mercurial > hg > orthanc
comparison OrthancFramework/Sources/HttpServer/IHttpHandler.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 | |
children | a01b1c9cbef4 |
comparison
equal
deleted
inserted
replaced
4328:ce9284aebd40 | 4329:9dc0e42f868b |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with this program. If not, see | |
19 * <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "../PrecompiledHeaders.h" | |
24 #include "IHttpHandler.h" | |
25 | |
26 #include "HttpOutput.h" | |
27 #include "HttpToolbox.h" | |
28 #include "StringHttpOutput.h" | |
29 | |
30 static const char* LOCALHOST = "127.0.0.1"; | |
31 | |
32 | |
33 namespace Orthanc | |
34 { | |
35 bool IHttpHandler::SimpleGet(std::string& result, | |
36 IHttpHandler& handler, | |
37 RequestOrigin origin, | |
38 const std::string& uri, | |
39 const IHttpHandler::Arguments& httpHeaders) | |
40 { | |
41 UriComponents curi; | |
42 IHttpHandler::GetArguments getArguments; | |
43 HttpToolbox::ParseGetQuery(curi, getArguments, uri.c_str()); | |
44 | |
45 StringHttpOutput stream; | |
46 HttpOutput http(stream, false /* no keep alive */); | |
47 | |
48 if (handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Get, curi, | |
49 httpHeaders, getArguments, NULL /* no body for GET */, 0)) | |
50 { | |
51 stream.GetOutput(result); | |
52 return true; | |
53 } | |
54 else | |
55 { | |
56 return false; | |
57 } | |
58 } | |
59 | |
60 | |
61 static bool SimplePostOrPut(std::string& result, | |
62 IHttpHandler& handler, | |
63 RequestOrigin origin, | |
64 HttpMethod method, | |
65 const std::string& uri, | |
66 const void* bodyData, | |
67 size_t bodySize, | |
68 const IHttpHandler::Arguments& httpHeaders) | |
69 { | |
70 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT | |
71 | |
72 UriComponents curi; | |
73 Toolbox::SplitUriComponents(curi, uri); | |
74 | |
75 StringHttpOutput stream; | |
76 HttpOutput http(stream, false /* no keep alive */); | |
77 | |
78 if (handler.Handle(http, origin, LOCALHOST, "", method, curi, | |
79 httpHeaders, getArguments, bodyData, bodySize)) | |
80 { | |
81 stream.GetOutput(result); | |
82 return true; | |
83 } | |
84 else | |
85 { | |
86 return false; | |
87 } | |
88 } | |
89 | |
90 | |
91 bool IHttpHandler::SimplePost(std::string& result, | |
92 IHttpHandler& handler, | |
93 RequestOrigin origin, | |
94 const std::string& uri, | |
95 const void* bodyData, | |
96 size_t bodySize, | |
97 const IHttpHandler::Arguments& httpHeaders) | |
98 { | |
99 return SimplePostOrPut(result, handler, origin, HttpMethod_Post, uri, bodyData, bodySize, httpHeaders); | |
100 } | |
101 | |
102 | |
103 bool IHttpHandler::SimplePut(std::string& result, | |
104 IHttpHandler& handler, | |
105 RequestOrigin origin, | |
106 const std::string& uri, | |
107 const void* bodyData, | |
108 size_t bodySize, | |
109 const IHttpHandler::Arguments& httpHeaders) | |
110 { | |
111 return SimplePostOrPut(result, handler, origin, HttpMethod_Put, uri, bodyData, bodySize, httpHeaders); | |
112 } | |
113 | |
114 | |
115 bool IHttpHandler::SimpleDelete(IHttpHandler& handler, | |
116 RequestOrigin origin, | |
117 const std::string& uri, | |
118 const IHttpHandler::Arguments& httpHeaders) | |
119 { | |
120 UriComponents curi; | |
121 Toolbox::SplitUriComponents(curi, uri); | |
122 | |
123 IHttpHandler::GetArguments getArguments; // No GET argument for DELETE | |
124 | |
125 StringHttpOutput stream; | |
126 HttpOutput http(stream, false /* no keep alive */); | |
127 | |
128 return handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Delete, curi, | |
129 httpHeaders, getArguments, NULL /* no body for DELETE */, 0); | |
130 } | |
131 } |