Skip to content

A lightweight HTTP/1.1 web server built in C++98 with CGI support, epoll-based I/O, and TOML configuration. Educational project demonstrating web server internals.

Notifications You must be signed in to change notification settings

WeismannS/WebServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

WebServer Project

A high-performance HTTP/1.1 web server written in C++98, implementing core web server functionality with support for multiple virtual hosts, CGI execution, and comprehensive error handling.

πŸš€ Features

Core Functionality

  • HTTP/1.1 Protocol Support: Full implementation of HTTP/1.1 specification
  • Multiple Virtual Hosts: Support for multiple server blocks with different configurations
  • Epoll-based I/O: High-performance event-driven architecture using Linux epoll
  • Connection Management: Automatic connection handling with timeout support
  • Request Parsing: Robust HTTP request parsing with header validation
  • Response Generation: Dynamic HTTP response generation with proper status codes

Advanced Features

  • CGI Support: Execute Python and PHP scripts with proper environment setup
  • Static File Serving: Serve static files with proper MIME type detection
  • Directory Listing: Auto-index functionality for directory browsing
  • Error Pages: Custom error pages for various HTTP status codes
  • Upload Support: File upload functionality with configurable limits
  • Redirection: HTTP redirection support
  • Request Methods: Support for GET, POST, DELETE HTTP methods

Configuration

  • TOML Configuration: Human-readable configuration file format
  • Flexible Routing: Configurable routes with method restrictions
  • Server Blocks: Multiple server configurations with different ports and hostnames
  • Error Page Mapping: Custom error pages for different status codes

πŸ“ Project Structure

webserv/
β”œβ”€β”€ main.cpp                     # Main entry point
β”œβ”€β”€ Makefile                     # Build configuration
β”œβ”€β”€ config_parser/               # Configuration parsing
β”‚   β”œβ”€β”€ Webserv.cpp             # Main configuration parser
β”‚   β”œβ”€β”€ server.cpp              # Server configuration handling
β”‚   β”œβ”€β”€ utilities/              # Parser utilities
β”‚   └── tests/                  # Parser tests
β”œβ”€β”€ HttpRequest/                 # HTTP request handling
β”‚   β”œβ”€β”€ HttpRequest.cpp         # Request object implementation
β”‚   β”œβ”€β”€ HttpRequest.hpp         # Request class definition
β”‚   β”œβ”€β”€ HttpRequestParser.cpp   # Request parsing logic
β”‚   └── HttpRequestParser.hpp   # Parser interface
β”œβ”€β”€ HttpResponse/                # HTTP response handling
β”‚   β”œβ”€β”€ HttpResponse.cpp        # Response object implementation
β”‚   β”œβ”€β”€ HttpResponse.hpp        # Response class definition
β”‚   β”œβ”€β”€ HttpResponseUtilities.cpp # Response utilities
β”‚   └── GetMethod.cpp           # GET method implementation
β”œβ”€β”€ Connection/                  # Connection management
β”‚   β”œβ”€β”€ Connection.cpp          # Connection handling
β”‚   └── Connection.hpp          # Connection class definition
β”œβ”€β”€ CGI/                        # CGI execution
β”‚   └── Cgi.cpp                 # CGI script execution
β”œβ”€β”€ SetupServer/                # Server setup
β”‚   β”œβ”€β”€ MultipSockets.cpp       # Multiple socket handling
β”‚   β”œβ”€β”€ StartServerSetup.cpp    # Server initialization
β”‚   └── includes.hpp            # Setup includes
β”œβ”€β”€ Includes/                   # Header files
β”œβ”€β”€ www/                        # Web root directory
β”‚   β”œβ”€β”€ index.html              # Default index page
β”‚   └── html/                   # Web content
β”‚       β”œβ”€β”€ pepe/               # Test content
β”‚       β”‚   β”œβ”€β”€ v1/             # Version 1 content
β”‚       β”‚   β”œβ”€β”€ script.py       # Python CGI script
β”‚       β”‚   └── script.php      # PHP CGI script
β”‚       └── ErrorPages/         # Custom error pages
└── nginx/                      # Nginx configuration examples

πŸ› οΈ Installation

Prerequisites

  • Linux operating system (for epoll support)
  • GCC compiler with C++98 support
  • Python 3 (for CGI scripts)
  • PHP (for CGI scripts)

Building the Project

  1. Clone the repository:

    git clone <repository-url>
    cd webserv
  2. Build the project:

    make
  3. Clean build artifacts:

    make clean
  4. Rebuild from scratch:

    make re

βš™οΈ Configuration

The webserver uses TOML format for configuration. Create a config.toml file in the project root:

Basic Configuration Example

# Global settings
default_max_body_size = "123"

# Server block
[[server]]
host = "127.0.0.1"
port = [9090, 9091, 8080]
server_name = ["127.0.0.1:8080"]
max_body_size = "23"

# Error pages
[[server.error_pages]]
301 = "301.html"
400 = "400.html"
403 = "403.html"
404 = "404.html"
405 = "405.html"
500 = "500.html"
505 = "505.html"

# Route configuration
[[server.route]]
path = "/"
methods = ["GET", "POST", "DELETE"]
autoindex = true
index = "index.html"
root = "./www/html/pepe/v1"
CGI_extensions = {py = "/usr/bin/python3", php = "/usr/bin/php"}
upload = "./www/html/uploads/"
redirection = "lmao"

Configuration Options

Option Description Default
host Server host address "127.0.0.1:port"
port Server port(s) [8080]
server_name Server name(s) []
max_body_size Maximum request body size "123"
path Route path "/"
methods Allowed HTTP methods ["GET"]
autoindex Enable directory listing false
index Default index file "index.html"
root Document root directory "./www"
CGI_extensions CGI script extensions and interpreters {}
upload Upload directory "./uploads"
redirection Redirect URL ""

πŸš€ Usage

Starting the Server

  1. With default configuration:

    ./webserv
  2. With custom configuration:

    ./webserv config.toml

Testing the Server

  1. Basic HTTP request:

    curl http://localhost:8080/
  2. CGI script execution:

    curl http://localhost:8080/script.py
    curl http://localhost:8080/script.php
  3. File upload (if configured):

    curl -X POST -F "file=@local_file.txt" http://localhost:8080/upload/
  4. Different HTTP methods:

    curl -X GET http://localhost:8080/
    curl -X POST http://localhost:8080/
    curl -X DELETE http://localhost:8080/

πŸ”§ Architecture

Core Components

  1. Main Loop (main.cpp):

    • Server initialization
    • Signal handling
    • Configuration loading
  2. Connection Manager (Connection/):

    • Client connection handling
    • Request/response lifecycle
    • Timeout management
  3. HTTP Parser (HttpRequest/):

    • Request line parsing
    • Header parsing
    • Body handling
  4. Response Generator (HttpResponse/):

    • Status code generation
    • Header construction
    • Body formatting
  5. CGI Executor (CGI/):

    • Script execution
    • Environment setup
    • Output handling

Event-Driven Architecture

The server uses Linux epoll for efficient I/O multiplexing:

// Epoll event loop
while (true) {
    int n = epoll_wait(epollfd, events, MAX_EPOLL_EVENT, timeout);
    for (int i = 0; i < n; i++) {
        if (events[i].events & EPOLLIN) {
            // Handle incoming data
        }
        if (events[i].events & EPOLLOUT) {
            // Handle outgoing data
        }
    }
}

πŸ§ͺ Testing

Manual Testing

  1. Basic Functionality:

    • Start the server
    • Access via browser: http://localhost:8080
    • Verify static file serving
  2. CGI Testing:

    • Access Python script: http://localhost:8080/script.py
    • Access PHP script: http://localhost:8080/script.php
    • Verify script execution
  3. Error Handling:

    • Access non-existent file: http://localhost:8080/notfound
    • Verify custom error pages
  4. Method Testing:

    • Test different HTTP methods
    • Verify method restrictions

Automated Testing

The project includes test scripts in the config_parser/tests/ directory for configuration parsing validation.

πŸ› Debugging

Common Issues

  1. Port Already in Use:

    # Check if port is in use
    netstat -tlnp | grep :8080
    
    # Kill process using port
    sudo fuser -k 8080/tcp
  2. Permission Issues:

    # Make sure CGI scripts are executable
    chmod +x www/html/pepe/script.py
    chmod +x www/html/pepe/script.php
  3. Configuration Errors:

    • Check TOML syntax
    • Verify file paths exist
    • Ensure proper indentation

Debug Output

The server includes debug output for:

  • Request parsing
  • Configuration loading
  • Connection handling
  • CGI execution

πŸ“š API Reference

HTTP Methods Supported

  • GET: Retrieve resources
  • POST: Submit data
  • DELETE: Remove resources

Status Codes

  • 200: OK
  • 301: Moved Permanently
  • 400: Bad Request
  • 403: Forbidden
  • 404: Not Found
  • 405: Method Not Allowed
  • 408: Request Timeout
  • 413: Payload Too Large
  • 500: Internal Server Error
  • 505: HTTP Version Not Supported

CGI Environment Variables

  • REQUEST_METHOD
  • QUERY_STRING
  • CONTENT_LENGTH
  • CONTENT_TYPE
  • SCRIPT_NAME
  • PATH_INFO
  • HTTP_* headers

πŸ“„ License

This project is part of the 42 school curriculum and follows the 42 coding standards.

πŸ‘₯ Authors

  • Saad ERRAOUI - Initial work (2024-12-22)
  • BOUZID Hicham - Connection management and event handling
  • Weismann - CGI, TOML Parser, Internal Logic and improvements

πŸ™ Acknowledgments

  • 42 school for the project requirements
  • Nginx for configuration inspiration
  • Linux epoll documentation
  • HTTP/1.1 RFC 2616 specification

Note: This webserver is designed for educational purposes and follows the 42 school coding standards. It implements core web server functionality while maintaining clean, readable code structure. This was made PURELY for the sake of learning about the internal work of servers, and not made for production

About

A lightweight HTTP/1.1 web server built in C++98 with CGI support, epoll-based I/O, and TOML configuration. Educational project demonstrating web server internals.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages