A lightweight, high-performance HTTP web server implemented in C++98, designed to handle multiple concurrent connections using an event-driven architecture with poll().
- HTTP Methods Support: GET, POST, DELETE
- CGI Support: Execute Python (.py) and PHP (.php) scripts
- Chunked Transfer Encoding: Efficient handling of large data transfers
- File Uploads: Support for multipart/form-data uploads with configurable temporary paths
- Autoindex: Automatic directory listing when enabled
- Configuration File: Nginx-style configuration syntax
- Multiple Servers: Support for multiple server blocks on different ports
- Client Body Size Limits: Configurable maximum request body size
- Timeout Handling: Connection timeout configuration
- Cookies Support: Session management and cookie handling
- Error Pages: Custom error responses
- C++98 compatible compiler (tested with g++)
- POSIX compliant system (Linux/macOS)
- Standard C++ libraries
-
Clone the repository:
git clone <repository-url> cd webserv
-
Build the project:
make
-
The executable
webservwill be created in the current directory.
Run the web server with a configuration file:
./webserv [configuration_file]If no configuration file is provided, it defaults to webserv.conf.
./webserv webserv.confThe server will start listening on the configured ports (default: 8080).
The server uses a configuration file similar to Nginx. The default configuration file is webserv.conf.
server {
root website;
host localhost;
index index.html;
listen 8080;
timeout 1;
autoindex on;
client_max_body_size 100m;
location / {
index index.html;
allowed_methods GET POST DELETE;
autoindex on;
root ./website;
cgi_pass .py;
cgi_pass .php;
client_body_temp_path ./website/uploads;
}
}root: Root directory for serving fileshost: Server hostnameindex: Default index file(s)listen: Port to listen ontimeout: Connection timeout in secondsautoindex: Enable/disable directory listing (on/off)client_max_body_size: Maximum request body sizeallowed_methods: Permitted HTTP methods for the locationcgi_pass: File extensions to pass to CGIclient_body_temp_path: Temporary path for file uploads
.
├── main.cpp # Main entry point
├── Makefile # Build configuration
├── webserv.conf # Default configuration file
├── includes/ # Header files
│ ├── Webserv.hpp
│ ├── Server.hpp
│ ├── Client.hpp
│ ├── Request.hpp
│ ├── Respond.hpp
│ └── ...
└── src/ # Source files
├── RequestRespond/ # Request/response handling
└── server/ # Server implementation
- Create a
websitedirectory with your HTML files - Configure the server in
webserv.conf - Run the server:
./webserv - Access via browser:
http://localhost:8080
The server supports CGI for Python and PHP scripts. Configure CGI extensions in your location blocks:
location /cgi-bin/ {
cgi_pass .py;
cgi_pass .php;
}File uploads are supported with configurable temporary paths:
client_body_temp_path ./website/uploads;The server provides appropriate HTTP status codes and error messages for various scenarios including:
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 413 Request Entity Too Large
- 500 Internal Server Error
- Event-Driven: Uses
poll()for handling multiple connections efficiently - Modular Design: Separate modules for request parsing, response generation, and server management
- Configuration Parser: Custom parser for the configuration file syntax
- Memory Management: Careful memory handling to prevent leaks