Skip to content

Commit

Permalink
Rewrite part of function retrieve_asset, include support for brotli a…
Browse files Browse the repository at this point in the history
…nd deflate (#312)

do not crash the app if reqwest throws, add support for deflate & brotli
  • Loading branch information
Sunshine authored Aug 7, 2022
1 parent 68a1531 commit 0df8613
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 24 deletions.
60 changes: 47 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ url = "2.2.2"
[dependencies.reqwest]
version = "0.11.11"
default-features = false
features = ["default-tls", "blocking", "gzip"]
features = ["default-tls", "blocking", "gzip", "brotli", "deflate"]

[dev-dependencies]
assert_cmd = "2.0.4"
41 changes: 31 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ pub fn retrieve_asset(
} else {
// URL not in cache, we retrieve the file
match client.get(url.as_str()).send() {
Ok(mut response) => {
if !options.ignore_errors && response.status() != 200 {
Ok(response) => {
if !options.ignore_errors && response.status() != reqwest::StatusCode::OK {
if !options.silent {
eprintln!(
"{}{}{} ({}){}",
Expand All @@ -258,19 +258,17 @@ pub fn retrieve_asset(
return Err(client.get("").send().unwrap_err());
}

let response_url: Url = response.url().clone();

if !options.silent {
if url.as_str() == response.url().as_str() {
if url.as_str() == response_url.as_str() {
eprintln!("{}{}", indent(depth).as_str(), &url);
} else {
eprintln!("{}{} -> {}", indent(depth).as_str(), &url, &response.url());
eprintln!("{}{} -> {}", indent(depth).as_str(), &url, &response_url);
}
}

let new_cache_key: String = clean_url(response.url().clone()).to_string();

// Convert response into a byte array
let mut data: Vec<u8> = vec![];
response.copy_to(&mut data).unwrap();
let new_cache_key: String = clean_url(response_url.clone()).to_string();

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

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

// Convert response into a byte array
let mut data: Vec<u8> = vec![];
match response.bytes() {
Ok(b) => {
data = b.to_vec();
}
Err(error) => {
if !options.silent {
eprintln!(
"{}{}{}{}",
indent(depth).as_str(),
if options.no_color { "" } else { ANSI_COLOR_RED },
error,
if options.no_color {
""
} else {
ANSI_COLOR_RESET
},
);
}
}
}

// Add retrieved resource to cache
cache.insert(new_cache_key, data.clone());

// Return
Ok((data, response.url().clone(), media_type, charset))
Ok((data, response_url, media_type, charset))
}
Err(error) => {
if !options.silent {
Expand Down

0 comments on commit 0df8613

Please sign in to comment.