A non-blocking, event-driven HTTP/1.1 server written from scratch in C++98, inspired by NGINX. Built as part of the 42 Network curriculum (
webserv).
A production-style HTTP/1.1 server that handles many simultaneous clients in a single thread using the Linux epoll API, parses an NGINX-like configuration file, executes CGI scripts (Python & PHP) over pipes with timeout protection, decodes chunked transfer encoding with a state machine, and streams multipart/form-data uploads through a clean factory + strategy pipeline — all within the strict subset of C++98 (no auto, no nullptr, no STL post-98, no select/poll mixing).
This is not "a wrapper around socket()". It's a real server design exercise where every byte that crosses the network is parsed by code in this repo. Highlights worth a closer look:
- Truly non-blocking I/O. Every socket — listening, client, CGI pipe — is registered in a single
epollinstance. There are no blocking reads or writes anywhere in the request path, andread/writereturn values are never used as a "did I block?" signal (per subject rules). - Hand-written HTTP/1.1 parser with support for headers,
Content-Length,Transfer-Encoding: chunked, query strings, malformed-request detection, and proper status-code mapping (400, 411, 413, 414, 501, 505, …). - Chunked decoder implemented as an explicit state machine (
READ_SIZE → READ_DATA → DONE / ERROR) so partial chunks split across multipleread()calls are handled correctly. - Body processing pipeline built on a
IBodyProcessorinterface +ABodyProcessorabstract base, with concrete strategies formultipart/form-data,application/x-www-form-urlencoded, raw bodies, and a no-op processor — all instantiated by aBodyProcessorFactory. - Method handlers via polymorphism.
GetHandler,PostHandler,DeleteHandler,CgiHandler,RedirectHandler,MethodNotAllowedHandler, andNotImplementedHandlerall implement a commonIMethodHandlerinterface — the server doesn't know or care what's behind a route, it just callshandleData()andgetResponse(). - CGI executed via
fork+pipe+execvewith its stdin/stdout registered back into the sameepollloop, plus a 30-second watchdog that reaps runaway scripts (while True: pass⇒ 504/500 instead of a wedged server). - Per-client idle timeout (60 s by default) prevents slow-loris-style connection hoarding.
- Graceful shutdown on
SIGINT— sockets are closed, clients are notified, no leaked file descriptors. - GitHub Actions CI builds the server and runs the full test suite (GET / POST / DELETE / REDIRECT / CGI) on every push.
| Area | Supported |
|---|---|
| HTTP version | HTTP/1.1 |
| Methods | GET, POST, DELETE (plus 405/501 for the rest) |
| I/O model | Single-threaded, epoll-based, fully non-blocking |
| Config syntax | NGINX-style, multi-server, multi-port, multi-IP |
| Routing | Per-location root, index, allowed methods, autoindex |
| Redirects | Configurable per-location (redirect <url>) |
| Static files | With directory listing (autoindex on) |
| Uploads | multipart/form-data parsed and stored at upload_store |
| Request bodies | Raw, URL-encoded, multipart, chunked, ignored |
| Max body size | client_max_body_size enforced per server (→ 413) |
| CGI | Python (.py) and PHP (.php) via python3 / php-cgi |
| CGI safety | Per-process timeout, error reporting (404/403/500), zombie reaping |
| Error pages | Custom error_page <code> <path> per server |
| Connection mgmt | Keep-Alive & Connection: close, idle-client timeout |
| Signals | Graceful SIGINT shutdown |
| Security | Path traversal blocked, method-deny via methods NONE |
┌────────────────────────────────┐
│ main.cpp │
│ Config(file) → Server.start() │
└──────────────┬─────────────────┘
│
▼
┌───────────────────────────────────────────────────────┐
│ Server │
│ one epoll_fd, N listening sockets, M clients, │
│ K CGI pipes — all in the same event loop │
└───────────────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼
┌──────────┐ ┌────────────┐ ┌────────────┐
│ Client │ ───► │HttpRequest │ ──────► │ Router │
│ (fd, │ │ (parser + │ │ (Location │
│ buffer, │ │ chunked + │ │ lookup) │
│ state) │ │ headers) │ └─────┬──────┘
└──────────┘ └────────────┘ │
▼
┌──────────────────────────────────────────┐
│ IMethodHandler (Strategy) │
│ ┌────────┐ ┌────────┐ ┌────────┐ ┌─────┐ │
│ │ Get │ │ Post │ │ Delete │ │ CGI │ │
│ └────────┘ └────────┘ └────────┘ └─────┘ │
│ ┌──────────┐ ┌───────────────┐ ┌───────┐ │
│ │ Redirect │ │ MethodNotAllow│ │ 501 │ │
│ └──────────┘ └───────────────┘ └───────┘ │
└──────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────┐
│ ABodyProcessor (built by Factory) │
│ Multipart │ UrlEncoded │ Raw │ Ignore │
└──────────────────────────────────────────┘
Design patterns actually in use (not just buzzwords):
- Strategy —
IMethodHandlerlets the server treat every request type uniformly. - Factory —
BodyProcessorFactory::createBodyProcessor(...)picks the right body parser from headers + location config. - Template Method / ABC —
ABodyProcessorshares common boilerplate; concrete classes override only what they need. - State machine —
ChunkedDecoderfor HTTP chunked bodies. - RAII — every
fd, everyCgiProcess, every dynamically allocated handler has a clear owner and a clear destruction point.
- Linux (the server uses
epolland POSIXfork/pipe/waitpid) g++/clang++with C++98 supportmake- (Optional, for CGI)
python3at/usr/bin/python3,php-cgiat/usr/bin/php-cgi
make # builds bin/webserv with -Wall -Wextra -Werror -std=c++98
make re # full rebuild
make clean # remove object files
make fclean # remove objects + binary./bin/webserv # uses config/default.conf
./bin/webserv config/av.conf # custom configStop with Ctrl+C — the SIGINT handler closes all sockets cleanly.
# Basic GET
curl -i http://localhost:8080/
# Upload via multipart/form-data
curl -i -F "file=@./somefile.txt" http://localhost:8080/upload
# Trigger CGI (Python)
curl -i http://localhost:8080/cgi-bin/hello.py
# Trigger a redirect
curl -i http://localhost:8080/oldpage
# Try to break it — header without ':' should return 400
printf "GET / HTTP/1.1\r\nHost: x\r\nBrokenHeader\r\n\r\n" | nc localhost 8080The parser accepts a familiar NGINX-style syntax — multiple server blocks, each with location blocks:
server {
listen localhost:8080;
server_name my_server;
error_page 404 errors/404.html;
error_page 403 errors/403.html;
client_max_body_size 105000000;
location / {
root www/html;
index index.html index.htm;
methods GET;
}
location /upload {
root www/uploads;
methods POST DELETE;
upload_store www/uploads;
}
location /files {
root www/files;
autoindex on;
methods GET;
}
location /oldpage {
redirect https://google.com;
}
location /cgi-bin {
root www/cgi-bin;
methods GET POST DELETE;
cgi .py /usr/bin/python3;
cgi .php /usr/bin/php-cgi;
}
location /secret {
methods NONE; # explicitly deny everything → 405
}
}
server {
listen 127.0.0.1:8082;
server_name another_server;
# ...another virtual host on a different port
}WebServer/
├── src/
│ ├── main.cpp
│ ├── core/ # Server / Client / ServerConfig / event loop
│ ├── config/ # NGINX-style config parser
│ ├── http/ # HttpRequest / HttpResponse / HttpStatus
│ ├── handlers/ # GET / POST / DELETE / CGI / Redirect / 405 / 501
│ ├── bodyProcessor/ # Multipart / Raw / UrlEncoded / Chunked / Factory
│ ├── abstracts/ # ABodyProcessor (template method base)
│ └── utils/ # ErrorPage, helpers
├── include/ # mirror of src/, public headers
├── config/ # *.conf files (default + per-test)
├── errors/ # custom 403 / 404 pages
├── tests/ # bash + pytest test suites
├── www/ # site root used by default.conf
├── .github/workflows/ # CI pipeline
└── Makefile
Two complementary test suites live under tests/:
1. Bash integration tests — fast smoke tests per HTTP method, runnable individually or via make test:
make test
# or, individually:
cd tests
bash get_test.sh
bash post_test.sh
bash delete_test.sh
bash redirect_test.sh
bash cgi_test.sh
bash permission_cgi_test.sh2. Pytest suite (tests/pytest/) — heavier scenarios: chunked uploads up to 50–100 MB, fragmented chunked POSTs, multipart with multiple files, status-code edge cases, idle-client behavior, CGI over chunked.
Both suites are wired into GitHub Actions (.github/workflows/webserv-tests.yml) and run on every push to main.
- One
epoll_eventloop, three kinds of fds. Listening sockets, client sockets, and CGI pipes all flow through the sameepoll_wait— there is no second event loop, no thread pool, noselectfallback. - CGI is fully async. The server
forks,execves the interpreter, sets the pipe ends non-blocking, registers them inepoll, and does not block waiting for the script to finish. Alast_activitytimestamp +CGI_TIMEOUTguard catches infinite loops without polling. - No memory leaks tolerated. Every
newhas a correspondingdelete; handlers are owned byClient, body processors by handlers, CGI processes by the server map.makebuilds with-Werror. - C++98 strictness. No
auto, no range-for, no<unordered_map>, no smart pointers, nostd::thread. The codebase reflects what disciplined pre-C++11 server code looks like. - Cross-method consistency. Methods that aren't enabled for a location go through
MethodNotAllowedHandlerinstead of anifchain — adding a new HTTP verb later is a matter of writing one class.
This is a 42 group project delivered as part of the webserv assignment. Code in this repository was written and is maintained by:
Built at 42 Rio.
Versão em Português 🇧🇷 (clique para expandir)
Servidor HTTP/1.1 não-bloqueante e orientado a eventos escrito do zero em C++98, inspirado no NGINX. Projeto do currículo da 42 (webserv).
- I/O 100% não-bloqueante com
epoll: um único loop cuida de sockets de escuta, conexões de clientes e pipes de CGI ao mesmo tempo. - Parser HTTP/1.1 feito à mão, com suporte a headers,
Content-Length,Transfer-Encoding: chunked, query string e detecção de requisições malformadas. - Decodificador
chunkedimplementado como máquina de estados explícita, lidando corretamente com chunks fragmentados em váriosread(). - Pipeline de body com
IBodyProcessor+ABodyProcessore estratégias concretas paramultipart/form-data,x-www-form-urlencoded, body raw e "ignorar", todas montadas por uma Factory. - Handlers por método (
GET,POST,DELETE,CGI,Redirect,405,501) implementam a mesma interfaceIMethodHandler— o servidor não precisa saber o que cada rota faz por baixo. - CGI (Python e PHP) executado via
fork+pipe+execve, registrado no mesmoepoll, com timeout de 30 s para matar scripts em loop infinito. - Timeout por cliente (60 s) evita conexões zumbis.
- Shutdown gracioso via
SIGINT— nada de fd vazado. - CI no GitHub Actions roda toda a suíte (bash + pytest) a cada push.
Múltiplos blocos server, múltiplas portas, blocos location com root, index, methods, autoindex, redirect, upload_store, cgi, e client_max_body_size. Veja config/default.conf.
make
./bin/webserv config/default.conf
# em outro terminal:
curl -i http://localhost:8080/make test # roda toda a suíte bash
cd tests/pytest && pytest # cenários pesados (chunked 50/100 MB, multipart, CGI)Projeto em equipe da 42 — código mantido por Yuri Fontenele (GitHub).