| 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 | GATE_UNUSED_ARG(context); | ||
| 72 | |||
| 73 | do | ||
| 74 | { | ||
| 75 | 3 | gate_uri_to_string(url, &http_path, true); | |
| 76 | |||
| 77 | 3 | ret = gate_http_request_init(&http_request, &http_method, &http_path); | |
| 78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATE_BREAK_IF_FAILED(ret); |
| 79 | |||
| 80 | 3 | 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 3 times.
|
3 | GATE_BREAK_IF_FAILED(ret); |
| 82 | |||
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (additional_request_headers) |
| 84 | { | ||
| 85 | ✗ | iter = gate_map_first(additional_request_headers); | |
| 86 | ✗ | while (gate_map_iterator_valid(iter)) | |
| 87 | { | ||
| 88 | ✗ | ptr_key = (gate_string_t const*)gate_map_iterator_key(iter); | |
| 89 | ✗ | ptr_value = (gate_string_t const*)gate_map_iterator_value(iter); | |
| 90 | ✗ | gate_map_add(&http_request.headers, ptr_key, ptr_value); | |
| 91 | ✗ | iter = gate_map_iterator_next(iter); | |
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (gate_string_equals(&url->scheme, &const_https_scheme)) |
| 96 | { | ||
| 97 | 2 | flags |= GATE_HTTPCLIENT_FLAG_SECURE; | |
| 98 | 2 | flags |= GATE_HTTPCLIENT_FLAG_DISABLE_VERIFICATION; | |
| 99 | } | ||
| 100 | |||
| 101 | 3 | ret = gate_httpclient_create(&http_client, &url->host, (gate_uint16_t)url->port, flags); | |
| 102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATE_BREAK_IF_FAILED(ret); |
| 103 | |||
| 104 | 3 | ret = gate_httpclient_send_request(&http_client, &http_request, &http_response); | |
| 105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATE_BREAK_IF_FAILED(ret); |
| 106 | |||
| 107 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (NULL != response_status) |
| 108 | { | ||
| 109 | 3 | *response_status = http_response.status_code; | |
| 110 | } | ||
| 111 | |||
| 112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (response_headers) |
| 113 | { | ||
| 114 | ✗ | if (NULL == gate_map_copy(response_headers, &http_response.headers)) | |
| 115 | { | ||
| 116 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 117 | ✗ | break; | |
| 118 | } | ||
| 119 | } | ||
| 120 |
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)) |
| 121 | { | ||
| 122 | 3 | ret = gate_stream_transfer(http_response.response_stream, response_content); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATE_BREAK_IF_FAILED(ret); |
| 124 | } | ||
| 125 | |||
| 126 | } while (0); | ||
| 127 | |||
| 128 | 3 | gate_httpclient_release(&http_client); | |
| 129 | 3 | gate_http_response_release(&http_response); | |
| 130 | 3 | gate_http_request_release(&http_request); | |
| 131 | 3 | gate_string_release(&http_path); | |
| 132 | |||
| 133 | 3 | return ret; | |
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 |