1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Client- and server-side abstractions for HTTP `multipart/form-data` requests.
//!
//! Features: 
//!
//! * `hyper`: Enable client- and server-side integration with the
//! [Hyper](https:://github.com/hyperium/hyper) HTTP library.
#![warn(missing_docs)]
#[macro_use] extern crate log;
extern crate env_logger;

extern crate mime;
extern crate mime_guess;
extern crate rand;

#[cfg(feature = "hyper")]
extern crate hyper;

use rand::Rng;

use std::path::PathBuf;

macro_rules! try_all {
    ($first_expr:expr, $($try_expr:expr),*) => (
        try!($first_expr $(.and_then(|_| $try_expr))*);
    )
}

macro_rules! chain_result {
    ($first_expr:expr, $($try_expr:expr),*) => (
        $first_expr $(.and_then(|_| $try_expr))*
    )
}

pub mod client;
pub mod server;

const BOUNDARY_LEN: usize = 16;
const DIRNAME_LEN: usize = 12;

fn temp_dir() -> PathBuf {
    random_alphanumeric(DIRNAME_LEN).into()
}

fn random_alphanumeric(len: usize) -> String {
    rand::thread_rng().gen_ascii_chars().take(len).collect()
}

fn gen_boundary() -> String {
    let mut boundary = "--".to_owned();
    boundary.extend(rand::thread_rng().gen_ascii_chars().take(BOUNDARY_LEN));
    boundary
}