Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(examples/streaming): use get getrandom to generate boundary #9546

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions examples/streaming/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use http::{header::*, response::Builder as ResponseBuilder, status::StatusCode};
use http_range::HttpRange;
use std::sync::{Arc, Mutex};
use std::{
io::{Read, Seek, SeekFrom, Write},
path::PathBuf,
Expand Down Expand Up @@ -36,13 +35,10 @@ fn main() {
assert!(video_file.exists());
}

// NOTE: for production use `rand` crate to generate a random boundary
let boundary_id = Arc::new(Mutex::new(0));

tauri::Builder::default()
.invoke_handler(tauri::generate_handler![video_uri])
.register_asynchronous_uri_scheme_protocol("stream", move |_app, request, responder| {
match get_stream_response(request, &boundary_id) {
match get_stream_response(request) {
Ok(http_response) => responder.respond(http_response),
Err(e) => responder.respond(
ResponseBuilder::new()
Expand Down Expand Up @@ -76,7 +72,6 @@ fn video_uri() -> (&'static str, std::path::PathBuf) {

fn get_stream_response(
request: http::Request<Vec<u8>>,
boundary_id: &Arc<Mutex<i32>>,
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
// skip leading `/`
let path = percent_encoding::percent_decode(request.uri().path()[1..].as_bytes())
Expand Down Expand Up @@ -171,9 +166,7 @@ fn get_stream_response(
})
.collect::<Vec<_>>();

let mut id = boundary_id.lock().unwrap();
*id += 1;
let boundary = format!("sadasq2e{id}");
let boundary = random_boundary();
let boundary_sep = format!("\r\n--{boundary}\r\n");
let boundary_closer = format!("\r\n--{boundary}\r\n");

Expand Down Expand Up @@ -215,3 +208,15 @@ fn get_stream_response(

http_response.map_err(Into::into)
}

fn random_boundary() -> String {
let mut x = [0_u8; 30];
getrandom::getrandom(&mut x).expect("failed to get random bytes");
(x[..])
.iter()
.map(|&x| format!("{x:x}"))
.fold(String::new(), |mut a, x| {
a.push_str(x.as_str());
a
})
}