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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2017 rust-hyper-multipart-rfc7578 Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//

//! This crate contains an implementation of the multipart/form-data media
//! type described in [RFC 7578](https://tools.ietf.org/html/rfc7578) for
//! hyper.
//!
//! Currently, only the client-side is implemented.
//!
//! ## Usage
//!
//! ```toml
//! [dependencies]
//! hyper-multipart-rfc7578 = "0.1.0-alpha2"
//! ```
//!
//! Because the name of this library is really wordy, I recommend shortening it:
//!
//! ```rust
//! extern crate hyper_multipart_rfc7578 as hyper_multipart;
//! ```
//!
//! Using this requires a hyper client compatible with the `multipart::Body`
//! data structure (see the documentation for more detailed examples):
//!
//! ```rust
//! # extern crate hyper;
//! # extern crate hyper_multipart_rfc7578;
//! # extern crate tokio_core;
//!
//! use hyper::{Method, Request};
//! use hyper::client::Client;
//! use hyper_multipart_rfc7578::client::{self, multipart};
//! use tokio_core::reactor::{Core, Handle};
//!
//! # fn main() {
//! let mut core = Core::new().unwrap();
//! let client: Client<_, multipart::Body> = client::create(&core.handle());
//! let mut req = Request::new(Method::Get, "http://localhost/upload".parse().unwrap());
//! let mut form = multipart::Form::default();
//!
//! form.add_text("test", "Hello World");
//! form.set_body(&mut req);
//!
//! core.run(client.request(req));
//! # }
//! ```
//!
extern crate bytes;
extern crate futures;
extern crate hyper;
extern crate rand;
extern crate tokio_core;

mod client_;

/// # Multipart RFC 7578 - Client
///
/// This module contains code related to making requests with a
/// multipart/form body.
///
/// All data structures related to constructing a multipart/form body
/// are in the submodule [`multipart`](/hyper_multipart_rfc7578/client/index.html).
///
/// This module contains a helper method to build a hyper client that can
/// send a multipart body. [See](/hyper_multipart_rfc7578/client/fn.create.html).
///
/// # Examples
///
/// The most basic scenario would be creating a client with the provided
/// function.
///
/// ```
/// # extern crate hyper;
/// # extern crate hyper_multipart_rfc7578;
/// # extern crate tokio_core;
/// #
/// use hyper::{Method, Request};
/// use hyper::client::Client;
/// use hyper_multipart_rfc7578::client::{self, multipart};
/// use tokio_core::reactor::{Core, Handle};
///
/// # fn main() {
/// let mut core = Core::new().unwrap();
/// let client: Client<_, multipart::Body> = client::create(&core.handle());
/// let mut req = Request::new(Method::Get, "http://localhost/upload".parse().unwrap());
/// let mut form = multipart::Form::default();
///
/// form.add_text("test", "Hello World");
/// form.set_body(&mut req);
///
/// core.run(client.request(req));
/// # }
/// ```
///
/// You can also manually create a hyper client with a different connector,
/// or different configuration using hyper's `Config` object.
///
/// ```
/// # extern crate hyper;
/// # extern crate hyper_multipart_rfc7578;
/// # extern crate hyper_tls;
/// # extern crate tokio_core;
/// #
/// use hyper::{Method, Request};
/// use hyper::client::{Client, Config};
/// use hyper_multipart_rfc7578::client::multipart;
/// use hyper_tls::HttpsConnector;
/// use tokio_core::reactor::{Core, Handle};
///
/// # fn main() {
/// let mut core = Core::new().unwrap();
/// let client: Client<HttpsConnector<_>, multipart::Body> =
///     Config::default()
///         .body::<multipart::Body>()
///         .connector(HttpsConnector::new(2, &core.handle()).unwrap())
///         .keep_alive(true)
///         .build(&core.handle());
/// let mut req = Request::new(Method::Get, "http://localhost/upload".parse().unwrap());
/// let mut form = multipart::Form::default();
///
/// form.add_text("test", "Hello World");
/// form.set_body(&mut req);
///
/// core.run(client.request(req));
/// # }
/// ```
///
pub mod client {
    /// This module contains data structures for building a multipart/form
    /// body to send a server.
    ///
    pub mod multipart {
        pub use client_::{Body, BoundaryGenerator, Form, Part};
    }

    use hyper::client::{Client, Config, HttpConnector};
    use tokio_core::reactor::Handle;

    /// Creates a hyper client with a multipart body.
    ///
    /// # Examples
    ///
    /// ```
    /// # extern crate hyper;
    /// # extern crate hyper_multipart_rfc7578;
    /// # extern crate tokio_core;
    /// #
    /// use hyper_multipart_rfc7578::client::{self, multipart};
    /// use hyper::client::Client;
    /// use tokio_core::reactor::{Core, Handle};
    ///
    /// # fn main() {
    /// let core = Core::new().unwrap();
    /// let client: Client<_, multipart::Body> = client::create(&core.handle());
    /// # }
    /// ```
    ///
    pub fn create(handle: &Handle) -> Client<HttpConnector, multipart::Body> {
        Config::default().body::<multipart::Body>().build(handle)
    }
}