Skip to content

HeartThanakorn/webserv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by yingzhan, tthajan.

webserv

Description

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.

Key Features

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

Instructions

Requirements

  • Linux / macOS (poll() — POSIX-compliant)
  • g++ / c++ with C++98 support
  • python3 (for .py CGI scripts and tests)
  • php-cgi (for .php CGI support, optional)
  • perl (for .pl CGI support, optional)

Compilation

make

Produces the ./webserv binary. Use make clean / make fclean / make re as usual.

Running the Server

./webserv config/webserv.conf

The 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

Configuration File Format

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;
    }
}

Running the Tests

# Full integration test suite (error pages, stress, scalability, redirects, CGI)
bash tests/full_test.sh

# CGI-focused unit test suite
python3 tests/cgi_test.py

See tests/TEST_GUIDE.md for a detailed description of every test case.


Resources

HTTP & Networking

CGI References

AI Usage

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.

About

A fully compliant HTTP/1.1 web server built from scratch in C++98. Implements non-blocking I/O with poll(), supporting GET/POST/DELETE methods, CGI execution, file uploads, and multiple virtual hosts.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors