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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::io::{self, Read, Write, Cursor, Chain};
use std::path::Path;
use std::fs::File;
use std::time::Duration;
#[cfg(feature = "tls")] use super::net_stream::HttpsStream;
use super::data_stream::{DataStream, kill_stream};
use super::net_stream::NetStream;
use ext::ReadExt;
use http::hyper;
use http::hyper::h1::HttpReader;
use http::hyper::h1::HttpReader::*;
use http::hyper::net::{HttpStream, NetworkStream};
pub type HyperBodyReader<'a, 'b> =
self::HttpReader<&'a mut hyper::buffer::BufReader<&'b mut NetworkStream>>;
pub type BodyReader = HttpReader<Chain<Cursor<Vec<u8>>, NetStream>>;
const PEEK_BYTES: usize = 512;
pub struct Data {
buffer: Vec<u8>,
is_complete: bool,
stream: BodyReader,
}
impl Data {
pub fn open(mut self) -> DataStream {
let buffer = ::std::mem::replace(&mut self.buffer, vec![]);
let empty_stream = Cursor::new(vec![]).chain(NetStream::Empty);
let empty_http_stream = HttpReader::SizedReader(empty_stream, 0);
let stream = ::std::mem::replace(&mut self.stream, empty_http_stream);
DataStream(Cursor::new(buffer).chain(stream))
}
pub(crate) fn from_hyp(mut body: HyperBodyReader) -> Result<Data, &'static str> {
let (mut hyper_buf, pos, cap) = body.get_mut().take_buf();
unsafe { hyper_buf.set_len(cap); }
let hyper_net_stream = body.get_ref().get_ref();
#[cfg(feature = "tls")]
#[inline(always)]
fn concrete_stream(stream: &&mut NetworkStream) -> Option<NetStream> {
stream.downcast_ref::<HttpsStream>()
.map(|s| NetStream::Https(s.clone()))
.or_else(|| {
stream.downcast_ref::<HttpStream>()
.map(|s| NetStream::Http(s.clone()))
})
}
#[cfg(not(feature = "tls"))]
#[inline(always)]
fn concrete_stream(stream: &&mut NetworkStream) -> Option<NetStream> {
stream.downcast_ref::<HttpStream>()
.map(|s| NetStream::Http(s.clone()))
}
let net_stream = match concrete_stream(hyper_net_stream) {
Some(net_stream) => net_stream,
None => return Err("Stream is not an HTTP(s) stream!")
};
net_stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout set");
trace_!("Hyper buffer: [{}..{}] ({} bytes).", pos, cap, cap - pos);
let mut cursor = Cursor::new(hyper_buf);
cursor.set_position(pos as u64);
let inner_data = cursor.chain(net_stream);
let http_stream = match body {
SizedReader(_, n) => SizedReader(inner_data, n),
EofReader(_) => EofReader(inner_data),
EmptyReader(_) => EmptyReader(inner_data),
ChunkedReader(_, n) => ChunkedReader(inner_data, n)
};
Ok(Data::new(http_stream))
}
#[inline(always)]
pub fn peek(&self) -> &[u8] {
if self.buffer.len() > PEEK_BYTES {
&self.buffer[..PEEK_BYTES]
} else {
&self.buffer
}
}
#[inline(always)]
pub fn peek_complete(&self) -> bool {
self.is_complete
}
#[inline(always)]
pub fn stream_to<W: Write>(self, writer: &mut W) -> io::Result<u64> {
io::copy(&mut self.open(), writer)
}
#[inline(always)]
pub fn stream_to_file<P: AsRef<Path>>(self, path: P) -> io::Result<u64> {
io::copy(&mut self.open(), &mut File::create(path)?)
}
#[inline(always)]
pub(crate) fn new(mut stream: BodyReader) -> Data {
trace_!("Date::new({:?})", stream);
let mut peek_buf = vec![0; PEEK_BYTES];
let eof = match stream.read_max(&mut peek_buf[..]) {
Ok(n) => {
trace_!("Filled peek buf with {} bytes.", n);
unsafe { peek_buf.set_len(n); }
n < PEEK_BYTES
}
Err(e) => {
error_!("Failed to read into peek buffer: {:?}.", e);
unsafe { peek_buf.set_len(0); }
false
},
};
trace_!("Peek bytes: {}/{} bytes.", peek_buf.len(), PEEK_BYTES);
Data {
buffer: peek_buf,
stream: stream,
is_complete: eof,
}
}
#[inline]
pub(crate) fn local(data: Vec<u8>) -> Data {
let empty_stream = Cursor::new(vec![]).chain(NetStream::Empty);
Data {
buffer: data,
stream: HttpReader::SizedReader(empty_stream, 0),
is_complete: true,
}
}
}
impl Drop for Data {
fn drop(&mut self) {
kill_stream(&mut self.stream);
}
}