From 6ad048d44567b6aed14a02ccb3729c6d5b9ff28f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 22 Oct 2017 09:30:05 -0700 Subject: [PATCH] multipart boundary extraction --- src/httprequest.rs | 2 +- src/multipart.rs | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/httprequest.rs b/src/httprequest.rs index 6e936d653..d101d40ea 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -191,7 +191,7 @@ impl HttpRequest { /// /// Content-type: multipart/form-data; pub fn multipart(&self, payload: Payload) -> Result { - Ok(Multipart::new(Multipart::boundary(self)?, payload)) + Ok(Multipart::new(Multipart::boundary(&self.headers)?, payload)) } /// Parse `application/x-www-form-urlencoded` encoded body. diff --git a/src/multipart.rs b/src/multipart.rs index 949fab049..09c73b633 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -15,7 +15,6 @@ use futures::task::{Task, current as current_task}; use error::ParseError; use payload::{Payload, PayloadError}; -use httprequest::HttpRequest; const MAX_HEADERS: usize = 32; @@ -126,6 +125,7 @@ struct InnerMultipart { } impl Multipart { + /// Create multipart instance for boundary. pub fn new(boundary: String, payload: Payload) -> Multipart { Multipart { safety: Safety::new(), @@ -139,8 +139,9 @@ impl Multipart { } } - pub fn boundary(req: &HttpRequest) -> Result { - if let Some(content_type) = req.headers().get(header::CONTENT_TYPE) { + /// Extract boundary info from headers. + pub fn boundary(headers: &HeaderMap) -> Result { + if let Some(content_type) = headers.get(header::CONTENT_TYPE) { if let Ok(content_type) = content_type.to_str() { if let Ok(ct) = content_type.parse::() { if let Some(boundary) = ct.get_param(mime::BOUNDARY) { @@ -689,3 +690,30 @@ impl Drop for Safety { } } } + +#[test] +fn test_boundary() { + let headers = HeaderMap::new(); + match Multipart::boundary(&headers) { + Err(MultipartError::NoContentType) => (), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert(header::CONTENT_TYPE, + header::HeaderValue::from_static("test")); + + match Multipart::boundary(&headers) { + Err(MultipartError::ParseContentType) => (), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static( + "multipart/mixed; boundary=\"5c02368e880e436dab70ed54e1c58209\"")); + + assert_eq!(Multipart::boundary(&headers).unwrap(), + "5c02368e880e436dab70ed54e1c58209"); +}