Skip to content

Commit

Permalink
Multiple bug fixes: websockets, null pointers and archlinux, #12
Browse files Browse the repository at this point in the history
  • Loading branch information
joexbayer committed Nov 22, 2024
1 parent 4225eb8 commit 4b2c615
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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`:
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions example/deploy.bat
Original file line number Diff line number Diff line change
@@ -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%"
2 changes: 1 addition & 1 deletion src/mngt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 13 additions & 4 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 4b2c615

Please sign in to comment.