Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Update to ureq 2
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Feb 7, 2021
1 parent 29c23c7 commit d94a0e1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 238 deletions.
212 changes: 2 additions & 210 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 @@ -23,7 +23,7 @@ fehler = "^1"
anyhow = "^1"
shell-words = "^1"
env_proxy = "^0.4"
ureq = { version = "^1.5", features = ["tls", "native-certs"] }
ureq = { version = "^2.0", features = ["tls", "native-certs"] }

[dependencies.clap]
version = "^2.33"
Expand Down
55 changes: 28 additions & 27 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use anyhow::{anyhow, Context, Error, Result};
use fehler::{throw, throws};
use std::fs::File;
use std::io::prelude::*;
use ureq::AgentBuilder;
use url::Url;

/// What kind of resources mdcat may access when rendering.
Expand Down Expand Up @@ -42,31 +43,31 @@ fn is_local(url: &Url) -> bool {

#[throws]
fn fetch_http(url: &Url) -> Vec<u8> {
let mut request = ureq::get(url.as_str());
request.set("User-Agent", concat!("mdcat/", env!("CARGO_PKG_VERSION")));
if let Some(proxy) = env_proxy::for_url(url).to_string() {
request.set_proxy(
ureq::Proxy::new(&proxy)
.with_context(|| format!("Failed to set proxy for URL {} to {}", url, &proxy))?,
);
}

let response = request.call();
if response.ok() {
let mut buffer = Vec::new();
response
.into_reader()
.read_to_end(&mut buffer)
.with_context(|| format!("Failed to read from URL {}", url))?;
buffer
} else {
throw!(anyhow!(
"GET {} failed with HTTP error status {} (synthetic error: {:?})",
url,
response.status_line(),
response.synthetic_error()
))
}
let proxy = match env_proxy::for_url(url).to_string() {
None => None,
Some(proxy_url) => {
let proxy = ureq::Proxy::new(&proxy_url).with_context(|| {
format!("Failed to set proxy for URL {} to {}", url, &proxy_url)
})?;
Some(proxy)
}
};

let mut buffer = Vec::new();
proxy
.map_or(AgentBuilder::new(), |proxy| {
AgentBuilder::new().proxy(proxy)
})
.build()
.request_url("GET", url)
.set("User-Agent", concat!("mdcat/", env!("CARGO_PKG_VERSION")))
.call()
.with_context(|| format!("Failed to GET {}", url))?
.into_reader()
.read_to_end(&mut buffer)
.with_context(|| format!("Failed to read {}", url))?;

buffer
}

/// Read the contents of the given `url` if supported.
Expand Down Expand Up @@ -151,8 +152,8 @@ mod tests {
.unwrap();
let result = read_url(&url, ResourceAccess::RemoteAllowed);
assert!(result.is_err(), "Unexpected success: {:?}", result);
let error = result.unwrap_err().to_string();
assert_eq!(error, "GET https://eu.httpbin.org/status/404 failed with HTTP error status HTTP/1.1 404 NOT FOUND (synthetic error: None)")
let error = format!("{:#}", result.unwrap_err());
assert_eq!(error, "Failed to GET https://eu.httpbin.org/status/404: https://eu.httpbin.org/status/404: status code 404")
}

#[test]
Expand Down

0 comments on commit d94a0e1

Please sign in to comment.