pub struct Compression { /* private fields */ }Expand description
Response compression with gzip (compress-lib), for deployments where nothing in front compresses.
Compression together with secrets and reflected input in one page can leak the secrets (BREACH). csrf-sec masks its tokens per response, which is the defence for them; other secrets in compressed pages are the application’s to consider.
use web_net::{App, Compression, get};
let app = App::builder()
.compress(Compression::default().min_bytes(512).level(6))
.route(get("/", |cx| cx.text(200, &"hello ".repeat(200))))
.build()?;
let r = serve_net::testing::respond(
&app,
b"GET / HTTP/1.1\r\nHost: localhost\r\nAccept-Encoding: gzip\r\n\r\n",
);
assert_eq!(r.header("content-encoding"), Some(&b"gzip"[..]));Implementations§
Source§impl Compression
impl Compression
Sourcepub fn min_bytes(self, bytes: usize) -> Self
pub fn min_bytes(self, bytes: usize) -> Self
Bodies smaller than this are sent as they are (default 1024).
use web_net::{App, Compression, get};
// A 600-byte page is under the minimum, so it goes out as it is.
let app = App::builder()
.compress(Compression::default().min_bytes(4096))
.route(get("/", |cx| cx.text(200, &"hello ".repeat(100))))
.build()?;
let r = serve_net::testing::respond(
&app,
b"GET / HTTP/1.1\r\nHost: localhost\r\nAccept-Encoding: gzip\r\n\r\n",
);
assert_eq!(r.header("content-encoding"), None);Sourcepub fn level(self, level: u8) -> Self
pub fn level(self, level: u8) -> Self
The level, 1 (fastest) to 9 (smallest); default 6.
use web_net::{App, Compression, get};
// Fastest; values outside 1 to 9 are clamped.
let app = App::builder()
.compress(Compression::default().level(1))
.route(get("/", |cx| cx.text(200, &"hello ".repeat(400))))
.build()?;
let r = serve_net::testing::respond(
&app,
b"GET / HTTP/1.1\r\nHost: localhost\r\nAccept-Encoding: gzip\r\n\r\n",
);
assert_eq!(r.header("content-encoding"), Some(&b"gzip"[..]));Trait Implementations§
Source§impl Clone for Compression
impl Clone for Compression
impl Copy for Compression
Source§impl Debug for Compression
impl Debug for Compression
Source§impl Default for Compression
impl Default for Compression
impl Eq for Compression
Source§impl PartialEq for Compression
impl PartialEq for Compression
impl StructuralPartialEq for Compression
Auto Trait Implementations§
impl Forget for Compression
impl Freeze for Compression
impl Move for Compression
impl RefUnwindSafe for Compression
impl Send for Compression
impl Sync for Compression
impl Unpin for Compression
impl UnsafeUnpin for Compression
impl UnwindSafe for Compression
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