Skip to main content

HttpClient

Struct HttpClient 

Source
pub struct HttpClient { /* private fields */ }
Expand description

HTTP transport for fetching control files and byte ranges.

Implementations§

Source§

impl HttpClient

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for HttpClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.