src/server/proxy/httpclient.c

Fri, 06 Feb 2026 19:37:41 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 06 Feb 2026 19:37:41 +0100
changeset 666
c99e0b352e36
parent 665
b8d5b797d090
permissions
-rw-r--r--

add non-blocking http client connect

/*
 * 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>

static int client_connected(EventHandler *ev, Event *event);

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;
}

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->writeev.cookie = client;
    client->writeev.fn = client_connected;
    
    int ret = ev_pollout(client->ev, socketfd, &client->writeev);
    if(ret) {
        close(socketfd);
    }
    return 1;
}

static int create_req_buffer(HttpClient *client) {
    CxBuffer buf;
    if(cxBufferInit(&buf, cxDefaultAllocator, NULL, 1024, CX_BUFFER_AUTO_EXTEND)) {
        return 1;
    }
    
    
    
    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
    }
    
    
    return 1;
}

mercurial