pub struct AsyncHttpRangeReader { /* private fields */ }
Expand description

An AsyncRangeReader enables reading from a file over HTTP using range requests.

See the crate level documentation for more information.

The general entrypoint is AsyncHttpRangeReader::new. Depending on the CheckSupportMethod, this will either call AsyncHttpRangeReader::initial_tail_request or AsyncHttpRangeReader::initial_head_request to send the initial request and then AsyncHttpRangeReader::from_tail_response or AsyncHttpRangeReader::from_head_response to initialize the async reader. If you want to apply a caching layer, you can send the initial head (or tail) request yourself with your cache headers (e.g. through the http-cache-semantics crate):

async fn get_reader_cached(
    url: Url,
) -> Result<Option<AsyncHttpRangeReader>, AsyncHttpRangeReaderError> {
    let etag = "63c550e8-5ae";
    let client = reqwest::Client::new();
    let response = client
        .head(url.clone())
        .header(reqwest::header::IF_NONE_MATCH, etag)
        .send()
        .await?;
    if response.status() == reqwest::StatusCode::NOT_MODIFIED {
        Ok(None)
    } else {
        let reader = AsyncHttpRangeReader::from_head_response(client, response, url, HeaderMap::default()).await?;
        Ok(Some(reader))
    }
}

Implementations§

source§

impl AsyncHttpRangeReader

source

pub async fn new( client: impl Into<ClientWithMiddleware>, url: Url, check_method: CheckSupportMethod, extra_headers: HeaderMap ) -> Result<(Self, HeaderMap), AsyncHttpRangeReaderError>

Construct a new AsyncHttpRangeReader.

source

pub async fn initial_tail_request( client: impl Into<ClientWithMiddleware>, url: Url, initial_chunk_size: u64, extra_headers: HeaderMap ) -> Result<Response, AsyncHttpRangeReaderError>

Send an initial range request to determine if the remote accepts range requests. This will return a number of bytes from the end of the stream. Use the initial_chunk_size parameter to define how many bytes should be requested from the end.

source

pub async fn from_tail_response( client: impl Into<ClientWithMiddleware>, tail_request_response: Response, url: Url, extra_headers: HeaderMap ) -> Result<Self, AsyncHttpRangeReaderError>

Initialize the reader from AsyncHttpRangeReader::initial_tail_request (or a user provided response that also has a range of bytes from the end as body)

source

pub async fn initial_head_request( client: impl Into<ClientWithMiddleware>, url: Url, extra_headers: HeaderMap ) -> Result<Response, AsyncHttpRangeReaderError>

Send an initial range request to determine if the remote accepts range requests and get the content length

source

pub async fn from_head_response( client: impl Into<ClientWithMiddleware>, head_response: Response, url: Url, extra_headers: HeaderMap ) -> Result<Self, AsyncHttpRangeReaderError>

Initialize the reader from AsyncHttpRangeReader::initial_head_request (or a user provided response the)

source

pub async fn requested_ranges(&self) -> Vec<Range<u64>>

Returns the ranges that this instance actually performed HTTP requests for.

source

pub async fn prefetch(&mut self, bytes: Range<u64>)

Prefetches a range of bytes from the remote. When specifying a large range this can drastically reduce the number of requests required to the server.

source

pub fn len(&self) -> u64

Returns the length of the stream in bytes

Trait Implementations§

source§

impl AsyncRead for AsyncHttpRangeReader

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_> ) -> Poll<Result<()>>

Attempts to read from the AsyncRead into buf. Read more
source§

impl AsyncSeek for AsyncHttpRangeReader

source§

fn start_seek(self: Pin<&mut Self>, position: SeekFrom) -> Result<()>

Attempts to seek to an offset, in bytes, in a stream. Read more
source§

fn poll_complete( self: Pin<&mut Self>, _cx: &mut Context<'_> ) -> Poll<Result<u64>>

Waits for a seek operation to complete. Read more
source§

impl Debug for AsyncHttpRangeReader

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more