A production-like HTTP/1.1 server built from scratch in C++ — handling multiple connections, CGI execution, and static file serving.
"This project taught me how web servers actually work — how HTTP requests are parsed byte by byte, how non-blocking I/O with select/poll allows a single process to handle thousands of simultaneous connections, and what CGI really is. After this project, concepts like Nginx config files, reverse proxies, and connection pooling stopped being magic — they became engineering decisions I now understand deeply."
This is one of the most demanding projects at 42. Building a functioning HTTP server from scratch — without any networking library — demonstrates a real understanding of the protocol and OS-level I/O that powers the entire web.
An HTTP/1.1 server supporting:
GET,POST,DELETEmethods- Static file serving with correct MIME types
- Custom error pages (400, 403, 404, 405, 500...)
- Chunked transfer encoding
- Persistent connections (
keep-alive)
- NGINX-inspired configuration file parser
- Multiple virtual servers (different ports/host)
- Per-route configuration: allowed methods, redirects, root, index, autoindex
- Upload directory configuration
- Max body size limit
- Full CGI/1.1 execution — runs Python, PHP, or any script
- Environment variables set per the CGI spec
- POST body forwarded to CGI stdin
- CGI stdout returned as HTTP response
- Non-blocking I/O with
select() - Multiple simultaneous client connections
- Graceful timeout and connection close
The server is structured around a clear three-layer model: Webserv (event loop + socket management), Connection (per-client state machine), and Client + Request/Response (HTTP parsing and generation). This separation of concerns — where each class has a single, well-defined responsibility — is the same architecture pattern used by nginx and most production HTTP servers. It demonstrates mature software design thinking in C++.
git clone https://github.com/gustavofsousa/webserv_42.git
cd webserv_42
make./webserv config/default.confOpen your browser at http://localhost:8080
server {
listen 8080;
server_name localhost;
root ./static_pages;
index index.html;
location / {
allowed_methods GET POST;
autoindex on;
}
location /upload {
allowed_methods POST;
upload_dir ./uploads;
}
location /cgi-bin {
allowed_methods GET POST;
cgi_extension .py;
}
}# GET request
curl http://localhost:8080/
# POST request
curl -X POST -d "name=gustavo" http://localhost:8080/form
# DELETE request
curl -X DELETE http://localhost:8080/file.txtwebserv_42/
├── source/
│ ├── main.cpp
│ ├── core/
│ │ ├── Webserv.cpp # Event loop (select), socket management
│ │ ├── Server.cpp # Virtual server + config binding
│ │ └── Connection.cpp # Per-client connection state
│ ├── http/
│ │ ├── Request.cpp # HTTP request parser
│ │ ├── Response.cpp # HTTP response builder
│ │ ├── Client.cpp # Client state machine
│ │ ├── CGI.cpp # CGI execution
│ │ └── cgi/ # CGI helper scripts
│ └── config/ # Config file parser
├── static_pages/ # Static HTML/CSS files
└── index.html
- HTTP/1.1 protocol implementation (parsing, headers, status codes)
- Non-blocking I/O with
select()system call - TCP socket programming (
socket,bind,listen,accept) - CGI/1.1 specification implementation
- NGINX-style configuration parsing
- Multi-client connection management
- Object-oriented design patterns in C++ (state machine, layered architecture)
This project was developed as part of the 42 School curriculum.
Made with ☕ at 42 Rio de Janeiro