GCC Code Coverage Report


Directory: src/gate/
File: src/gate/tech/webapis.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 35 44 79.5%
Functions: 2 2 100.0%
Branches: 12 26 46.2%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, 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 };
44
45 static gate_struct_descriptor_t const context_descriptor =
46 GATE_STRUCT_DESCRIPTOR(gate_tech_webapi_context_t, GATE_WEBAPIS_CONTEXT_NAME, context_members);
47
48
49 4 void gate_tech_webapi_context_init(gate_tech_webapi_context_t* context)
50 {
51 4 gate_mem_clear(context, sizeof(gate_tech_webapi_context_t));
52 4 context->struct_base.struct_descriptor = &context_descriptor;
53 4 }
54
55 3 gate_result_t gate_tech_webapi_get(gate_tech_webapi_context_t* context,
56 gate_uri_t const* url, gate_map_t const* additional_request_headers,
57 gate_uint32_t* response_status, gate_stream_t* response_content, gate_map_t* response_headers)
58 {
59 3 gate_result_t ret = GATE_RESULT_FAILED;
60 3 gate_string_t http_path = GATE_STRING_INIT_EMPTY;
61 3 gate_httpclient_t http_client = GATE_INIT_EMPTY;
62 3 gate_http_request_t http_request = GATE_INIT_EMPTY;
63 3 gate_http_response_t http_response = GATE_INIT_EMPTY;
64 static gate_string_t const http_method = GATE_STRING_INIT_STATIC("GET");
65 static gate_string_t const_https_scheme = GATE_STRING_INIT_STATIC(GATE_URI_SCHEME_HTTPS);
66 gate_map_iterator_t iter;
67 3 gate_uint32_t flags = 0;
68 gate_string_t const* ptr_key;
69 gate_string_t const* ptr_value;
70
71 do
72 {
73 3 gate_uri_to_string(url, &http_path, true);
74
75 3 ret = gate_http_request_init(&http_request, &http_method, &http_path);
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
77
78 3 ret = gate_uri_parse_user_info(&url->user_info, &http_request.auth_user, &http_request.auth_pass);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
80
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (additional_request_headers)
82 {
83 iter = gate_map_first(additional_request_headers);
84 while (gate_map_iterator_valid(iter))
85 {
86 ptr_key = (gate_string_t const*)gate_map_iterator_key(iter);
87 ptr_value = (gate_string_t const*)gate_map_iterator_value(iter);
88 gate_map_add(&http_request.headers, ptr_key, ptr_value);
89 iter = gate_map_iterator_next(iter);
90 }
91 }
92
93
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 if (gate_string_equals(&url->scheme, &const_https_scheme))
94 {
95 2 flags |= GATE_HTTPCLIENT_FLAG_SECURE;
96 2 flags |= GATE_HTTPCLIENT_FLAG_DISABLE_VERIFICATION;
97 }
98
99 3 ret = gate_httpclient_create(&http_client, &url->host, (gate_uint16_t)url->port, flags);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
101
102 3 ret = gate_httpclient_send_request(&http_client, &http_request, &http_response);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
104
105
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (NULL != response_status)
106 {
107 3 *response_status = http_response.status_code;
108 }
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (response_headers)
111 {
112 if (NULL == gate_map_copy(response_headers, &http_response.headers))
113 {
114 ret = GATE_RESULT_OUTOFMEMORY;
115 break;
116 }
117 }
118
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if ((NULL != response_content) && (NULL != http_response.response_stream))
119 {
120 3 ret = gate_stream_transfer(http_response.response_stream, response_content);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
122 }
123
124 } while (0);
125
126 3 gate_httpclient_release(&http_client);
127 3 gate_http_response_release(&http_response);
128 3 gate_http_request_release(&http_request);
129 3 gate_string_release(&http_path);
130
131 3 return ret;
132 }
133
134
135
136
137
138
139
140
141
142