Skip to content

Commit 0df8613

Browse files
author
Sunshine
authored
Rewrite part of function retrieve_asset, include support for brotli and deflate (#312)
do not crash the app if reqwest throws, add support for deflate & brotli
1 parent 68a1531 commit 0df8613

File tree

3 files changed

+79
-24
lines changed

3 files changed

+79
-24
lines changed

Cargo.lock

Lines changed: 47 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ url = "2.2.2"
3737
[dependencies.reqwest]
3838
version = "0.11.11"
3939
default-features = false
40-
features = ["default-tls", "blocking", "gzip"]
40+
features = ["default-tls", "blocking", "gzip", "brotli", "deflate"]
4141

4242
[dev-dependencies]
4343
assert_cmd = "2.0.4"

src/utils.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ pub fn retrieve_asset(
238238
} else {
239239
// URL not in cache, we retrieve the file
240240
match client.get(url.as_str()).send() {
241-
Ok(mut response) => {
242-
if !options.ignore_errors && response.status() != 200 {
241+
Ok(response) => {
242+
if !options.ignore_errors && response.status() != reqwest::StatusCode::OK {
243243
if !options.silent {
244244
eprintln!(
245245
"{}{}{} ({}){}",
@@ -258,19 +258,17 @@ pub fn retrieve_asset(
258258
return Err(client.get("").send().unwrap_err());
259259
}
260260

261+
let response_url: Url = response.url().clone();
262+
261263
if !options.silent {
262-
if url.as_str() == response.url().as_str() {
264+
if url.as_str() == response_url.as_str() {
263265
eprintln!("{}{}", indent(depth).as_str(), &url);
264266
} else {
265-
eprintln!("{}{} -> {}", indent(depth).as_str(), &url, &response.url());
267+
eprintln!("{}{} -> {}", indent(depth).as_str(), &url, &response_url);
266268
}
267269
}
268270

269-
let new_cache_key: String = clean_url(response.url().clone()).to_string();
270-
271-
// Convert response into a byte array
272-
let mut data: Vec<u8> = vec![];
273-
response.copy_to(&mut data).unwrap();
271+
let new_cache_key: String = clean_url(response_url.clone()).to_string();
274272

275273
// Attempt to obtain media type and charset by reading Content-Type header
276274
let content_type: &str = response
@@ -281,11 +279,34 @@ pub fn retrieve_asset(
281279

282280
let (media_type, charset, _is_base64) = parse_content_type(&content_type);
283281

282+
// Convert response into a byte array
283+
let mut data: Vec<u8> = vec![];
284+
match response.bytes() {
285+
Ok(b) => {
286+
data = b.to_vec();
287+
}
288+
Err(error) => {
289+
if !options.silent {
290+
eprintln!(
291+
"{}{}{}{}",
292+
indent(depth).as_str(),
293+
if options.no_color { "" } else { ANSI_COLOR_RED },
294+
error,
295+
if options.no_color {
296+
""
297+
} else {
298+
ANSI_COLOR_RESET
299+
},
300+
);
301+
}
302+
}
303+
}
304+
284305
// Add retrieved resource to cache
285306
cache.insert(new_cache_key, data.clone());
286307

287308
// Return
288-
Ok((data, response.url().clone(), media_type, charset))
309+
Ok((data, response_url, media_type, charset))
289310
}
290311
Err(error) => {
291312
if !options.silent {

0 commit comments

Comments
 (0)