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 "proxy.h" #include <sys/socket.h> #include <arpa/inet.h> #include "../util/pblock.h" #include "../proxy/httpclient.h" // Gets the uri part from an http request line static cxstring get_uri_from_clfreq(const char *clfreq) { cxstring uri = { NULL, 0 }; const char *begin = NULL; const char *str = clfreq; for(;*str != '\0';str++) { if(*str < 33) { if(begin) { str++; break; } } else { if(!begin) { begin = str; } } } begin = NULL; for(;*str != '\0';str++) { if(*str > 32) { if(!begin) { begin = str; } } else { if(begin) { break; } } } if(begin && *str != '\0') { return cx_strn(begin, str-begin); } return uri; } typedef struct ProxyData { Session *sn; Request *rq; } ProxyData; static int proxy_response_start(HttpClient *client, int status, char *message, void *userdata) { ProxyData *proxy = userdata; protocol_status(proxy->sn, proxy->rq, status, message); protocol_start_response(proxy->sn, proxy->rq); return 0; } static ssize_t proxy_response_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) { ProxyData *proxy = userdata; ssize_t ret = net_write(proxy->sn->csd, buf, nbytes); // TODO: handle errors return ret; } int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) { EventHandler *ev = sn->ev; const char *method = pblock_findkeyval(pb_key_method, rq->reqpb); const char *clfreq = pblock_findkeyval(pb_key_clf_request, rq->reqpb); cxstring uri = get_uri_from_clfreq(clfreq); if(uri.length == 0) { return REQ_ABORTED; } // setup HttpClient HttpClient *client = http_client_new(ev); if(!client) { return REQ_ABORTED; } if(http_client_set_method(client, method)) { http_client_free(client); return REQ_ABORTED; } if(http_client_set_uri_len(client, uri.ptr, uri.length)) { http_client_free(client); return REQ_ABORTED; } // test address struct sockaddr_in address; inet_pton(AF_INET, "127.0.0.1", &address.sin_addr); address.sin_family = AF_INET; address.sin_port = htons(8080); http_client_set_addr(client, (struct sockaddr*)&address, sizeof(address)); // add request headers to the client CxIterator i = pblock_iterator(rq->headers); cx_foreach(pb_entry*, entry, i) { // TODO: don't pass all headers if(http_client_add_request_header(client, cx_mutstr(entry->param->name), cx_mutstr(entry->param->value))) { http_client_free(client); return REQ_ABORTED; } } ProxyData *proxy = pool_malloc(sn->pool, sizeof(ProxyData)); proxy->sn = sn; proxy->rq = rq; client->response_start = proxy_response_start; client->response_start_userdata = proxy; client->response_body_write = proxy_response_write; client->response_body_write_userdata = proxy; //net_setnonblock(sn->csd, 1); if(http_client_start(client)) { //net_setnonblock(sn->csd, 0); http_client_free(client); return REQ_ABORTED; } return REQ_PROCESSING; } static CX_TEST(test_safs_proxy_get_uri_from_clfreq) { CX_TEST_DO { cxstring ret; ret = get_uri_from_clfreq("GET /uri HTTP/1.1"); CX_TEST_ASSERT(!cx_strcmp(ret, "/uri")); ret = get_uri_from_clfreq("G / HTTP/1.1"); CX_TEST_ASSERT(!cx_strcmp(ret, "/")); ret = get_uri_from_clfreq("POST /test%20/path HTTP/1.1"); CX_TEST_ASSERT(!cx_strcmp(ret, "/test%20/path")); ret = get_uri_from_clfreq(" GET /leading_space HTTP/1.1"); CX_TEST_ASSERT(!cx_strcmp(ret, "/leading_space")); ret = get_uri_from_clfreq(" PROPFIND /space2 HTTP/1.1"); CX_TEST_ASSERT(!cx_strcmp(ret, "/space2")); ret = get_uri_from_clfreq("HEAD /trailing_space HTTP/1.1"); CX_TEST_ASSERT(!cx_strcmp(ret, "/trailing_space")); ret = get_uri_from_clfreq(" GET /space3 HTTP/1.1 "); CX_TEST_ASSERT(!cx_strcmp(ret, "/space3")); // fail test ret = get_uri_from_clfreq(""); CX_TEST_ASSERT(ret.ptr == NULL); ret = get_uri_from_clfreq(" "); CX_TEST_ASSERT(ret.ptr == NULL); ret = get_uri_from_clfreq("GET"); CX_TEST_ASSERT(ret.ptr == NULL); ret = get_uri_from_clfreq("GET /path"); CX_TEST_ASSERT(ret.ptr == NULL); ret = get_uri_from_clfreq(" /path2/ "); CX_TEST_ASSERT(ret.ptr == NULL); } } void http_reverse_proxy_add_tests(CxTestSuite *suite) { cx_test_register(suite, test_safs_proxy_get_uri_from_clfreq); }