chiark / gitweb /
server: wip, mime type
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 8 Aug 2021 11:25:30 +0000 (12:25 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 8 Aug 2021 11:25:30 +0000 (12:25 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/server.rs

index b885b9591f36b77a4a2743d94fd52961f73547a6..94fa8bcfda810c5f7a1e2f25e886af5f69814e93 100644 (file)
@@ -17,26 +17,29 @@ pub struct Opts {
 async fn handle(
 //    context: (),
 //    addr: SocketAddr,
-    _req: hyper::Request<hyper::Body>
+    req: hyper::Request<hyper::Body>
 ) -> Result<hyper::Response<hyper::Body>, Infallible> {
 
   let (boundary, warning) = (||{
-    let mut ctypes = req.headers().get_all();
+    let mut ctypes = req.headers().get_all("Content-Type").iter();
     let t = ctypes.next().ok_or_else(|| anyhow!("missing Content-Type"))?;
-    if ctypes.next().is_soe() { throw!(anyhow!("several Content-Type")) }
+    if ctypes.next().is_some() { throw!(anyhow!("several Content-Type")) }
+    let t = t.to_str().context("interpret Content-Type as utf-8")?;
     let t: mime::Mime = t.parse().context("parse Content-Type")?;
     if t.type_() != "multipart" { throw!(anyhow!("not multipart/")) }
-    let b = t.get_param(mime::BOUNDARY).context("missing boundary=...")?;
+    let b = mime::BOUNDARY;
+    let b = t.get_param(b).ok_or_else(|| anyhow!("missing boundary=..."))?;
     let warning = (||{
-      if t.subtype != "form-data" { throw!(anyhow("not multipart/form-data")) }
+      if t.subtype() != "form-data" { throw!(anyhow!("not /form-data"))}
       Ok::<_,AE>(())
     })();
-    Ok::<_,AE>((b, warning))
-  }()).unwrap_or_else(|e| {
-    ("b", Err(e.context("guessing boundary")))
+    Ok::<_,AE>((b.as_str().to_owned(), warning))
+  })().unwrap_or_else(|e| {
+    ("b".into(), Err(e.wrap_err("guessing boundary")))
   });
 
-  eprintln!("boundary={} warning={}", boundary, warning);
+  eprintln!("boundary={}", boundary);
+  if let Err(w) = &warning { eprintln!("warning={}", w); }
 
 
   Ok(hyper::Response::new(hyper::Body::from("Hello World")))