Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | #include "gate/net/httptypes.hpp" | ||
29 | #include "gate/strings.hpp" | ||
30 | #include "gate/exceptions.hpp" | ||
31 | #include "gate/utilities.h" | ||
32 | |||
33 | namespace gate | ||
34 | { | ||
35 | namespace net | ||
36 | { | ||
37 | |||
38 | 4 | HttpRequest::HttpRequest(String const& method, String const& path) | |
39 | { | ||
40 | 4 | gate_mem_clear(this->c_impl(), sizeof(gate_http_request_t)); | |
41 | 4 | result_t result = gate_http_request_init(this->c_impl(), method.c_impl(), path.c_impl()); | |
42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | GATEXX_CHECK_ERROR(result); |
43 | 4 | } | |
44 | |||
45 | 1 | HttpRequest::HttpRequest(HttpRequest const& src) | |
46 | { | ||
47 | 1 | result_t result = gate_http_request_copy(this->c_impl(), src.c_impl()); | |
48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
49 | 1 | } | |
50 | 2 | HttpRequest::HttpRequest(gate_http_request_t const& src) | |
51 | { | ||
52 | 2 | result_t result = gate_http_request_copy(this->c_impl(), &src); | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATEXX_CHECK_ERROR(result); |
54 | 2 | } | |
55 | |||
56 | 1 | HttpRequest& HttpRequest::operator=(HttpRequest const& src) | |
57 | { | ||
58 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
59 | { | ||
60 | 1 | gate_http_request_t tmp = GATE_INIT_EMPTY; | |
61 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_http_request_copy(&tmp, src.c_impl()); |
62 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(result); |
63 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | gate_http_request_release(this->c_impl()); |
64 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | gate_mem_copy(this->c_impl(), &tmp, sizeof(tmp)); |
65 | } | ||
66 | 1 | return *this; | |
67 | } | ||
68 | 14 | HttpRequest::~HttpRequest() noexcept | |
69 | { | ||
70 | 7 | gate_http_request_release(this->c_impl()); | |
71 | 7 | } | |
72 | |||
73 | 20 | gate_http_request_t* HttpRequest::c_impl() noexcept | |
74 | { | ||
75 | 20 | return static_cast<gate_http_request_t*>(this); | |
76 | } | ||
77 | 2 | gate_http_request_t const* HttpRequest::c_impl() const noexcept | |
78 | { | ||
79 | 2 | return static_cast<gate_http_request_t const*>(this); | |
80 | } | ||
81 | |||
82 | |||
83 | 6 | static void replace_string(gate_string_t& target, String const& src, char const* funcName) | |
84 | { | ||
85 | gate_string_t tmp; | ||
86 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
|
6 | if (NULL == gate_string_clone(&tmp, src.c_impl())) |
87 | { | ||
88 | ✗ | gate::raiseError(results::OutOfMemory, funcName); | |
89 | } | ||
90 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | gate_string_release(&target); |
91 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | gate_mem_copy(&target, &tmp, sizeof(tmp)); |
92 | 6 | } | |
93 | |||
94 | 2 | String HttpRequest::getMethod() const | |
95 | { | ||
96 | 2 | return String::duplicate(this->method); | |
97 | } | ||
98 | ✗ | void HttpRequest::setMethod(String const& value) | |
99 | { | ||
100 | ✗ | replace_string(this->method, value, "HttpRequest::setMethod"); | |
101 | ✗ | } | |
102 | |||
103 | 2 | String HttpRequest::getPath() const | |
104 | { | ||
105 | 2 | return String::duplicate(this->path); | |
106 | } | ||
107 | ✗ | void HttpRequest::setPath(String const& value) | |
108 | { | ||
109 | ✗ | replace_string(this->path, value, "HttpRequest::setPath"); | |
110 | ✗ | } | |
111 | |||
112 | 1 | void HttpRequest::setAuthentication(String const& user, String const& password) | |
113 | { | ||
114 | 1 | replace_string(this->auth_user, user, "HttpRequest::setAuthentication"); | |
115 | 1 | replace_string(this->auth_pass, password, "HttpRequest::setAuthentication"); | |
116 | 1 | } | |
117 | 1 | String HttpRequest::getAuthUser() const | |
118 | { | ||
119 | 1 | return String::duplicate(this->auth_user); | |
120 | } | ||
121 | 1 | String HttpRequest::getAuthPass() const | |
122 | { | ||
123 | 1 | return String::duplicate(this->auth_pass); | |
124 | } | ||
125 | |||
126 | |||
127 | 1 | String HttpRequest::getVersion() const | |
128 | { | ||
129 | 1 | return String::duplicate(this->version); | |
130 | } | ||
131 | 1 | void HttpRequest::setVersion(String const& value) | |
132 | { | ||
133 | 1 | replace_string(this->version, value, "HttpRequest::setVersion"); | |
134 | 1 | } | |
135 | |||
136 | 2 | String HttpRequest::getHeader(String const& headerKey) const | |
137 | { | ||
138 | 2 | gate_string_t const* str = gate_util_stringmap_get_string((gate_map_t*)&this->headers, headerKey.c_impl()); | |
139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (str == NULL) |
140 | { | ||
141 | ✗ | GATEXX_RAISE_EXCEPTION(results::NoMatch, "Header entry not found", 0); | |
142 | ✗ | return String(); | |
143 | } | ||
144 | 2 | return String::duplicate(*str); | |
145 | } | ||
146 | 3 | void HttpRequest::setHeader(String const& headerKey, String const& headerValue) | |
147 | { | ||
148 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | if (NULL == gate_util_stringmap_add_string(&this->headers, headerKey.c_impl(), headerValue.c_impl())) |
149 | { | ||
150 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
151 | } | ||
152 | 3 | } | |
153 | ✗ | void HttpRequest::clearHeaders() | |
154 | { | ||
155 | ✗ | gate_map_clear(&this->headers); | |
156 | ✗ | } | |
157 | |||
158 | ✗ | void HttpRequest::addAcceptType(String const& acceptType) | |
159 | { | ||
160 | ✗ | if (!gate_util_stringset_add_string(&this->accept_types, acceptType.c_impl())) | |
161 | { | ||
162 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
163 | } | ||
164 | ✗ | } | |
165 | ✗ | void HttpRequest::removeAcceptType(String const& acceptType) | |
166 | { | ||
167 | ✗ | gate_util_stringset_remove_string(&this->accept_types, acceptType.c_impl()); | |
168 | ✗ | } | |
169 | ✗ | void HttpRequest::clearAcceptTypes() | |
170 | { | ||
171 | ✗ | gate_set_clear(&this->accept_types); | |
172 | ✗ | } | |
173 | |||
174 | 1 | uint32_t HttpRequest::getConnectTimeout() const | |
175 | { | ||
176 | 1 | return this->connect_timeout_ms; | |
177 | } | ||
178 | 1 | void HttpRequest::setConnectTimeout(uint32_t valueMS) | |
179 | { | ||
180 | 1 | this->connect_timeout_ms = valueMS; | |
181 | 1 | } | |
182 | |||
183 | 1 | uint32_t HttpRequest::getSendTimeout() const | |
184 | { | ||
185 | 1 | return this->send_timeout_ms; | |
186 | } | ||
187 | 1 | void HttpRequest::setSendTimeout(uint32_t valueMS) | |
188 | { | ||
189 | 1 | this->send_timeout_ms = valueMS; | |
190 | 1 | } | |
191 | |||
192 | 1 | uint32_t HttpRequest::getReceiveTimeout() const | |
193 | { | ||
194 | 1 | return this->receive_timeout_ms; | |
195 | } | ||
196 | 1 | void HttpRequest::setReceiveTimeout(uint32_t valueMS) | |
197 | { | ||
198 | 1 | this->receive_timeout_ms = valueMS; | |
199 | 1 | } | |
200 | |||
201 | 1 | void HttpRequest::setProxy(String const& proxyServer, uint16_t proxyPort, String const& user, String const& pass) | |
202 | { | ||
203 | static char const* const funcName = "HttpRequest::setProxy"; | ||
204 | 1 | replace_string(this->proxy_server, proxyServer, funcName); | |
205 | 1 | replace_string(this->proxy_user, user, funcName); | |
206 | 1 | replace_string(this->proxy_pass, pass, funcName); | |
207 | 1 | this->proxy_port = proxyPort; | |
208 | 1 | } | |
209 | ✗ | void HttpRequest::noProxy() | |
210 | { | ||
211 | ✗ | gate_string_release(&this->proxy_server); | |
212 | ✗ | gate_string_release(&this->proxy_user); | |
213 | ✗ | gate_string_release(&this->proxy_pass); | |
214 | ✗ | this->proxy_port = 0; | |
215 | ✗ | } | |
216 | 1 | String HttpRequest::getProxyServer() const | |
217 | { | ||
218 | 1 | return String::duplicate(this->proxy_server); | |
219 | } | ||
220 | 1 | uint16_t HttpRequest::getProxyPort() const | |
221 | { | ||
222 | 1 | return this->proxy_port; | |
223 | } | ||
224 | 1 | String HttpRequest::getProxyUser() const | |
225 | { | ||
226 | 1 | return String::duplicate(this->proxy_user); | |
227 | } | ||
228 | 1 | String HttpRequest::getProxyPass() const | |
229 | { | ||
230 | 1 | return String::duplicate(this->proxy_pass); | |
231 | } | ||
232 | |||
233 | 1 | Stream HttpRequest::getUploadStream() const | |
234 | { | ||
235 | 1 | Stream strm(this->upload_stream); | |
236 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_object_retain(this->upload_stream); |
237 | 1 | return strm; | |
238 | } | ||
239 | |||
240 | 1 | void HttpRequest::setUploadStream(Stream& strm) | |
241 | { | ||
242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (this->upload_stream != NULL) |
243 | { | ||
244 | ✗ | gate_object_release(this->upload_stream); | |
245 | } | ||
246 | 1 | this->upload_stream = strm.c_impl(); | |
247 | 1 | gate_object_retain(this->upload_stream); | |
248 | 1 | } | |
249 | |||
250 | |||
251 | |||
252 | |||
253 | |||
254 | |||
255 | |||
256 | |||
257 | |||
258 | |||
259 | |||
260 | |||
261 | |||
262 | |||
263 | 4 | HttpResponse::HttpResponse() | |
264 | { | ||
265 | 4 | result_t result = gate_http_response_init(this); | |
266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | GATEXX_CHECK_ERROR(result); |
267 | 4 | } | |
268 | 1 | HttpResponse::HttpResponse(HttpResponse const& src) | |
269 | { | ||
270 | 1 | result_t result = gate_http_response_copy(this, &src); | |
271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
272 | 1 | } | |
273 | 1 | HttpResponse& HttpResponse::operator=(HttpResponse const& src) | |
274 | { | ||
275 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
276 | { | ||
277 | 1 | gate_http_response_t tmp = GATE_INIT_EMPTY; | |
278 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_http_response_copy(&tmp, &src); |
279 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(result); |
280 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_http_response_release(this); |
281 | 1 | *static_cast<gate_http_response_t*>(this) = tmp; | |
282 | } | ||
283 | 1 | return *this; | |
284 | } | ||
285 | 10 | HttpResponse::~HttpResponse() noexcept | |
286 | { | ||
287 | 5 | gate_http_response_release(this); | |
288 | 5 | } | |
289 | 3 | uint32_t HttpResponse::getStatus() const | |
290 | { | ||
291 | 3 | return this->status_code; | |
292 | } | ||
293 | 2 | Map<String, String> HttpResponse::getHeaders() const | |
294 | { | ||
295 | 2 | Map<String, String> ret; | |
296 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | gate_map_iterator_t iter = gate_map_first(&this->headers); |
297 |
3/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | while (gate_map_iterator_valid(iter)) |
298 | { | ||
299 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | gate_string_t const* ptr_key = (gate_string_t const*)gate_map_iterator_key(iter); |
300 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | gate_string_t const* ptr_value = (gate_string_t const*)gate_map_iterator_value(iter); |
301 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (ptr_key && ptr_value) |
302 | { | ||
303 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | String key(*ptr_key); |
304 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | String value(*ptr_value); |
305 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | ret.add(key, value); |
306 | } | ||
307 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | iter = gate_map_iterator_next(iter); |
308 | } | ||
309 | 2 | return ret; | |
310 | } | ||
311 | 2 | Stream HttpResponse::getResponseStream() const | |
312 | { | ||
313 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (this->response_stream) |
314 | { | ||
315 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | Stream strm(this->response_stream); |
316 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | gate_object_retain(this->response_stream); |
317 | 2 | return strm; | |
318 | } | ||
319 | else | ||
320 | { | ||
321 | ✗ | return NullStream(); | |
322 | } | ||
323 | } | ||
324 | |||
325 | } // end of namespace net | ||
326 | } // end of namespace gate | ||
327 | |||
328 |