No description
  • C++ 98.2%
  • Makefile 1%
  • Shell 0.5%
  • HTML 0.2%
Find a file
kporceil 9d353d0980 Merge pull request 'dev' (#68) from dev into main
Reviewed-on: #68
Reviewed-by: kporceil <kporceil@student.42lyon.fr>
2026-03-03 12:59:12 +00:00
.forgejo/workflows ci: use --jobs for make to take full advantage of processor 2026-02-03 16:08:58 +01:00
docs docs: remove internal doc 2026-03-02 13:49:33 +01:00
hooks CI: git hook 2026-01-09 14:39:09 +01:00
includes feat: http redirection 2026-03-03 13:55:26 +01:00
lib style: format 2026-02-16 13:32:08 +01:00
site Merge stash 2026-02-23 16:13:41 +01:00
src Merge branch 'dev' of ssh://forgejo.asventi.net/webserv/webserv into feature/http-redirection 2026-03-03 13:55:57 +01:00
tests refactor: remove type directive who is useless since we have only one type of location 2026-03-02 13:51:14 +01:00
.clang-format add: clang-format 2026-01-12 12:25:51 +01:00
.gitignore feat: best path retrieve for request 2026-01-28 14:37:09 +01:00
CONTRIBUTING.md chore: update contribution rules 2026-01-09 14:41:43 +01:00
Makefile feat: SessionManager integration in ServerManager and test for SessionManager 2026-02-23 20:40:59 +01:00
README.md docs: remove internal doc and some modification to readme and config docs 2026-03-02 13:48:10 +01:00
webserv.conf feat: http redirection 2026-03-03 13:55:26 +01:00

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 epoll event 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++).
  • make build 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

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.