Skip to content

Latest commit

 

History

91 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌐 Webserv - HTTP/1.0 Server Implementation

A high-performance HTTP/1.0 web server written in C++98 from scratch, featuring non-blocking I/O with poll, CGI support, and RFC 1945 compliance.

Language Status Tests Performance

📋 Table of Contents

✨ Features

Core Functionality

  • HTTP/1.0 Protocol - Full RFC 1945 compliance
  • Multiple HTTP Methods - GET, POST, DELETE
  • Non-Blocking I/O - Using poll for multiplexing
  • Virtual Hosts - Multiple servers on different ports
  • CGI Execution - Python, PHP, and other script support
  • File Uploads - Multipart/form-data handling
  • Static File Serving - With proper MIME type detection

Advanced Features

  • 🔒 Security - Path traversal protection
  • 📁 Directory Listing - Configurable autoindex
  • 🎯 Custom Routes - Flexible location-based configuration
  • ⚙️ Configurable - Nginx-style configuration file
  • 🚦 Error Pages - Custom 403, 404, 50x pages
  • 📊 Performance - 4,400+ requests/second with 100 concurrent users
  • 💾 Zero Memory Leaks - Verified with valgrind

🚀 Quick Start

# Clone the repository
git clone https://github.com/dragon-piece/Webserv.git
cd Webserv

# Compile
make

# Run with default configuration
./webserv test.conf

# Access the server
open http://localhost:8080

📦 Installation

Prerequisites

  • C++ Compiler with C++98 support (g++ or clang++)
  • Make build system
  • Python 3 (for CGI and tests)

Build

make                 # Compile the server
make clean           # Remove object files
make fclean          # Remove all build artifacts
make re              # Rebuild from scratch

⚙️ Configuration

Configuration uses an Nginx-style syntax. Here's a minimal example:

http {
    server {
        listen 8080;
        server_name localhost;
        root ./server_files;
        index index.html;
        
        client_max_body_size 10M;
        
        location / {
            allowed_methods GET POST;
            autoindex on;
        }
        
        location /upload {
            allowed_methods POST DELETE;
            upload_path ./server_files/uploads;
        }
        
        location /cgi {
            allowed_methods GET POST;
            cgi_extension .py;
            cgi_path /usr/bin/python3;
        }
        
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
    }
}

Configuration Directives

Directive Context Description
listen server Port and host to bind to
server_name server Virtual host names
root server, location Document root directory
index server, location Default index files
autoindex server, location Enable directory listing
allowed_methods location Allowed HTTP methods
client_max_body_size server, location Maximum request body size
upload_path location Directory for file uploads
cgi_extension location CGI file extension
cgi_path location Path to CGI interpreter
error_page server, location Custom error pages

See SERVER_SETUP.md for complete configuration examples.

🧪 Testing

Comprehensive Test Suite

The project includes a full test suite with 7 categories and 55+ individual tests:

cd tests
python3 run_all_tests.py

Test Categories

  1. HTTP Methods - GET, POST, DELETE validation
  2. Static Files - File serving and MIME types
  3. File Upload - Multipart form data handling
  4. CGI Execution - Script execution and environment
  5. Error Handling - HTTP error codes (403, 404, 500, etc.)
  6. Configuration - Multi-port, routes, limits
  7. Stress Testing - Concurrent connections, load testing

Interactive Testing

cd tests
python3 menu.py      # Interactive test menu

Manual Testing

# Test GET request
curl http://localhost:8080/

# Test file upload
curl -X POST -F "file=@test.txt" http://localhost:8080/upload

# Test CGI
curl http://localhost:8080/cgi/hello.py

# Test DELETE
curl -X DELETE http://localhost:8080/uploads/test.txt

📊 Performance

Benchmarks (siege -c 100 -t 30s)

  • Throughput: 4,416 requests/second
  • Concurrency: 100 simultaneous users (99.56 average)
  • Latency: 0.02s average response time
  • Availability: 100% (no failed requests)
  • Transactions: 121,939 successful in 27.61 seconds
  • Memory: Zero leaks (valgrind verified)
  • Stability: No crashes under load

Stress Test Results

Siege Benchmark:
Transactions:          121,939 hits
Availability:          100.00 %
Elapsed time:          27.61 secs
Data transferred:      122.13 MB
Response time:         0.02 secs
Transaction rate:      4,416.48 trans/sec
Throughput:            4.42 MB/sec
Concurrency:           99.56
Successful:            121,939
Failed:                0

🏗️ Architecture

Core Components

webserv/
├── config/              # Configuration parser
│   ├── ConfigParser.cpp # Nginx-style config parser
│   ├── ServerConfig.cpp # Server configuration
│   └── Tokenizer.cpp    # Lexical analyzer
├── src/                 # Core server implementation
│   ├── main.cpp         # Entry point and event loop
│   ├── handleClient.cpp # Request/response handling
│   └── classes/         # Request, Response, Client classes
├── server_files/        # Web content and assets
└── tests/               # Comprehensive test suite

Design Patterns

  • Event-Driven - Non-blocking I/O with poll multiplexing
  • State Machine - Request parsing and processing
  • Strategy Pattern - Different handlers for HTTP methods
  • Factory Pattern - Response generation

Request Processing Flow

Client Connection
    ↓
Accept & Add to poll
    ↓
Read Request (non-blocking)
    ↓
Parse HTTP Request
    ↓
Route Matching
    ↓
[Static File | CGI | Upload | Error]
    ↓
Generate Response
    ↓
Send Response (non-blocking)
    ↓
Close

Web Documentation

Once the server is running, visit:

💡 Examples

Example 1: Simple Static Server

http {
    server {
        listen 8080;
        root ./www;
        index index.html;
        
        location / {
            allowed_methods GET;
            autoindex on;
        }
    }
}

Example 2: Upload Server

http {
    server {
        listen 8080;
        root ./server_files;
        client_max_body_size 10M;
        
        location /upload {
            allowed_methods POST DELETE;
            upload_path ./uploads;
        }
        
        location /uploads {
            allowed_methods GET DELETE;
            autoindex on;
        }
    }
}

Example 3: CGI Server

http {
    server {
        listen 8080;
        root ./server_files;
        
        location /cgi {
            allowed_methods GET POST;
            cgi_extension .py;
            cgi_path /usr/bin/python3;
        }
    }
}

Example 4: Multiple Hosts

http {
    # Main server
    server {
        listen 8080;
        server_name www.example.com;
        root ./site1;
    }
    
    # Alternative server
    server {
        listen 8081;
        server_name api.example.com;
        root ./site2;
    }
}

🔒 Security Features

  • Path Traversal Protection - Blocks .. in URIs
  • Method Restrictions - Per-location HTTP method filtering
  • Upload Size Limits - Configurable max body size
  • Error Handling - No information leakage in errors
  • Input Validation - Strict HTTP parsing

Built using C++98 | 4,400+ req/s | Zero memory leaks | 100% test coverage

About

Our own HTTP server

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages