Crate warp_range[][src]

Expand description

warp-range

A Rust library for creating a warp filter for serving file content with range like mp3 audio or mp4 video. This warp filter can be used in a HTTP server based on warp.

The content is served like streaming. If you view a movie served by this filter, you can seek through it even if the file is not completely downloaded.

Here is an easy example to add range to an existing warp server:

use hyper::{Body, Response};
use warp::{Filter, Reply, fs::{File, dir}};
use warp_range::{filter_range, get_range, with_partial_content_status};
 
#[tokio::main]
async fn main() {
    let test_video = "/home/uwe/Videos/Drive.mkv";
     
    let port = 9860;
    println!("Running test server on http://localhost:{}", port);
 
    let route_get_range = 
        warp::path("getvideo")
        .and(warp::path::end())
        .and(filter_range())
        .and_then(move |range_header| get_range(range_header, test_video, "video/mp4"))
        .map(with_partial_content_status);
 
    let route_static = dir(".");
     
    let routes = route_get_range
        .or(route_static);
 
    warp::serve(routes)
        .run(([127, 0, 0, 1], port))
        .await;    
}

If you want to serve the media file without range, which is sometimes necessary, because the browser requests it perhaps without range request header, you can add another route without range:

    let route_get_video = 
    warp::path("getvideo")
    .and(warp::path::end())
    .and_then(move || get_range("".to_string(), test_video, "video/mp4"));

You have to add this route after the range route:

    let routes = route_get_range
        .or(route_get_video)
        .or(route_static);

Functions

filter_range

This function filters and extracts the “Range”-Header

get_range

This function retrives the range of bytes requested by the web client

with_partial_content_status

This function adds the “206 Partial Content” header