Sun, 15 Feb 2026 12:24:38 +0100
add socket utils
--- a/src/server/proxy/httpclient.c Sun Feb 15 11:16:50 2026 +0100 +++ b/src/server/proxy/httpclient.c Sun Feb 15 12:24:38 2026 +0100 @@ -28,6 +28,8 @@ #include "httpclient.h" +#include "../util/socket.h" + #include <cx/buffer.h> #include <cx/string.h> #include <stdlib.h> @@ -160,14 +162,11 @@ return 1; } - int flags; - if ((flags = fcntl(socketfd, F_GETFL, 0)) == -1) { - flags = 0; - } - if (fcntl(socketfd, F_SETFL, flags | O_NONBLOCK) != 0) { + if(util_socket_setnonblock(socketfd, 1)) { close(socketfd); return 1; } + client->socketfd = socketfd; client->writeev.cookie = client; @@ -353,3 +352,17 @@ return client->req_buffer_pos < client->req_buffer_len; } + + + +/* --------------------------------- Tests --------------------------------- */ + +static CX_TEST(test_http_client_send_request) { + CX_TEST_DO { + + } +} + +void http_client_add_tests(CxTestSuite *suite) { + cx_test_register(suite, test_http_client_send_request); +}
--- a/src/server/proxy/httpclient.h Sun Feb 15 11:16:50 2026 +0100 +++ b/src/server/proxy/httpclient.h Sun Feb 15 12:24:38 2026 +0100 @@ -35,6 +35,7 @@ #include <sys/socket.h> #include <cx/string.h> #include <cx/mempool.h> +#include <cx/test.h> #ifdef __cplusplus extern "C" { @@ -149,6 +150,9 @@ int http_client_start(HttpClient *client); + +void http_client_add_tests(CxTestSuite *suite); + #ifdef __cplusplus } #endif
--- a/src/server/test/main.c Sun Feb 15 11:16:50 2026 +0100 +++ b/src/server/test/main.c Sun Feb 15 12:24:38 2026 +0100 @@ -40,6 +40,7 @@ #include "../util/date.h" #include "../daemon/vfs.h" #include "../safs/proxy.h" +#include "../proxy/httpclient.h" #include "test.h" @@ -195,6 +196,9 @@ // saf tests http_reverse_proxy_add_tests(suite); + // http tests + http_client_add_tests(suite); + // plugin tests #ifdef ENABLE_POSTGRESQL register_pg_tests(argc, argv, suite);
--- a/src/server/util/objs.mk Sun Feb 15 11:16:50 2026 +0100 +++ b/src/server/util/objs.mk Sun Feb 15 12:24:38 2026 +0100 @@ -48,6 +48,7 @@ UTILOBJ += hashing.o UTILOBJ += pblock.o UTILOBJ += uri.o +UTILOBJ += socket.o UTILOBJS = $(UTILOBJ:%=$(UTIL_OBJPRE)%) UTILSOURCE = $(UTILOBJ:%.o=util/%.c)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/util/socket.c Sun Feb 15 12:24:38 2026 +0100 @@ -0,0 +1,104 @@ +/* + * 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 "socket.h" + +#include <unistd.h> +#include <sys/fcntl.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> + +int util_server_socket_local(short *port) { + int socket_fd = socket(AF_INET, SOCK_STREAM, 0); + if(socket_fd < 0) { + return -1; + } + + short socket_port = 0; + if(port) { + socket_port = *port; + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = htons(socket_port); + + if(bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr))) { + close(socket_fd); + return -1; + } + + socklen_t len = sizeof(addr); + if(getsockname(socket_fd, (struct sockaddr *)&addr, &len) < 0) { + close(socket_fd); + return -1; + } + + if(port) { + *port = ntohs(addr.sin_port); + } + + return socket_fd; +} + +int util_socket_connect_local(short port) { + int fd = socket(AF_INET, SOCK_STREAM, 0); + if(fd < 0) { + return -1; + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = htons(port); + + if(connect(fd, (struct sockaddr*)&addr, sizeof(addr))) { + close(fd); + return -1; + } + + return fd; +} + +int util_socket_setnonblock(int fd, int nonblock) { + int flags; + if ((flags = fcntl(fd, F_GETFL, 0)) == -1) { + flags = 0; + } + if(nonblock) { + flags |= O_NONBLOCK; + } else { + flags &= ~O_NONBLOCK; + } + if (fcntl(fd, F_SETFL, flags) != 0) { + return 1; + } + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/util/socket.h Sun Feb 15 12:24:38 2026 +0100 @@ -0,0 +1,57 @@ +/* + * 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. + */ + +#ifndef UTIL_SOCKET_H +#define UTIL_SOCKET_H + +#ifdef __cplusplus +extern "C" { +#endif + +// Small socket utility functions for creating server or client sockets +// on localhost. Useful for testing. + +// creates a socket, binds it to localhost and optionally to the specified port +// port: if *port is 0, a random port is choosen +// returns the socket fd +int util_server_socket_local(short *port); + +// creates a socket and connects to localhost:<port> +// returns the socket fd +int util_socket_connect_local(short port); + +// enables/disables non-blocking mode the for specified socket +int util_socket_setnonblock(int fd, int nonblock); + + +#ifdef __cplusplus +} +#endif + +#endif /* UTIL_SOCKET_H */ +