| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_HTTPS_CLIENT_H |
| 4 | #define NETDATA_HTTPS_CLIENT_H |
| 5 | |
| 6 | #include "libnetdata/libnetdata.h" |
| 7 | |
| 8 | typedef enum https_client_resp { |
| 9 | HTTPS_CLIENT_RESP_OK = 0, |
| 10 | |
| 11 | // all the ND_SOCK_ERR_XXX are place here |
| 12 | |
| 13 | HTTPS_CLIENT_RESP_UNKNOWN_ERROR = ND_SOCK_ERR_MAX, |
| 14 | HTTPS_CLIENT_RESP_NO_MEM, |
| 15 | HTTPS_CLIENT_RESP_NONBLOCK_FAILED, |
| 16 | HTTPS_CLIENT_RESP_PROXY_NEGOTIATION_FAILED, |
| 17 | HTTPS_CLIENT_RESP_NO_SSL_CTX, |
| 18 | HTTPS_CLIENT_RESP_NO_SSL_VERIFY_PATHS, |
| 19 | HTTPS_CLIENT_RESP_NO_SSL_NEW, |
| 20 | HTTPS_CLIENT_RESP_NO_TLS_SNI, |
| 21 | HTTPS_CLIENT_RESP_SSL_CONNECT_FAILED, |
| 22 | HTTPS_CLIENT_RESP_SSL_START_FAILED, |
| 23 | HTTPS_CLIENT_RESP_UNKNOWN_REQUEST_TYPE, |
| 24 | HTTPS_CLIENT_RESP_HEADER_WRITE_FAILED, |
| 25 | HTTPS_CLIENT_RESP_PAYLOAD_WRITE_FAILED, |
| 26 | HTTPS_CLIENT_RESP_POLL_ERROR, |
| 27 | HTTPS_CLIENT_RESP_TIMEOUT, |
| 28 | HTTPS_CLIENT_RESP_READ_ERROR, |
| 29 | HTTPS_CLIENT_RESP_PARSE_ERROR, |
| 30 | HTTPS_CLIENT_RESP_ENV_AGENT_NOT_CLAIMED, |
| 31 | HTTPS_CLIENT_RESP_ENV_NOT_200, |
| 32 | HTTPS_CLIENT_RESP_ENV_EMPTY, |
| 33 | HTTPS_CLIENT_RESP_ENV_NOT_JSON, |
| 34 | HTTPS_CLIENT_RESP_OTP_CHALLENGE_NOT_200, |
| 35 | HTTPS_CLIENT_RESP_OTP_CHALLENGE_INVALID, |
| 36 | HTTPS_CLIENT_RESP_OTP_PASSWORD_NOT_201, |
| 37 | HTTPS_CLIENT_RESP_OTP_PASSWORD_EMPTY, |
| 38 | HTTPS_CLIENT_RESP_OTP_PASSWORD_NOT_JSON, |
| 39 | HTTPS_CLIENT_RESP_OTP_AGENT_NOT_CLAIMED, |
| 40 | HTTPS_CLIENT_RESP_OTP_CHALLENGE_DECRYPTION_FAILED, |
| 41 | |
| 42 | // terminator |
| 43 | HTTPS_CLIENT_RESP_MAX, |
| 44 | } https_client_resp_t; |
| 45 | |
| 46 | ENUM_STR_DEFINE_FUNCTIONS_EXTERN(https_client_resp_t); |
| 47 | |
| 48 | typedef enum http_req_type { |
| 49 | HTTP_REQ_GET = 0, |
| 50 | HTTP_REQ_POST, |
| 51 | HTTP_REQ_INVALID |
| 52 | } http_req_type_t; |
| 53 | |
| 54 | typedef struct { |
| 55 | http_req_type_t request_type; |
| 56 | |
| 57 | char *host; |
| 58 | int port; |
| 59 | char *url; |
| 60 | |
| 61 | time_t timeout_s; //timeout in seconds for the network operation (send/recv) |
| 62 | |
| 63 | void *payload; |
| 64 | size_t payload_size; |
| 65 | |
| 66 | const char *proxy_host; |
| 67 | int proxy_port; |
| 68 | const char *proxy_username; |
| 69 | const char *proxy_password; |
| 70 | const char *proxy; |
| 71 | int proxy_type; // enum mqtt_wss_proxy_type (int avoids extra header coupling) |
| 72 | } https_req_t; |
| 73 | |
| 74 | typedef struct { |
| 75 | int http_code; |
| 76 | |
| 77 | void *payload; |
| 78 | size_t payload_size; |
| 79 | } https_req_response_t; |
| 80 | |
| 81 | |
| 82 | // Non feature complete URL parser |
| 83 | // feel free to extend when needed |
| 84 | // currently implements only what ACLK |
| 85 | // needs |
| 86 | // proto://host[:port]/path |
| 87 | typedef struct { |
| 88 | char *proto; |
| 89 | char *host; |
| 90 | int port; |
| 91 | char* path; |
| 92 | } url_t; |
| 93 | |
| 94 | int url_parse(const char *url, url_t *parsed); |
| 95 | void url_t_destroy(url_t *url); |
| 96 | |
| 97 | void https_req_response_free(https_req_response_t *res); |
| 98 | |
| 99 | #define HTTPS_REQ_RESPONSE_T_INITIALIZER \ |
| 100 | { \ |
| 101 | .http_code = 0, \ |
| 102 | .payload = NULL, \ |
| 103 | .payload_size = 0 \ |
| 104 | } |
| 105 | |
| 106 | #define HTTPS_REQ_T_INITIALIZER \ |
| 107 | { \ |
| 108 | .request_type = HTTP_REQ_GET, \ |
| 109 | .host = NULL, \ |
| 110 | .port = 443, \ |
| 111 | .url = NULL, \ |
| 112 | .timeout_s = 30, \ |
| 113 | .payload = NULL, \ |
| 114 | .payload_size = 0, \ |
| 115 | .proxy_host = NULL, \ |
| 116 | .proxy_port = 8080, \ |
| 117 | .proxy_type = 0 \ |
| 118 | } |
| 119 | |
| 120 | https_client_resp_t https_request(https_req_t *request, https_req_response_t *response, bool *fallback_ipv4); |
| 121 | |
| 122 | // we expose previously internal parser as this is usefull also from |
| 123 | // other parts of the code |
| 124 | enum http_parse_state { |
| 125 | HTTP_PARSE_PROXY_CONNECT = 0, |
| 126 | HTTP_PARSE_INITIAL, |
| 127 | HTTP_PARSE_HEADERS, |
| 128 | HTTP_PARSE_CONTENT |
| 129 | }; |
| 130 | |
| 131 | typedef uint32_t parse_ctx_flags_t; |
| 132 | |
| 133 | #define HTTP_PARSE_FLAG_DONT_WAIT_FOR_CONTENT ((parse_ctx_flags_t)0x01) |
| 134 | |
| 135 | #define HTTP_PARSE_FLAGS_DEFAULT ((parse_ctx_flags_t)0) |
| 136 | |
| 137 | typedef struct { |
| 138 | parse_ctx_flags_t flags; |
| 139 | |
| 140 | enum http_parse_state state; |
| 141 | int content_length; |
| 142 | int http_code; |
| 143 | |
| 144 | c_rhash headers; |
| 145 | |
| 146 | // for chunked data only |
| 147 | char *chunked_response; |
| 148 | size_t chunked_response_size; |
| 149 | size_t chunked_response_written; |
| 150 | |
| 151 | enum chunked_content_state { |
| 152 | CHUNKED_CONTENT_CHUNK_SIZE = 0, |
| 153 | CHUNKED_CONTENT_CHUNK_DATA, |
| 154 | CHUNKED_CONTENT_CHUNK_END_CRLF, |
| 155 | CHUNKED_CONTENT_FINAL_CRLF |
| 156 | } chunked_content_state; |
| 157 | |
| 158 | size_t chunk_size; |
| 159 | size_t chunk_got; |
| 160 | } http_parse_ctx; |
| 161 | |
| 162 | void http_parse_ctx_create(http_parse_ctx *ctx, enum http_parse_state parse_state); |
| 163 | void http_parse_ctx_destroy(http_parse_ctx *ctx); |
| 164 | |
| 165 | typedef enum { |
| 166 | HTTP_PARSE_ERROR = -1, |
| 167 | HTTP_PARSE_NEED_MORE_DATA = 0, |
| 168 | HTTP_PARSE_SUCCESS = 1 |
| 169 | } http_parse_rc; |
| 170 | |
| 171 | http_parse_rc parse_http_response(rbuf_t buf, http_parse_ctx *parse_ctx); |
| 172 | |
| 173 | const char *get_http_header_by_name(http_parse_ctx *ctx, const char *name); |
| 174 | |
| 175 | #endif /* NETDATA_HTTPS_CLIENT_H */ |