This project has been created as part of the 42 curriculum by yingzhan, tthajan.
webserv is a fully compliant HTTP/1.1 web server written in C++98, built from scratch as part of the 42 school curriculum. The goal is to deepen understanding of how web servers work at the network level — from raw TCP socket management to HTTP request parsing, response building, and CGI execution.
The server is designed around a non-blocking I/O event loop using poll(), enabling it to handle multiple simultaneous client connections without threads. It supports virtual hosting (multiple server {} blocks), configurable routes, file uploads, directory listing, custom error pages, HTTP redirects, and a multi-type CGI interface.
| Feature | Details |
|---|---|
| HTTP/1.1 | GET, POST, DELETE methods; persistent connections |
| Non-blocking I/O | Single-thread poll()-based event loop |
| Virtual hosts | Multiple server {} blocks, matched by port and Host header |
| Static files | Serve files, directory autoindex, configurable root |
| File uploads | POST to upload, DELETE to remove, per-route body size limits |
| Custom error pages | Per-server error_page directives (400, 403, 404, 405, 413, 500, 501, 504) |
| Redirects | 301/302/307 via return directive |
| CGI | Supports .py, .sh, .php, .pl — extensible via config |
| Chunked transfer | Chunked request body decoding |
| Config file | NGINX-inspired syntax with full validation |
- Linux / macOS (
poll()— POSIX-compliant) g++/c++with C++98 supportpython3(for.pyCGI scripts and tests)php-cgi(for.phpCGI support, optional)perl(for.plCGI support, optional)
makeProduces the ./webserv binary. Use make clean / make fclean / make re as usual.
./webserv config/webserv.confThe included config/webserv.conf starts two virtual hosts:
| Host | Port | Notes |
|---|---|---|
localhost |
8080 | Main site — 10 MB body limit, full CGI, uploads, redirects |
api.localhost |
8081 | Minimal API — 512 KB body limit |
server {
listen 8080;
server_name localhost;
client_max_body_size 10M;
error_page 404 ./www/errors/404.html;
location / {
methods GET;
root ./www;
index index.html;
autoindex off;
}
location /cgi-bin/ {
methods GET POST;
root ./www/cgi-bin;
cgi .py /usr/bin/python3;
cgi .php /usr/bin/php-cgi;
}
location /old-page {
return 301 /index.html;
}
}# Full integration test suite (error pages, stress, scalability, redirects, CGI)
bash tests/full_test.sh
# CGI-focused unit test suite
python3 tests/cgi_test.pySee tests/TEST_GUIDE.md for a detailed description of every test case.
- RFC 7230 — HTTP/1.1 Message Syntax
- RFC 3875 — The CGI/1.1 Specification
- Beej's Guide to Network Programming
- poll(2) Linux man page
- MDN HTTP documentation
AI tools (GitHub Copilot / ChatGPT) were used in this project for the following tasks:
- Understanding Concepts: Explaining network I/O, CGI environment variables, and HTTP protocol details.
- Debugging: Identifying misconfigurations and tracing logic errors in CGI handling.
- Test Scripting: Assisting in writing test scripts and fixing false-positive test cases.
- Documentation: Helping to draft and structure this README.
All AI-generated code and suggestions were reviewed, tested, and understood before being included in the project.