//! # PTTH Core //! //! Common code used by both `ptth_relay` and `ptth_server`. #![warn (clippy::pedantic)] /// Wrapper for graceful and forced shutdowns of `ptth_relay` and `ptth_server` pub mod graceful_shutdown; /// An abstraction over HTTP that is easy to (de)serialize pub mod http_serde; pub mod prelude; /// `ptth_server` packs its response headers into this request header /// /// This allows the server's response body to be wrapped verbatim /// in the request body. /// The header value is the response's status code and headers, /// wrapped in a MessagePack structure, and encoded in base64 /// to make it ASCII. 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] #[deprecated ( since = "1.4.0", note = "Use `str::strip_prefix instead`" )] 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); } } }