#rocket #response-headers #file-serving #partial-content #range-request #rocket-rs #206

rocket_seek_stream

Rocket-rs 0.5.0-rc.3 responder to range requests using types that implement AsyncRead + AsyncSeek

6 releases

0.2.6 May 4, 2023
0.2.5 Aug 26, 2022
0.2.4 Aug 3, 2021
0.2.3 Sep 7, 2019

#11 in #response-headers

Download history 69/week @ 2024-07-22 45/week @ 2024-07-29 5/week @ 2024-08-05 23/week @ 2024-08-12 33/week @ 2024-08-19 61/week @ 2024-08-26 69/week @ 2024-09-02 5/week @ 2024-09-09 23/week @ 2024-09-16 77/week @ 2024-09-23 66/week @ 2024-09-30 15/week @ 2024-10-07 40/week @ 2024-10-14 78/week @ 2024-10-21 17/week @ 2024-10-28 29/week @ 2024-11-04

166 downloads per month

MIT license

23KB
379 lines

rocket_seek_stream

crates.io

A Rocket responder for types implementing the AsyncRead + AsyncSeek traits, such as files and rocket::futures::io::Cursor, that will respond to range requests with a 206 Partial Content response. The Content-Type can optionally be inferred by taking a sample of bytes from the beginning of the stream, or given manually. An Accept-Ranges: bytes header will be sent in all responses to notify browsers that range requests are supported for the resource.

This supports both single and multipart/byterange requests. https://tools.ietf.org/html/rfc7233

Cargo.toml

Add this to your dependencies.

rocket_seek_stream = {git="https://github.com/rydz/rocket_seek_stream"}

Examples

Serving a file from the disk

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use rocket_seek_stream::SeekStream;

#[get("/")]
fn home<'a>() -> std::io::Result<SeekStream<'a>> {
    SeekStream::from_path("kosmodrom.webm")
}

#[rocket::main]
async fn main() {
    match rocket::build().mount("/", routes![home]).launch().await {
        Ok(_) => (),
        Err(e) => {
            eprintln!("Rocket stopped unexpectedly. (Error {})", e);
        }
    };
}

Serving an in memory buffer

#[get("/")]
fn cursor<'a>() -> SeekStream<'a> {
    let bytes = &include_bytes!("./fly_me_to_the_moon.webm")[..];
    let len = bytes.len();
    let stream = std::io::Cursor::new(bytes);

    SeekStream::with_opts(stream, len as u64, "video/webm")
}

Use cargo run --example server to run the example. run examples/download.sh to download the media it depends on using yt-dlp.

TODO

  • Write some tests

I've compared the output of the Golang stdlib http router's multipart response to what I output here and it looks about the same except for a small difference in whitespace.

Dependencies

~19–53MB
~853K SLoC