post body format okay

new-idea
Avril 4 years ago
parent a85286edb9
commit 9ba00f6722
Signed by: flanchan
GPG Key ID: 284488987C31F630

13
Cargo.lock generated

@ -67,6 +67,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bitflags"
version = "1.2.1"
@ -531,7 +537,7 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed18eb2459bf1a09ad2d6b1547840c3e5e62882fa09b9a6a20b1de8e3228848f"
dependencies = [
"base64",
"base64 0.12.3",
"bitflags",
"bytes",
"headers-core",
@ -994,7 +1000,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7170d73bf11f39b4ce1809aabc95bf5c33564cdc16fc3200ddda17a5f6e5e48b"
dependencies = [
"base64",
"base64 0.12.3",
"crypto-mac",
"hmac",
"rand 0.7.3",
@ -1682,7 +1688,7 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [
"base64",
"base64 0.12.3",
"byteorder",
"bytes",
"http",
@ -1896,6 +1902,7 @@ dependencies = [
name = "yuurei"
version = "0.1.0"
dependencies = [
"base64 0.13.0",
"cfg-if 1.0.0",
"chrono",
"color-eyre",

@ -11,6 +11,7 @@ default = ["nightly"]
nightly = ["smallvec/const_generics"]
[dependencies]
base64 = "0.13.0"
cfg-if = "1.0.0"
chrono = {version = "0.4", features=["serde"]}
color-eyre = {version = "0.5.10", default-features=false}

@ -242,7 +242,7 @@ pub mod formats
/// A format spec that must satisfy both these format specs in order
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BothFormat<T, U = AnyFormat>(PhantomData<(T, U)>)
where T: FormatSpec, U: FormatSpec;
where T: FormatSpec, U: FormatSpec;
#[derive(Debug)]
pub enum MultiFormatError<T,U>
@ -379,9 +379,61 @@ pub mod formats
type Error = PEMFormatError;
fn validate(_s: &str) -> Result<(), Self::Error> {
todo!("Learn the PEM ciphertext format");
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub enum Base64Format{}
impl Base64Format
{
const CHARSET_STR: &'static str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
}
impl FormatSpec for Base64Format
{
type Error = InvalidBase64Error;
fn validate(s: &str) -> Result<(), Self::Error> {
lazy_static! {
static ref CHARSET: [bool; 256] = {
let mut map = [false; 256];
for byte in Base64Format::CHARSET_STR.bytes()
{
map[byte as usize] = true;
}
map
};
}
let mut iter = s.as_bytes().chunks(4).peekable();
while let Some(window) = iter.next()
{
let is_last = iter.peek().is_none();
// eprintln!("Window: {:?} ({})", std::str::from_utf8(window), is_last);
for byte in window.iter().copied()
{
if !(CHARSET[byte as usize] || (is_last && byte == b'=')) {
return Err(InvalidBase64Error(byte as char));
}
}
}
Ok(())
}
}
/// On invalid base64 string
#[derive(Debug)]
pub struct InvalidBase64Error(char);
impl error::Error for InvalidBase64Error{}
impl fmt::Display for InvalidBase64Error
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "invalid char {:?} in base64 string", self.0)
}
}
pub type MaxLenStr<const MAX: usize> = FormattedStr<MaxLenFormat<MAX>>;
pub type MaxLenString<const MAX: usize> = FormattedString<MaxLenFormat<MAX>>;
@ -389,9 +441,12 @@ pub mod formats
pub type HexFormattedStr = FormattedStr<HexFormat>;
pub type HexFormattedString = FormattedString<HexFormat>;
pub type Base64FormattedStr = FormattedStr<Base64Format>;
pub type Base64FormattedString = FormattedString<Base64Format>;
pub type PEMFormattedStr = FormattedStr<PEMFormat>;
pub type PEMFormattedString = FormattedString<PEMFormat>;
#[cfg(test)]
mod tests
{
@ -402,6 +457,20 @@ pub mod formats
let _invalid = HexFormattedStr::new("ab120982039840i ").expect_err("Invalidation");
let _valid = HexFormattedStr::new("abc123982095830495adcfDD").expect("Validation");
}
#[test]
fn base64()
{
let mut random = [0u8; 256];
for len in 9..101
{
getrandom::getrandom(&mut random[..len]).expect("rng");
let encoded = base64::encode(&random[..len]);
println!("String: {}", encoded);
let _encoded = Base64FormattedString::new(encoded).expect("Encode validate failed");
}
}
}
}

@ -18,7 +18,7 @@ pub type IDMaxString = MaxLenString<{defaults::POST_ID_MAX_LEN}>;
pub type PostTimestamp = chrono::DateTime<defaults::Timezone>;
/// A size limited PEM formatting specifier
type PostBodyFormat = formats::BothFormat<formats::MaxLenFormat<{defaults::POST_BODY_MAX_SIZE}>, formats::PEMFormat>;
type PostBodyFormat = formats::BothFormat<formats::MaxLenFormat<{defaults::POST_BODY_MAX_SIZE}>, formats::Base64Format>;
/// A size limited PEM string
pub type PostBodyString = hard_format::FormattedString<PostBodyFormat>;
@ -173,7 +173,7 @@ mod tests
email: None,
tripcode: Some(super::Tripcode::generate("uhh hello").unwrap()),
},
body: unsafe { super::PostBodyStr::new_unchecked("test").to_owned() }, //TODO: temporary
body: unsafe { super::PostBodyString::new_unchecked(base64::encode("test")) },
signature: None,
hash: Default::default(),

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
</head>
<body>
</body>
</html>
Loading…
Cancel
Save