pub struct RequestFile<const FF_WINDOW: u64 = DEFAULT_FF_WINDOW, const FF_BUFFER: usize = DEFAULT_FF_BUFFER> { /* private fields */ }
Expand description

Type that provides an asynchronous file-like interface to a web resource.

This type implements AsyncRead and AsyncSeek, so it can be used like an asynchronous file.

Seeking is implemented as sending out a new request with a Range header. All http requests made by this type include this header.

If the webserver does not support range requests, seeking to anything other than the start of the file will return a NotSeekable error (excluding fast-forwards). Note that the Accept-Ranges response header is not used to check range request support before sending one.

If the webserver does not provide the Content-Length header, and no size was given using RequestFile::with_size(), then seeking relative to the file end will return an Unsupported error.

If the http request fails during a read or seek operation, it returns an Other error that wraps the original http error. If the webserver returns anything other than status code 206 Partial Content, or 200 Ok for responses starting at the first byte, that is also considered a failure.

For transient errors that require a new HTTP request, the reset() method can be used.

Assumptions

This type assumes that the http resource is of constant size, and thus that the separate requests performed while seeking are all consistent.

Reading

Reads are implemented by wrapping the response body stream in StreamReader.

Seeking

Seeking a position before the first byte will return an InvalidInput error.

This type performs no special handling of seeking beyond the end of the file (EOF), so what happens in this case depends on the webserver.

Optimizations

Fast-Forward

As an optimization, seeking forward by a small amount (by default up to 128KiB) will not perform a new request, but rather fast-forward through the response body.

This type of seek is always allowed, even if the webserver does not support Range requests.

Customization

The settings for fast-forwards can be changed through two constant parameters.

  • FF_WINDOW limits how much can be fast-forwarded; only seeking up to this number of bytes forwards will read through the request (discarding the data) to avoid sending out a new request.

  • FF_BUFFER defines the internal buffer size used to read into during a fast-foward.

Implementations

Create a new file-like object for a web resource.

Panics

This function panics if the request:

  • already includes a Range header
  • contains a streaming body
  • building the request fails.

Create a new file-like object for a web resource.

This function allows you to set the response body size if you happen to know it before performing the request. This value must be known—either through this method or via the Content-Length header on the response—to seek relative to the file end.

If the response specifies a different size using the Content-Length header, the value given here is overwritten.

Panics

This function panics if the request:

  • already includes a Range header
  • contains a streaming body
  • building the request fails.

Check the size of this file, which may be unknown.

Force a new HTTP request to begin, without changing the current position.

This method can be used if the request is broken in a way that can be fixed by restarting it, for example due to a transient network issue.

Perform the HTTP request so the response is ready to be read.

Trait Implementations

Attempts to read from the AsyncRead into buf. Read more

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

Waits for a seek operation to complete. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Creates a new AsyncRead instance that chains this stream with next. Read more

Pulls some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. Read more

Reads the exact number of bytes required to fill buf. Read more

Reads an unsigned 8 bit integer from the underlying reader. Read more

Reads a signed 8 bit integer from the underlying reader. Read more

Reads an unsigned 16-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in big-endian order from the underlying reader. Read more

Reads an 32-bit floating point type in big-endian order from the underlying reader. Read more

Reads an 64-bit floating point type in big-endian order from the underlying reader. Read more

Reads an unsigned 16-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in little-endian order from the underlying reader. Read more

Reads an 32-bit floating point type in little-endian order from the underlying reader. Read more

Reads an 64-bit floating point type in little-endian order from the underlying reader. Read more

Reads all bytes until EOF in this source, placing them into buf. Read more

Reads all bytes until EOF in this source, appending them to buf. Read more

Creates an adaptor which reads at most limit bytes from it. Read more

Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more

Creates a future which will rewind to the beginning of the stream. Read more

Creates a future which will return the current seek position from the start of the stream. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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