Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(encoding): use an extension for encoding, which allow a specific handler to disable compression if needed #3534

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions actix-web/src/middleware/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,24 @@

#[allow(clippy::borrow_interior_mutable_const)]
fn call(&self, req: ServiceRequest) -> Self::Future {
if req.extensions().get::<Encoding>().is_some() {
return Either::left(CompressResponse {
fut: self.service.call(req),
_phantom: PhantomData,
});
}

// negotiate content-encoding
let accept_encoding = req.get_header::<AcceptEncoding>();

let accept_encoding = match accept_encoding {

Check warning on line 123 in actix-web/src/middleware/compress.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/actix-web/actix-web/actix-web/src/middleware/compress.rs
// missing header; fallback to identity
None => {
req.extensions_mut().insert::<Encoding>(Encoding::identity());

return Either::left(CompressResponse {
encoding: Encoding::identity(),
fut: self.service.call(req),
_phantom: PhantomData,

Check warning on line 130 in actix-web/src/middleware/compress.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/actix-web/actix-web/actix-web/src/middleware/compress.rs
})
}

Expand All @@ -143,11 +151,14 @@
.map_into_right_body()))
}

Some(encoding) => Either::left(CompressResponse {
fut: self.service.call(req),
encoding,
_phantom: PhantomData,
}),
Some(encoding) => {
req.extensions_mut().insert::<Encoding>(encoding);

Either::left(CompressResponse {
fut: self.service.call(req),

Check warning on line 158 in actix-web/src/middleware/compress.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/actix-web/actix-web/actix-web/src/middleware/compress.rs
_phantom: PhantomData,
})
},
}
}
}
Expand All @@ -159,7 +170,6 @@
{
#[pin]
fut: S::Future,
encoding: Encoding,
_phantom: PhantomData<B>,
}
}
Expand All @@ -176,8 +186,19 @@

match ready!(this.fut.poll(cx)) {
Ok(resp) => {
let enc = match this.encoding {
Encoding::Known(enc) => *enc,
let request_encoding = resp.request().extensions().get::<Encoding>().cloned();

let encoding = match request_encoding {
Some(enc) => enc.clone(),

Check warning on line 192 in actix-web/src/middleware/compress.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/actix-web/actix-web/actix-web/src/middleware/compress.rs
None => {
return Poll::Ready(Ok(resp.map_body(move |head, body| {
EitherBody::left(Encoder::response(ContentEncoding::Identity, head, body))
})));
}
};

let enc = match encoding {
Encoding::Known(enc) => enc,
Encoding::Unknown(enc) => {
unimplemented!("encoding '{enc}' should not be here");
}
Expand Down
Loading