Skip to content

beratmutlu/c-distributed-http-server

Repository files navigation

webserver

A simple HTTP server in C with:

  • static resources
  • in-memory dynamic resources
  • optional distributed lookup/routing over a small DHT ring

Requirements

  • GCC or Clang
  • CMake ≥ 3.5
  • Python 3 + pip (for tests)
  • POSIX-compatible OS (Linux, macOS)

Build

cmake -B build && make -C build

The binary is placed at build/webserver.


Test

Install the test dependencies once:

pip install -r requirements.txt

Run

Single-node mode

./build/webserver <hostname> <port>

Example — listen on all interfaces, port 8080:

./build/webserver 0.0.0.0 8080

Example — localhost only:

./build/webserver 127.0.0.1 4000

DHT mode

PRED_ID=<id> PRED_IP=<ip> PRED_PORT=<port> \
SUCC_ID=<id> SUCC_IP=<ip> SUCC_PORT=<port> \
./build/webserver <hostname> <port> <node-id>

Example 3-node ring:

Node 0

PRED_ID=50000 PRED_IP=127.0.0.1 PRED_PORT=4002 \
SUCC_ID=20000 SUCC_IP=127.0.0.1 SUCC_PORT=4001 \
./build/webserver 127.0.0.1 4000 0

Node 20000

PRED_ID=0     PRED_IP=127.0.0.1 PRED_PORT=4000 \
SUCC_ID=50000 SUCC_IP=127.0.0.1 SUCC_PORT=4002 \
./build/webserver 127.0.0.1 4001 20000

Node 50000

PRED_ID=20000 PRED_IP=127.0.0.1 PRED_PORT=4001 \
SUCC_ID=0     SUCC_IP=127.0.0.1 SUCC_PORT=4000 \
./build/webserver 127.0.0.1 4002 50000

Endpoints

Static resources

Method Path Response
GET /static/foo 200 Foo
GET /static/bar 200 Bar
GET /static/baz 200 Baz

Dynamic resources

Method Path Behaviour
PUT /dynamic/<name> Creates the resource (201) or updates it (204)
GET /dynamic/<name> Returns the stored content (200) or 404
DELETE /dynamic/<name> Deletes the resource (204) or 404 if not found

Attempting PUT or DELETE on a non-/dynamic/ path returns 403.
Any other HTTP method returns 501.


Usage examples

Single-node / local examples

# Create a resource
curl -i -X PUT http://127.0.0.1:4000/dynamic/hello \
     -H "Content-Length: 11" \
     -d "hello world"

# Read it back
curl -i http://127.0.0.1:4000/dynamic/hello

# Update it
curl -i -X PUT http://127.0.0.1:4000/dynamic/hello \
     -H "Content-Length: 5" \
     -d "world"

# Delete it
curl -i -X DELETE http://127.0.0.1:4000/dynamic/hello

# Static resource
curl -i http://127.0.0.1:4000/static/foo

DHT examples

curl -i -X PUT http://127.0.0.1:4000/dynamic/hello \
     -d "hello world"

curl -i -L http://127.0.0.1:4000/dynamic/hello

Notes

  • Dynamic resources are stored in memory only and are lost when the server stops.
  • In DHT mode, a node stores a dynamic resource only if it is responsible for that resource.
  • DHT lookups are performed over UDP.
  • Only one TCP client is handled at a time.
  • BUFFER_SIZE is set to 1024 bytes in networking.h. Larger requests are received across multiple recv calls and reassembled.

About

Distributed HTTP server in C with a standalone socket-based HTTP implementation on main and a DHT-enabled variant on a separate dht branch.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors