-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Select compression algorithm using features flags #2250
Conversation
2437f99
to
84c7e6b
Compare
You can declare an internal feature like https://github.com/seanmonstar/reqwest/blob/bbeb1ede4e8098481c3de6f2cafb8ecca1db4ede/Cargo.toml#L64 |
actix-http/src/encoding/decoder.rs
Outdated
@@ -8,10 +8,13 @@ use std::{ | |||
}; | |||
|
|||
use actix_rt::task::{spawn_blocking, JoinHandle}; | |||
#[cfg(feature = "compress-br")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally nitpicky and a matter of taste, but I personally find import sequences combined with a lot of cfg(...)
hard to read. I think it's better to separate compress-*
imports with an empty line, so we have a more or less clean sequence of unconditional imports and then a sequence of imports guarded by cfg(...)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second that. An empty line makes it much easier to find.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it looks better.
I have update the import related to compression but there is still other import not related to compress-*
which do not have empty line before.
Would you like I update them in this PR ? create a PR for this ? or just let them as it ?
Thanks for your suggestion @jplatte. |
awc/src/request.rs
Outdated
#[cfg(feature = "compress")] | ||
#[cfg(feature = "__compress")] | ||
const HTTPS_ENCODING: &str = "br, gzip, deflate"; | ||
#[cfg(not(feature = "compress"))] | ||
#[cfg(not(feature = "__compress"))] | ||
const HTTPS_ENCODING: &str = "br"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is more complicated now, suggest cfg_if!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have add cfg_if!
macro as you suggest.
However I did not add it everywhere.
- in struct definition, it make
cargo fmt
crash - in
awc/src/sender.rs
to me it make the code less readable and produce a big change. I can update it if you think it is better with cfg_if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm mistaken, as it's written now enabling any compression flag will signal support for all of them. My suggestion for cfg_if! was to make it easier to enumerate the feature flag combinations to get the correct string here.
compress-gzip => "gzip, deflate"
compress-brotli => "br"
compress-brotli+compress-gzip => "br, gzip, deflate"
compress-brotli+compress-gzip+compress-zstd => "br, gzip, deflate, zstd"
etc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, I have not read usage of this but just replace cfg!
calls.
Actually looking at the current code I do not really understand how it may works.
For now there is the current definition
const HTTPS_ENCODING: &str = {
cfg_if::cfg_if! {
if #[cfg(feature = "__compress")] {
"br, gzip, deflate"
} else {
"br"
}
}
};
And the only usage of HTTPS_ENCODING
is in awc/src/request.rs+495
:
if slf.response_decompress {
let https = slf
.head
.uri
.scheme()
.map(|s| s == &uri::Scheme::HTTPS)
.unwrap_or(true);
if https {
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, HTTPS_ENCODING));
} else {
#[cfg(feature = "__compress")]
{
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, "gzip, deflate"));
}
};
}
Please note, here the only change with previous code is compress
becoming __compress
.
Moreover the if statement related to switching HTTP / HTTPS is just weird.
To me code should be something like this:
#[cfg(feature = "__compress")]
if slf.response_decompress {
let mut encoding = vec![];
#[cfg(feature = "compress-brotli")]
encoding.push("br");
#[cfg(feature = "compress-gzip")]
{
encoding.push("gzip");
encoding.push("deflate");
}
#[cfg(feature = "compress-zstd")]
encoding.push("zstd");
assert!(!encoding.is_empty(), "encoding cannot be empty unless __compress feature has been explictily enabled.");
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, encoding.join(",")));
}
If everyone is ok with this changes, maybe we can merge this PR 😄 |
I have push new commits to fix I will be happy to get a review and discuss on this if anyone has few minutes for this subject 😄 |
What is still pending on this PR? |
To me, the only thing that was not resolved since your last review was the change in So from my point of view, all lights are now green. |
Yep, LGTM |
21b41c5
to
50b2bee
Compare
Add compress-* feature flags in actix-http / actix-web / awc. This allow enable / disable not wanted compression algorithm.
33177e0
to
b7e17f0
Compare
macOS CI failure can be ignored; I've run the tests locally. Got some doc tweaks to make but will do it after this PR is merged. Thanks for adding this; good work. |
PR Type
Refactor
PR Checklist
Overview
Update
compress
feature flag to allow selection of enabled algorithms withcompress-brotli
,compress-gzip
andcompress-zstd
flags.Breaking change
I totaly remove
compress
flag and replace it bycompress-<alg>
flags.Having both
compress
flag andcompress-<alg>
flags may lead to invalid selection:Change for developpers
For developers, checking if compression is supported or not is done through new internal flag:
__compress
.This flag is enabled by selecting any of
compress-brotli
,compress-gzip
orcompress-zstd
flags.Before it was:
#[cfg(feature = "compress")]
Now it is:
#[cfg(feature = "__compress")]
Other changes
Completely refact
awc
selection ofAccept-Encoding
HTTP headerThis PR follow works that has been started in PR [#2244].