Skip to content

zone-01oujda/localserver

Repository files navigation

Server Proxy

A high-performance HTTP server written in Rust with support for CGI execution, file uploads, routing, session management, and dynamic configuration.

Features

  • Multi-server Support: Host multiple servers on different ports and addresses
  • Advanced Routing: Flexible route configuration with methods, redirects, and default files
  • CGI Execution: Execute server-side scripts (Python, shell, etc.) with configurable interpreters
  • File Upload Handling: Built-in file upload support with configurable upload directories and size limits
  • Session Management: Server-side session handling with configurable storage
  • Custom Error Pages: Configure error pages for different HTTP status codes
  • Auto-indexing: Optional directory listing
  • Cookie & Session Support: Automatic session and cookie management
  • Asynchronous I/O: Non-blocking networking using mio for high concurrency
  • Static File Serving: Efficient serving of static content with path handling
  • Request Validation: Configuration validation and client body size limits

Project Structure

├── src/
│   ├── bin/main.rs              # Entry point
│   ├── server.rs                # Main server loop and listener management
│   ├── router.rs                # Request routing logic
│   ├── http/                    # HTTP protocol handling
│   │   ├── http_connection.rs   # Connection management
│   │   ├── request.rs           # HTTP request parsing
│   │   └── response.rs          # HTTP response building
│   ├── config/                  # Configuration parsing and validation
│   ├── handlers/                # Request handlers
│   │   ├── get_handler.rs       # GET request handling
│   │   ├── post_handler.rs      # POST request handling
│   │   └── delete_handler.rs    # DELETE request handling
│   ├── cgi.rs                   # CGI execution
│   ├── upload.rs                # File upload handling
│   ├── utils/                   # Utilities
│   │   ├── session.rs           # Session management
│   │   ├── cookie.rs            # Cookie parsing
│   │   └── set_cookie.rs        # Cookie setting
│   └── lexer/                   # YAML parsing support
├── parser/                      # Custom YAML/config parser library
├── parser_derive/               # Macro for deriving parser traits
├── derive_yaml/                 # Macro for YAML deserialization
├── proxy_log/                   # Logging utilities
├── www/                         # Static web files
│   ├── index.html               # Main page
│   ├── upload.html              # Upload interface
│   ├── api/                     # API endpoints
│   ├── css/                     # Stylesheets
│   └── errors/                  # Custom error pages
├── cgis/                        # CGI scripts
│   └── auth/                    # Authentication scripts
├── tests/                       # Integration tests
└── config.yaml                  # Server configuration

Configuration

The server is configured via a YAML file (config.yaml). Key configuration options:

Server Block

servers:
  - host: "127.0.0.2"              # Listen address
    ports: [8080]                   # Listen ports
    server_name: "localhost"        # Server name
    root: "www"                     # Root directory for static files
    default_server: true            # Default server for this address
    client_max_body_size: 10000000000 # Max upload size in bytes
    error_pages:
      404: "./errors/404.html"      # Custom error pages
      405: "./errors/405.html"
    routes: [...]

Route Configuration

routes:
  - path: "/"                       # Route path
    methods: ["GET","POST","DELETE"] # Allowed HTTP methods
    root: "./www"                   # Document root for this route
    default_file: "index.html"      # Default file to serve
    autoindex: true                 # Enable directory listing
    upload_dir: "uploads"           # Directory for file uploads
    cgi_ext: ".py"                  # CGI script extension
    cgi_path: "/usr/bin/python3.10" # CGI interpreter path
    redirection: "https://google.com" # Redirect destination
    redirect_code: 302              # HTTP redirect code

Building and Running

Prerequisites

  • Rust 1.70+ (or 2024 edition)
  • Python 3.10+ (for CGI scripts)

Build

cargo build --release

Run

cargo run --release

The server will read config.yaml and start listening on the configured addresses and ports.

Usage Examples

Basic Configuration

Start a simple HTTP server on localhost:8080 serving files from www/:

servers:
  - host: "127.0.0.1"
    ports: [8080]
    server_name: "localhost"
    root: "www"
    routes:
      - path: "/"
        methods: ["GET", "POST"]
        default_file: "index.html"
        autoindex: true

CGI Execution

Execute Python scripts as CGI handlers:

routes:
  - path: "/cgi-bin"
    methods: ["GET", "POST"]
    cgi_ext: ".py"
    cgi_path: "/usr/bin/python3"
    upload_dir: "uploads"

File Uploads

Configure file upload endpoint:

routes:
  - path: "/upload"
    methods: ["POST", "GET"]
    upload_dir: "uploads"
    autoindex: false

Components

HTTP Server (server.rs)

Manages TCP listeners, client connections, and the main event loop using non-blocking I/O.

Router (router.rs)

Matches incoming requests to configured routes and determines how to handle them.

Request Handlers (handlers/)

  • get_handler.rs: Serves static files, handles directory listing
  • post_handler.rs: Processes form data and file uploads
  • delete_handler.rs: Handles DELETE requests

CGI Execution (cgi.rs)

Spawns CGI processes with proper environment variables and handles bidirectional I/O.

Configuration (config/)

YAML parsing and validation for server configuration.

Session Management (utils/session.rs)

Server-side session storage with automatic cleanup.

Testing

Run the test suite:

cargo test

Available tests:

  • Configuration parsing tests
  • Server feature tests
  • Chunked transfer encoding tests
  • Validation tests

Development

Scripts

  • expand.sh: Expand macros for debugging
  • push.zsh: Utility script (purpose varies)

License

MIT

Contributing

aelmir | houtayb | muboutoub

About

A lightweight, high-performance HTTP/1.1 compliant web server built from scratch in Rust. This project focuses on the fundamentals of networking, utilizing an event-driven, non-blocking I/O architecture to handle multiple concurrent connections within a single thread.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors