use base64::Engine as _;
use rand::Rng as _;
pub(crate) fn generate_boundary() -> String {
let mut bytes = [0u8; 16];
rand::rng().fill_bytes(&mut bytes);
format!(
"omnidev-{}",
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
)
}
pub(crate) fn build_related_body(
metadata: &serde_json::Value,
content: &[u8],
content_type: &str,
boundary: &str,
) -> Vec<u8> {
let mut body = Vec::with_capacity(content.len() + 256);
body.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
body.extend_from_slice(b"Content-Type: application/json; charset=UTF-8\r\n\r\n");
body.extend_from_slice(metadata.to_string().as_bytes());
body.extend_from_slice(b"\r\n");
body.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
body.extend_from_slice(format!("Content-Type: {content_type}\r\n\r\n").as_bytes());
body.extend_from_slice(content);
body.extend_from_slice(b"\r\n");
body.extend_from_slice(format!("--{boundary}--").as_bytes());
body
}
pub(crate) fn generate_boundary_absent_from(content: &[u8]) -> String {
loop {
let candidate = generate_boundary();
if !contains_subslice(content, candidate.as_bytes()) {
return candidate;
}
}
}
fn contains_subslice(haystack: &[u8], needle: &[u8]) -> bool {
!needle.is_empty() && haystack.windows(needle.len()).any(|w| w == needle)
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn multipart_body_has_two_parts_separated_by_the_boundary() {
let metadata = serde_json::json!({"name": "photo.jpg", "parents": ["p1"]});
let body = build_related_body(&metadata, b"JPEGDATA", "image/jpeg", "BOUNDARY");
let body_str = String::from_utf8(body).unwrap();
assert_eq!(
body_str,
"--BOUNDARY\r\n\
Content-Type: application/json; charset=UTF-8\r\n\r\n\
{\"name\":\"photo.jpg\",\"parents\":[\"p1\"]}\r\n\
--BOUNDARY\r\n\
Content-Type: image/jpeg\r\n\r\n\
JPEGDATA\r\n\
--BOUNDARY--"
);
}
#[test]
fn multipart_body_preserves_binary_content_byte_for_byte() {
let metadata = serde_json::json!({"name": "bin"});
let binary_content: Vec<u8> = vec![0x00, 0xFF, 0x0D, 0x0A, 0x2D, 0x2D, 0x01];
let body = build_related_body(&metadata, &binary_content, "application/octet-stream", "B");
let needle_pos = body
.windows(binary_content.len())
.position(|w| w == binary_content.as_slice());
assert!(
needle_pos.is_some(),
"binary content not found intact in body"
);
}
#[test]
fn multipart_body_ends_with_the_closing_boundary_no_trailing_bytes() {
let metadata = serde_json::json!({});
let body = build_related_body(&metadata, b"x", "text/plain", "B");
assert!(body.ends_with(b"--B--"));
}
#[test]
fn multipart_body_preserves_lone_cr_and_lf_and_boundary_like_lines() {
let metadata = serde_json::json!({});
let content = b"a\rb\nc\r\n--not-really-a-boundary\r\nd".to_vec();
let body = build_related_body(&metadata, &content, "message/rfc822", "B");
assert!(body.windows(content.len()).any(|w| w == content.as_slice()));
}
#[test]
fn generate_boundary_produces_distinct_values() {
let a = generate_boundary();
let b = generate_boundary();
assert_ne!(a, b);
assert!(a.starts_with("omnidev-"));
}
#[test]
fn generate_boundary_absent_from_never_collides_with_content() {
let content = b"ordinary .eml bytes, nothing boundary-like here";
let boundary = generate_boundary_absent_from(content);
assert!(!contains_subslice(content, boundary.as_bytes()));
}
#[test]
fn contains_subslice_finds_needle_at_any_position() {
assert!(contains_subslice(b"abcdef", b"cd"));
assert!(contains_subslice(b"abcdef", b"abc"));
assert!(contains_subslice(b"abcdef", b"def"));
assert!(!contains_subslice(b"abcdef", b"xyz"));
}
#[test]
fn contains_subslice_empty_needle_is_never_contained() {
assert!(!contains_subslice(b"abc", b""));
}
}