Fri, 13 Feb 2026 12:18:12 +0100
test a variety of method names in test_safs_proxy_get_uri_from_clfreq
/* * 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 <stdlib.h> #include <string.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; } void http_client_free(HttpClient *client) { cxMempoolFree(client->mp); header_array_free(client->request_headers); 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->writeev.cookie = client; client->writeev.fn = client_connected; int ret = ev_pollout(client->ev, socketfd, &client->writeev); 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 } return 1; }