1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! # PTTH Core
//! 
//! Common code used by both `ptth_relay` and `ptth_server`.
//! Most users will want to use those binary crates directly.

#![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";

/// 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 test_gen_key () {
		// Smoke test
		
		let blank = base64::encode (&vec! [0_u8; 64]);
		let a = gen_key ();
		let b = gen_key ();
		
		assert_ne! (blank, a);
		assert_ne! (blank, b);
		assert_ne! (a, b);
	}
}