src/server/proxy/httpclient.c

Fri, 06 Feb 2026 17:07:58 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 06 Feb 2026 17:07:58 +0100
changeset 665
b8d5b797d090
parent 662
70fdf948b642
child 666
c99e0b352e36
permissions
-rw-r--r--

add first http client code

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

mercurial