Struct ChunkedReadFile

Source
pub struct ChunkedReadFile<D: 'static + Send + Buf + From<Vec<u8>> + From<&'static [u8]>, E: 'static + Send + Into<Box<dyn StdError + Send + Sync>> + From<Box<dyn StdError + Send + Sync>>> { /* private fields */ }
Expand description

HTTP entity created from a std::fs::File which reads the file chunk-by-chunk within a tokio::task::block_in_place closure.

ChunkedReadFile is cheap to clone and reuse for many requests.

Expects to be served from a tokio threadpool.

type BoxedError = Box<dyn std::error::Error + 'static + Send + Sync>;
async fn serve_dictionary(req: Request<Body>) -> Result<Response<Body>, BoxedError> {
    let f = tokio::task::block_in_place::<_, Result<_, BoxedError>>(
        move || {
            let f = std::fs::File::open("/usr/share/dict/words")?;
            let mut headers = http::header::HeaderMap::new();
            headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
            Ok(http_serve::ChunkedReadFile::new(f, headers)?)
        },
    )?;
    Ok(http_serve::serve(f, &req))
}

Implementations§

Source§

impl<D, E> ChunkedReadFile<D, E>
where D: 'static + Send + Sync + Buf + From<Vec<u8>> + From<&'static [u8]>, E: 'static + Send + Sync + Into<Box<dyn StdError + Send + Sync>> + From<Box<dyn StdError + Send + Sync>>,

Source

pub fn new(file: File, headers: HeaderMap) -> Result<Self, Error>

Creates a new ChunkedReadFile.

read(2) calls will be wrapped in tokio::task::block_in_place calls so that they don’t block the tokio reactor thread on local disk I/O. Note that std::fs::File::open and this constructor (specifically, its call to fstat(2)) may also block, so they typically should be wrapped in tokio::task::block_in_place as well.

Source

pub fn new_with_metadata( file: File, metadata: &Metadata, headers: HeaderMap, ) -> Result<Self, Error>

Creates a new ChunkedReadFile, with presupplied metadata.

This is an optimization for the case where the caller has already called fstat(2). Note that on Windows, this still may perform a blocking file operation, so it should still be wrapped in tokio::task::block_in_place.

Trait Implementations§

Source§

impl<D: Clone + 'static + Send + Buf + From<Vec<u8>> + From<&'static [u8]>, E: Clone + 'static + Send + Into<Box<dyn StdError + Send + Sync>> + From<Box<dyn StdError + Send + Sync>>> Clone for ChunkedReadFile<D, E>

Source§

fn clone(&self) -> ChunkedReadFile<D, E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<D, E> Entity for ChunkedReadFile<D, E>
where D: 'static + Send + Sync + Buf + From<Vec<u8>> + From<&'static [u8]>, E: 'static + Send + Sync + Into<Box<dyn StdError + Send + Sync>> + From<Box<dyn StdError + Send + Sync>>,

Source§

type Data = D

The type of a data chunk. Read more
Source§

type Error = E

Source§

fn len(&self) -> u64

Returns the length of the entity’s body in bytes.
Source§

fn get_range( &self, range: Range<u64>, ) -> Box<dyn Stream<Item = Result<Self::Data, Self::Error>> + Send + Sync>

Gets the body bytes indicated by range.
Source§

fn add_headers(&self, h: &mut HeaderMap)

Adds entity headers such as Content-Type to the supplied Headers object. In particular, these headers are the “other representation header fields” described by RFC 7233 section 4.1; they should exclude Content-Range, Date, Cache-Control, ETag, Expires, Content-Location, and Vary. Read more
Source§

fn etag(&self) -> Option<HeaderValue>

Returns an etag for this entity, if available. Implementations are encouraged to provide a strong etag. RFC 7232 section 2.1 notes that only strong etags are usable for sub-range retrieval.
Source§

fn last_modified(&self) -> Option<SystemTime>

Returns the last modified time of this entity, if available. Note that serve may serve an earlier Last-Modified: date than the one returned here if this time is in the future, as required by RFC 7232 section 2.2.1.
Source§

fn is_empty(&self) -> bool

Returns true iff the entity’s body has length 0.

Auto Trait Implementations§

§

impl<D, E> Freeze for ChunkedReadFile<D, E>

§

impl<D, E> RefUnwindSafe for ChunkedReadFile<D, E>

§

impl<D, E> Send for ChunkedReadFile<D, E>

§

impl<D, E> Sync for ChunkedReadFile<D, E>
where D: Sync, E: Sync,

§

impl<D, E> Unpin for ChunkedReadFile<D, E>
where D: Unpin, E: Unpin,

§

impl<D, E> UnwindSafe for ChunkedReadFile<D, E>
where D: UnwindSafe, E: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

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.