-
Notifications
You must be signed in to change notification settings - Fork 166
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
Precompressed files #156
Precompressed files #156
Conversation
7d2692b
to
b5bb0a3
Compare
Cleaned up a bit and added more tests and support for |
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.
Great work! I think it looks good as it.
I'll put this on 0.2 milestone.
@@ -74,7 +117,7 @@ impl ServeFile { | |||
} | |||
} | |||
|
|||
impl<R> Service<R> for ServeFile { | |||
impl<ReqBody> Service<Request<ReqBody>> for ServeFile { |
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.
Its a shame that this required a breaking change. I think for 0.2 we should audit all services and make sure they implement Service<Request<B>>
so we can make changes like this in the future.
Thanks! I'll fix the CI as well either today or tomorrow |
There were some general CI issues which I'm fixing in #158 |
Ah great, yeah the the |
async fn only_precompressed_variant_existing() { | ||
let svc = ServeDir::new("../test-files").precompressed_gzip(); | ||
|
||
let request = Request::builder().body(Body::empty()).unwrap(); |
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.
Should this specify the .uri
to match?
tower-http/src/content_encoding.rs
Outdated
} | ||
} | ||
|
||
pub(crate) fn to_file_extention(self) -> Option<&'static OsStr> { |
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.
typo: "extension"
/// Informs the service that it should look for a precompressed (by the gzip algorithm) | ||
/// version of _any_ file in the directory with the correct file extension that can be | ||
/// served if the client have the correct corresponding `Accept-Encoding`. | ||
/// (i.e `dir/foo.txt.gz` when the served directory is `dir` and the requested file is `foo.txt`) | ||
/// If not the uncompressed version will be served instead. Note that this means | ||
/// that both the precompressed version(s) and the uncompressed file is expected | ||
/// to be present in the same directory. |
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.
/// Informs the service that it should look for a precompressed (by the gzip algorithm) | |
/// version of _any_ file in the directory with the correct file extension that can be | |
/// served if the client have the correct corresponding `Accept-Encoding`. | |
/// (i.e `dir/foo.txt.gz` when the served directory is `dir` and the requested file is `foo.txt`) | |
/// If not the uncompressed version will be served instead. Note that this means | |
/// that both the precompressed version(s) and the uncompressed file is expected | |
/// to be present in the same directory. | |
/// Informs the service that it should look for a precompressed gzip | |
/// version of _any_ file in the directory. | |
/// | |
/// If the client has an `Accept-Encoding` header that allows the gzip encoding, | |
/// the file `dir/foo.txt.gz` will be served instead of `dir/foo.txt`. | |
/// If the precompressed file is not available, or the client doesn't support it, | |
/// the uncompressed version will be served instead. | |
/// Both the precompressed version and the uncompressed version are expected | |
/// to be present in the directory. |
(ditto similar suggestions to the sibling methods)
I'm not sure where it should be mentioned about the q
settings for browser preference.
4e71029
to
7ab662d
Compare
7ab662d
to
fbf1147
Compare
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
|
|||
# Unreleased | |||
|
|||
- None. | |||
- `ServeDir` and `ServeFile`: Ability to serve precompressed files ([#156]) |
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 think its missing a link to [#156]
🤔
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.
Huh? Isn't that the last part ([#156])
or do I need to do something else as well?
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.
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.
ah right completely missed that, fixed now!
fbf1147
to
9e58410
Compare
Hi there, I'm wondering if it is a design problem that ServeDir/File will fail when precompressed_gzip enabled and path/file.ext.gz not exists but path/file.ext exists. this should works without content-encoding. |
Hi @c5soft if the developer has explicitly indicated that there should exist an precompressed variant I think its fair to return 404 to clients that supports the encoding (all browsers) since it would be an error on the side of the developer and it makes it easy to notice after a basic check. Otherwise the developer might think it serves precompressed files without them actually existing. Another option would be to remove the |
I didn't catch that about this implementation. Not sure thats ideal since |
@davidpdrsn Yeah that's fair, I didn't think about the |
That would be great 😊 and yeah adding it to |
Motivation
Suggestion for an implementation that should fix #120.
Solution
The user specifies which types of precompression is available in the same directory as the original file by specifying
precompressed_[gzip|br|deflate]
to theServeFile
service. It is not required to have the compression feature enabled since no compression/decompression actually happens on the server. TheServeFile
service looks for copies of the original file with the extension.gz
,.br
or.zz
depending on the compression used/specified. If a client requests the file and doesn't support any of the precompressed variants the "normal" file will be served instead. This means it's expected that the uncompressed version of the file also exists if all clients should be able to request the file. Otherwise a 404 will be returned if the client doesn't support the compression algorithm.