|
| 1 | +//! Utilities for chunk upload tests. |
| 2 | +use std::collections::HashSet; |
| 3 | +use std::error::Error; |
| 4 | +use std::str; |
| 5 | +use std::sync::LazyLock; |
| 6 | + |
| 7 | +use mockito::Request; |
| 8 | +use regex::bytes::Regex; |
| 9 | + |
| 10 | +/// This regex is used to extract the boundary from the content-type header. |
| 11 | +/// We need to match the boundary, since it changes with each request. |
| 12 | +/// The regex matches the format as specified in |
| 13 | +/// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html. |
| 14 | +static CONTENT_TYPE_REGEX: LazyLock<Regex> = LazyLock::new(|| { |
| 15 | + Regex::new( |
| 16 | + r#"^multipart\/form-data; boundary=(?<boundary>[\w'\(\)+,\-\.\/:=? ]{0,69}[\w'\(\)+,\-\.\/:=?])$"# |
| 17 | + ) |
| 18 | + .expect("Regex is valid") |
| 19 | +}); |
| 20 | + |
| 21 | +/// A trait which abstracts over accessing headers from a mock request. |
| 22 | +/// Allows future compatibility in case we switch to a different mock library. |
| 23 | +pub trait HeaderContainer { |
| 24 | + fn header(&self, header_name: &str) -> Vec<&[u8]>; |
| 25 | +} |
| 26 | + |
| 27 | +impl HeaderContainer for Request { |
| 28 | + fn header(&self, header_name: &str) -> Vec<&[u8]> { |
| 29 | + self.header(header_name) |
| 30 | + .iter() |
| 31 | + .map(|h| h.as_bytes()) |
| 32 | + .collect() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Split a multipart/form-data body into its constituent chunks. |
| 37 | +/// The chunks are returned as a set, since chunk uploading code |
| 38 | +/// does not guarantee any specific order of the chunks in the body. |
| 39 | +/// We only want to check the invariant that each expected chunk is |
| 40 | +/// in the body, not the order of the chunks. |
| 41 | +pub fn split_chunk_body<'b>( |
| 42 | + body: &'b [u8], |
| 43 | + boundary: &str, |
| 44 | +) -> Result<HashSet<&'b [u8]>, Box<dyn Error>> { |
| 45 | + let escaped_boundary = regex::escape(boundary); |
| 46 | + |
| 47 | + let inner_body = entire_body_regex(&escaped_boundary) |
| 48 | + .captures(body) |
| 49 | + .ok_or("body does not match multipart form regex")? |
| 50 | + .name("body") |
| 51 | + .expect("the regex has a \"body\" capture group which should always match") |
| 52 | + .as_bytes(); |
| 53 | + |
| 54 | + // Using HashSet does have the small disadvantage that we don't |
| 55 | + // preserve the count of any duplicate chunks, so our tests will |
| 56 | + // fail to detect when the same chunk is included multiple times |
| 57 | + // (this would be a bug). But, this way, we don't need to keep |
| 58 | + // track of counts of chunks. |
| 59 | + Ok(boundary_regex(&escaped_boundary) |
| 60 | + .split(inner_body) |
| 61 | + .collect()) |
| 62 | +} |
| 63 | + |
| 64 | +/// Extract the boundary from a multipart/form-data request content-type header. |
| 65 | +/// Returns an error if the content-type header is not present exactly once, |
| 66 | +/// if the content-type does not match the multipart/form-data regex, or if the |
| 67 | +/// boundary is not valid UTF-8. |
| 68 | +pub fn boundary_from_request(request: &impl HeaderContainer) -> Result<&str, Box<dyn Error>> { |
| 69 | + let content_type_headers = request.header("content-type"); |
| 70 | + |
| 71 | + if content_type_headers.len() != 1 { |
| 72 | + return Err(format!( |
| 73 | + "content-type header should be present exactly once, found {} times", |
| 74 | + content_type_headers.len() |
| 75 | + ) |
| 76 | + .into()); |
| 77 | + } |
| 78 | + |
| 79 | + let content_type = content_type_headers[0]; |
| 80 | + |
| 81 | + let boundary = CONTENT_TYPE_REGEX |
| 82 | + .captures(content_type) |
| 83 | + .ok_or("content-type does not match multipart/form-data regex")? |
| 84 | + .name("boundary") |
| 85 | + .expect("if the regex matches, the boundary should match as well.") |
| 86 | + .as_bytes(); |
| 87 | + |
| 88 | + Ok(str::from_utf8(boundary)?) |
| 89 | +} |
| 90 | + |
| 91 | +/// Given the regex-escaped boundary of a multipart form, return a regex which |
| 92 | +/// should match the entire body of the form. The regex includes a named capture |
| 93 | +/// group for the body (named "body"), which includes everything from the first starting |
| 94 | +/// boundary to the final ending boundary (non-inclusive of the boundaries). |
| 95 | +/// May panic if the boundary is not regex-escaped. |
| 96 | +fn entire_body_regex(regex_escaped_boundary: &str) -> Regex { |
| 97 | + Regex::new(&format!( |
| 98 | + r#"^--{regex_escaped_boundary}(?<body>(?s-u:.*?))--{regex_escaped_boundary}--\s*$"# |
| 99 | + )) |
| 100 | + .expect("This regex should be valid") |
| 101 | +} |
| 102 | + |
| 103 | +/// Given the regex-escaped boundary of a multipart form, return a regex which |
| 104 | +/// matches the start of a section of the form. |
| 105 | +fn boundary_regex(regex_escaped_boundary: &str) -> Regex { |
| 106 | + Regex::new(&format!(r#"--{regex_escaped_boundary}"#)).expect("This regex should be valid") |
| 107 | +} |
0 commit comments