Skip to main content

ResponseExt

Trait ResponseExt 

Source
pub trait ResponseExt {
    // Required methods
    fn collect_bytes(self) -> impl Future<Output = Result<Bytes, Error>> + Send;
    fn collect_bytes_limited(
        self,
        max: usize,
    ) -> impl Future<Output = Result<Bytes, Error>> + Send;
    fn into_read(self) -> impl AsyncRead + Send + Unpin + 'static;
}
Expand description

Helper methods for http::Response.

Required Methods§

Source

fn collect_bytes(self) -> impl Future<Output = Result<Bytes, Error>> + Send

Collect the response body into a bytes::Bytes with no size limit.

Use this only when the body is locally generated or otherwise trusted to be small. For any body read from a network peer (a control/DERP/upstream server response — all of which this fork treats as hostile-capable), prefer collect_bytes_limited: collect() here buffers the entire body into memory before returning, so a malicious or MITM’d server answering a short request with a multi-gigabyte streamed body would OOM the client (a length check after collect_bytes is too late — the allocation already happened).

Source

fn collect_bytes_limited( self, max: usize, ) -> impl Future<Output = Result<Bytes, Error>> + Send

Collect the response body into a bytes::Bytes, failing if it exceeds max bytes.

The body is wrapped in http_body_util::Limited, which aborts collection as soon as more than max bytes arrive — so the allocation is bounded during the read, mirroring Go’s io.LimitedReader-bounded body reads. An over-limit body yields Error::BodyTooLarge (distinct from a transient Error::Io, so callers can treat it as terminal). This is the body reader every network-response caller should use; pick max to comfortably fit the largest legitimate response for that endpoint.

Source

fn into_read(self) -> impl AsyncRead + Send + Unpin + 'static

Convert the response body into an AsyncRead.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<B> ResponseExt for Response<B>
where B: Body + Send + Unpin + 'static, B::Data: Send + 'static, B::Error: Error + Send + Sync + 'static,