6 releases (3 breaking)

0.4.0 Dec 29, 2023
0.3.0 Sep 10, 2023
0.2.2 Sep 10, 2023
0.1.0 Sep 9, 2023

#1765 in Web programming

Download history 40/week @ 2024-07-23 89/week @ 2024-07-30 178/week @ 2024-08-06 120/week @ 2024-08-13 108/week @ 2024-08-20 136/week @ 2024-08-27 193/week @ 2024-09-03 119/week @ 2024-09-10 159/week @ 2024-09-17 101/week @ 2024-09-24 123/week @ 2024-10-01 154/week @ 2024-10-08 97/week @ 2024-10-15 90/week @ 2024-10-22 126/week @ 2024-10-29 177/week @ 2024-11-05

520 downloads per month
Used in rustic_server

MIT license

23KB
391 lines

axum-range

HTTP range responses for axum.

Documentation.

MIT license.

Example usage

use axum::TypedHeader;
use axum::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("archlinux-x86_64.iso").await.unwrap();
    let body = KnownSize::file(file).await.unwrap();
    let range = range.map(|TypedHeader(range)| range);
    Ranged::new(range, body)
}

lib.rs:

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();
}

Dependencies

~6–13MB
~150K SLoC