use crate::{Error, Result, client::Request, http1::Header};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Part {
name: String,
filename: Option<String>,
content_type: Option<String>,
body: Vec<u8>,
}
impl Part {
pub fn text(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
filename: None,
content_type: None,
body: value.into().into_bytes(),
}
}
pub fn file(name: impl Into<String>, filename: impl Into<String>, bytes: Vec<u8>) -> Self {
Self {
name: name.into(),
filename: Some(filename.into()),
content_type: None,
body: bytes,
}
}
pub fn content_type(mut self, content_type: impl Into<String>) -> Self {
self.content_type = Some(content_type.into());
self
}
fn media_type(&self) -> Option<&str> {
match (&self.content_type, &self.filename) {
(Some(content_type), _) => Some(content_type),
(None, Some(_)) => Some("application/octet-stream"),
(None, None) => None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Form {
parts: Vec<Part>,
}
impl Form {
pub fn new() -> Self {
Self::default()
}
pub fn text(self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.part(Part::text(name, value))
}
pub fn file(
self,
name: impl Into<String>,
filename: impl Into<String>,
bytes: Vec<u8>,
) -> Self {
self.part(Part::file(name, filename, bytes))
}
pub fn part(mut self, part: Part) -> Self {
self.parts.push(part);
self
}
pub fn parts(&self) -> &[Part] {
&self.parts
}
pub fn encode(&self, entropy: [u8; 16]) -> Result<Encoded> {
for part in &self.parts {
if part
.media_type()
.is_some_and(|t| t.bytes().any(|b| b.is_ascii_control()))
{
return Err(Error::new(
"UND_ERR_INVALID_ARG",
"invalid multipart content type",
));
}
}
let names: Vec<_> = self
.parts
.iter()
.map(|p| (escape(&p.name), p.filename.as_deref().map(escape)))
.collect();
let boundary = self.boundary(entropy, &names);
let mut body = Vec::with_capacity(
self.parts
.iter()
.zip(&names)
.map(|(part, (name, filename))| {
part.body.len()
+ name.len()
+ filename.as_ref().map_or(0, String::len)
+ part.media_type().map_or(0, str::len)
+ boundary.len()
+ 128
})
.sum::<usize>()
+ boundary.len()
+ 6,
);
for (part, (name, filename)) in self.parts.iter().zip(&names) {
body.extend_from_slice(b"--");
body.extend_from_slice(boundary.as_bytes());
body.extend_from_slice(b"\r\nContent-Disposition: form-data; name=\"");
body.extend_from_slice(name.as_bytes());
body.push(b'"');
if let Some(filename) = filename {
body.extend_from_slice(b"; filename=\"");
body.extend_from_slice(filename.as_bytes());
body.push(b'"');
}
if let Some(media_type) = part.media_type() {
body.extend_from_slice(b"\r\nContent-Type: ");
body.extend_from_slice(media_type.as_bytes());
}
body.extend_from_slice(b"\r\n\r\n");
body.extend_from_slice(&part.body);
body.extend_from_slice(b"\r\n");
}
body.extend_from_slice(b"--");
body.extend_from_slice(boundary.as_bytes());
body.extend_from_slice(b"--\r\n");
Ok(Encoded { boundary, body })
}
fn boundary(&self, entropy: [u8; 16], names: &[(String, Option<String>)]) -> String {
let entropy = u128::from_le_bytes(entropy);
let (low, high) = (entropy as u64, (entropy >> 64) as u64);
let mut index = 0u64;
loop {
let boundary = candidate(low, high, index);
let needle = boundary.as_bytes();
let collides = self
.parts
.iter()
.zip(names)
.any(|(part, (name, filename))| {
[
Some(part.body.as_slice()),
Some(name.as_bytes()),
filename.as_deref().map(str::as_bytes),
part.media_type().map(str::as_bytes),
]
.into_iter()
.flatten()
.any(|bytes| contains(bytes, needle))
});
if !collides {
return boundary;
}
index += 1;
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Encoded {
pub boundary: String,
pub body: Vec<u8>,
}
impl Encoded {
pub fn content_type(&self) -> String {
format!("multipart/form-data; boundary={}", self.boundary)
}
pub fn apply(self, request: &mut Request) {
let content_type = self.content_type();
request
.headers
.retain(|h| !h.name.eq_ignore_ascii_case("content-type"));
request
.headers
.push(Header::new("content-type", content_type));
request.body = self.body;
}
}
const PREFIX: &str = "turnloop-";
const BOUNDARY_LEN: usize = PREFIX.len() + 32;
fn candidate(low: u64, high: u64, index: u64) -> String {
const GAMMA: u64 = 0x9e37_79b9_7f4a_7c15;
let first = mix(low.wrapping_add(index.wrapping_mul(GAMMA)));
let second = mix(high ^ first);
let mut boundary = String::with_capacity(BOUNDARY_LEN);
boundary.push_str(PREFIX);
for word in [first, second] {
for shift in (0..16).rev() {
let digit = ((word >> (shift * 4)) & 15) as u8;
boundary.push(char::from(if digit < 10 {
b'0' + digit
} else {
b'a' + digit - 10
}));
}
}
boundary
}
fn mix(mut z: u64) -> u64 {
z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
z ^ (z >> 31)
}
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
haystack
.windows(needle.len())
.any(|window| window == needle)
}
fn escape(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for c in value.chars() {
match c {
'"' => escaped.push_str("%22"),
'\r' => escaped.push_str("%0D"),
'\n' => escaped.push_str("%0A"),
c => escaped.push(c),
}
}
escaped
}