Crate http_handle

Source
Expand description

Http Handle logo

§HTTP Handle (http-handle)

A Rust-based HTTP server for serving static websites.

Made With Love Crates.io lib.rs Docs.rs Codecov Build Status GitHub

WebsiteDocumentationReport BugRequest FeatureContributing Guidelines

§Overview

The http-handle is a robust Rust library designed for serving static websites. It provides a simple yet efficient HTTP server implementation with features like request parsing, response generation, and basic security measures. The library is not intended to be a full-fledged web server but rather a lightweight solution for serving static files over HTTP for development and testing purposes.

§Features

  • Static File Serving: Serve static files from a configured document root.
  • Request Parsing: Parse incoming HTTP requests with proper error handling.
  • Response Generation: Generate appropriate HTTP responses based on requests.
  • Security Measures: Prevent directory traversal attacks.
  • Content Type Detection: Automatically detect and set appropriate content types for files.
  • Customizable 404 Handling: Support for custom 404 error pages.
  • Threaded Connections: Handle multiple connections concurrently using threads.
  • Configurable Server: Easy configuration of server address and document root.

§Installation

Add this to your Cargo.toml:

[dependencies]
http-handle = "0.0.2"

§Usage

Here’s a basic example of how to use http-handle:

use http_handle::Server;
use std::thread;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    // Create a new server with an address and document root
    let server = Server::new("127.0.0.1:8080", "./public");

    // Run the server in a separate thread so it doesn't block
    let server_handle = thread::spawn(move || {
        server.start().expect("Server failed to start");
    });

    // Let the server run for 2 seconds before shutting it down
    thread::sleep(Duration::from_secs(2));

    println!("Server has been running for 2 seconds, shutting down...");
    
    // In a real-world scenario, you would need to implement a proper shutdown signal
    // This just exits the program after the duration.
    
    Ok(())
}

This will start a server listening on 127.0.0.1:8080, serving files from the ./public directory.

§Documentation

For full API documentation, please visit docs.rs/http-handle.

§Examples

To explore more examples, clone the repository and run the following command:

cargo run --example example_name

§Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

§License

This project is licensed under either of

at your option.

§Acknowledgements

Special thanks to all contributors who have helped build the http-handle library.

§HTTP Handle

The http-handle is a robust Rust library designed for serving static websites. It provides a simple yet efficient HTTP server implementation with features like request parsing, response generation, and basic security measures. The library is not intended to be a full-fledged web server but rather a lightweight solution for serving static files over HTTP for development and testing purposes.

§Modules

  • server: Contains the core Server struct and logic for managing HTTP connections.
  • request: Handles incoming HTTP requests, parsing and validation.
  • response: Provides utilities for crafting HTTP responses.
  • error: Defines errors related to the server’s operation.

Re-exports§

Modules§

  • The error module defines various errors that can occur during server operation, including those related to connections and malformed requests. Error types for the Http Handle.
  • The request module is responsible for parsing and validating incoming HTTP requests. HTTP request parsing module for the Http Handle.
  • The response module provides tools and utilities for crafting HTTP responses. HTTP Response module for handling and sending server responses.
  • The server module contains the core Server struct and associated methods for starting and managing the HTTP server. Server module for handling HTTP requests and responses.