pub struct HttpClient { /* private fields */ }Expand description
HTTP transport for fetching control files and byte ranges.
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new() -> Self
pub fn new() -> Self
A client permitting plain HTTP, with a bound on reaching a host.
zsync is routinely served over HTTP and across mirror redirects, so
the scheme policy is permissive on purpose. An embedder with a
stricter one, such as one that must refuse a downgrade to HTTP or
pin its own trust roots, should build an agent and pass it to
HttpClient::with_agent rather than relying on these.
CONNECT_TIMEOUT and RESOLVE_TIMEOUT apply. Neither bounds the
transfer itself, so a mirror that trickles bytes for an hour is left
alone; they only cap how long a host that never answers can cost.
Sourcepub fn with_agent(agent: Agent) -> Self
pub fn with_agent(agent: Agent) -> Self
A client using a caller-supplied agent.
The agent carries the whole transport policy: TLS roots, whether a redirect may downgrade to HTTP, timeouts and proxies. Supplying one keeps that decision with the embedder, which is the only place that knows what the transferred bytes are trusted for.
Sourcepub fn fetch_control_file(&self, url: &str) -> Result<ControlFile, HttpError>
pub fn fetch_control_file(&self, url: &str) -> Result<ControlFile, HttpError>
Fetches and parses the control file at url.
§Errors
Returns HttpError::Http when the request fails or the response
does not parse as a control file.
Sourcepub fn fetch_range_reader(
&self,
url: &str,
start: u64,
end: u64,
) -> Result<HttpRangeReader, HttpError>
pub fn fetch_range_reader( &self, url: &str, start: u64, end: u64, ) -> Result<HttpRangeReader, HttpError>
A reader over start..=end of url.
The reader is capped at the requested length. Without that cap the origin decides how much the client reads: a response longer than the range is consumed in full, and callers that accumulate it, such as block assembly, grow a buffer to match. Bytes past the range were never asked for and are of no use, so they are not read.
A 200 OK is accepted as well as 206 Partial Content. A server that
ignores Range sends the file from its first byte, so for a range
that does not start at zero the reader yields the wrong bytes, and it
falls to the caller’s block checksums to reject them.
§Errors
Returns HttpError::Http when the request fails or the status is
neither 200 nor 206.
Sourcepub fn fetch_range(
&self,
url: &str,
start: u64,
end: u64,
) -> Result<Vec<u8>, HttpError>
pub fn fetch_range( &self, url: &str, start: u64, end: u64, ) -> Result<Vec<u8>, HttpError>
Fetches start..=end of url into memory.
The same cap and status handling as Self::fetch_range_reader
apply.
§Errors
Returns HttpError::Http when the request fails and
HttpError::Io when reading the body fails.
Sourcepub fn fetch_ranges(
&self,
url: &str,
ranges: &[(u64, u64)],
blocksize: usize,
) -> Result<Vec<(u64, Vec<u8>)>, HttpError>
pub fn fetch_ranges( &self, url: &str, ranges: &[(u64, u64)], blocksize: usize, ) -> Result<Vec<(u64, Vec<u8>)>, HttpError>
Fetches each inclusive range in ranges, one request at a time.
Each body is paired with its start offset rounded down to a multiple
of blocksize. The ranges are expected to start on a block boundary,
as those from byte_ranges_from_block_ranges do; otherwise the
offset names a point before the body’s first byte.
§Errors
Stops at and returns the first error from Self::fetch_range.