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