Skip to content

About

HTTP server using POSIX sockets for low-level request handling and static file serving

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Mini-HTTP-server

A mini HTTP server built from scratch in C++ using raw POSIX sockets.

Features

  • Serves static files from a public/ directory
  • Event-driven I/O with epoll on Linux (falls back to kqueue on macOS/BSD): one thread handles thousands of clients (see Performance)
  • Non-blocking sockets with buffered reads/writes (handles partial recv()/send())
  • Parses HTTP requests and routes by path (query strings are ignored)
  • Correct Content-Type headers for HTML, CSS, JS, JSON, SVG and images
  • Custom 404 page (public/404.html)
  • Error responses: 400 Bad Request, 405 Method Not Allowed, 431 Request Header Fields Too Large
  • Blocks directory traversal (/../)

Project Structure

Mini-HTTP-server/
├── src/
│   └── server.cpp
└── public/
    ├── index.html
    ├── style.css
    └── 404.html

Build and Run

From the project root:

g++ -std=c++20 -O2 src/server.cpp -o server
./server                 # port 8080, serves ./public
./server 9000 ./public   # optional: port and document root

Then open your browser and visit:

http://localhost:8080

Performance

The short version: one thread, a couple of megabytes of memory, and it doesn't really care how many clients are connected.

I benchmarked it on my M5 MacBook with ApacheBench, serving the small index.html, and compared it with the old version of this server that spawned a thread per client. Laptop numbers bounce around a bit, so take them as ballpark figures:

Clients at once This server Old thread-per-client version
100 ~29k req/s · 1.9 MB · 1 thread ~30k req/s · 3 MB · 69 threads
1,000 ~29k req/s · 2.4 MB · 1 thread ~22k req/s · 14 MB · 572 threads
2,000 ~23k req/s · 3 MB · 1 thread ~19k req/s · 19 MB · 943 threads

No requests failed at any level. With a handful of clients the two versions are about even (threads are cheap when there are few of them), but as the load grows the event loop pulls ahead while using a fraction of the memory.

The bigger difference shows up with idle connections, clients that connect and then just sit there. With 10,000 of them open, this server used about 2.5 MB, stayed on one thread, and still answered a fresh request in 13 ms. The old version needed one thread per idle client and crashed at around 6,150, which is the macOS limit on threads per process.

Why it's cheap:

  • An idle connection is just a small buffer (about 100 bytes), not a whole thread with its own stack
  • epoll/kqueue only wakes the server for sockets that are actually ready, so idle clients cost no CPU
  • Everything is non-blocking, so one slow client can't hold up the rest

What it doesn't do (yet):

  • It runs on a single core, so a big file or slow disk read makes everyone wait
  • There's no idle timeout, so enough clients that never send a request can use up the open-file limit
  • No keep-alive and no file caching: every request opens a new connection and reads from disk

Want to try it yourself?

g++ -std=c++20 -O2 src/server.cpp -o server && ./server &
ab -n 20000 -c 1000 http://127.0.0.1:8080/

How It Works

1. Socket setup Creates a TCP socket, binds it to the port, listens with a SOMAXCONN backlog, and makes it non-blocking.

2. Event loop The listening socket is registered with epoll (or kqueue). The loop calls epoll_wait(), which blocks until one or more sockets are ready, then handles only those. No thread is created per client.

3. Accept When the listening socket is readable, accept() is called repeatedly until it returns EAGAIN. Each new client is made non-blocking and registered for read events.

4. Read the request When a client is readable, recv() drains available bytes into that client's buffer. Once the buffer contains \r\n\r\n (end of headers), the request is complete. The first line looks like:

GET /index.html HTTP/1.1

5. Parse and build the response Extracts the method and path, strips any query string, rejects .. paths, and loads the file from public/. The response is built with the right status code and headers, and the client is switched to write events.

6. Write and close When the client is writable, send() writes as much as the socket accepts and tracks progress, so large files survive partial sends. Once everything is sent, the socket is closed (Connection: close).

Example Request/Response

Request received:

GET / HTTP/1.1
Host: localhost:8080

Response sent:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 342
Connection: close

<!DOCTYPE html>...

About

HTTP server using POSIX sockets for low-level request handling and static file serving

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages