Skip to main content

CompressionLayer

Struct CompressionLayer 

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

A Tower layer that gzips responses for clients that accept it.

  • The client chooses with Accept-Encoding. Its items are read as gzip or x-gzip and identity (any case), each with an optional ;q= weight of 0 to 1 with up to three decimals; other codings are ignored, and so is an item whose parameter is anything but a valid weight. The response is gzipped when gzip has a weight above 0 that is at least that of identity. A header value that is not visible ASCII is skipped.
  • Some responses are never compressed: one that already has a Content-Encoding or a Content-Range, one with status 206 (a multipart/byteranges body has none of its own), one with Cache-Control: no-transform, one of a route with no_compression, one with a status that has no body (1xx, 204, 205, 304), a body of fewer than 32 bytes (by its exact size, or else its Content-Length; a body of unknown size counts as large), and the content types image/* (but image/svg+xml), application/grpc* (but application/grpc-web*) and text/event-stream, in any case.
  • HEAD is GET without the body: the router empties the body of a HEAD response and keeps its length, so for HEAD the length is what says whether the body is under 32 bytes (none: unknown, so large), and the response gets the headers of the compressed GET response (no Content-Length, a weak ETag, Content-Encoding: gzip) with no body.
  • BREACH: a compressed length gives away how much of a body repeats. With a secret (a CSRF token) and reflected input in one response, and cookies that a cross-site request carries, an attacker who sees lengths on the network recovers the secret request by request. Opt such a response out with no_compression or no-transform. Every compressor of the app reads the same rule (may_compress), the UI toolkit’s brotli for pages included.
  • Vary: accept-encoding is added to every response that could be compressed, whatever the client accepts, unless a Vary value already contains accept-encoding (in any case). A 304 carries it as well, unless it has no-transform, a Content-Encoding or a route that opts out: it has the header fields of the 200 it stands in for, and what that one is, a cache has to be told.
  • A compressed response loses its Content-Length and Accept-Ranges, a strong ETag becomes a weak one, and it gets Content-Encoding: gzip. Its body is a gzip stream at level 6 that keeps streaming, in frames of at most 4 KiB: when the handler’s body has nothing ready, what it sent so far is flushed to the client, and the body is read no faster than the client takes the frames. Its trailers follow the gzip stream, and its error ends the response with that error.
use navree_http::{AppRouter, CompressionLayer};
use navree_http::routing::Router;

let app = AppRouter::new(Router::new()).layer(CompressionLayer::new());

Implementations§

Source§

impl CompressionLayer

Source

pub fn new() -> Self

The layer.

Trait Implementations§

Source§

impl Clone for CompressionLayer

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for CompressionLayer

Source§

impl Debug for CompressionLayer

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for CompressionLayer

Source§

fn default() -> Self

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

impl<S> Layer<S> for CompressionLayer

Source§

type Service = Compression<S>

The wrapped service
Source§

fn layer(&self, inner: S) -> Compression<S>

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

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> 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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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