13 releases

0.6.0 Jan 1, 2021
0.5.3 May 26, 2020
0.5.2 Apr 28, 2020
0.5.1 Dec 16, 2019
0.1.1 Aug 8, 2017

#20 in HTTP server

Download history 433/week @ 2021-01-21 490/week @ 2021-01-28 573/week @ 2021-02-04 660/week @ 2021-02-11 691/week @ 2021-02-18 524/week @ 2021-02-25 577/week @ 2021-03-04 573/week @ 2021-03-11 563/week @ 2021-03-18 632/week @ 2021-03-25 543/week @ 2021-04-01 474/week @ 2021-04-08 426/week @ 2021-04-15 677/week @ 2021-04-22 352/week @ 2021-04-29 287/week @ 2021-05-06

2,297 downloads per month
Used in 11 crates (10 directly)

MIT license

27KB
434 lines

hyper-staticfile

Docs Crate Build Status

Static file-serving for Hyper 0.14.

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

Documentation


lib.rs:

Static file-serving for Hyper 0.14.

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.
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 resolve and ResponseBuilder. You can achieve the same by doing something similar to the following:

use std::path::Path;

#[tokio::main]
async fn main() {
    // Document root path.
    let root = Path::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 = hyper_staticfile::resolve(&root, &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 function tries to find the file in the 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.

The ResponseBuilder in turn uses FileResponseBuilder to serve files that are found. The FileResponseBuilder can also be used directly if you have an existing open tokio::fs::File and want to serve it. It takes care of basic headers, 'not modified' responses, and streaming the file in the body.

Finally, there's FileBytesStream, which is used by FileResponseBuilder to stream the file. This is a struct wrapping a tokio::fs::File and implementing a futures::Stream that produces Bytess. It can be used for streaming a file in custom response.

Dependencies

~8MB
~162K SLoC