C++ & Networking
Webserv: HTTP/1.1 Web Server in C++98
An HTTP/1.1 web server written from scratch in C++98, with NGINX-style configuration, virtual hosts, CGI, uploads, and non-blocking I/O.
Last updated:

Introduction
Webserv is an HTTP/1.1 server written from scratch in C++98 by a team of three at 42 and inspired by NGINX. It serves static sites, handles GET, POST, and DELETE, accepts file uploads, runs PHP, Python, and Perl scripts through CGI, and is driven by an NGINX-style configuration file.
Building a server at the protocol level meant handling everything a framework normally hides: sockets, the event loop, request parsing, chunked bodies, routing, status codes, timeouts, and process management for CGI.
The problem
A web server has to serve many clients at once from a single process without ever blocking on one slow connection, and it has to interpret a loosely structured text protocol correctly, including partial reads, chunked bodies, size limits, and malformed requests.
It also has to be configurable: several sites on different addresses and ports, each with its own root, allowed methods, error pages, upload directory, redirects, and CGI interpreters, all validated before the server starts.
Architecture
The code is split into four modules. config tokenizes and validates the configuration file and builds Server and Location objects; server creates the listening sockets and runs the select() event loop; Request_Response parses requests and builds responses, including directory listings and error pages; and CGI runs scripts in a child process.
Every request follows the same path: accept the connection, read until the request is complete, decode chunked bodies, choose the virtual server from the address, port, and Host header, match the location, then serve a file or directory, store an upload, delete a resource, or run CGI. The connection is then kept alive or closed.
Implementation decisions
- A single select() event loop
- All listening and client sockets are non-blocking and multiplexed with one select() call, so a slow client never stalls the others and the server needs no threads.
- An NGINX-like configuration model
- Directives shared by server and location blocks, such as root, index, autoindex, allowed methods, body size, error pages, logs, upload directory, and CGI, live in a common base class, so locations inherit server defaults and override only what they need.
- Strict validation at startup
- The parser rejects unknown directives, duplicate listen addresses, duplicate locations, and invalid values with explicit errors, so a bad configuration fails immediately instead of at request time.
- CGI with a hard timeout
- Scripts run in a forked child with a CGI/1.1 environment. The server waits for the child without blocking, kills it after five seconds, and returns 504 Gateway Timeout, or 500 if execution fails.
- Explicit limits
- Configurable body size and URI length limits, together with a keep-alive timeout, protect the server from oversized or idle requests.
Technical challenges
- Reading requests that arrive in arbitrary pieces and knowing when the body is complete, with both Content-Length and chunked transfer encoding
- Choosing the right virtual server and location when several servers share ports and paths overlap
- Running CGI scripts without blocking the event loop and recovering from scripts that hang or crash
- Returning correct status codes and headers for redirects, missing resources, conflicts, oversized bodies, and disallowed methods
Results
- Serves several virtual hosts on different addresses and ports from a single process
- Supports GET, POST, and DELETE, file uploads, directory listings, redirects, custom error pages, and cookies
- Runs PHP, Python, and Perl CGI scripts with timeout protection
- Stays responsive under concurrent connections thanks to non-blocking sockets and keep-alive handling
Related case studies
Containers & Infrastructure
Inception: Dockerized NGINX, WordPress & MariaDB stack
A three-container web stack, NGINX with TLS, WordPress on PHP-FPM, and MariaDB, built from Debian images with Docker Compose and persistent volumes.
Unix & Systems Programming
Minishell: Bash-like Unix shell in C
A Bash-like Unix shell in C with pipes, redirections, here-docs, variable expansion, built-ins, && and || operators, subshells, and wildcards.
Kubernetes & GitOps
Inception of Things: K3s & Argo CD GitOps lab
A Kubernetes and GitOps lab: K3s clusters provisioned with Vagrant, hostname-based ingress routing, and continuous delivery with K3d and Argo CD.