- C++ 98.2%
- Makefile 1%
- Shell 0.5%
- HTML 0.2%
|
|
||
|---|---|---|
| .forgejo/workflows | ||
| docs | ||
| hooks | ||
| includes | ||
| lib | ||
| site | ||
| src | ||
| tests | ||
| .clang-format | ||
| .gitignore | ||
| CONTRIBUTING.md | ||
| Makefile | ||
| README.md | ||
| webserv.conf | ||
This project has been created as part of the 42 curriculum by pjarnac, kporceil.
Webserv
Description
Webserv is an HTTP/1.0 web server with some features of 1.1 written in C++98, developed as part of the 42 school curriculum. The goal of this project is to implement a fully functional web server from scratch, capable of serving static websites, handling file uploads, executing CGI scripts, and managing multiple servers through a configuration file inspired by NGINX.
The server uses a single
epoll instance for all I/O multiplexing, ensuring non-blocking operation across all client connections, listener sockets, and CGI pipes. It supports the
GET, POST, and DELETE HTTP methods.
Key Features
- Non-blocking I/O — All socket and pipe operations are driven by a single
epollevent loop. - Static file serving — Serve HTML, CSS, JS, images, and other static content.
- Directory listing — Optional autoindex for browsing directory contents.
- File uploads — Clients can upload files to configurable storage locations.
- CGI execution — Execute scripts (e.g., Python) based on file extension, with full environment variable support.
- Session management — Cookie-based session tracking with configurable timeouts (bonus).
- Multiple servers — Listen on multiple interface:port pairs, each with independent configuration.
- Custom error pages — Define custom error pages per server.
- NGINX-inspired configuration — Familiar syntax with server blocks, location blocks, and directives.
Instructions
Prerequisites
- A Linux operating system (the server uses
epoll). - A C++98 compatible compiler (
c++/g++/clang++). makebuild tool.
Compilation
# Clone the repository
git clone https://forgejo.asventi.net/webserv/webserv.git
cd webserv
# Build the server
make
The Makefile supports the following targets:
| Target | Description |
|---|---|
all |
Build the server (default) |
clean |
Remove object files |
fclean |
Remove object files and the binary |
re |
Full rebuild |
debug |
Build with debug symbols |
fsanitize |
Build with AddressSanitizer |
test |
Build and run the test suite |
Running
# Run with a configuration file
./webserv webserv.conf
# Run with the default configuration
./webserv
Configuration
The server is configured through a configuration file with NGINX-inspired syntax. See docs/CONFIG_FORMAT.md for the complete reference.
A minimal configuration example:
server {
listen 0.0.0.0:8080;
location / {
root ./site;
autoindex on;
allow_methods GET POST DELETE;
}
}
Main Directives
| Directive | Context | Description |
|---|---|---|
listen |
server | Interface and port to listen on (required) |
server_name |
server | Virtual host name(s) |
root |
server, location | Document root directory |
client_max_body_size |
server, location | Maximum request body size (e.g., 10M) |
error_page |
server | Custom error page for HTTP status codes |
autoindex |
location | Enable/disable directory listing |
allow_methods |
location | Allowed HTTP methods (GET, POST, DELETE) |
index |
location | Default file for directory requests |
cgi_pass |
location | CGI interpreter mapping (e.g., .py /usr/bin/python3) |
enable_upload |
location | Enable file upload support |
upload_dir |
location | Directory for uploaded files |
enable_session |
location | Enable cookie-based session management |
Testing
# Run the unit test suite
make test
./webserv
# Or test manually with curl
curl -v http://localhost:8080/
curl -X POST -F "file=@myfile.txt" http://localhost:8080/test
curl -X DELETE http://localhost:8080/test/myfile.txt
The project can also be tested with any standard web browser by navigating to the configured address (e.g.,
http://localhost:8080).
Architecture
The project is organized into the following modules:
src/
├── main.cpp # Entry point
├── ServerManager.cpp # Main event loop and server orchestration
├── http/ # HTTP request/response handling
│ ├── ARequest.cpp # Abstract request base
│ ├── Get.cpp # GET method handler
│ ├── Post.cpp # POST method handler
│ ├── Delete.cpp # DELETE method handler
│ ├── Response.cpp # HTTP response builder
│ ├── RequestFactory.cpp # Request creation factory
│ ├── CGIResponseFactory.cpp # CGI response handling
│ ├── RequestValidator.cpp # Request validation
│ └── SessionManager.cpp # Cookie/session management
├── io/ # I/O abstraction layer
│ ├── EpollManager.cpp # epoll wrapper
│ ├── AIOFile.cpp # Abstract I/O file descriptor
│ ├── IOSocket.cpp # Socket I/O wrapper
│ └── CGISocket.cpp # CGI pipe I/O wrapper
├── network/ # Network socket management
│ ├── ANetworkSocket.cpp # Abstract network socket
│ ├── ListenerSocket.cpp # Server listener socket
│ └── ClientSocket.cpp # Client connection socket
├── parsing/ # Configuration file parsing
│ ├── ConfigParser.cpp # Orchestrator
│ ├── Tokenizer.cpp # Lexical analysis
│ ├── Parser.cpp # Syntactic analysis (AST)
│ └── validators/ # Semantic validation
├── server/
│ └── Server.cpp # Server instance (binds config to sockets)
├── locations/
│ └── Location.cpp # Route/location configuration
└── logger/
└── Logger.cpp # Logging utility
For details on the configuration parsing system, see docs/PARSING_ARCHITECTURE.md.
Resources
References
- RFC 2616 — HTTP/1.1 — The HTTP/1.1 specification.
- RFC 1945 — HTTP/1.0 — The HTTP/1.0 specification.
- RFC 3875 — The Common Gateway Interface (CGI) Version 1.1 — CGI specification.
- NGINX Documentation — Used as a reference for configuration syntax and HTTP behaviour.
- Beej's Guide to Network Programming — Socket programming fundamentals.
- The Linux
epollman page — epoll I/O event notification reference.
AI Usage
AI tools (GitHub Copilot, ChatGPT) were used during this project for the following tasks:
- Boilerplate reduction — Generating repetitive code patterns (e.g., getters/setters, canonical form implementations).
- Documentation — Assisting with the writing and formatting of documentation files (CONFIG_FORMAT.md, README.md).
All AI-generated content was reviewed, tested, before being integrated into the project.