From 4b2c6156597318879148584a59269fe6a74624a9 Mon Sep 17 00:00:00 2001 From: joexbayer Date: Fri, 22 Nov 2024 10:25:10 +0100 Subject: [PATCH] Multiple bug fixes: websockets, null pointers and archlinux, #12 --- README.md | 25 +++++++++++++++++++++++-- example/deploy.bat | 11 +++++++++++ src/mngt.c | 2 +- src/server.c | 17 +++++++++++++---- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 example/deploy.bat diff --git a/README.md b/README.md index 40ed49f..331341b 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,9 @@ This isn’t a silver bullet—it’s a proof of concept. But **c-web-modules** --- +## Getting started + + ## Example: Counter Module Here’s a simple example of a module that keeps track of a counter and returns its value every time you visit `/counter`. @@ -99,7 +102,6 @@ Currently supported external libraries: Deploying code to the server is simple and can be done in multiple ways, depending on your workflow. - ### 1. Basic Deployment with `curl` At its core, deploying code to the server involves sending a POST request with the C file attached. Here’s an example using `curl`: @@ -128,6 +130,16 @@ example2.c When using the .ini files you run: `./cweb deploy` +## Windows + +For Windows there currently only is a very primitve deploy.bat script: + +```bash +.\deploy.bat file.c +``` + +The server needs to be specified inside the .bat file, default is: http://localhost:8080/mgnt + ### Errors Error messages are forwarded back to you over http. @@ -142,11 +154,20 @@ Error messages are forwarded back to you over http. The project depends on: ```bash -# Linux +# Debian sudo apt-get install libssl-dev sudo apt-get install libsqlite3-dev sudo apt-get install libjansson-dev +# Arch +sudo pacman -S openssl +sudo pacman -S sqlite +sudo pacman -S jansson + +# Bug with archlinux and fanitizser, ref: https://github.com/joexbayer/c-web-modules/issues/12 +sudo sysctl vm.mmap_rnd_bits=30 + + # MacOS brew install openssl@3 brew install sqlite diff --git a/example/deploy.bat b/example/deploy.bat new file mode 100644 index 0000000..dddfb2a --- /dev/null +++ b/example/deploy.bat @@ -0,0 +1,11 @@ +@echo off +setlocal enabledelayedexpansion + +:deploy_module +set "code=%~1" +set "server_url=http://localhost:8080/mgnt" + +echo Deploying "%code%" to "%server_url%"... + +REM Send the file with curl and capture the response +curl -X POST "%server_url%" -F "code=@%code%" \ No newline at end of file diff --git a/src/mngt.c b/src/mngt.c index c7d5dda..df399b0 100644 --- a/src/mngt.c +++ b/src/mngt.c @@ -17,7 +17,7 @@ #define CFLAGS "-fPIC -shared -I./include -I/opt/homebrew/opt/jansson/include" #define LIBS "-L./libs -lmodule -L/opt/homebrew/opt/jansson/lib -ljansson" #elif __linux__ - #define CFLAGS "-fPIC -shared -I./include" + #define CFLAGS "-fPIC -shared -I./include -Wl,-rpath=./libs" /* -Wl,-rpath needed for archlinux. */ #define LIBS "-L./libs -lmodule -ljansson" #else #error "Unsupported platform" diff --git a/src/server.c b/src/server.c index a40d8a2..ae93bc0 100644 --- a/src/server.c +++ b/src/server.c @@ -233,9 +233,6 @@ static void thread_handle_client(void *arg) { int ret; struct connection *c = (struct connection *)arg; - /* Set timeout for client */ - thread_set_timeout(c->sockfd, 2); - while(1){ char buffer[8*1024] = {0}; int read_size = read(c->sockfd, buffer, sizeof(buffer) - 1); @@ -249,6 +246,9 @@ static void thread_handle_client(void *arg) { } buffer[read_size] = '\0'; + printf("[SERVER] Received %d bytes\n", read_size); + printf("[SERVER] Received %s\n", buffer); + struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); @@ -270,7 +270,12 @@ static void thread_handle_client(void *arg) { read_size += ret; buffer[read_size] = '\0'; } - req.body = strdup(strstr(buffer, "\r\n\r\n") + 4); + char* body_ptr = strstr(buffer, "\r\n\r\n"); + if (body_ptr) { + req.body = strdup(body_ptr + 4); + } else { + req.body = strdup(""); + } http_parse_data(&req); @@ -320,6 +325,10 @@ static void thread_handle_client(void *arg) { if (req.websocket) { ws_confirm_open(c->sockfd); + return; /* Websocket connection is handled by the websocket thread */ + } else { + /* Set timeout for client */ + thread_set_timeout(c->sockfd, 2); } if (thread_pool_is_full(pool)) {