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 asgziporx-gzipandidentity(any case), each with an optional;q=weight of0to1with 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 whengziphas a weight above 0 that is at least that ofidentity. A header value that is not visible ASCII is skipped. - Some responses are never compressed: one that already has a
Content-Encodingor aContent-Range, one with status 206 (amultipart/byterangesbody has none of its own), one withCache-Control: no-transform, one of a route withno_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 itsContent-Length; a body of unknown size counts as large), and the content typesimage/*(butimage/svg+xml),application/grpc*(butapplication/grpc-web*) andtext/event-stream, in any case. HEADisGETwithout the body: the router empties the body of aHEADresponse and keeps its length, so forHEADthe length is what says whether the body is under 32 bytes (none: unknown, so large), and the response gets the headers of the compressedGETresponse (noContent-Length, a weakETag,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_compressionorno-transform. Every compressor of the app reads the same rule (may_compress), the UI toolkit’s brotli for pages included. Vary: accept-encodingis added to every response that could be compressed, whatever the client accepts, unless aVaryvalue already containsaccept-encoding(in any case). A 304 carries it as well, unless it hasno-transform, aContent-Encodingor 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-LengthandAccept-Ranges, a strongETagbecomes a weak one, and it getsContent-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§
Trait Implementations§
Source§impl Clone for CompressionLayer
impl Clone for CompressionLayer
impl Copy for CompressionLayer
Source§impl Debug for CompressionLayer
impl Debug for CompressionLayer
Source§impl Default for CompressionLayer
impl Default for CompressionLayer
Source§impl<S> Layer<S> for CompressionLayer
impl<S> Layer<S> for CompressionLayer
Source§type Service = Compression<S>
type Service = Compression<S>
The wrapped service
Source§fn layer(&self, inner: S) -> Compression<S>
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§
impl Forget for CompressionLayer
impl Freeze for CompressionLayer
impl Move for CompressionLayer
impl RefUnwindSafe for CompressionLayer
impl Send for CompressionLayer
impl Sync for CompressionLayer
impl Unpin for CompressionLayer
impl UnsafeUnpin for CompressionLayer
impl UnwindSafe for CompressionLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more