swagger/multipart/
related.rs

1//! Helper functions for multipart/related support
2
3use hyper_0_10::header::{ContentType, Headers};
4use mime_0_2::Mime;
5
6/// Construct the Body for a multipart/related request. The mime 0.2.6 library
7/// does not parse quoted-string parameters correctly. The boundary doesn't
8/// need to be a quoted string if it does not contain a '/', hence ensure
9/// no such boundary is used.
10pub fn generate_boundary() -> Vec<u8> {
11    let mut boundary = mime_multipart::generate_boundary();
12    for b in boundary.iter_mut() {
13        if *b == b'/' {
14            *b = b'.';
15        }
16    }
17
18    boundary
19}
20
21/// Create the multipart headers from a request so that we can parse the
22/// body using `mime_multipart::read_multipart_body`.
23pub fn create_multipart_headers(
24    content_type: Option<&hyper::header::HeaderValue>,
25) -> Result<Headers, String> {
26    let content_type = content_type
27        .ok_or_else(|| "Missing Content-Type header".to_string())?
28        .to_str()
29        .map_err(|e| format!("Couldn't read Content-Type header value: {}", e))?
30        .parse::<Mime>()
31        .map_err(|_e| "Couldn't parse Content-Type header value".to_string())?;
32
33    // Insert top-level content type header into a Headers object.
34    let mut multipart_headers = Headers::new();
35    multipart_headers.set(ContentType(content_type));
36
37    Ok(multipart_headers)
38}