Webserv is a fully functional HTTP/1.1 web server written in C++ 98. It is designed to be non-blocking and event-driven, utilizing epoll for I/O multiplexing to handle multiple client connections simultaneously without the overhead of threads.
This project was built to understand the intricacies of the HTTP protocol, socket programming, and low-level server architecture. It mimics the behavior of industry-standard servers like NGINX.
- Non-Blocking I/O: Uses a single-threaded event loop (
epoll) to manage connections efficiently. - State Machine Parsing: Robust request parsing that handles partial data and keeps connections alive.
- Multiplexing: simultaneous handling of read/write events across multiple sockets.
- Methods: Full support for
GET,POST, andDELETE. - Static File Serving: Serves HTML, CSS, JS, images, and large binary files.
- File Uploads: Handles
multipart/form-dataand chunked transfer encoding for large file uploads. - Directory Listing: Auto-generates HTML directory listings when
autoindexis enabled. - Redirections: Supports HTTP 3xx redirections.
- Session Management: Cookies and Session ID support (if implemented).
- Nginx-style Config: Custom configuration parser supporting:
- Multiple Server Blocks (Virtual Hosting).
- Port & Host binding.
- Custom Error Pages (404, 500, etc.).
- Client Body Size Limits.
- Route-specific rules (
limit_except,root,return,cgi_pass).
The server follows a React pattern architecture:
- Parsing Phase: The configuration file is tokenized and parsed into a virtual server hierarchy.
- Setup Phase: Sockets are created, bound to ports, and set to non-blocking mode.
- Event Loop: The main loop waits for events via
epoll_wait.- New Connections: Accepted and added to the monitored set.
- Read Events: Data is read into a buffer and passed to the HTTP Parser.
- Write Events: Responses are constructed and sent back in non-blocking chunks.
The configuration file uses a block-based syntax similar to NGINX:
server {
listen 8080;
server_name localhost;
root ./www;
client_max_body_size 1000000; # 1MB Limit
error_page 404 /errors/404.html;
location / {
limit_except GET POST;
autoindex on;
}
location /uploads {
limit_except GET POST DELETE;
root ./www/uploads;
upload_store ./www/uploads;
}
location /old-page {
return 301 [https://google.com](https://google.com);
}
location *.php {
cgi_pass /usr/bin/php-cgi;
}
}We have included comprehensive shell scripts to stress-test the server's capabilities:
test_get.sh: Validates file serving, 404 handling, and autoindexing.
test_post.sh: Tests multipart uploads, large payloads (413), and chunked encoding.
test_delete.sh: Verifies resource deletion and permission handling.
test_stress.sh: Simulates high load and boundary conditions.
Run a test suite:
chmod +x tests/test_post.sh
./tests/test_post.shThe project includes a built-in Control Panel (index.html) to interact with the server visually:
Real-time File Uploads.
Gallery View.
One-click File Deletion.
Animation Demos (Canvas API).
Mouaffak Mehdi - Core Architecture, Parsing, Routing
Faure Lucien - CGI Implementation, Signals, Event Loop
Matayi Dianka - Config Parsing, CGI Setup, Refactoring
Inspired by NGINX.
Built as part of the 42 Curriculum.