49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
#![warn (clippy::pedantic)]
|
|
|
|
pub mod graceful_shutdown;
|
|
pub mod http_serde;
|
|
pub mod prelude;
|
|
|
|
// It's easier if the server can stream its response body
|
|
// back to the relay un-changed inside its request body
|
|
// So we wrap the server's actual response head
|
|
// (status code, headers, etc.) in this one header field.
|
|
|
|
pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4";
|
|
|
|
// The arguments are in order so they are in order overall:
|
|
// e.g. prefix_match ("/prefix", "/prefix/middle/suffix") -> "/middle/suffix"
|
|
|
|
#[must_use]
|
|
pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str>
|
|
{
|
|
hay.strip_prefix (prefix)
|
|
}
|
|
|
|
/// Generates 64 bytes of entropy and returns it as Base64
|
|
|
|
pub fn gen_key () -> String {
|
|
use rand::RngCore;
|
|
let mut buffer = vec! [0_u8; 64];
|
|
rand::thread_rng ().fill_bytes (&mut buffer);
|
|
|
|
base64::encode (&buffer)
|
|
}
|
|
|
|
#[cfg (test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn prefix () {
|
|
for (p, h, expected) in &[
|
|
("/files/", "/files/a", Some ("a")),
|
|
("/files/", "/files/abc/def", Some ("abc/def")),
|
|
("/files/", "/files", None),
|
|
("/files/", "/not_files", None),
|
|
("/files/", "/files/", Some ("")),
|
|
] {
|
|
assert_eq! (prefix_match (*p, *h), *expected);
|
|
}
|
|
}
|
|
}
|