1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

multipart boundary extraction

This commit is contained in:
Nikolay Kim 2017-10-22 09:30:05 -07:00
parent f85925a652
commit 6ad048d445
2 changed files with 32 additions and 4 deletions

View File

@ -191,7 +191,7 @@ impl HttpRequest {
/// ///
/// Content-type: multipart/form-data; /// Content-type: multipart/form-data;
pub fn multipart(&self, payload: Payload) -> Result<Multipart, MultipartError> { pub fn multipart(&self, payload: Payload) -> Result<Multipart, MultipartError> {
Ok(Multipart::new(Multipart::boundary(self)?, payload)) Ok(Multipart::new(Multipart::boundary(&self.headers)?, payload))
} }
/// Parse `application/x-www-form-urlencoded` encoded body. /// Parse `application/x-www-form-urlencoded` encoded body.

View File

@ -15,7 +15,6 @@ use futures::task::{Task, current as current_task};
use error::ParseError; use error::ParseError;
use payload::{Payload, PayloadError}; use payload::{Payload, PayloadError};
use httprequest::HttpRequest;
const MAX_HEADERS: usize = 32; const MAX_HEADERS: usize = 32;
@ -126,6 +125,7 @@ struct InnerMultipart {
} }
impl Multipart { impl Multipart {
/// Create multipart instance for boundary.
pub fn new(boundary: String, payload: Payload) -> Multipart { pub fn new(boundary: String, payload: Payload) -> Multipart {
Multipart { Multipart {
safety: Safety::new(), safety: Safety::new(),
@ -139,8 +139,9 @@ impl Multipart {
} }
} }
pub fn boundary(req: &HttpRequest) -> Result<String, MultipartError> { /// Extract boundary info from headers.
if let Some(content_type) = req.headers().get(header::CONTENT_TYPE) { pub fn boundary(headers: &HeaderMap) -> Result<String, MultipartError> {
if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
if let Ok(content_type) = content_type.to_str() { if let Ok(content_type) = content_type.to_str() {
if let Ok(ct) = content_type.parse::<mime::Mime>() { if let Ok(ct) = content_type.parse::<mime::Mime>() {
if let Some(boundary) = ct.get_param(mime::BOUNDARY) { 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");
}