#![forbid(unsafe_code)]
#![warn(
missing_debug_implementations,
rust_2018_idioms,
trivial_casts,
unused_qualifications
)]
#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
#![doc(test(attr(allow(unused_extern_crates, unused_variables))))]
pub use bytes;
pub use constraints::Constraints;
pub use error::Error;
pub use field::Field;
pub use multipart::Multipart;
pub use size_limit::SizeLimit;
#[cfg(feature = "log")]
macro_rules! trace {
($($t:tt)*) => (::log::trace!($($t)*););
}
#[cfg(not(feature = "log"))]
macro_rules! trace {
($($t:tt)*) => {};
}
mod buffer;
mod constants;
mod constraints;
mod content_disposition;
mod content_type;
mod error;
mod field;
mod helpers;
mod multipart;
mod size_limit;
pub type Result<T, E = Error> = std::result::Result<T, E>;
const fn is_boundary_char_no_space(byte: u8) -> bool {
byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'\'' | b'(' | b')' | b'+' | b'_' | b',' | b'-' | b'.' | b'/' | b':' | b'=' | b'?'
)
}
const fn is_boundary_char(byte: u8) -> bool {
byte == b' ' || is_boundary_char_no_space(byte)
}
fn validate_boundary(boundary: &str) -> Result<()> {
let bytes = boundary.as_bytes();
if bytes.is_empty()
|| bytes.len() > 70
|| !bytes[..bytes.len() - 1]
.iter()
.copied()
.all(is_boundary_char)
|| !is_boundary_char_no_space(bytes[bytes.len() - 1])
{
return Err(Error::InvalidBoundary {
boundary: boundary.to_owned(),
});
}
Ok(())
}
pub fn parse_boundary<T: AsRef<str>>(content_type: T) -> Result<String> {
let parsed = content_type::ContentType::parse(content_type.as_ref())?;
let m = &parsed.essence;
if !(m.type_() == mime::MULTIPART && m.subtype() == mime::FORM_DATA && m.suffix().is_none()) {
return Err(Error::NoMultipart);
}
let boundary = parsed.get_param("boundary").ok_or(Error::NoBoundary)?;
validate_boundary(boundary)?;
Ok(boundary.to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_boundary_requires_the_complete_form_data_subtype() {
for content_type in [
"multipart/form-data+json; boundary=B",
"multipart/form-data+xml; boundary=B",
"MULTIPART/FORM-DATA+CUSTOM; boundary=B",
] {
assert_eq!(parse_boundary(content_type), Err(Error::NoMultipart));
}
assert_eq!(
parse_boundary("MULTIPART/FORM-DATA; boundary=MixedCase"),
Ok("MixedCase".to_owned())
);
}
#[test]
fn test_parse_boundary() {
let content_type = "multipart/form-data; boundary=ABCDEFG";
assert_eq!(parse_boundary(content_type), Ok("ABCDEFG".to_owned()));
let content_type = "multipart/form-data; boundary=------ABCDEFG";
assert_eq!(parse_boundary(content_type), Ok("------ABCDEFG".to_owned()));
let content_type = "boundary=------ABCDEFG";
assert!(parse_boundary(content_type).is_err());
let content_type = "text/plain";
assert!(parse_boundary(content_type).is_err());
let content_type = "text/plain; boundary=------ABCDEFG";
assert!(parse_boundary(content_type).is_err());
let boundary = "a".repeat(70);
let content_type = format!("multipart/form-data; boundary={boundary}");
assert_eq!(parse_boundary(content_type), Ok(boundary));
let boundary = "a".repeat(71);
let content_type = format!("multipart/form-data; boundary={boundary}");
assert!(matches!(
parse_boundary(content_type),
Err(Error::InvalidBoundary { .. })
));
let content_type = "multipart/form-data; boundary=\"abc@def\"";
assert!(matches!(
parse_boundary(content_type),
Err(Error::InvalidBoundary { .. })
));
}
}