GCC Code Coverage Report


Directory: src/gate/
File: src/gate/net/httpclients.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 339 442 76.7%
Functions: 14 15 93.3%
Branches: 162 358 45.3%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, 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
29 #include "gate/net/httpclients.h"
30 #include "gate/results.h"
31 #include "gate/utilities.h"
32
33 #if defined(GATE_SYS_WIN)
34 # define GATE_NET_HTTPCLIENT_NATIVE_IMPL 1
35 # if defined(GATE_SYS_WINSTORE) || defined(GATE_SYS_WIN16)
36 # define GATE_NET_HTTPCLIENT_NO_IMPL 1
37 # else
38 //# define GATE_NET_HTTPCLIENT_CURL
39 # define GATE_NET_HTTPCLIENT_WINAPI 1
40 # endif
41
42 #elif defined(GATE_SYS_WASM_WEB)
43 # define GATE_NET_HTTPCLIENT_WASM_IMPL 1
44 #else
45 # define GATE_NET_HTTPCLIENT_NATIVE_IMPL
46 # define GATE_NET_HTTPCLIENT_CURL
47 #endif
48
49 #if defined(GATE_NET_HTTPCLIENT_NATIVE_IMPL)
50
51 #include "gate/net/sockets.h"
52 #include "gate/net/sslsessions.h"
53 #include "gate/net/nameresolvers.h"
54 #include "gate/net/sockettools.h"
55
56 4 static gate_result_t native_httpclient_release(gate_httpclient_t* client)
57 {
58
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (client)
59 {
60 4 gate_string_release(&client->server);
61 4 gate_mem_clear(client, sizeof(gate_httpclient_t));
62 }
63 4 return GATE_RESULT_OK;
64 }
65
66 4 static gate_result_t native_httpclient_create(gate_httpclient_t* client, gate_string_t const* server, gate_uint16_t port, gate_enumint_t flags)
67 {
68 4 gate_result_t ret = GATE_RESULT_FAILED;
69 gate_socket_endpoint_t ep;
70 4 gate_size_t ep_count = 1;
71 gate_uint32_t ipaddr;
72
73 do
74 {
75
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!client || !server)
76 {
77 ✗ ret = GATE_RESULT_INVALIDARG;
78 ✗ break;
79 }
80
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (port == 0)
81 {
82
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_FLAG_ENABLED(flags, GATE_HTTPCLIENT_FLAG_SECURE))
83 {
84 2 port = 443;
85 }
86 else
87 {
88 ✗ port = 80;
89 }
90 }
91
92 4 gate_mem_clear(client, sizeof(gate_httpclient_t));
93 4 client->flags = flags;
94 4 client->port = port;
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (NULL == gate_string_clone(&client->server, server))
96 {
97 ✗ ret = GATE_RESULT_OUTOFMEMORY;
98 ✗ break;
99 }
100 4 ret = gate_socket_init();
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
102
103 4 ret = gate_net_resolve_host(server, GATE_SOCKET_FAMILY_INET4, &ep, &ep_count);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
105
106 4 ipaddr = ep.ip4.address.item[0];
107 4 ipaddr |= ((gate_uint32_t)ep.ip4.address.item[1]) << 8;
108 4 ipaddr |= ((gate_uint32_t)ep.ip4.address.item[2]) << 16;
109 4 ipaddr |= ((gate_uint32_t)ep.ip4.address.item[3]) << 24;
110 4 *((gate_uint32_t*)&client->handles[0]) = ipaddr;
111 4 ret = GATE_RESULT_OK;
112 } while (0);
113
114
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (GATE_FAILED(ret) && client)
115 {
116 ✗ native_httpclient_release(client);
117 }
118 4 return ret;
119 }
120
121 static gate_string_t const default_http_version = GATE_STRING_INIT_STATIC("1.1");
122
123 4 static gate_result_t native_create_http_header(
124 gate_http_request_t* ptr_request,
125 gate_string_t* ptr_out_header,
126 gate_bool_t* ptr_out_connclose)
127 {
128 gate_result_t ret;
129 4 gate_stringstream_t* ss = NULL;
130 do
131 {
132 gate_string_t const* value_connection;
133 4 ss = gate_stringstream_create(256);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ss == NULL)
135 {
136 ✗ ret = GATE_RESULT_OUTOFMEMORY;
137 ✗ break;
138 }
139
140 4 ret = gate_http_request_build_header(ptr_request, (gate_stream_t*)ss);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
142
143 4 ret = gate_stringstream_to_string(ss, ptr_out_header);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
145
146 4 value_connection = gate_util_stringmap_get(&ptr_request->headers, GATE_HTTP_HEADER_CONNECTION);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (value_connection == NULL)
148 {
149 ✗ *ptr_out_connclose = true;
150 }
151 else
152 {
153 4 *ptr_out_connclose = gate_string_equals_str_ic(value_connection, GATE_HTTP_HEADERVALUE_CONNECTION_KEEPALIVE);
154 }
155 4 ret = GATE_RESULT_OK;
156 } while (0);
157
158
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ss != NULL)
159 {
160 4 gate_object_release(ss);
161 }
162 4 return ret;
163 }
164
165 4 static gate_string_t* native_read_response_header(gate_stream_t* strm, gate_string_t* header)
166 {
167 4 gate_string_t* ret = NULL;
168 4 gate_strbuilder_t builder = GATE_INIT_EMPTY;
169 char buffer[4];
170 gate_size_t received;
171 gate_result_t result;
172 char const* ptr_content;
173 do
174 {
175 4 gate_strbuilder_create(&builder, 512);
176
177 for (;;)
178 {
179 778 received = 0;
180 782 result = gate_stream_read(strm, &buffer[0], 1, &received);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 782 times.
782 if (GATE_FAILED(result))
182 {
183 ✗ break;
184 }
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 782 times.
782 if (received == 0)
186 {
187 ✗ result = GATE_RESULT_ENDOFSTREAM;
188 ✗ break;
189 }
190 782 gate_strbuilder_append_text(&builder, &buffer[0], 1);
191 782 received = gate_strbuilder_length(&builder);
192
2/2
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 12 times.
782 if (received >= 4)
193 {
194 770 ptr_content = gate_strbuilder_ptr(&builder, received - 4);
195
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 766 times.
770 if (gate_str_compare(ptr_content, 4, "\r\n\r\n", 4) == 0)
196 {
197 /* end of header found */
198 4 break;
199 }
200 }
201 }
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(result);
203
204 4 ret = gate_strbuilder_to_string(&builder, header);
205 } while (0);
206
207 4 gate_strbuilder_release(&builder);
208
209 4 return ret;
210 }
211
212 4 static gate_result_t native_parse_response_header(gate_string_t const* http_header, gate_http_response_t* response)
213 {
214 4 gate_result_t ret = GATE_RESULT_FAILED;
215 4 gate_string_t line = GATE_STRING_INIT_EMPTY;
216 4 gate_string_t header = GATE_STRING_INIT_EMPTY;
217 4 gate_string_t key = GATE_STRING_INIT_EMPTY;
218 4 gate_string_t value = GATE_STRING_INIT_EMPTY;
219 gate_size_t pos;
220 4 gate_bool_t first_line = true;
221 4 gate_uint64_t status = 0;
222
223 do
224 {
225
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (NULL == gate_string_clone(&header, http_header))
226 {
227 ✗ ret = GATE_RESULT_OUTOFMEMORY;
228 ✗ break;
229 }
230 4 ret = GATE_RESULT_OK;
231
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 4 times.
32 while (NULL != gate_string_read_line(&line, &header, &header))
232 {
233
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (first_line)
234 {
235 /* "HTTP/x.y 222 TEXT" */
236
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!gate_string_starts_with_str(&line, "HTTP/"))
237 {
238 ✗ ret = GATE_RESULT_INVALIDHEADER;
239 ✗ break;
240 }
241 4 gate_str_parse_uint64(gate_string_ptr(&line, 9), 3, &status);
242 4 response->status_code = (gate_uint32_t)status;
243 4 first_line = false;
244 }
245 else
246 {
247 24 gate_string_trim(&line, &line);
248
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 4 times.
24 if (!gate_string_is_empty(&line))
249 {
250 20 pos = gate_string_char_pos(&line, ':', 0);
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (pos == GATE_STR_NPOS)
252 {
253 ✗ ret = GATE_RESULT_INVALIDCONTENT;
254 ✗ break;
255 }
256 20 gate_string_substr(&key, &line, 0, pos);
257 20 gate_string_substr(&value, &line, pos + 1, GATE_STR_NPOS);
258 20 gate_string_trim(&value, &value);
259
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (!gate_string_is_empty(&key))
260 {
261 20 gate_map_add(&response->headers, &key, &value);
262 }
263 20 gate_string_release(&key);
264 20 gate_string_release(&value);
265 }
266 }
267 28 gate_string_release(&line);
268 }
269 } while (0);
270
271 4 gate_string_release(&header);
272 4 gate_string_release(&line);
273 4 return ret;
274 }
275
276 4 gate_result_t native_httpclient_send_request(gate_httpclient_t* client, gate_http_request_t* ptr_request, gate_http_response_t* response)
277 {
278 4 gate_result_t ret = GATE_RESULT_FAILED;
279 4 gate_string_t request_header = GATE_STRING_INIT_EMPTY;
280 4 gate_string_t response_header = GATE_STRING_INIT_EMPTY;
281 gate_socket_endpoint_t ep;
282 4 gate_controlstream_t* ptr_controlstream = NULL;
283 4 gate_stream_t* ptr_sockstream = NULL;
284 4 gate_stream_t* ptr_sslstream = NULL;
285 gate_ssl_session_params_t ssl_params;
286 4 gate_size_t written = 0;
287 4 gate_bool_t keep_alive = false;
288 4 gate_http_request_t request = GATE_INIT_EMPTY;
289
290 do
291 {
292 4 ret = gate_http_request_copy(&request, ptr_request);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
294
295 4 ret = gate_http_response_init(response);
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
297
298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (gate_string_is_empty(&request.host))
299 {
300 ✗ gate_string_duplicate(&request.host, &client->server);
301 }
302
303 4 ret = native_create_http_header(&request, &request_header, &keep_alive);
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
305
306 4 ep.ip4.family = GATE_SOCKET_FAMILY_INET4;
307 4 *((gate_uint32_t*)&ep.ip4.address.item[0]) = *((gate_uint32_t*)&client->handles[0]);
308 4 ep.ip4.port = client->port;
309
310 4 ret = gate_socketstream_create_endpoint(&ep, &ptr_controlstream, false);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
312 4 ptr_sockstream = (gate_stream_t*)ptr_controlstream;
313
314
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_SECURE))
315 {
316 2 gate_mem_clear(&ssl_params, sizeof(ssl_params));
317 2 ssl_params.server_side = false;
318 2 ssl_params.session_version = GATE_SSL_SESSION_TYPE_AUTO;
319 2 gate_string_clone(&ssl_params.host_name, &client->server);
320 2 ret = gate_ssl_stream_create(&ssl_params, ptr_sockstream, &ptr_sslstream);
321 2 gate_string_release(&ssl_params.host_name);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
323
324 2 gate_object_release(ptr_sockstream);
325 2 ptr_controlstream = NULL;
326 2 ptr_sockstream = ptr_sslstream;
327 2 ptr_sslstream = NULL;
328
329 /* ensure tls negotiation: */
330 2 ret = gate_stream_flush(ptr_sockstream);
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
332 }
333
334 /* send HTTP header */
335 4 ret = gate_stream_write_block(ptr_sockstream, gate_string_ptr(&request_header, 0), gate_string_length(&request_header), &written);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
337
338
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (request.upload_stream)
339 {
340 /* send HTTP upload content */
341 2 gate_size_t transferred = 0;
342 do
343 {
344 4 ret = gate_http_request_build_upload_chunk(&request, ptr_sockstream, 0, &transferred);
345
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4 } while (GATE_SUCCEEDED(ret) && (transferred > 0));
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
347 }
348
349
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4 if (!keep_alive && (ptr_controlstream != NULL))
350 {
351 /* socket shutdown-send */
352 2 gate_stream_close(ptr_controlstream, GATE_STREAM_CLOSE_OUTPUT);
353 }
354
355 /* await response */
356
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (NULL == native_read_response_header(ptr_sockstream, &response_header))
357 {
358 ✗ ret = GATE_RESULT_INVALIDHEADER;
359 ✗ break;
360 }
361
362 4 ret = native_parse_response_header(&response_header, response);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
364
365 4 response->response_stream = ptr_sockstream;
366 4 ptr_sockstream = NULL;
367
368 } while (0);
369
370 4 gate_http_request_release(&request);
371
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ptr_sockstream)
373 {
374 ✗ gate_object_release(ptr_sockstream);
375 }
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (GATE_FAILED(ret))
377 {
378 ✗ gate_http_response_release(response);
379 }
380 4 gate_string_release(&response_header);
381 4 gate_string_release(&request_header);
382 4 return ret;
383 }
384
385
386 #endif
387
388
389 #if defined(GATE_NET_HTTPCLIENT_WINAPI)
390
391 #include "gate/net/platform/wininet_api.h"
392 #include "gate/utilities.h"
393 #include "gate/debugging.h"
394
395
396 static gate_result_t wininet_httpclient_create(gate_httpclient_t* client, gate_string_t const* server, gate_uint16_t port, gate_enumint_t flags)
397 {
398 gate_result_t ret;
399 HINTERNET hinet = NULL;
400 HINTERNET hconnect = NULL;
401 DWORD dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG; /* INTERNET_OPEN_TYPE_DIRECT */
402 DWORD dwFlags = 0;
403 TCHAR httphost[4096];
404 gate_bool_t secure = GATE_FLAG_ENABLED(flags, GATE_HTTPCLIENT_FLAG_SECURE);
405 INTERNET_PORT httpport = (port == 0) ? (secure ? 443 : 80) : port;
406
407 do
408 {
409 ret = load_wininet_api();
410 GATE_BREAK_IF_FAILED_TRACE(ret, "load_wininet_api() failed");
411
412 gate_mem_clear(client, sizeof(gate_httpclient_t));
413
414 if (0 == gate_win32_utf8_2_winstr(server->str, server->length, httphost, sizeof(httphost) / sizeof(httphost[0])))
415 {
416 ret = GATE_RESULT_INVALIDARG;
417 break;
418 }
419
420 hinet = WinInet.InetOpen(_T("OpenGATE HTTP Client"), dwAccessType, NULL, NULL, dwFlags);
421 if (hinet == NULL)
422 {
423 GATE_DEBUG_TRACE("InternetOpen() failed");
424 gate_win32_print_lasterror(&ret, NULL, 0);
425 break;
426 }
427
428 hconnect = WinInet.InetConnect(hinet, httphost, httpport, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)client);
429 if (hconnect == NULL)
430 {
431 GATE_DEBUG_TRACE("InternetConnect() failed");
432 gate_win32_print_lasterror(&ret, NULL, 0);
433 break;
434 }
435
436 if (NULL == gate_string_clone(&client->server, server))
437 {
438 ret = GATE_RESULT_OUTOFMEMORY;
439 break;
440 }
441
442 client->flags = flags;
443 client->port = port;
444 client->handles[0] = hinet;
445 client->handles[1] = hconnect;
446 hinet = NULL;
447 hconnect = NULL;
448 ret = GATE_RESULT_OK;
449 } while (0);
450
451 if (hconnect != NULL)
452 {
453 WinInet.InetCloseHandle(hconnect);
454 }
455 if (hinet != NULL)
456 {
457 WinInet.InetCloseHandle(hinet);
458 }
459 return ret;
460 }
461
462 gate_result_t wininet_httpclient_release(gate_httpclient_t* client)
463 {
464 gate_result_t ret = GATE_RESULT_OK;
465 HINTERNET hinet = (HINTERNET)client->handles[0];
466 HINTERNET hconnect = (HINTERNET)client->handles[1];
467
468 if (hconnect)
469 {
470 WinInet.InetCloseHandle(hconnect);
471 }
472 if (hinet)
473 {
474 WinInet.InetCloseHandle(hinet);
475 }
476
477 gate_string_release(&client->server);
478 gate_mem_clear(client, sizeof(gate_httpclient_t));
479
480 return ret;
481 }
482
483
484 static void gate_http_response_release_impl(void* self);
485 static int gate_http_response_retain_impl(void* self);
486 static char const* gate_http_response_interface_name_impl(void* self);
487 static gate_result_t gate_http_response_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
488 static gate_result_t gate_http_response_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
489 static gate_result_t gate_http_response_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
490 static gate_result_t gate_http_response_flush_impl(void* self);
491
492
493 static GATE_INTERFACE_VTBL(gate_stream) gate_http_client_response_vtbl;
494 static void gate_init_http_client_response_vtbl()
495 {
496 if (!gate_http_client_response_vtbl.get_interface_name)
497 {
498 GATE_INTERFACE_VTBL(gate_stream) const local_vtbl =
499 {
500 &gate_http_response_interface_name_impl,
501 &gate_http_response_release_impl,
502 &gate_http_response_retain_impl,
503 &gate_http_response_read_impl,
504 &gate_http_response_peek_impl,
505 &gate_http_response_write_impl,
506 &gate_http_response_flush_impl
507 };
508 gate_http_client_response_vtbl = local_vtbl;
509 }
510 }
511
512
513 typedef struct gate_http_response_impl
514 {
515 GATE_INTERFACE_VTBL(gate_stream)* vtbl;
516
517 gate_atomic_int_t ref_counter;
518 HINTERNET handle;
519 } gate_http_response_impl_t;
520
521 static void gate_http_response_release_impl(void* self)
522 {
523 gate_http_response_impl_t* impl = (gate_http_response_impl_t*)self;
524 if (gate_atomic_int_dec(&impl->ref_counter) == 0)
525 {
526 WinInet.InetCloseHandle(impl->handle);
527 gate_mem_dealloc(impl);
528 }
529 }
530 static int gate_http_response_retain_impl(void* self)
531 {
532 gate_http_response_impl_t* impl = (gate_http_response_impl_t*)self;
533 return gate_atomic_int_inc(&impl->ref_counter);
534 }
535 static char const* gate_http_response_interface_name_impl(void* self)
536 {
537 GATE_UNUSED_ARG(self);
538 return GATE_INTERFACE_NAME_STREAM;
539 }
540 static gate_result_t gate_http_response_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
541 {
542 gate_http_response_impl_t* impl = (gate_http_response_impl_t*)self;
543 gate_result_t ret;
544 DWORD bytesreceived = 0;
545 if (0 == WinInet.InetReadFile(impl->handle, buffer, (DWORD)bufferlength, &bytesreceived))
546 {
547 GATE_DEBUG_TRACE("InternetReadFile() failed");
548 gate_win32_print_lasterror(&ret, NULL, 0);
549 }
550 else
551 {
552 if (returned != NULL)
553 {
554 *returned = (gate_size_t)bytesreceived;
555 }
556 ret = GATE_RESULT_OK;
557 }
558 return ret;
559 }
560 static gate_result_t gate_http_response_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
561 {
562 GATE_UNUSED_ARG(self);
563 GATE_UNUSED_ARG(buffer);
564 GATE_UNUSED_ARG(bufferlength);
565 GATE_UNUSED_ARG(returned);
566 return GATE_RESULT_NOTSUPPORTED;
567 }
568 static gate_result_t gate_http_response_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
569 {
570 GATE_UNUSED_ARG(self);
571 GATE_UNUSED_ARG(buffer);
572 GATE_UNUSED_ARG(bufferlength);
573 GATE_UNUSED_ARG(written);
574 return GATE_RESULT_NOTSUPPORTED;
575 }
576 static gate_result_t gate_http_response_flush_impl(void* self)
577 {
578 GATE_UNUSED_ARG(self);
579 return GATE_RESULT_NOTSUPPORTED;
580 }
581
582 static gate_bool_t gate_httpclient_add_header(gate_map_t* headers, LPCTSTR headerline, gate_size_t headerlinelength)
583 {
584 gate_bool_t ret = false;
585 char key[1024];
586 char value[4096];
587 gate_size_t keylength;
588 gate_size_t valuelength;
589 gate_string_t strkey = GATE_STRING_INIT_EMPTY;
590 gate_string_t strvalue = GATE_STRING_INIT_EMPTY;
591 gate_string_t strkey2 = GATE_STRING_INIT_EMPTY;
592 gate_string_t strvalue2 = GATE_STRING_INIT_EMPTY;
593 gate_size_t pos = gate_win32_winstr_char_pos(headerline, headerlinelength, ':', 0);
594
595 do
596 {
597 if (pos != GATE_STR_NPOS)
598 {
599 keylength = gate_win32_winstr_2_utf8(&headerline[0], pos, key, sizeof(key));
600 valuelength = gate_win32_winstr_2_utf8(&headerline[pos + 1], headerlinelength - pos - 1, value, sizeof(value));
601
602 if (NULL == gate_string_create(&strkey, key, keylength)) break;
603 if (NULL == gate_string_create(&strvalue, value, valuelength)) break;
604
605 gate_string_trim(&strkey2, &strkey);
606 gate_string_trim(&strvalue2, &strvalue);
607
608 ret = (NULL != gate_util_stringmap_add_string(headers, &strkey2, &strvalue2)) ? true : false;
609 }
610 } while (0);
611
612 gate_string_release(&strkey2);
613 gate_string_release(&strvalue2);
614
615 gate_string_release(&strkey);
616 gate_string_release(&strvalue);
617 return ret;
618 }
619
620 #ifndef INTERNET_OPTION_CLIENT_CERT_CONTEXT
621 #define INTERNET_OPTION_CLIENT_CERT_CONTEXT 84
622 #endif
623
624 static gate_result_t wininet_httpclient_send_request(gate_httpclient_t* client, gate_http_request_t* request, gate_http_response_t* response)
625 {
626 gate_result_t ret;
627 HINTERNET hconnect = (HINTERNET)client->handles[1];
628 HINTERNET hrequest = NULL;
629 gate_uint32_t flags = client->flags;
630
631 DWORD dwFlags = 0
632 | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS
633 | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_UI
634 | INTERNET_FLAG_RELOAD | INTERNET_FLAG_KEEP_CONNECTION
635 ;
636
637 DWORD dwReqFlags = 0;
638 DWORD dwReqFlagsSize = sizeof(dwReqFlags);
639 DWORD dwTimeOut;
640 DWORD dwHttpVersion = 0x02;
641 DWORD dwPathSize;
642 DWORD dwIndex;
643 DWORD dwStatusCode = 0;
644 DWORD dwStatusCodeSize = (DWORD)sizeof(dwStatusCode);
645 DWORD dwHeaderSize;
646 INTERNET_PROXY_INFO proxyinfo;
647
648 TCHAR user[128] = GATE_INIT_EMPTY;
649 TCHAR pass[128] = GATE_INIT_EMPTY;
650 TCHAR proxy[128] = GATE_INIT_EMPTY;
651 TCHAR proxyuser[128] = GATE_INIT_EMPTY;
652 TCHAR proxypass[128] = GATE_INIT_EMPTY;
653 TCHAR version[32] = GATE_INIT_EMPTY;
654 TCHAR verb[32] = GATE_INIT_EMPTY;
655 TCHAR path[2048] = GATE_INIT_EMPTY;
656 TCHAR referrer[512] = GATE_INIT_EMPTY;
657 TCHAR headers[4096] = GATE_INIT_EMPTY;
658 LPTSTR accepttypes[64] = GATE_INIT_EMPTY;
659 LPTSTR ptrresponseheaders = NULL;
660 LPTSTR ptrtstr = NULL;
661 LPTSTR ptrversion = NULL;
662 LPTSTR ptrrefer = NULL;
663
664 gate_size_t headerlength;
665 gate_strbuilder_t headerbuilder;
666 gate_map_iterator_t iterator;
667 gate_string_t str;
668 gate_memstream_t* memstream = NULL;
669 char const* uploadptr = NULL;
670 gate_size_t uploadsize = 0;
671 DWORD senderror;
672 gate_http_response_impl_t* responseimpl = NULL;
673 gate_string_t const* ptr_referrer = NULL;
674
675 do
676 {
677 ret = load_wininet_api();
678 GATE_BREAK_IF_FAILED_TRACE(ret, "load_wininet_api()");
679
680 gate_mem_clear(response, sizeof(gate_http_response_t));
681
682 gate_strbuilder_create(&headerbuilder, 0);
683
684 gate_win32_string_2_winstr(&request->auth_user, user, sizeof(user) / sizeof(user[0]));
685 gate_win32_string_2_winstr(&request->auth_pass, pass, sizeof(pass) / sizeof(pass[0]));
686 gate_win32_string_2_winstr(&request->proxy_server, proxy, sizeof(proxy) / sizeof(proxy[0]));
687 gate_win32_string_2_winstr(&request->proxy_user, proxyuser, sizeof(proxyuser) / sizeof(proxyuser[0]));
688 gate_win32_string_2_winstr(&request->proxy_pass, proxypass, sizeof(proxypass) / sizeof(proxypass[0]));
689 if (gate_win32_string_2_winstr(&request->version, version, sizeof(version) / sizeof(version[0])))
690 {
691 ptrversion = &version[0];
692 }
693 gate_win32_string_2_winstr(&request->method, verb, sizeof(verb) / sizeof(verb[0]));
694 gate_win32_string_2_winstr(&request->path, path, sizeof(path) / sizeof(path[0]));
695 ptr_referrer = gate_util_stringmap_get(&request->headers, GATE_HTTP_HEADER_REFERER);
696 if (ptr_referrer)
697 {
698 if (gate_win32_string_2_winstr(ptr_referrer, referrer, sizeof(referrer) / sizeof(referrer[0])))
699 {
700 ptrrefer = &referrer[0];
701 }
702 }
703
704 if (request->upload_stream != NULL)
705 {
706 ret = gate_util_buffer_load(&uploadptr, &uploadsize, request->upload_stream, &memstream);
707 GATE_BREAK_IF_FAILED_TRACE(ret, "gate_util_buffer_load() failed");
708 }
709
710 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_SECURE))
711 {
712 dwFlags |= INTERNET_FLAG_SECURE;
713 }
714
715 hrequest = WinInet.HTTPOpenRequest(hconnect, verb, path, ptrversion, ptrrefer, (LPCTSTR*)&accepttypes[0], dwFlags, (DWORD_PTR)client);
716 if ((NULL == hrequest) && (GetLastError() == ERROR_INVALID_PARAMETER))
717 {
718 /* Win95 / IE3 does not support some caching flags, try again without them */
719 dwFlags &= ~((DWORD)INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_UI);
720 hrequest = WinInet.HTTPOpenRequest(hconnect, verb, path, ptrversion, ptrrefer, (LPCTSTR*)&accepttypes[0], dwFlags, (DWORD_PTR)client);
721 }
722 if (NULL == hrequest)
723 {
724 GATE_DEBUG_TRACE_MSG_VALUE("HttpOpenRequest() failed", gate_platform_get_last_error());
725 gate_win32_print_lasterror(&ret, NULL, 0);
726 break;
727 }
728
729 WinInet.InetQueryOption(hrequest, INTERNET_OPTION_SECURITY_FLAGS, &dwReqFlags, &dwReqFlagsSize);
730 if (GATE_FLAG_ENABLED(flags, GATE_HTTPCLIENT_FLAG_DISABLE_VERIFICATION))
731 {
732 dwReqFlags |= (SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
733 | SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_REVOCATION
734 | SECURITY_FLAG_IGNORE_REDIRECT_TO_HTTP | SECURITY_FLAG_IGNORE_REDIRECT_TO_HTTPS
735 );
736 }
737 WinInet.InetSetOption(hrequest, INTERNET_OPTION_SECURITY_FLAGS, &dwReqFlags, sizeof(dwReqFlags));
738
739 {
740 /* in case of Windows 10+, enable HTTP 2 support: */
741 #ifndef INTERNET_OPTION_HTTP_PROTOCOL_USED
742 # define INTERNET_OPTION_HTTP_PROTOCOL_USED 149
743 #endif
744 WinInet.InetSetOption(hrequest, INTERNET_OPTION_HTTP_PROTOCOL_USED, &dwHttpVersion, sizeof(dwHttpVersion));
745 }
746
747 if (request->connect_timeout_ms != 0)
748 {
749 /* setup connect timeout in milliseconds */
750 dwTimeOut = (DWORD)request->connect_timeout_ms;
751 WinInet.InetSetOption(hrequest, INTERNET_OPTION_CONNECT_TIMEOUT, &dwTimeOut, sizeof(dwTimeOut));
752 }
753 if (request->send_timeout_ms != 0)
754 {
755 /* setup send timeout in milliseconds */
756 dwTimeOut = (DWORD)request->send_timeout_ms;
757 WinInet.InetSetOption(hrequest, INTERNET_OPTION_SEND_TIMEOUT, &dwTimeOut, sizeof(dwTimeOut));
758 }
759 if (request->receive_timeout_ms != 0)
760 {
761 /* setup receive timeout in milliseconds */
762 dwTimeOut = (DWORD)request->receive_timeout_ms;
763 WinInet.InetSetOption(hrequest, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeOut, sizeof(dwTimeOut));
764 }
765
766 if ((user[0] != 0) || (pass[0] != 0))
767 {
768 /* setup user/password authentication */
769 WinInet.InetSetOption(hrequest, INTERNET_OPTION_USERNAME, (user[0] == 0) ? NULL : &user[0], (DWORD)gate_win32_winstr_length(user));
770 WinInet.InetSetOption(hrequest, INTERNET_OPTION_PASSWORD, (pass[0] == 0) ? NULL : &pass[0], (DWORD)gate_win32_winstr_length(pass));
771 }
772
773 if (proxy[0] != 0)
774 {
775 gate_mem_clear(&proxyinfo, sizeof(proxyinfo));
776 proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
777 proxyinfo.lpszProxy = proxy;
778 WinInet.InetSetOption(hrequest, INTERNET_OPTION_PROXY, &proxyinfo, sizeof(proxyinfo));
779 }
780
781 if ((proxyuser[0] != 0) || (proxypass[0] != 0))
782 {
783 WinInet.InetSetOption(hrequest, INTERNET_OPTION_PROXY_USERNAME, (proxyuser[0] == 0) ? NULL : &proxyuser[0], (DWORD)gate_win32_winstr_length(proxyuser));
784 WinInet.InetSetOption(hrequest, INTERNET_OPTION_PROXY_PASSWORD, (proxypass[0] == 0) ? NULL : &proxypass[0], (DWORD)gate_win32_winstr_length(proxypass));
785 }
786
787 iterator = gate_map_first(&request->headers);
788 while (gate_map_iterator_valid(iterator))
789 {
790 str = GATE_MAP_ITER_KEY(gate_string_t, iterator);
791 gate_strbuilder_append_text(&headerbuilder, str.str, str.length);
792 gate_strbuilder_append_text(&headerbuilder, ": ", 2);
793 str = GATE_MAP_ITER_VALUE(gate_string_t, iterator);
794 gate_strbuilder_append_text(&headerbuilder, str.str, str.length);
795 gate_strbuilder_append_text(&headerbuilder, "\r\n", 2);
796
797 iterator = gate_map_iterator_next(iterator);
798 }
799
800 headerlength = gate_win32_utf8_2_winstr(
801 gate_strbuilder_ptr(&headerbuilder, 0), gate_strbuilder_length(&headerbuilder),
802 headers, sizeof(headers) / sizeof(headers[0]));
803 if (FALSE == WinInet.HTTPSendRequest(hrequest, headers, (DWORD)headerlength, (LPVOID)uploadptr, (DWORD)uploadsize))
804 {
805 senderror = gate_win32_getlasterror();
806
807 #ifndef ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION
808 #define ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION (INTERNET_ERROR_BASE + 168)
809 #endif
810
811 if (ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION == senderror)
812 {
813 /* HTTP redirection was issued -> confirm automatically and retry */
814 WinInet.InetErrorDlg(NULL, hrequest, senderror, FLAGS_ERROR_UI_FLAGS_NO_UI, NULL);
815
816 dwPathSize = sizeof(path);
817 dwIndex = 0;
818 WinInet.HTTPQueryInfo(hrequest, HTTP_QUERY_LOCATION, path, &dwPathSize, &dwIndex);
819 }
820 else if (ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED == senderror)
821 {
822 /* If optional client certificate was requested, try to continue without certificate */
823 WinInet.InetErrorDlg(NULL, hrequest, senderror, FLAGS_ERROR_UI_FLAGS_NO_UI, NULL);
824
825 WinInet.InetSetOption(hrequest, INTERNET_OPTION_CLIENT_CERT_CONTEXT, NULL, 0);
826 }
827 else
828 {
829 /* any other error will cancel the operation */
830 GATE_DEBUG_TRACE("HttpSendRequest() failed");
831 ret = GATE_RESULT_FAILED;
832 break;
833 }
834
835 /* second attempt to access http service */
836 if (FALSE == WinInet.HTTPSendRequest(hrequest, headers, (DWORD)headerlength, (LPVOID)uploadptr, (DWORD)uploadsize))
837 {
838 GATE_DEBUG_TRACE("HttpSendRequest() failed");
839 ret = GATE_RESULT_FAILED;
840 break;
841 }
842 }
843
844 /* HTTP request succeeded -> receive HTTP response */
845
846 if (FALSE == WinInet.HTTPQueryInfo(hrequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatusCode, &dwStatusCodeSize, 0))
847 {
848 /* no status code, maybe socket was closed ... */
849 GATE_DEBUG_TRACE("HttpQueryInfo() failed");
850 ret = GATE_RESULT_INVALIDINPUT;
851 break;
852 }
853
854 response->status_code = (gate_uint32_t)dwStatusCode;
855
856 dwHeaderSize = 0;
857 if (FALSE == WinInet.HTTPQueryInfo(hrequest, HTTP_QUERY_RAW_HEADERS, NULL, &dwHeaderSize, NULL))
858 {
859 senderror = gate_win32_getlasterror();
860 if (ERROR_INSUFFICIENT_BUFFER != senderror)
861 {
862 /* cannot receive header size */
863 GATE_DEBUG_TRACE("HttpQueryInfo() failed");
864 ret = GATE_RESULT_INVALIDHEADER;
865 break;
866 }
867 }
868
869 if (dwHeaderSize != 0)
870 {
871 /* attach response headers to response object */
872 if (NULL == gate_util_stringmap_create(&response->headers))
873 {
874 ret = GATE_RESULT_OUTOFMEMORY;
875 break;
876 }
877
878 dwHeaderSize += 32; /* add some extra space */
879 ptrresponseheaders = (LPTSTR)gate_mem_alloc(dwHeaderSize * sizeof(TCHAR));
880 if (NULL == ptrresponseheaders)
881 {
882 ret = GATE_RESULT_OUTOFMEMORY;
883 break;
884 }
885
886 if (FALSE == WinInet.HTTPQueryInfo(hrequest, HTTP_QUERY_RAW_HEADERS, ptrresponseheaders, &dwHeaderSize, NULL))
887 {
888 GATE_DEBUG_TRACE("HttpQueryInfo() failed");
889 ret = GATE_RESULT_INVALIDHEADER;
890 break;
891 }
892
893 ptrtstr = ptrresponseheaders;
894
895 while (0 != (headerlength = gate_win32_winstr_length(ptrtstr)))
896 {
897 gate_httpclient_add_header(&response->headers, ptrtstr, headerlength);
898
899 ptrtstr += (headerlength + 1);
900 }
901 }
902
903 /* generate response stream to access downloaded data */
904
905 responseimpl = (gate_http_response_impl_t*)gate_mem_alloc(sizeof(gate_http_response_impl_t));
906 if (responseimpl == NULL)
907 {
908 ret = GATE_RESULT_OUTOFMEMORY;
909 break;
910 }
911 gate_init_http_client_response_vtbl();
912 responseimpl->vtbl = &gate_http_client_response_vtbl;
913 gate_atomic_int_init(&responseimpl->ref_counter, 1);
914 responseimpl->handle = hrequest;
915 hrequest = NULL;
916
917 /* success case */
918 response->response_stream = (gate_stream_t*)responseimpl;
919 ret = GATE_RESULT_OK;
920 } while (0);
921
922 gate_strbuilder_release(&headerbuilder);
923
924 if (hrequest != NULL)
925 {
926 WinInet.InetCloseHandle(hrequest);
927 }
928
929 if (memstream != NULL)
930 {
931 memstream->vtbl->release(memstream);
932 }
933
934 if (ptrresponseheaders != NULL)
935 {
936 gate_mem_dealloc(ptrresponseheaders);
937 }
938
939 if (GATE_FAILED(ret))
940 {
941 gate_http_response_release(response);
942 }
943
944 return ret;
945 }
946
947
948 gate_result_t gate_httpclient_create(gate_httpclient_t* client, gate_string_t const* server, gate_uint16_t port, gate_enumint_t flags)
949 {
950 if (GATE_FLAG_ENABLED(flags, GATE_HTTPCLIENT_FLAG_NATIVE_IMPL))
951 {
952 return native_httpclient_create(client, server, port, flags);
953 }
954 else
955 {
956 return wininet_httpclient_create(client, server, port, flags);
957 }
958 }
959
960 gate_result_t gate_httpclient_send_request(gate_httpclient_t* client, gate_http_request_t* request, gate_http_response_t* response)
961 {
962 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_NATIVE_IMPL))
963 {
964 return native_httpclient_send_request(client, request, response);
965 }
966 else
967 {
968 return wininet_httpclient_send_request(client, request, response);
969 }
970 }
971
972 gate_result_t gate_httpclient_release(gate_httpclient_t* client)
973 {
974 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_NATIVE_IMPL))
975 {
976 return native_httpclient_release(client);
977 }
978 else
979 {
980 return wininet_httpclient_release(client);
981 }
982 }
983
984
985
986 #endif /* GATE_NET_HTTPCLIENT_WINAPI */
987
988
989
990 #if defined(GATE_NET_HTTPCLIENT_CURL)
991
992 #include "gate/net/platform/libcurl_api.h"
993 #include "gate/uris.h"
994 #include "gate/utilities.h"
995 #include "gate/debugging.h"
996
997
998 19 static gate_result_t curl_httpclient_create(gate_httpclient_t* client, gate_string_t const* server, gate_uint16_t port, gate_enumint_t flags)
999 {
1000 19 gate_result_t ret = GATE_RESULT_FAILED;
1001
1002 do
1003 {
1004 19 gate_mem_clear(client, sizeof(gate_httpclient_t));
1005
1006 19 ret = load_libcurl_functions();
1007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 GATE_BREAK_IF_FAILED(ret);
1008
1009
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (NULL == gate_string_clone(&client->server, server))
1010 {
1011 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1012 ✗ break;
1013 }
1014
1015 19 client->flags = flags;
1016 19 client->port = port;
1017
1018 19 ret = GATE_RESULT_OK;
1019 } while (0);
1020
1021 19 return ret;
1022 }
1023
1024
1025 23 gate_result_t gate_httpclient_create(gate_httpclient_t* client, gate_string_t const* server, gate_uint16_t port, gate_enumint_t flags)
1026 {
1027
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 19 times.
23 if (GATE_FLAG_ENABLED(flags, GATE_HTTPCLIENT_FLAG_NATIVE_IMPL))
1028 {
1029 4 return native_httpclient_create(client, server, port, flags);
1030 }
1031 else
1032 {
1033 19 return curl_httpclient_create(client, server, port, flags);
1034 }
1035 }
1036
1037 ✗ static gate_result_t setup_multipart_formdata(CURL* curl_session, gate_http_request_t* request)
1038 {
1039 ✗ gate_result_t ret = GATE_RESULT_FAILED;
1040
1041 /*
1042 // multipart form data
1043 size_t boundarypos = contentType.indexOf("boundary=");
1044 if(boundarypos == StrToken::npos)
1045 {
1046 throw Exception(results::InvalidData, "Missing multipart form data boundary", FuncName);
1047 }
1048 StrToken boundary = contentType.subString(boundarypos + 9);
1049
1050
1051 parts = Http::parseMultipartMessages(strm.str(), boundary);
1052 for(partlist_t::iterator p(parts.begin()), e(parts.end()); p != e; ++p)
1053 {
1054 if(!p->Name.empty())
1055 {
1056 char const* ctype = Http::ContentType_Binary;
1057 if(!p->Type.empty())
1058 {
1059 ctype = p->Type.c_str();
1060 }
1061
1062 if(p->FileName.empty())
1063 {
1064 curl::Code.formadd(&formpost, &formlast,
1065 CURLFORM_COPYNAME, p->Name.c_str(),
1066 CURLFORM_PTRCONTENTS, p->Message.c_str(),
1067 CURLFORM_CONTENTSLENGTH, (long)p->Message.size(),
1068 CURLFORM_CONTENTTYPE, ctype,
1069 CURLFORM_END
1070 );
1071 }
1072 else
1073 {
1074 curl::Code.formadd(&formpost, &formlast,
1075 CURLFORM_COPYNAME, p->Name.c_str(),
1076 CURLFORM_BUFFERPTR, p->Message.c_str(),
1077 CURLFORM_BUFFERLENGTH, (long)p->Message.size(),
1078 CURLFORM_BUFFER, p->FileName.c_str(),
1079 CURLFORM_CONTENTTYPE, ctype,
1080 CURLFORM_END
1081 );
1082 }
1083 }
1084 }
1085
1086 result = curl::Code.easy_setopt(pcurl, CURLOPT_HTTPPOST, formpost);
1087 curl::CurlLib::check_error(result, "curl_easy_setopt(CURLOPT_HTTPPOST)", FuncName);
1088
1089 */
1090
1091 ✗ return ret;
1092 }
1093
1094 #define GATE_CURL_CHECK_ERROR(curl_code, origin) { ret = gate_curl_map_result(curl_code, &current_error_msg); GATE_DEBUG_TRACE_RESULT(ret, origin, current_error_msg, 0); GATE_BREAK_IF_FAILED(ret); }
1095
1096 1 static gate_result_t extract_stream_data(CURL* curl_session, gate_stream_t* source_stream, gate_stream_t** ptr_upload_data_stream, gate_bool_t put_upload)
1097 {
1098 1 gate_result_t ret = GATE_RESULT_FAILED;
1099 1 gate_memstream_t* source_memstream = NULL;
1100 CURLcode curl_code;
1101 1 char const* current_error_msg = NULL;
1102 1 char const* memstream_data = NULL;
1103 1 gate_size_t memstream_data_size = 0;
1104
1105 do
1106 {
1107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(put_upload)
1108 {
1109 /* PUT */
1110 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_UPLOAD, (long)1);
1111 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[UPLOAD]");
1112
1113 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_READDATA, (void*)source_stream);
1114 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[READDATA]");
1115
1116 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_READFUNCTION, &gate_curl_read_stream_cb);
1117 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[READFUNCTION]");
1118 }
1119 else
1120 {
1121 /* POST */
1122
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (GATE_OBJECT_IMPLEMENTS(source_stream, GATE_INTERFACE_NAME_MEMSTREAM))
1123 {
1124 ✗ source_memstream = (gate_memstream_t*)source_stream;
1125 }
1126 else
1127 {
1128 1 source_memstream = gate_memstream_create(GATE_MAX_BLOCK_COPYBUFFER_LENGTH);
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (NULL == source_memstream)
1130 {
1131 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1132 ✗ break;
1133 }
1134 1 ret = gate_stream_transfer(source_stream, (gate_stream_t*)source_memstream);
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
1136 {
1137 ✗ gate_object_release(source_memstream);
1138 ✗ break;
1139 }
1140 1 *ptr_upload_data_stream = (gate_stream_t*)source_memstream;
1141 }
1142 1 memstream_data = gate_memstream_get_data(source_memstream);
1143 1 memstream_data_size = gate_memstream_size(source_memstream);
1144
1145 1 curl_code = curl.easy_setopt(curl_session, CURLOPT_POST, (long)1);
1146
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[POST]");
1147
1148 1 curl_code = curl.easy_setopt(curl_session, CURLOPT_POSTFIELDS, memstream_data);
1149
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[POSTFIELDS]");
1150
1151 1 curl_code = curl.easy_setopt(curl_session, CURLOPT_POSTFIELDSIZE, (long)memstream_data_size);
1152
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[POSTFIELDSIZE]");
1153 }
1154
1155 } while (0);
1156
1157 1 return ret;
1158 }
1159
1160 19 static gate_result_t parse_response_header(char const* data, gate_size_t data_len, gate_map_t* m, gate_uint32_t* status)
1161 {
1162 static gate_string_t const http_key = GATE_STRING_INIT_STATIC("HTTP");
1163 static gate_string_t const status_key = GATE_STRING_INIT_STATIC("Status");
1164 static gate_string_t const http_prefix = GATE_STRING_INIT_STATIC("HTTP/");
1165 19 gate_result_t ret = GATE_RESULT_OK;
1166 19 gate_string_t header = GATE_STRING_INIT_EMPTY;
1167 gate_string_t line;
1168 gate_string_t key;
1169 gate_string_t value;
1170 gate_size_t pos;
1171 19 gate_uint64_t ui64 = 0;
1172 do
1173 {
1174 19 gate_mem_clear(m, sizeof(gate_map_t));
1175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (NULL == gate_map_create(m, &gate_compare_string,
1176 sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor,
1177 sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor))
1178 {
1179 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1180 ✗ break;
1181 }
1182
1183
1184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (NULL == gate_string_create(&header, data, data_len))
1185 {
1186 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1187 ✗ break;
1188 }
1189
1190
2/2
✓ Branch 1 taken 224 times.
✓ Branch 2 taken 19 times.
243 while (gate_string_read_line(&line, &header, &header))
1191 {
1192 224 gate_mem_clear(&key, sizeof(key));
1193 224 gate_mem_clear(&value, sizeof(value));
1194
1195
1196
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 205 times.
224 if (gate_string_starts_with(&line, &http_prefix))
1197 {
1198 19 gate_map_add(m, &http_key, &line);
1199 19 pos = gate_string_char_pos(&line, ' ', gate_string_length(&http_key));
1200
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (GATE_STR_NPOS != pos)
1201 {
1202 19 gate_string_substr(&value, &line, pos + 1, GATE_STR_NPOS);
1203 19 gate_string_ltrim(&value, &value);
1204 19 gate_map_add(m, &status_key, &value);
1205
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (status != NULL)
1206 {
1207 19 gate_str_parse_uint64(value.str, value.length, &ui64);
1208 19 *status = (gate_uint32_t)ui64;
1209 }
1210 }
1211 }
1212 else
1213 {
1214 205 pos = gate_string_char_pos(&line, ':', 0);
1215
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 19 times.
205 if (GATE_STR_NPOS != pos)
1216 {
1217 186 gate_string_substr(&key, &line, 0, pos);
1218 186 gate_string_substr(&value, &line, pos + 1, GATE_STR_NPOS);
1219 186 gate_string_trim(&key, &key);
1220 186 gate_string_trim(&value, &value);
1221 186 gate_map_add(m, &key, &value);
1222 }
1223 }
1224 224 gate_string_release(&key);
1225 224 gate_string_release(&value);
1226 224 gate_string_release(&line);
1227 }
1228
1229 } while (0);
1230
1231 19 gate_string_release(&header);
1232
1233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (GATE_FAILED(ret))
1234 {
1235 ✗ gate_map_destroy(m);
1236 }
1237
1238 19 return ret;
1239 }
1240
1241 static gate_string_t const gate_uri_scheme_http = GATE_STRING_INIT_STATIC(GATE_URI_SCHEME_HTTP);
1242 static gate_string_t const gate_uri_scheme_https = GATE_STRING_INIT_STATIC(GATE_URI_SCHEME_HTTPS);
1243
1244
1245
1246 19 static gate_result_t curl_httpclient_send_request(gate_httpclient_t* client, gate_http_request_t* request, gate_http_response_t* response)
1247 {
1248 static gate_string_t const header_entry_separator = GATE_STRING_INIT_STATIC(": ");
1249 static gate_string_t const header_entry_content_type = GATE_STRING_INIT_STATIC(GATE_HTTP_HEADER_CONTENTTYPE);
1250 19 gate_result_t ret = GATE_RESULT_FAILED;
1251 19 CURL* curl_session = NULL;
1252 19 gate_uri_t url = GATE_INIT_EMPTY;
1253 19 gate_string_t str_url = GATE_STRING_INIT_EMPTY;
1254 CURLcode curl_code;
1255 19 char const* current_error_msg = NULL;
1256 char buffer[4096];
1257 gate_size_t buffer_len;
1258 19 gate_bool_t is_secure = GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_SECURE);
1259 gate_bool_t is_get_request;
1260 gate_bool_t is_post_request;
1261 gate_bool_t is_put_request;
1262 19 struct curl_slist* curl_headers = NULL;
1263 gate_map_iterator_t iter;
1264 gate_string_t const* key;
1265 gate_string_t const* value;
1266 char username[256];
1267 char password[256];
1268
1269 19 gate_stream_t* local_upload_stream = NULL;
1270 19 gate_memstream_t* response_header = NULL;
1271 19 gate_memstream_t* response_content = NULL;
1272
1273 do
1274 {
1275 19 gate_uri_init(&url);
1276 19 gate_http_response_init(response);
1277
1278 19 ret = load_libcurl_functions();
1279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 GATE_BREAK_IF_FAILED(ret);
1280
1281 19 curl_session = curl.easy_init();
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (NULL == curl_session)
1283 {
1284 ✗ ret = GATE_RESULT_NOTAVAILABLE;
1285 ✗ GATE_DEBUG_TRACE_RESULT(ret, "curl_easy_init", "Faile to get CURL context", 0);
1286 ✗ break;
1287 }
1288
1289
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
19 if (client->port == 0)
1290 {
1291
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 url.port = is_secure ? 443 : 80;
1292 }
1293 else
1294 {
1295 5 url.port = client->port;
1296 }
1297
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
19 gate_string_duplicate(&url.scheme, is_secure ? &gate_uri_scheme_https : &gate_uri_scheme_http);
1298
1299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (!gate_string_is_empty(&request->version))
1300 {
1301 ✗ long http_version = CURL_HTTP_VERSION_NONE;
1302 ✗ if (gate_string_equals_str(&request->version, "1.0")) { http_version = CURL_HTTP_VERSION_1_0; }
1303 ✗ else if (gate_string_equals_str(&request->version, "1.1")) { http_version = CURL_HTTP_VERSION_1_1; }
1304 ✗ else if (gate_string_equals_str(&request->version, "2.0")) { http_version = CURL_HTTP_VERSION_2_0; }
1305 ✗ else if (gate_string_equals_str(&request->version, "3.0")) { http_version = CURL_HTTP_VERSION_3; }
1306
1307 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_HTTP_VERSION, http_version);
1308 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[HTTP_VERSION]");
1309 }
1310
1311 {
1312 19 long followlocation = 1;
1313 19 long max_redirects = 8;
1314
1315 19 curl_code = curl.easy_setopt(curl_session, CURLOPT_FOLLOWLOCATION, followlocation);
1316
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[FOLLOWLOCATION]");
1317
1318 19 curl_code = curl.easy_setopt(curl_session, CURLOPT_MAXREDIRS, max_redirects);
1319
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[MAXREDIRS]");
1320 }
1321
1322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (!gate_string_is_empty(&request->auth_user))
1323 {
1324 ✗ struct curl_version_info_data* version = curl.version_info(CURLVERSION_NOW);
1325 ✗ unsigned long auth_bitmask = CURLAUTH_BASIC | CURLAUTH_DIGEST;
1326 /* NOTICE: if we request auth-types not included by libcurl,
1327 we end with useless 401-responses */
1328 ✗ if(version->features & CURL_VERSION_SSPI) auth_bitmask |= CURLAUTH_DIGEST | CURLAUTH_NEGOTIATE | CURLAUTH_NTLM;
1329 ✗ if(version->features & CURL_VERSION_NTLM) auth_bitmask |= CURLAUTH_NTLM;
1330 ✗ if(version->features & CURL_VERSION_GSSNEGOTIATE) auth_bitmask |= CURLAUTH_NEGOTIATE;
1331
1332 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_HTTPAUTH, auth_bitmask);
1333 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[HTTPAUTH]");
1334 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_UNRESTRICTED_AUTH, 1L);
1335 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[UNRESTRICTED_AUTH]");
1336
1337 #if 1
1338 ✗ gate_string_to_buffer(&request->auth_user, username, sizeof(username));
1339 ✗ gate_string_to_buffer(&request->auth_pass, password, sizeof(password));
1340 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_USERNAME, username);
1341 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[USERNAME]");
1342 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_PASSWORD, password);
1343 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[PASSWORD]");
1344 #else
1345 ret = gate_uri_build_user_info(&request->auth_user, &request->auth_pass, &url.user_info);
1346 GATE_BREAK_IF_FAILED(ret);
1347 #endif
1348 }
1349
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (NULL == gate_string_clone(&url.host, &client->server))
1350 {
1351 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1352 ✗ break;
1353 }
1354
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (NULL == gate_string_clone(&url.absolute_path, &request->path))
1355 {
1356 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1357 ✗ break;
1358 }
1359
1360 19 ret = gate_uri_to_string(&url, &str_url, false);
1361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 GATE_BREAK_IF_FAILED(ret);
1362
1363 19 curl_code = curl.easy_setopt(curl_session, CURLOPT_URL, str_url.str);
1364
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[URL]");
1365
1366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (!gate_string_is_empty(&request->proxy_server))
1367 {
1368 ✗ GATE_STRING_TO_BUFFER(&request->proxy_server, buffer);
1369 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_PROXY, buffer);
1370 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[PROXY]");
1371
1372 ✗ if (request->proxy_port)
1373 {
1374 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_PROXYPORT, (long)request->proxy_port);
1375 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[PROXYPORT]");
1376 }
1377
1378 ✗ if (!gate_string_is_empty(&request->proxy_user))
1379 {
1380 ✗ GATE_STRING_TO_BUFFER(&request->proxy_user, buffer);
1381 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_PROXYUSERNAME, buffer);
1382 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[PROXYUSERNAME]");
1383 }
1384 ✗ if (!gate_string_is_empty(&request->proxy_pass))
1385 {
1386 ✗ GATE_STRING_TO_BUFFER(&request->proxy_pass, buffer);
1387 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_PROXYPORT, buffer);
1388 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[PROXYPORT]");
1389 }
1390 }
1391
1392
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
19 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_DISABLE_VERIFICATION))
1393 {
1394 14 curl_code = curl.easy_setopt(curl_session, CURLOPT_SSL_VERIFYHOST, 0L);
1395
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
14 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[SSL_VERIFYHOST]");
1396
1397 14 curl_code = curl.easy_setopt(curl_session, CURLOPT_SSL_VERIFYPEER, 0L);
1398
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
14 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[SSL_VERIFYPEER]");
1399 }
1400
1401
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
19 if (request->connect_timeout_ms)
1402 {
1403 2 curl_code = curl.easy_setopt(curl_session, CURLOPT_CONNECTTIMEOUT_MS, (long)request->connect_timeout_ms);
1404
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[CONNECTTIMEOUT_MS]");
1405 }
1406
1407
4/6
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 17 times.
19 if (request->connect_timeout_ms || request->send_timeout_ms || request->receive_timeout_ms)
1408 {
1409 4 curl_code = curl.easy_setopt(curl_session, CURLOPT_TIMEOUT_MS,
1410 2 (long)(request->connect_timeout_ms + request->send_timeout_ms + request->receive_timeout_ms));
1411
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[TIMEOUT_MS]");
1412 }
1413
1414
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
19 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_SECURE))
1415 {
1416 14 curl_code = curl.easy_setopt(curl_session, CURLOPT_REDIR_PROTOCOLS, (long)CURLPROTO_HTTPS);
1417
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
14 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[REDIR_PROTOCOLS]");
1418 }
1419 else
1420 {
1421 5 curl_code = curl.easy_setopt(curl_session, CURLOPT_REDIR_PROTOCOLS, (long)CURLPROTO_HTTP);
1422
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
5 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[REDIR_PROTOCOLS]");
1423 }
1424
1425 19 is_get_request = gate_string_equals_str(&request->method, GATE_HTTP_METHOD_GET);
1426 19 is_post_request = gate_string_equals_str(&request->method, GATE_HTTP_METHOD_POST);
1427 19 is_put_request = gate_string_equals_str(&request->method, GATE_HTTP_METHOD_PUT);
1428
1429
2/2
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 19 times.
24 for (iter = gate_map_first(&request->headers); gate_map_iterator_valid(iter); iter = gate_map_iterator_next(iter))
1430 {
1431 5 key = (gate_string_t const*)gate_map_iterator_key(iter);
1432 5 value = (gate_string_t const*)gate_map_iterator_value(iter);
1433
1434
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
5 if (is_post_request || is_put_request)
1435 {
1436 /* exclude non-supported custom fields in POST mode */
1437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (gate_string_equals_str(key, GATE_HTTP_HEADER_CONTENTLENGTH)) continue;
1438 }
1439 5 buffer_len = gate_string_to_buffer(key, buffer, sizeof(buffer));
1440 5 buffer_len += gate_string_to_buffer(&header_entry_separator, &buffer[buffer_len], sizeof(buffer) - buffer_len);
1441 5 buffer_len += gate_string_to_buffer(value, &buffer[buffer_len], sizeof(buffer) - buffer_len);
1442
1443 5 curl_headers = curl.slist_append(curl_headers, buffer);
1444 }
1445
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 if (curl_headers != NULL)
1446 {
1447 5 curl_code = curl.easy_setopt(curl_session, CURLOPT_HTTPHEADER, curl_headers);
1448
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
5 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[HTTPHEADER]");
1449 }
1450
1451
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 if (request->upload_stream == NULL)
1452 {
1453
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
18 if (!is_get_request && !is_post_request && !is_put_request)
1454 {
1455 ✗ gate_string_to_buffer(&request->method, buffer, sizeof(buffer));
1456 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_CUSTOMREQUEST, buffer);
1457 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[CUSTOMREQUEST]");
1458 }
1459 }
1460 else
1461 {
1462 1 value = (gate_string_t const*)gate_map_get_value(&request->headers, &header_entry_content_type);
1463
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (is_post_request)
1464 {
1465
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 if (value && gate_string_starts_with_str(value, GATE_HTTP_HEADERVALUE_CONTENTTYPE_MULTIPARTFORMDATA))
1466 {
1467 ✗ ret = setup_multipart_formdata(curl_session, request);
1468 ✗ GATE_BREAK_IF_FAILED(ret);
1469 }
1470 else
1471 {
1472 1 ret = extract_stream_data(curl_session, request->upload_stream, &local_upload_stream, false);
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
1474 }
1475 }
1476 else
1477 {
1478 ✗ ret = extract_stream_data(curl_session, request->upload_stream, &local_upload_stream, is_put_request);
1479 ✗ GATE_BREAK_IF_FAILED(ret);
1480
1481 ✗ gate_string_to_buffer(&request->method, buffer, sizeof(buffer));
1482 ✗ curl_code = curl.easy_setopt(curl_session, CURLOPT_CUSTOMREQUEST, buffer);
1483 ✗ GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[CUSTOMREQUEST]");
1484 }
1485 }
1486
1487
1488
1489 19 response_header = gate_memstream_create(GATE_MAX_COPYBUFFER_LENGTH);
1490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (NULL == response_header)
1491 {
1492 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1493 ✗ break;
1494 }
1495 19 response_content = gate_memstream_create(GATE_MAX_BLOCK_COPYBUFFER_LENGTH);
1496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (NULL == response_content)
1497 {
1498 ✗ ret = GATE_RESULT_OUTOFMEMORY;
1499 ✗ break;
1500 }
1501
1502 19 curl_code = curl.easy_setopt(curl_session, CURLOPT_WRITEDATA, response_content);
1503
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[WRITEDATA]");
1504
1505 19 curl_code = curl.easy_setopt(curl_session, CURLOPT_WRITEFUNCTION, &gate_curl_write_stream_cb);
1506
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[WRITEFUNCTION]");
1507
1508 19 curl_code = curl.easy_setopt(curl_session, CURLOPT_WRITEHEADER, response_header);
1509
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[WRITEHEADER]");
1510
1511 19 curl_code = curl.easy_setopt(curl_session, CURLOPT_HEADERFUNCTION, gate_curl_write_stream_cb);
1512
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_setopt[HEADERFUNCTION]");
1513
1514 19 curl_code = curl.easy_perform(curl_session);
1515
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 GATE_CURL_CHECK_ERROR(curl_code, "curl.easy_perform");
1516
1517 /* HTTP transmission completed -> fill response object */
1518 19 ret = parse_response_header(gate_memstream_get_data(response_header),
1519 19 gate_memstream_size(response_header),
1520 &response->headers, &response->status_code);
1521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 GATE_BREAK_IF_FAILED(ret);
1522
1523 19 response->response_stream = (gate_stream_t*)response_content;
1524 19 response_content = NULL;
1525
1526 19 ret = GATE_RESULT_OK;
1527
1528 } while (0);
1529
1530
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (curl_session)
1531 {
1532 19 curl.easy_cleanup(curl_session);
1533 19 curl_session = NULL;
1534 }
1535
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 if (curl_headers != NULL)
1536 {
1537 5 curl.slist_free_all(curl_headers);
1538 }
1539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (response_content != NULL)
1540 {
1541 ✗ gate_object_release(response_content);
1542 }
1543
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (response_header != NULL)
1544 {
1545 19 gate_object_release(response_header);
1546 }
1547
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
19 if (local_upload_stream != NULL)
1548 {
1549 1 gate_object_release(local_upload_stream);
1550 }
1551
1552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (GATE_FAILED(ret))
1553 {
1554 ✗ gate_http_response_release(response);
1555 }
1556
1557 19 gate_string_release(&str_url);
1558 19 gate_uri_destroy(&url);
1559 19 return ret;
1560 }
1561
1562 23 gate_result_t gate_httpclient_send_request(gate_httpclient_t* client, gate_http_request_t* request, gate_http_response_t* response)
1563 {
1564
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 19 times.
23 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_NATIVE_IMPL))
1565 {
1566 4 return native_httpclient_send_request(client, request, response);
1567 }
1568 else
1569 {
1570 19 return curl_httpclient_send_request(client, request, response);
1571 }
1572 }
1573
1574
1575 19 static gate_result_t curl_httpclient_release(gate_httpclient_t* client)
1576 {
1577 19 gate_string_release(&client->server);
1578 19 gate_mem_clear(client, sizeof(gate_httpclient_t));
1579 19 return GATE_RESULT_OK;
1580 }
1581
1582 23 gate_result_t gate_httpclient_release(gate_httpclient_t* client)
1583 {
1584
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 19 times.
23 if (GATE_FLAG_ENABLED(client->flags, GATE_HTTPCLIENT_FLAG_NATIVE_IMPL))
1585 {
1586 4 return native_httpclient_release(client);
1587 }
1588 else
1589 {
1590 19 return curl_httpclient_release(client);
1591 }
1592 }
1593
1594 #endif /* GATE_NET_HTTPCLIENT_CURL */
1595
1596
1597 #if defined(GATE_NET_HTTPCLIENT_WASM_IMPL)
1598
1599 #include <emscripten/fetch.h>
1600 #include <emscripten/wasm_worker.h>
1601
1602 gate_result_t gate_httpclient_create(gate_httpclient_t* client, gate_string_t const* server, gate_uint16_t port, gate_enumint_t flags)
1603 {
1604 gate_mem_clear(client, sizeof(gate_httpclient_t));
1605
1606 if (NULL == gate_string_clone(&client->server, server))
1607 {
1608 return GATE_RESULT_OUTOFMEMORY;
1609 }
1610 client->port = port;
1611 client->flags = flags;
1612 return GATE_RESULT_OK;
1613 }
1614
1615 static void httpclient_on_success(emscripten_fetch_t* fetch)
1616 {
1617 emscripten_semaphore_t* ptr_semaphore = (emscripten_semaphore_t*)fetch->userData;
1618 emscripten_semaphore_release(ptr_semaphore, 1);
1619 }
1620
1621 static void httpclient_on_error(emscripten_fetch_t* fetch)
1622 {
1623 emscripten_semaphore_t* ptr_semaphore = (emscripten_semaphore_t*)fetch->userData;
1624 emscripten_semaphore_release(ptr_semaphore, 1);
1625 }
1626
1627 #define HTTPCLIENT_MAX_HEADERS 32
1628 gate_result_t gate_httpclient_send_request(gate_httpclient_t* client, gate_http_request_t* request, gate_http_response_t* response)
1629 {
1630 gate_result_t ret = GATE_RESULT_FAILED;
1631 emscripten_fetch_t* ptr_fetch = NULL;
1632 gate_string_t header_strings[HTTPCLIENT_MAX_HEADERS * 2] = GATE_INIT_EMPTY;
1633 gate_size_t header_strings_count = 0;
1634 char const* headers[HTTPCLIENT_MAX_HEADERS * 2 + 1];
1635 gate_enumerator_t e = GATE_INIT_EMPTY;
1636
1637 gate_map_enumerate(&request->headers, &e);
1638 while (gate_enumerator_valid(&e) && (header_strings_count < HTTPCLIENT_MAX_HEADERS * 2))
1639 {
1640 gate_mapping_t const* ptr_map_entry = (gate_mapping_t const*)gate_enumerator_get(&e);
1641 gate_string_t const* ptr_str = (gate_string_t const*)ptr_map_entry->key;
1642 if (ptr_str->length > 0)
1643 {
1644 if (ptr_str->str[ptr_str->length] == 0)
1645 {
1646 gate_string_clone(&header_strings[header_strings_count], ptr_str);
1647 }
1648 else
1649 {
1650 gate_string_create_copy(&header_strings[header_strings_count], ptr_str);
1651 }
1652 }
1653 headers[header_strings_count] = gate_string_ptr(&header_strings[header_strings_count], 0);
1654 ++header_strings_count;
1655
1656 ptr_str = (gate_string_t const*)ptr_map_entry->value;
1657 if (ptr_str->length > 0)
1658 {
1659 if (ptr_str->str[ptr_str->length] == 0)
1660 {
1661 gate_string_clone(&header_strings[header_strings_count], ptr_str);
1662 }
1663 else
1664 {
1665 gate_string_create_copy(&header_strings[header_strings_count], ptr_str);
1666 }
1667 }
1668 headers[header_strings_count] = gate_string_ptr(&header_strings[header_strings_count], 0);
1669 ++header_strings_count;
1670
1671 gate_enumerator_next(&e);
1672 }
1673 headers[header_strings_count] = NULL;
1674
1675 do
1676 {
1677 char url_buffer[2048];
1678 gate_strbuilder_t builder;
1679 emscripten_semaphore_t sem_completed = EMSCRIPTEN_SEMAPHORE_T_STATIC_INITIALIZER(0);
1680 emscripten_fetch_attr_t attr;
1681
1682 gate_strbuilder_create_static(&builder, url_buffer, sizeof(url_buffer), 0);
1683 gate_strbuilder_append_cstr(&builder, "http://");
1684 gate_strbuilder_append_string(&builder, &client->server);
1685 gate_strbuilder_append_cstr(&builder, ":");
1686 gate_strbuilder_append_uint32(&builder, client->port);
1687 gate_strbuilder_append_string(&builder, &request->path);
1688
1689 emscripten_fetch_attr_init(&attr);
1690 gate_str_print_text(attr.requestMethod, sizeof(attr.requestMethod),
1691 gate_string_ptr(&request->method, 0),
1692 gate_string_length(&request->method));
1693 attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
1694 attr.requestHeaders = headers;
1695 attr.onsuccess = &httpclient_on_success;
1696 attr.onerror = &httpclient_on_error;
1697 attr.userData = (emscripten_semaphore_t*)&sem_completed;
1698 ptr_fetch = emscripten_fetch(&attr, url_buffer);
1699
1700 emscripten_semaphore_waitinf_acquire(&sem_completed, 1);
1701
1702 if (response)
1703 {
1704 gate_memstream_t* response_stream;
1705 gate_http_response_init(response);
1706 response_stream = gate_memstream_create(ptr_fetch->numBytes);
1707 gate_stream_write_block((gate_stream_t*)response_stream, ptr_fetch->data, ptr_fetch->numBytes, NULL);
1708 response->status_code = ptr_fetch->status;
1709 response->response_stream = (gate_stream_t*)response_stream;
1710 }
1711 ret = GATE_RESULT_OK;
1712 } while (0);
1713
1714 if (ptr_fetch)
1715 {
1716 emscripten_fetch_close(ptr_fetch);
1717 }
1718
1719 while (header_strings_count > 0)
1720 {
1721 --header_strings_count;
1722 gate_string_release(&header_strings[header_strings_count]);
1723 }
1724
1725 return ret;
1726 }
1727 gate_result_t gate_httpclient_release(gate_httpclient_t* client)
1728 {
1729 GATE_UNUSED_ARG(client);
1730 return GATE_RESULT_NOTIMPLEMENTED;
1731 }
1732
1733 #endif /* GATE_NET_HTTPCLIENT_WASM_IMPL */
1734
1735
1736 #if defined(GATE_NET_HTTPCLIENT_NO_IMPL)
1737
1738 gate_result_t gate_httpclient_create(gate_httpclient_t* client, gate_string_t const* server, gate_uint16_t port, gate_enumint_t flags)
1739 {
1740 GATE_UNUSED_ARG(client);
1741 GATE_UNUSED_ARG(server);
1742 GATE_UNUSED_ARG(port);
1743 GATE_UNUSED_ARG(flags);
1744 return GATE_RESULT_NOTIMPLEMENTED;
1745 }
1746 gate_result_t gate_httpclient_send_request(gate_httpclient_t* client, gate_http_request_t* request, gate_http_response_t* response)
1747 {
1748 GATE_UNUSED_ARG(client);
1749 GATE_UNUSED_ARG(request);
1750 GATE_UNUSED_ARG(response);
1751 return GATE_RESULT_NOTIMPLEMENTED;
1752 }
1753 gate_result_t gate_httpclient_release(gate_httpclient_t* client)
1754 {
1755 GATE_UNUSED_ARG(client);
1756 return GATE_RESULT_NOTIMPLEMENTED;
1757 }
1758
1759 #endif /* GATE_NET_HTTPCLIENT_NO_IMPL */
1760