Sat, 14 Feb 2026 18:08:24 +0100
implement basic http client IO
/* * 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" #include <cx/buffer.h> #include <cx/string.h> #include <stdlib.h> #include <string.h> #include <errno.h> static int client_connected(EventHandler *ev, Event *event); static int client_io(EventHandler *ev, Event *event); static int client_send_request(HttpClient *client); 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->ev = ev; client->request_headers = req_headers; client->response_headers = resp_headers; client->buffer.maxsize = HTTP_CLIENT_BUFFER_SIZE; client->buffer.inbuf = malloc(HTTP_CLIENT_BUFFER_SIZE); HttpParser *parser = http_parser_new2(1, &client->buffer, resp_headers); if(!parser || !client->buffer.inbuf) { http_client_free(client); return NULL; } client->parser = parser; return client; } void http_client_free(HttpClient *client) { cxMempoolFree(client->mp); header_array_free(client->request_headers); http_parser_free(client->parser); free(client->buffer.inbuf); free(client->addr); free(client->method); free(client->uri); free(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_set_method(HttpClient *client, const char *method) { return http_client_set_method_len(client, method, method ? strlen(method) : 0); } int http_client_set_uri(HttpClient *client, const char *uri) { return http_client_set_uri_len(client, uri, uri ? strlen(uri) : 0); } static int client_set_str(char **ptr, const char *str, size_t len) { free(*ptr); if(str) { char *newvalue = malloc(len+1); if(!newvalue) { *ptr = NULL; return 1; } memcpy(newvalue, str, len); newvalue[len] = 0; *ptr = newvalue; } else { *ptr = NULL; } return 0; } int http_client_set_method_len(HttpClient *client, const char *method, size_t len) { return client_set_str(&client->method, method, len); } int http_client_set_uri_len(HttpClient *client, const char *uri, size_t len) { return client_set_str(&client->uri, uri, len); } 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; } int http_client_start(HttpClient *client) { int socketfd = socket(AF_INET, SOCK_STREAM, 0); if(socketfd < 0) { return 1; } int flags; if ((flags = fcntl(socketfd, F_GETFL, 0)) == -1) { flags = 0; } if (fcntl(socketfd, F_SETFL, flags | O_NONBLOCK) != 0) { close(socketfd); return 1; } client->socketfd = socketfd; client->writeev.cookie = client; client->writeev.fn = client_connected; int ret = 1; if(connect(socketfd, client->addr, client->addrlen)) { int err = errno; if(err == EINPROGRESS) { ret = ev_pollout(client->ev, socketfd, &client->writeev); } else { log_ereport(LOG_FAILURE, "http-client-start: connect failed: %s", strerror(err)); } } else { // TODO: call client_connected directly } if(ret) { close(socketfd); } return ret; } static int create_req_buffer(HttpClient *client) { CxBuffer buf; if(cxBufferInit(&buf, cxDefaultAllocator, NULL, 1024, CX_BUFFER_AUTO_EXTEND)) { return 1; } if(client->method) { cxBufferPutString(&buf, "GET "); } else { cxBufferPutString(&buf, client->method); } cxBufferPutString(&buf, client->uri ? client->uri : "/"); cxBufferPutString(&buf, " HTTP/1.1\r\n"); HeaderArray *hdr = client->request_headers; while(hdr) { for(int i=0;i<hdr->len;i++) { cxBufferPutString(&buf, hdr->headers[i].name); cxBufferPutString(&buf, ": "); cxBufferPutString(&buf, hdr->headers[i].value); cxBufferPutString(&buf, "\r\n"); } hdr = hdr->next; } cxBufferPutString(&buf, "\r\n"); client->req_buffer = buf.space; client->req_buffer_len = buf.size; return 0; } static int client_connected(EventHandler *ev, Event *event) { HttpClient *client = event->cookie; if(create_req_buffer(client)) { // TODO: set error return 0; // end } event->fn = client_io; return client_io(ev, event); } static int client_io(EventHandler *ev, Event *event) { HttpClient *client = event->cookie; if(client->req_buffer_pos < client->req_buffer_len) { if(client_send_request(client)) { if(client->error) { return 0; // TODO: set error } return 1; } } // make sure to receive read-ready events in the future event->events |= EVENT_POLLIN; char *buffer; size_t nbytes; if(client->header_complete) { buffer = client->buffer.inbuf; nbytes = client->buffer.maxsize; } else { buffer = client->buffer.inbuf + client->buffer.pos; nbytes = client->buffer.maxsize - client->buffer.cursize; } ssize_t r; while((r = read(client->socketfd, buffer, nbytes)) > 0) { client->buffer.cursize += r; if(!client->header_complete) { switch(http_parser_process(client->parser)) { case 0: { // finish if(!http_parser_validate(client->parser)) { client->error = 1; return 0; } client->header_complete = 1; if(client->response_start) { cxmutstr msg = client->parser->msg; char t = msg.ptr[msg.length]; msg.ptr[msg.length] = 0; int ret = client->response_start(client, client->parser->status, msg.ptr, client->response_start_userdata); msg.ptr[msg.length] = t; // TODO: check ret } break; } case 1: { // need more data continue; } case 2: { // error client->error = 1; return 0; } } } // header complete char *out = client->buffer.inbuf + client->buffer.pos; size_t len = client->buffer.cursize - client->buffer.pos; if(client->response_body_write) { int ret = client->response_body_write(client, out, len, client->response_body_write_userdata); // TODO: check ret } client->buffer.pos = 0; client->buffer.cursize = 0; } return 0; } static int client_send_request(HttpClient *client) { size_t nbytes = client->req_buffer_len - client->req_buffer_pos; ssize_t w = write(client->socketfd, client->req_buffer + client->req_buffer_pos, nbytes); if(w <= 0) { if(errno != EAGAIN) { // TODO: log correct host log_ereport(LOG_VERBOSE, "http-client %s - %s: write failed: %s", "localhost", client->uri, strerror(errno)); client->error = 1; } return 1; } client->req_buffer_pos += w; return client->req_buffer_pos < client->req_buffer_len; }