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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use mime::Mime;
use std::borrow::{Borrow, BorrowMut};
use std::convert::AsRef;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
#[cfg(feature = "hyper")]
pub mod hyper;
mod sized;
pub use self::sized::SizedRequest;
pub struct Multipart<S: HttpStream> {
stream: S,
boundary: String,
last_err: Option<S::Error>,
data_written: bool,
}
impl Multipart<io::Sink> {
pub fn from_request<R: HttpRequest>(mut req: R) -> Result<Multipart<R::Stream>, R::Error> {
let boundary = ::gen_boundary();
req.apply_headers(&boundary, None);
let stream = try!(req.open_stream());
Ok(Multipart {
stream: stream,
boundary: boundary,
last_err: None,
data_written: false,
})
}
}
impl<S: HttpStream> Multipart<S> {
pub fn last_err(&self) -> Option<&S::Error> {
self.last_err.as_ref()
}
pub fn take_err(&mut self) -> Option<S::Error> {
self.last_err.take()
}
pub fn write_text<N: Borrow<str>, V: Borrow<str>>(mut self, name: N, val: V) -> Self {
if self.last_err.is_none() {
self.last_err = chain_result! {
self.write_field_headers(name.borrow(), None, None),
self.write_line(val.borrow()),
self.write_boundary()
}.err().map(|err| err.into())
}
self
}
pub fn write_file<N: Borrow<str>, P: AsRef<Path>>(mut self, name: N, path: P) -> Self {
if self.last_err.is_none() {
let path = path.as_ref();
self.last_err = chain_result! {
{
let content_type = ::mime_guess::guess_mime_type(path);
let filename = path.file_name().and_then(|filename| filename.to_str());
self.write_field_headers(name.borrow(), filename, Some(content_type))
},
File::open(path).and_then(|ref mut file| io::copy(file, &mut self.stream)),
self.write_boundary()
}.err().map(|err| err.into());
}
self
}
pub fn write_stream<N: Borrow<str>, St: Read, St_: BorrowMut<St>>(
mut self, name: N, mut read: St_, filename: Option<&str>, content_type: Option<Mime>
) -> Self {
if self.last_err.is_none() {
let content_type = content_type.unwrap_or_else(::mime_guess::octet_stream);
self.last_err = chain_result! {
self.write_field_headers(name.borrow(), filename, Some(content_type)),
io::copy(read.borrow_mut(), &mut self.stream),
self.write_boundary()
}.err().map(|err| err.into());
}
self
}
fn write_field_headers(&mut self, name: &str, filename: Option<&str>, content_type: Option<Mime>)
-> io::Result<()> {
chain_result! {
write!(self.stream, "Content-Disposition: form-data; name=\"{}\"", name),
filename.map(|filename| write!(self.stream, "; filename=\"{}\"", filename))
.unwrap_or(Ok(())),
content_type.map(|content_type| write!(self.stream, "\r\nContent-Type: {}", content_type))
.unwrap_or(Ok(())),
self.write_line("\r\n")
}
}
fn write_line(&mut self, line: &str) -> io::Result<()> {
write!(self.stream, "{}\r\n", line)
}
fn write_boundary(&mut self) -> io::Result<()> {
write!(self.stream, "{}\r\n", self.boundary)
.map(|res| { self.data_written = true; res })
}
pub fn send(mut self) -> Result<S::Response, S::Error> {
match self.last_err {
None => {
if self.data_written {
try!(self.stream.write(b"--"));
}
self.stream.finish()
},
Some(err) => Err(err),
}
}
}
impl<R: HttpRequest> Multipart<SizedRequest<R>>
where <R::Stream as HttpStream>::Error: From<R::Error> {
pub fn from_request_sized(req: R) -> Result<Self, R::Error> {
Multipart::from_request(SizedRequest::from_request(req))
}
}
pub trait HttpRequest {
type Stream: HttpStream;
type Error: From<io::Error> + Into<<Self::Stream as HttpStream>::Error>;
fn apply_headers(&mut self, boundary: &str, content_len: Option<u64>) -> bool;
fn open_stream(self) -> Result<Self::Stream, Self::Error>;
}
pub trait HttpStream: Write {
type Request: HttpRequest;
type Response;
type Error: From<io::Error> + From<<Self::Request as HttpRequest>::Error>;
fn finish(self) -> Result<Self::Response, Self::Error>;
}
impl HttpRequest for () {
type Stream = io::Sink;
type Error = io::Error;
fn apply_headers(&mut self, _: &str, _: Option<u64>) -> bool { true }
fn open_stream(self) -> Result<Self::Stream, Self::Error> { Ok(io::sink()) }
}
impl HttpStream for io::Sink {
type Request = ();
type Response = ();
type Error = io::Error;
fn finish(self) -> Result<Self::Response, Self::Error> { Ok(()) }
}