Crate axum_range

Source
Expand description

§axum-range

HTTP range responses for axum.

Fully generic, supports any body implementing the RangeBody trait.

Any type implementing both AsyncRead and AsyncSeekStart can be used the KnownSize adapter struct. There is also special cased support for tokio::fs::File, see the KnownSize::file method.

AsyncSeekStart is a trait defined by this crate which only allows seeking from the start of a file. It is automatically implemented for any type implementing AsyncSeek.

use axum::Router;
use axum::routing::get;
use axum_extra::TypedHeader;
use axum_extra::headers::Range;

use tokio::fs::File;

use axum_range::Ranged;
use axum_range::KnownSize;

async fn file(range: Option<TypedHeader<Range>>) -> Ranged<KnownSize<File>> {
    let file = File::open("The Sims 1 - The Complete Collection.rar").await.unwrap();
    let body = KnownSize::file(file).await.unwrap();
    let range = range.map(|TypedHeader(range)| range);
    Ranged::new(range, body)
}

#[tokio::main]
async fn main() {
    // build our application with a single route
    let _app = Router::<()>::new().route("/", get(file));

    // run it with hyper on localhost:3000
    #[cfg(feature = "run_server_in_example")]
    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
       .serve(_app.into_make_service())
       .await
       .unwrap();
}

Structs§

KnownSize
Implements RangeBody for any AsyncRead and AsyncSeekStart, constructed with a fixed byte size.
RangeNotSatisfiable
Error type indicating that the requested range was not satisfiable. Implements IntoResponse.
Ranged
The main responder type. Implements IntoResponse.
RangedResponse
Data type containing computed headers and body for a range response. Implements IntoResponse.
RangedStream
Response body stream. Implements Stream, Body, and IntoResponse.

Traits§

AsyncSeekStart
AsyncSeek narrowed to only allow seeking from start.
RangeBody
An AsyncRead and AsyncSeekStart with a fixed known byte size.