chiark / gitweb /
server, multipart: adjust some dbg
[hippotat.git] / src / multipart.rs
index b076458100315b3cf37469a0086b763043def6bd..77d3335ce30678e3b606676955aaa1a5f77d4f75 100644 (file)
@@ -53,6 +53,11 @@ pub fn process_boundary<'b>(warnings: &mut Warnings,
       if disposition.len() >= 100 { throw!(anyhow!(
         "Content-Disposition value implausibly long"
       )) }
+
+      // todo: replace with mailparse?
+      // (not in side, dep on charset not in sid)
+      // also seems to box for all the bits
+
       // This let's us pretend it's a mime type, so we can use mime::Mime
       let disposition = format!("dummy/{}", disposition);
 
@@ -77,9 +82,73 @@ pub fn process_boundary<'b>(warnings: &mut Warnings,
     };
   }
 
+  //dbg!(DumpHex(rhs));
+
   Some(Component { name: part_name.unwrap_or(expected), payload: rhs })
 }
 
+pub struct ComponentIterator<'b> {
+  at_boundary: &'b [u8],
+  boundary_finder: BoundaryFinder,
+}
+
+#[derive(Error,Debug)]
+#[error("missing mime multipart boundary")]
+pub struct MissingBoundary;
+
+impl<'b> ComponentIterator<'b> {
+  #[throws(MissingBoundary)]
+  pub fn resume_mid_component(buf: &'b [u8], boundary_finder: BoundaryFinder)
+                              -> (&'b [u8], Self) {
+    let next_boundary = boundary_finder.find(buf).ok_or(MissingBoundary)?;
+    let part = &buf[0..next_boundary];
+    let part = Self::payload_trim(part);
+
+    //dbg!(DumpHex(part));
+
+    (part, ComponentIterator {
+      at_boundary: &buf[next_boundary..],
+      boundary_finder,
+    })
+  }
+
+  fn payload_trim(payload: &[u8]) -> &[u8] {
+    payload.strip_suffix(b"\r").unwrap_or(payload)
+  }
+
+  #[throws(AE)]
+  pub fn next(&mut self, warnings: &mut Warnings, expected: PartName)
+              -> Option<Component<'b>> {
+    if self.at_boundary.is_empty() { return None }
+
+    let mut comp = match {
+      //dbg!(DumpHex(self.boundary_finder.needle()));
+      let boundary_len = self.boundary_finder.needle().len();
+      //dbg!(boundary_len);
+      process_boundary(warnings,
+                       &self.at_boundary[boundary_len..],
+                       expected)?
+    } {
+      None => {
+        self.at_boundary = &self.at_boundary[0..0];
+        return None;
+      },
+      Some(c) => c,
+    };
+
+    let next_boundary = self.boundary_finder.find(&comp.payload)
+      .ok_or(MissingBoundary)?;
+
+    comp.payload = Self::payload_trim(&comp.payload[0..next_boundary]);
+    self.at_boundary = &self.at_boundary[next_boundary..];
+
+    //dbg!(DumpHex(comp.payload));
+    //dbg!(DumpHex(&self.at_boundary[0..5]));
+
+    Some(comp)
+  }
+}
+
 pub struct MetadataFieldIterator<'b> {
   buf: &'b [u8],
   last: Option<usize>,