pub struct Http { /* private fields */ }Expand description
A Transport that fetches over HTTP and HTTPS, behind the crate’s http feature.
TLS is rustls with Mozilla’s root certificates compiled in (the webpki-roots crate), so it
needs no OpenSSL and ignores the operating system’s certificate store. It follows up to ten
redirects and sends Accept-Encoding: gzip, unpacking gzip bodies. Any status but 2xx is an
error, as are a timeout and a body that unpacks to more than the limit. An HTTP or HTTPS proxy
from the environment is used. A SOCKS one is refused with an error rather than bypassed, since
this build cannot speak SOCKS; hosts NO_PROXY exempts are fetched directly, with no redirect
followed, since the next address might not be exempt.
Cloning is cheap and the clones share one pool of connections.
use hpr_net::{Cache, Client, Http, Mode, Source};
let dir = Cache::platform_dir().expect("a home or local app data folder");
let client = Client::new(Http::new(), Cache::new(dir), Mode::Online);
let source = Source {
name: "Example".into(),
attribution: "Example data".into(),
ttl_s: 3600,
};
let now_s = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs());
let answer = client.fetch(&source, "https://example.com/", now_s)?;
println!("{} bytes, {:?}", answer.body.len(), answer.freshness);Implementations§
Source§impl Http
impl Http
Sourcepub const USER_AGENT: &str = "fusionspace-hpr/0.1.0 (+https://hpr.fusionspace.co)"
pub const USER_AGENT: &str = "fusionspace-hpr/0.1.0 (+https://hpr.fusionspace.co)"
The User-Agent header sent: the package’s name and version and the docs’ address, so a
data provider can tell who is calling: fusionspace-hpr/0.1.0 (+https://hpr.fusionspace.co)
(ADR-199 §4).
Sourcepub const MAX_TIMEOUT: Duration
pub const MAX_TIMEOUT: Duration
The longest timeout used: 30 days. A longer one, such as Duration::MAX for “none”,
counts as this, since a deadline past the clock’s range would overflow.
Sourcepub fn new() -> Self
pub fn new() -> Self
A transport with HttpConfig::default.
Sourcepub fn with_config(config: HttpConfig) -> Self
pub fn with_config(config: HttpConfig) -> Self
A transport with config.
Source§impl Http
impl Http
Sourcepub fn received(&self) -> Arc<AtomicU64>
pub fn received(&self) -> Arc<AtomicU64>
The body bytes, unpacked, that the fetch in progress has read so far, or the last fetch
read in all: set to 0 as a fetch starts and counted up as its body arrives, so another
thread can show a long fetch’s progress while Transport::get waits. A clone of this
transport shares the count.