GCC Code Coverage Report


Directory: src/gate/
File: src/gate/tech/webapis.c
Date: 2026-10-19 17:06:30
Exec Total Coverage
Lines: 42 48 87.5%
Functions: 2 2 100.0%
Branches: 15 28 53.6%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2026, Stefan Meislinger |
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/tech/webapis.h"
30 #include "gate/net/httpclients.h"
31 #include "gate/results.h"
32 #include "gate/properties.h"
33 #include "gate/encode/json.h"
34
35
36 #define GATE_WEBAPIS_CONTEXT_NAME GATE_STRUCT_WEBAPIS_NAME("context")
37
38 static gate_struct_item_t const context_members[] =
39 {
40 GATE_STRUCT_ITEM_EX(gate_tech_webapi_context_t, GATE_TYPE_STRING, proxy_server, "Proxy server address", GATE_STRUCT_FLAG_DEFAULT),
41 GATE_STRUCT_ITEM_EX(gate_tech_webapi_context_t, GATE_TYPE_UI16, proxy_port, "Proxy server port", GATE_STRUCT_FLAG_DEFAULT),
42 GATE_STRUCT_ITEM_EX(gate_tech_webapi_context_t, GATE_TYPE_BOOL, native_client, "Use native HTTP client implementation", GATE_STRUCT_FLAG_DEFAULT),
43 GATE_STRUCT_ITEM_EX(gate_tech_webapi_context_t, GATE_TYPE_UI32, timeout_ms, "HTTP request timeout in milliseconds", GATE_STRUCT_FLAG_DEFAULT)
44 };
45
46 static gate_struct_descriptor_t const context_descriptor =
47 GATE_STRUCT_DESCRIPTOR(gate_tech_webapi_context_t, GATE_WEBAPIS_CONTEXT_NAME, context_members);
48
49
50 9 void gate_tech_webapi_context_init(gate_tech_webapi_context_t* context)
51 {
52 9 gate_mem_clear(context, sizeof(gate_tech_webapi_context_t));
53 9 context->struct_base.struct_descriptor = &context_descriptor;
54 9 }
55
56 14 gate_result_t gate_tech_webapi_get(gate_tech_webapi_context_t* context,
57 gate_uri_t const* url, gate_map_t const* additional_request_headers,
58 gate_uint32_t* response_status, gate_stream_t* response_content, gate_map_t* response_headers)
59 {
60 14 gate_result_t ret = GATE_RESULT_FAILED;
61 14 gate_string_t http_path = GATE_STRING_INIT_EMPTY;
62 14 gate_httpclient_t http_client = GATE_INIT_EMPTY;
63 14 gate_http_request_t http_request = GATE_INIT_EMPTY;
64 14 gate_http_response_t http_response = GATE_INIT_EMPTY;
65
66 GATE_UNUSED_ARG(context);
67
68 do
69 {
70 static gate_string_t const http_method = GATE_STRING_INIT_STATIC("GET");
71 static gate_string_t const_https_scheme = GATE_STRING_INIT_STATIC(GATE_URI_SCHEME_HTTPS);
72 gate_map_iterator_t iter;
73 14 gate_enumint_t flags = 0;
74
75 14 gate_uri_to_string(url, &http_path, true);
76
77 14 ret = gate_http_request_init(&http_request, &http_method, &url->host, &http_path);
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 GATE_BREAK_IF_FAILED(ret);
79
80 14 ret = gate_uri_parse_user_info(&url->user_info, &http_request.auth_user, &http_request.auth_pass);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 GATE_BREAK_IF_FAILED(ret);
82
83
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 if (additional_request_headers)
84 {
85 3 iter = gate_map_first(additional_request_headers);
86
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
6 while (gate_map_iterator_valid(iter))
87 {
88 3 gate_string_t const* const ptr_key = (gate_string_t const*)gate_map_iterator_key(iter);
89 3 gate_string_t const* const ptr_value = (gate_string_t const*)gate_map_iterator_value(iter);
90 3 gate_map_add(&http_request.headers, ptr_key, ptr_value);
91 3 iter = gate_map_iterator_next(iter);
92 }
93 }
94
95
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (gate_string_equals(&url->scheme, &const_https_scheme))
96 {
97 14 flags |= GATE_HTTPCLIENT_FLAG_SECURE;
98 14 flags |= GATE_HTTPCLIENT_FLAG_DISABLE_VERIFICATION;
99 }
100
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (context->timeout_ms != 0)
102 {
103 ✗ http_request.connect_timeout_ms = context->timeout_ms;
104 ✗ http_request.send_timeout_ms = context->timeout_ms;
105 ✗ http_request.receive_timeout_ms = context->timeout_ms;
106 }
107
108 14 ret = gate_httpclient_create(&http_client, &url->host, (gate_uint16_t)url->port, flags);
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 GATE_BREAK_IF_FAILED(ret);
110
111 14 ret = gate_httpclient_send_request(&http_client, &http_request, &http_response);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 GATE_BREAK_IF_FAILED(ret);
113
114
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (NULL != response_status)
115 {
116 14 *response_status = http_response.status_code;
117 }
118
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (response_headers)
120 {
121 ✗ if (NULL == gate_map_copy(response_headers, &http_response.headers))
122 {
123 ✗ ret = GATE_RESULT_OUTOFMEMORY;
124 ✗ break;
125 }
126 }
127
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 if ((NULL != response_content) && (NULL != http_response.response_stream))
128 {
129 14 ret = gate_stream_transfer(http_response.response_stream, response_content);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 GATE_BREAK_IF_FAILED(ret);
131 }
132
133 } while (0);
134
135 14 gate_httpclient_release(&http_client);
136 14 gate_http_response_release(&http_response);
137 14 gate_http_request_release(&http_request);
138 14 gate_string_release(&http_path);
139
140 14 return ret;
141 }
142
143
144
145
146
147
148
149
150
151