Skip to content

Antinna/rust-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust HTTP Server

A comprehensive HTTP server implementation built from scratch in Rust, supporting HTTP/1.0, HTTP/1.1, HTTP/2, and HTTP/3.

Features

  • Complete HTTP Methods Support: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH, WebDAV methods, and custom methods
  • Multi-Version HTTP Support: HTTP/1.0, HTTP/1.1, HTTP/2, HTTP/3
  • Comprehensive Status Codes: All standard HTTP status codes with reason phrases
  • Advanced Request/Response Handling: Headers, body parsing, content types, CORS, cookies
  • Robust Router: Pattern matching, middleware support, route parameters
  • TCP Server: Multi-threaded connection handling with timeouts
  • HTTP Parser: Complete request parsing with chunked encoding support
  • Utilities: MIME type detection, date/time formatting, encoding helpers

Project Structure

src/
├── lib.rs                     # Main library with environment config
├── main.rs                    # Application entry point with example routes
├── http/                      # HTTP protocol implementation
│   ├── mod.rs                 # HTTP module exports
│   ├── method.rs              # HTTP methods enum (GET, POST, etc.)
│   ├── version.rs             # HTTP versions (1.0, 1.1, 2, 3)
│   ├── status.rs              # HTTP status codes (200, 404, etc.)
│   ├── request.rs             # Request structure and methods
│   ├── response.rs            # Response structure and methods
│   └── router.rs              # Router implementation
├── server/                    # Server implementation
│   ├── mod.rs                 # Server module exports
│   ├── tcp_server.rs          # TCP server with connection handling
│   ├── http_server.rs         # HTTP server built on TCP server
│   ├── connection.rs          # Connection management and I/O
│   └── listener.rs            # Server listener utilities
├── parsing/                   # HTTP parsing utilities
│   ├── mod.rs                 # Parsing module exports
│   ├── http_parser.rs         # Complete HTTP request parser
│   ├── url_parser.rs          # URL and query string parsing
│   ├── header_parser.rs       # HTTP header parsing
│   └── body_parser.rs         # Request body parsing
└── utils/                     # Utility functions
    ├── mod.rs                 # Utils module exports
    ├── mime_types.rs          # MIME type detection
    ├── date_time.rs           # Date/time formatting (RFC2822, ISO8601)
    ├── encoding.rs            # Text encoding utilities
    └── validation.rs          # Input validation helpers

Usage

Basic Server

use http::{SystemEnv, get_server_config};
use http::{Request, Response, Router, HttpStatus};
use server::HttpServer;

fn main() -> Result<(), String> {
    let system_env = SystemEnv;
    let (host, port) = get_server_config(&system_env)?;
    
    let mut router = Router::new(host.clone(), port);
    
    router.get("/", |_req: &Request, res: &mut Response| {
        res.set_html("<h1>Hello, World!</h1>");
    });
    
    let mut server = HttpServer::new(host, port, router);
    server.start().map_err(|e| format!("Server error: {}", e))?;
    
    Ok(())
}

Environment Configuration

Set environment variables:

  • HOST: Server host (default: "127.0.0.1")
  • PORT: Server port (default: "8080")
HOST=0.0.0.0 PORT=3000 cargo run

Building and Running

# Build
cargo build

# Run
cargo run

# Run with custom host/port
HOST=0.0.0.0 PORT=8080 cargo run

# Run tests
cargo test

Architecture

This HTTP server is built with a modular architecture:

  1. HTTP Layer: Protocol-specific types and utilities
  2. Server Layer: TCP connection handling and HTTP server implementation
  3. Parsing Layer: Request parsing and protocol handling
  4. Utils Layer: Common utilities for MIME types, dates, etc.

No Third-Party Dependencies

This implementation uses only Rust's standard library, providing complete control and no external dependencies to manage.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published