Fri, 06 Feb 2026 17:07:58 +0100
add first http client code
--- a/src/server/daemon/httprequest.c Fri Feb 06 15:57:37 2026 +0100 +++ b/src/server/daemon/httprequest.c Fri Feb 06 17:07:58 2026 +0100 @@ -487,10 +487,13 @@ return array; } -void header_array_add(HeaderArray *hd, cxmutstr name, cxmutstr value) { +int header_array_add(HeaderArray *hd, cxmutstr name, cxmutstr value) { while(hd->len >= hd->alloc) { if(hd->next == NULL) { HeaderArray *block = header_array_create(); + if(!block) { + return 1; + } hd->next = block; } hd = hd->next; @@ -498,9 +501,13 @@ hd->headers[hd->len].name = name; hd->headers[hd->len].value = value; hd->len++; + return 0; } void header_array_free(HeaderArray *hd) { + if(!hd) { + return; + } HeaderArray *next; while(hd) { next = hd->next;
--- a/src/server/daemon/httprequest.h Fri Feb 06 15:57:37 2026 +0100 +++ b/src/server/daemon/httprequest.h Fri Feb 06 17:07:58 2026 +0100 @@ -94,7 +94,7 @@ HeaderArray* header_array_create(void); -void header_array_add(HeaderArray *hd, cxmutstr name, cxmutstr value); +int header_array_add(HeaderArray *hd, cxmutstr name, cxmutstr value); void header_array_free(HeaderArray *hd); int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq);
--- a/src/server/proxy/httpclient.c Fri Feb 06 15:57:37 2026 +0100 +++ b/src/server/proxy/httpclient.c Fri Feb 06 17:07:58 2026 +0100 @@ -1,5 +1,88 @@ /* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/cFiles/file.c to edit this template + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2026 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ +#include "httpclient.h" + +HttpClient* http_client_new(EventHandler *ev) { + CxMempool *mp = cxMempoolCreate(32, CX_MEMPOOL_TYPE_PURE); + if(!mp) { + return NULL; + } + + HttpClient *client = malloc(sizeof(HttpClient)); + HeaderArray *req_headers = header_array_create(); + HeaderArray *resp_headers = header_array_create(); + if(!client || !req_headers || !resp_headers) { + free(client); + header_array_free(req_headers); + header_array_free(resp_headers); + cxMempoolFree(mp); + return NULL; + } + + memset(client, 0, sizeof(HttpClient)); + client->request_headers = req_headers; + client->response_headers = resp_headers; + + return client; +} + +int http_client_set_addr(HttpClient *client, const struct sockaddr *addr, socklen_t addrlen) { + free(client->addr); + client->addr = NULL; + client->addrlen = 0; + + void *newaddr = malloc(addrlen); + if(!newaddr) { + return 1; + } + memcpy(newaddr, addr, addrlen); + client->addr = newaddr; + client->addrlen = addrlen; + + return 0; +} + +int http_client_add_request_header(HttpClient *client, cxmutstr name, cxmutstr value) { + return header_array_add(client->request_headers, name, value); +} + +int http_client_add_request_header_copy(HttpClient *client, cxstring name, cxstring value) { + cxmutstr n = cx_strdup_a(client->mp->allocator, name); + cxmutstr v = cx_strdup_a(client->mp->allocator, value); + + int err = 1; + if(n.ptr && v.ptr) { + err = http_client_add_request_header(client, n, v); + } + if(err) { + cxFree(client->mp->allocator, n.ptr); + cxFree(client->mp->allocator, v.ptr); + } + return err; +}
--- a/src/server/proxy/httpclient.h Fri Feb 06 15:57:37 2026 +0100 +++ b/src/server/proxy/httpclient.h Fri Feb 06 17:07:58 2026 +0100 @@ -30,12 +30,87 @@ #define PROXY_HTTPCLIENT_H #include "../public/nsapi.h" +#include "../daemon/httpparser.h" + +#include <sys/socket.h> +#include <cx/string.h> +#include <cx/mempool.h> #ifdef __cplusplus extern "C" { #endif +typedef struct HttpClient HttpClient; +struct HttpClient { + EventHandler *ev; + CxMempool *mp; + char *uri; + + struct sockaddr *addr; + size_t addrlen; + + HeaderArray *request_headers; + HeaderArray *response_headers; + + /* + * Request body callback function + * + * size_t request_body_read(HttpClient *client, void *buf, size_t size, void *userdata) + */ + // TODO: fix, doesn't work this way + //size_t (*request_body_read)(HttpClient *, void *, size_t, void *); + //void *request_body_read_userdata; + + /* + * Response start callback function + * + * int response_start(HttpClient *client, int status, char *message, void *userdata) + */ + int (*response_start)(HttpClient *, int, char *, void *); + void *response_start_userdata; + + /* + * Response body write function + * + * int response_body_write(HttpClient *client, void *buf, size_t size, void *userdata) + */ + int (*response_body_write)(HttpClient *, void *, size_t, void *); + void *response_body_write_userdata; + + /* + * Response finished callback + * + * void response_finished(HttpClient *client, int error, void *userdata) + */ + void (*response_finished)(HttpClient *, int, void *); + void *response_finished_userdata; + + + // internals + + Event out_event; + Event in_event; +}; +HttpClient* http_client_new(EventHandler *ev); + +int http_client_set_addr(HttpClient *client, const struct sockaddr *addr, socklen_t addrlen); + +/* + * Adds a request header + * + * This function does not create a copy of name/value. The string pointers must + * be valid for the lifetime of the HttpClient. + */ +int http_client_add_request_header(HttpClient *client, cxmutstr name, cxmutstr value); + +/* + * Adds a request header + * + * This functions creates a copy of name/value and the original pointers can be + * discarded after the function returns. + */ +int http_client_add_request_header_copy(HttpClient *client, cxstring name, cxstring value); #ifdef __cplusplus