Docs.rs
  • hyper-staticfile-0.10.1
    • hyper-staticfile 0.10.1
    • Permalink
    • Docs.rs crate page
    • MIT
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • stephank
    • Dependencies
      • futures-util ^0.3.1 normal
      • http ^1.0.0 normal
      • http-range ^0.1.4 normal
      • httpdate ^1.0.1 normal
      • hyper ^1.0.0 normal
      • mime_guess ^2.0.1 normal
      • percent-encoding ^2.1.0 normal
      • rand ^0.8.4 normal
      • tokio ^1.0.0 normal
      • url ^2.1.0 normal
      • http-body-util ^0.1.0 dev
      • hyper ^1.0.0 dev
      • hyper-util ^0.1.1 dev
      • tempfile ^3 dev
      • tokio ^1.0.0 dev
      • winapi ^0.3.6 normal
    • Versions
    • 100% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Badges
    • Builds
    • Metadata
    • Shorthand URLs
    • Download
    • Rustdoc JSON
    • Build queue
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate hyper_staticfile

hyper_staticfile0.10.1

  • All Items

Sections

  • Basic usage
  • Advanced usage

Crate Items

  • Modules
  • Structs
  • Enums
  • Type Aliases

Crates

  • hyper_staticfile

Crate hyper_staticfile

Source
Expand description

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.

Modules§

util
Lower level utilities.
vfs
Types to implement a custom (virtual) filesystem to serve files from.

Structs§

AcceptEncoding
Flags for which encodings to resolve.
ResolveParams
All of the parsed request parameters used in resolving a file.
ResolvedFile
Struct containing all the required data to serve a file.
Resolver
Resolves request paths to files.
ResponseBuilder
Utility to build the default response for a resolve result.
Static
High-level interface for serving static files.

Enums§

Body
Hyper Body implementation for the various types of streams used in static serving.
Encoding
Type of response encoding.
ResolveResult
The result of Resolver methods.

Type Aliases§

BoxRewriteFuture
Future returned by a rewrite function. See Resolver::set_rewrite.

Results

Settings
Help
    struct
    hyper_staticfile::util::FileResponseBuilder
    Utility to build responses for serving a file.
    struct field
    hyper_staticfile::ResponseBuilder::file_response_builder
    Inner file response builder.
    struct field
    hyper_staticfile::util::FileResponseBuilder::range
    FileResponseBuilder -> Option
    The file ranges to read, if any, otherwise we read from …
    struct field
    hyper_staticfile::util::FileResponseBuilder::is_head
    FileResponseBuilder -> bool
    Whether this is a HEAD request, with no response body.
    struct field
    hyper_staticfile::util::FileResponseBuilder::if_range
    FileResponseBuilder -> Option
    The unparsed value of the If-Range request header. May …
    struct field
    hyper_staticfile::util::FileResponseBuilder::cache_headers
    FileResponseBuilder -> Option
    Whether to send cache headers, and what lifespan to …
    struct field
    hyper_staticfile::util::FileResponseBuilder::if_modified_since
    FileResponseBuilder -> Option
    The parsed value of the If-Modified-Since request header.
    method
    hyper_staticfile::util::FileResponseBuilder::clone
    &FileResponseBuilder -> FileResponseBuilder
    method
    hyper_staticfile::util::FileResponseBuilder::fmt
    &FileResponseBuilder, &mut Formatter -> Result
    method
    hyper_staticfile::util::FileResponseBuilder::is_head
    &mut FileResponseBuilder, bool -> &mut FileResponseBuilder
    Set whether this is a HEAD request, with no response body.
    method
    hyper_staticfile::util::FileResponseBuilder::build
    &FileResponseBuilder, ResolvedFile<F> -> Result<Response<Body>>
    Build a response for the given resolved file.
    method
    hyper_staticfile::util::FileResponseBuilder::cache_headers
    &mut FileResponseBuilder, Option<u32> -> &mut FileResponseBuilder
    Add cache headers to responses for the given lifespan.
    method
    hyper_staticfile::util::FileResponseBuilder::request_method
    &mut FileResponseBuilder, &Method -> &mut FileResponseBuilder
    Apply parameters based on a request method.
    method
    hyper_staticfile::util::FileResponseBuilder::request_headers
    &mut FileResponseBuilder, &HeaderMap -> &mut FileResponseBuilder
    Apply parameters based on request headers.
    method
    hyper_staticfile::util::FileResponseBuilder::if_modified_since
    &mut FileResponseBuilder, Option<SystemTime> -> &mut FileResponseBuilder
    Build responses for the given If-Modified-Since date-time.
    method
    hyper_staticfile::util::FileResponseBuilder::request
    &mut FileResponseBuilder, &Request<B> -> &mut FileResponseBuilder
    Apply parameters based on a request.
    method
    hyper_staticfile::util::FileResponseBuilder::if_range
    &mut FileResponseBuilder, Option<&HeaderValue> -> &mut FileResponseBuilder
    Build responses for the given If-Range request header …
    method
    hyper_staticfile::util::FileResponseBuilder::range_header
    &mut FileResponseBuilder, Option<&HeaderValue> -> &mut FileResponseBuilder
    Build responses for the given Range request header value.
    method
    hyper_staticfile::util::FileResponseBuilder::if_modified_since_header
    &mut FileResponseBuilder, Option<&HeaderValue> -> &mut FileResponseBuilder
    Build responses for the given If-Modified-Since request …
    method
    hyper_staticfile::util::FileResponseBuilder::request_parts
    &mut FileResponseBuilder, &Method, &HeaderMap -> &mut FileResponseBuilder
    Apply parameters based on request parts.
    method
    hyper_staticfile::util::FileResponseBuilder::new
    -> FileResponseBuilder
    Create a new builder with a default configuration.
    method
    hyper_staticfile::util::FileResponseBuilder::default
    -> FileResponseBuilder
    struct field
    hyper_staticfile::ResponseBuilder::file_response_builder
    ResponseBuilder -> FileResponseBuilder
    Inner file response builder.
    method
    hyper_staticfile::util::FileResponseBuilder::clone
    &FileResponseBuilder -> FileResponseBuilder
    method
    hyper_staticfile::util::FileResponseBuilder::is_head
    &mut FileResponseBuilder, bool -> &mut FileResponseBuilder
    Set whether this is a HEAD request, with no response body.
    method
    hyper_staticfile::util::FileResponseBuilder::cache_headers
    &mut FileResponseBuilder, Option<u32> -> &mut FileResponseBuilder
    Add cache headers to responses for the given lifespan.
    method
    hyper_staticfile::util::FileResponseBuilder::request_method
    &mut FileResponseBuilder, &Method -> &mut FileResponseBuilder
    Apply parameters based on a request method.
    method
    hyper_staticfile::util::FileResponseBuilder::request_headers
    &mut FileResponseBuilder, &HeaderMap -> &mut FileResponseBuilder
    Apply parameters based on request headers.
    method
    hyper_staticfile::util::FileResponseBuilder::if_modified_since
    &mut FileResponseBuilder, Option<SystemTime> -> &mut FileResponseBuilder
    Build responses for the given If-Modified-Since date-time.
    method
    hyper_staticfile::util::FileResponseBuilder::request
    &mut FileResponseBuilder, &Request<B> -> &mut FileResponseBuilder
    Apply parameters based on a request.
    method
    hyper_staticfile::util::FileResponseBuilder::if_range
    &mut FileResponseBuilder, Option<&HeaderValue> -> &mut FileResponseBuilder
    Build responses for the given If-Range request header …
    method
    hyper_staticfile::util::FileResponseBuilder::range_header
    &mut FileResponseBuilder, Option<&HeaderValue> -> &mut FileResponseBuilder
    Build responses for the given Range request header value.
    method
    hyper_staticfile::util::FileResponseBuilder::if_modified_since_header
    &mut FileResponseBuilder, Option<&HeaderValue> -> &mut FileResponseBuilder
    Build responses for the given If-Modified-Since request …
    method
    hyper_staticfile::util::FileResponseBuilder::request_parts
    &mut FileResponseBuilder, &Method, &HeaderMap -> &mut FileResponseBuilder
    Apply parameters based on request parts.