A minimal HTTP server written in C. It listens on TCP port 12345, serves files from
client/files/ and answers a subset of the HTTP methods using a pre-allocated thread pool.
main()spawns 20 threads (THREAD_POOL_SIZE), then opens a server socket (AF_INET/SOCK_STREAM) bound toINADDR_ANY:12345.- Every accepted connection is pushed onto a linked-list queue
(
utils/myqueue.c,enqueue/dequeue) under apthread_mutex, then apthread_cond_signalwakes up one thread from the pool. - An idle thread pops a client socket and calls
handle_connection(): read the request (4096-byte buffer, up to the first\n), parse, respond,close(). fill_request()(utils/http.c) splits the request line withstrtok_rand fills the globalhttprqstruct with the method and the URL.- Routing is a plain string comparison on the method, then
sendResponse()writes the header followed by the file contents to the socket.
| Method | File served (from client/files/) |
Status returned |
|---|---|---|
GET |
the file named in the URL | 200 OK |
POST |
post.html |
201 Created |
HEAD |
post.html |
200 Created (wrong reason phrase in the code) |
PUT |
put.html |
200 Created (same) |
DELETE |
— (DeleteFunction() is an empty stub) |
no response |
| anything else | unknown.html |
404 Not Found |
When the requested file cannot be opened, unknown.html is served instead — but with the
header of the original method, so a failed GET still returns 200 OK.
Every response is sent with Content-Type: text/html; charset=UTF-8, whatever the actual
file type is.
main.c server: socket, thread pool, method routing, sendResponse()
utils/myqueue.{c,h} linked-list queue of pending client sockets (enqueue/dequeue)
utils/http.{c,h} http_request struct, fill_request(), per-method stubs
utils/Consts.h empty
client/client.c interactive TCP test client (sends a line, prints the response)
client/client.rb Ruby test client (requests client/files/<ARGV[0]>.c)
client/files/ document root (1.html, post.html, put.html, unknown.html, 3.c … 10.c)
Linux only: the code uses <linux/limits.h> and _GNU_SOURCE.
make # gcc main.c utils/myqueue.c utils/http.c -o out -lpthread
./out # listens on port 12345Paths are resolved relative to the current working directory (client/files/…), so the
server must be started from the repository root.
curl http://localhost:12345/1.html
curl -X POST http://localhost:12345/
curl -X PUT http://localhost:12345/
# or with the bundled clients
gcc client/client.c -o client/out && ./client/out
ruby client/client.rb 3 # requests client/files/3.cThis is a learning exercise, not a production server. As it stands:
- no path safety: the URL is concatenated as-is onto the
client/files/prefix, with no directory-traversal check; strcat()is called on local arrays sized to the prefix itself (char path[] = "client/files/"), which overflows;DELETEreturns nothing, andgetFonction()/PostFunction()/PutFunction()/HeadFunction()inutils/http.care unused stubs;httprqis a shared global: concurrent requests overwrite each other;- request headers and bodies are never parsed (no
Content-Length, no HTTP version, and none of thehttp_requestfields besides method and URL); - files larger than 4096 bytes are sent in chunks with the header length re-counted on every
sendResponse()iteration; close(client_socket)is called twice (insendResponse(), then inhandle_connection());- the
accept()loop runs forever, with no clean shutdown and no thread teardown.
MIT — see LICENSE.