#static-file #hyper #static #http-file #web #http #file

hyper-staticfile

Static file serving for Hyper 1.0

6 releases

0.10.0 Nov 18, 2023
0.10.0-alpha.7 Jul 18, 2023
0.10.0-alpha.6 May 30, 2023
0.10.0-alpha.5 Dec 23, 2022
0.1.1 Aug 8, 2017

#57 in HTTP server

Download history 2839/week @ 2023-12-01 3378/week @ 2023-12-08 2160/week @ 2023-12-15 1180/week @ 2023-12-22 2076/week @ 2023-12-29 3064/week @ 2024-01-05 2672/week @ 2024-01-12 3526/week @ 2024-01-19 3017/week @ 2024-01-26 3471/week @ 2024-02-02 2904/week @ 2024-02-09 2596/week @ 2024-02-16 2780/week @ 2024-02-23 3044/week @ 2024-03-01 3457/week @ 2024-03-08 2306/week @ 2024-03-15

12,090 downloads per month
Used in 25 crates (20 directly)

MIT license

61KB
1K SLoC

hyper-staticfile

Docs Crate Build Status

Static file-serving for Hyper 1.0.

See examples/doc_server.rs for a complete example that you can compile.

Documentation


lib.rs:

Static file-serving for Hyper 1.0.

This library exports a high-level interface Static for simple file-serving, and lower-level interfaces for more control over responses.

Basic usage

The Static type is essentially a struct containing some settings, and a serve method to handle the request. It follows the builder pattern, and also implements the hyper::Service trait. It can be used as:

// Instance of `Static` containing configuration. Can be cheaply cloned.
let static_ = hyper_staticfile::Static::new("my/doc/root/");

// A dummy request, but normally obtained from Hyper.
let request = http::Request::get("/foo/bar.txt")
    .body(())
    .unwrap();

// Serve the request. Returns a future for a `hyper::Response`.
let response_future = static_.serve(request);

Typically, you'd store the Static instance somewhere, such as in your own hyper::Service implementation.

Advanced usage

The Static type is a simple wrapper for Resolver and ResponseBuilder. You can achieve the same by doing something similar to the following:

use std::path::Path;

#[tokio::main]
async fn main() {
    // Create a resolver. This can be cheaply cloned.
    let resolver = hyper_staticfile::Resolver::new("my/doc/root/");

    // A dummy request, but normally obtained from Hyper.
    let request = http::Request::get("/foo/bar.txt")
        .body(())
        .unwrap();

    // First, resolve the request. Returns a future for a `ResolveResult`.
    let result = resolver.resolve_request(&request).await.unwrap();

    // Then, build a response based on the result.
    // The `ResponseBuilder` is typically a short-lived, per-request instance.
    let response = hyper_staticfile::ResponseBuilder::new()
        .request(&request)
        .build(result)
        .unwrap();
}

The resolve_request method tries to find the file in the document root, and returns a future for the ResolveResult enum, which determines what kind of response should be sent. The ResponseBuilder is then used to create a default response. It holds some settings, and can be constructed using the builder pattern.

It's useful to sit between these two steps to implement custom 404 pages, for example. Your custom logic can override specific cases of ResolveResult, and fall back to the default behavior using ResponseBuilder if necessary.

Dependencies

~7.5MB
~155K SLoC