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
#![doc(html_root_url = "https://docs.rs/reqwest_resume/0.1.0")]
#![feature(nll)]
#![warn(
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
unused_results,
)]
#![cfg_attr(feature = "cargo-clippy", warn(clippy_pedantic))]
#![cfg_attr(feature = "cargo-clippy", allow(new_without_default))]
#[cfg(test)]
extern crate flate2;
extern crate reqwest;
#[macro_use]
extern crate log;
use std::io;
pub trait ClientExt {
fn resumable(self) -> Client;
}
impl ClientExt for reqwest::Client {
fn resumable(self) -> Client {
Client(self)
}
}
#[derive(Debug)]
pub struct Client(reqwest::Client);
impl Client {
pub fn new() -> Self {
Client(reqwest::Client::new())
}
pub fn get(&self, url: reqwest::Url) -> RequestBuilder {
RequestBuilder(self.0.clone(), reqwest::Method::Get, url)
}
}
#[derive(Debug)]
pub struct RequestBuilder(reqwest::Client, reqwest::Method, reqwest::Url);
impl RequestBuilder {
pub fn send(&mut self) -> reqwest::Result<Response> {
let mut builder = self.0.request(self.1.clone(), self.2.clone());
Ok(Response(
self.0.clone(),
self.1.clone(),
self.2.clone(),
builder.send()?,
0,
))
}
}
#[derive(Debug)]
pub struct Response(
reqwest::Client,
reqwest::Method,
reqwest::Url,
reqwest::Response,
u64,
);
impl Response {}
impl io::Read for Response {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
loop {
match self.3.read(buf) {
Ok(n) => {
self.4 += n as u64;
break Ok(n);
}
Err(err) => {
let accept_byte_ranges =
if let Some(&reqwest::header::AcceptRanges(ref ranges)) =
self.3.headers().get()
{
ranges
.iter()
.any(|u| *u == reqwest::header::RangeUnit::Bytes)
} else {
false
};
if accept_byte_ranges {
trace!("resuming HTTP request due to error {:?}", err);
let mut builder = self.0.request(self.1.clone(), self.2.clone());
let _ = builder.header(reqwest::header::Range::Bytes(vec![
reqwest::header::ByteRangeSpec::AllFrom(self.4),
]));
if let Ok(response) = builder.send() {
self.3 = response;
continue;
}
} else {
trace!("couldn't resume HTTP request with error {:?}", err);
}
break Err(err);
}
}
}
}
}
pub fn get(url: reqwest::Url) -> reqwest::Result<Response> {
Client::new().get(url).send()
}
#[cfg(test)]
mod test {
use flate2;
use reqwest;
use std::{
io::{self, BufRead, BufReader}, thread
};
#[test]
#[ignore]
fn dl_s3() {
let body = reqwest::get(
"http://commoncrawl.s3.amazonaws.com/crawl-data/CC-MAIN-2018-30/warc.paths.gz",
).unwrap();
let body = flate2::read::MultiGzDecoder::new(body);
let handles = BufReader::new(body)
.lines()
.map(|url| format!("http://commoncrawl.s3.amazonaws.com/{}", url.unwrap()))
.take(10)
.map(|url| {
println!("{}", url);
thread::spawn(move || {
let body = super::get(url.parse().unwrap()).unwrap();
let mut body = flate2::read::MultiGzDecoder::new(body);
let n = io::copy(&mut body, &mut io::sink()).unwrap();
println!("{}", n);
})
}).collect::<Vec<_>>();
for handle in handles {
handle.join().unwrap();
}
}
}