UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2026 Olaf Wintermann. 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 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef PROXY_HTTPCLIENT_H 30 #define PROXY_HTTPCLIENT_H 31 32 #include "../public/nsapi.h" 33 #include "../daemon/httpparser.h" 34 #include "../util/io.h" 35 36 #include <sys/socket.h> 37 #include <cx/string.h> 38 #include <cx/mempool.h> 39 #include <cx/test.h> 40 41 #include <inttypes.h> 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif 46 47 #define HTTP_CLIENT_BUFFER_SIZE 4096 48 49 #define HTTP_CLIENT_CALLBACK_WOULD_BLOCK -1 50 #define HTTP_CLIENT_CALLBACK_ERROR -2 51 52 typedef struct HttpClient HttpClient; 53 struct HttpClient { 54 EventHandler *ev; 55 CxMempool *mp; 56 char *method; 57 char *uri; 58 59 struct sockaddr *addr; 60 size_t addrlen; 61 62 int socketfd; 63 HttpStream *stream; 64 65 HeaderArray *request_headers; 66 HeaderArray *response_headers; 67 68 /* 69 * request content length 70 * 0: no request body 71 * > 0: request body with static length 72 * -1: request body with chunked transfer encoding 73 */ 74 int64_t req_content_length; 75 76 int error; 77 int statuscode; 78 79 /* 80 * Request body callback function 81 * 82 * ssize_t request_body_read(HttpClient *client, void *buf, size_t size, void *userdata) 83 * 84 * Return: number of processed bytes, 85 * HTTP_CLIENT_CALLBACK_WOULD_BLOCK or HTTP_CLIENT_CALLBACK_ERROR. 86 */ 87 ssize_t (*request_body_read)(HttpClient *, void *, size_t, void *); 88 void *request_body_read_userdata; 89 90 /* 91 * Response start callback function 92 * 93 * int response_start(HttpClient *client, int status, char *message, void *userdata) 94 */ 95 int (*response_start)(HttpClient *, int, char *, void *); 96 void *response_start_userdata; 97 98 /* 99 * Response body write function 100 * 101 * int response_body_write(HttpClient *client, void *buf, size_t size, void *userdata) 102 * 103 * Return: number of processed bytes, 104 * HTTP_CLIENT_CALLBACK_WOULD_BLOCK or HTTP_CLIENT_CALLBACK_ERROR. 105 */ 106 ssize_t (*response_body_write)(HttpClient *, void *, size_t, void *); 107 void *response_body_write_userdata; 108 109 /* 110 * Response finished callback 111 * 112 * After this callback, the client object is no longer used. The callback 113 * is allowed to free the client object or reuse it. 114 * 115 * void response_finished(HttpClient *client, void *userdata) 116 */ 117 void (*response_finished)(HttpClient *, void *); 118 void *response_finished_userdata; 119 120 121 // internals 122 HttpParser *parser; 123 netbuf buffer; 124 125 char *transfer_buffer; 126 size_t transfer_buffer_alloc; 127 size_t transfer_buffer_len; 128 size_t transfer_buffer_pos; 129 130 size_t req_contentlength_pos; 131 132 int request_body_complete; 133 int request_body_terminated; 134 int response_header_complete; 135 136 Event readev; 137 Event writeev; 138 }; 139 140 HttpClient* http_client_new(EventHandler *ev); 141 142 void http_client_free(HttpClient *client); 143 144 int http_client_set_addr(HttpClient *client, const struct sockaddr *addr, socklen_t addrlen); 145 146 int http_client_set_method(HttpClient *client, const char *method); 147 148 int http_client_set_uri(HttpClient *client, const char *uri); 149 150 int http_client_set_method_len(HttpClient *client, const char *method, size_t len); 151 152 int http_client_set_uri_len(HttpClient *client, const char *uri, size_t len); 153 154 /* 155 * Adds a request header 156 * 157 * This function does not create a copy of name/value. The string pointers must 158 * be valid for the lifetime of the HttpClient. 159 */ 160 int http_client_add_request_header(HttpClient *client, cxmutstr name, cxmutstr value); 161 162 /* 163 * Adds a request header 164 * 165 * This functions creates a copy of name/value and the original pointers can be 166 * discarded after the function returns. 167 */ 168 int http_client_add_request_header_copy(HttpClient *client, cxstring name, cxstring value); 169 170 /* 171 * Sets the content length for the request body 172 */ 173 int http_client_set_content_length(HttpClient *client, int64_t contentlength); 174 175 /* 176 * Enables a request body with a chunked transfer encoding 177 */ 178 int http_client_enable_chunked_transfer_encoding(HttpClient *client); 179 180 int http_client_start(HttpClient *client); 181 182 183 void http_client_add_tests(CxTestSuite *suite); 184 185 #ifdef __cplusplus 186 } 187 #endif 188 189 #endif /* PROXY_HTTPCLIENT_H */ 190 191