-
Notifications
You must be signed in to change notification settings - Fork 346
HTTP compression support for static responses #1587
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hongzhidao
reviewed
Apr 8, 2025
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!
Just wondering are you going to include some tests as well?
Good question. Certainly not as part of this PR, if I have time to figure it out in the future maybe.... |
hongzhidao
approved these changes
Apr 11, 2025
This is to store the MIME type of the response which will be used by the HTTP compression patches as part of determining whether or not to compress the response. Signed-off-by: Andrew Clayton <[email protected]>
This will be used by the compression code to signal no requested compression method is available and the client said to not use identity. Signed-off-by: Andrew Clayton <[email protected]>
This is the initial step to enabling HTTP compression on both static and application responses. This code itself doesn't do any actual compression, that will come in subsequent commits. It just contains the core functions for initialising structures that describe the available compressors and functions for checking if compression should be done depending on various criteria. Signed-off-by: Andrew Clayton <[email protected]>
This adds support for both deflate & gzip compressors. Signed-off-by: Andrew Clayton <[email protected]>
Signed-off-by: Andrew Clayton <[email protected]>
Signed-off-by: Andrew Clayton <[email protected]>
This allows to actually build unit with support for zlib, zstd and brotli compression. Any or all can be specified. E.g. $ ./configure --zlib ... $ ./configure --zlib --zstd --brotli ... During configure you will see if support for the requested compressions has been found and what version of the library is being used. E.g. ... checking for zlib ... found + zlib version: 1.3.1.zlib-ng checking for zstd ... found + zstd version: 1.5.6 checking for brotli ... found + brotli version: 1.1.0 ... Unit configuration summary: ... zlib support: .............. YES zstd support: .............. YES brotli support: ............ YES ... Co-authored-by: Alejandro Colomar <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]> Signed-off-by: Andrew Clayton <[email protected]>
This exposes a new "settings.http.compression" configuration object. Under which are types & compressors objects. types is used to specify what MIME types should be considered compressible. compressors is used to configure an array of compressors that are available. For each of these, you specify the encoding, e.g gzip and optional level and min_length parameters. Where level is what compression level to use and min_length is the minimum length of data that should be compressed. By default the default compression level for the specified compressor is used and there is no minimum data length considered for compression. It may look something like "settings": { "http": { "server_version": true, "static": { "mime_types": { "text/x-c": [ ".c", ".h" ] } }, "compression": { "types": [ "text/*" ], "compressors": [ { "encoding": "gzip", "level": 3, "min_length": 2048 }, { "encoding": "deflate", "min_length": 1024 }, { "encoding": "zstd", "min_length": 2048 }, { "encoding": "br", "min_length": 256 } ] } } }, Currently this is a global option that will effect both static and application responses. In future it should be possible to add per-application (and perhaps even per-static) configuration. Signed-off-by: Andrew Clayton <[email protected]>
This adds two helper functions that will be used in subsequent commits. nxt_http_comp_compress() does the actual compression. nxt_http_comp_bound() returns the maximum compressed size for the given size. Signed-off-by: Andrew Clayton <[email protected]>
Signed-off-by: Andrew Clayton <[email protected]>
Rebased with master
|
Thanks Zhidao! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull-request introduces initial support for HTTP compression of static responses.
It does not include support for compressing applications responses, which will hopefully follow at some point. I thought it best to get the bulk of the work committed in the meantime while I continue took at compressing application responses.
This work introduces some new configuration options. Firstly there is a new
settings.http.compression
section under which you can specify one or two optionssettings.http.compression.types
Is an array where you can specify a list of MIME types that should be considered suitable for compressing.
settings.http.compression.compressors
This is an array of objects describing what compressors to enable. While Unit itself can be compiled with support for one or more compression types, you can then enable one or more of these at run-time.
Each compressor object describes the compression scheme to enable,
encoding
, an optionallevel
that says what compression level to use and an optionalmin_length
that says what the minimum amount of data that should be compressed is.An example configuration might look like
First of all we have a
settings.http.compression.types
settings which lists what MIME types should be considered for compression, in this case, text files.Then we have
settings.http.compression.compressors
which is a list of compression schemes to enable. In this case we enable support for deflate, gzip, zstd and brotli.For gzip we specify a compression level of 3, the others will just use that schemes default.
For gzip, zstd and brotli we specify minimum lengths for compression, anything under this size won't be compressed.
For deflate we don't set a minimum level so anything matching the requested MIME type of any size will be compressed.
The compressing of static share files is done by creating mmap(2)'s for both the requested file and file compressed, negating the need for temporary buffers. We are also able to properly set the
Content-Length
for the compressed files.A note about the compressors. The compressors themselves
nxt_{brotli,zlib,zstd}.c
have been kept intentionally isolated from any knowledge of the Unit core. They are as small as possible to basically just take a buffer and compress it.This means adding support for future compression schemes should be relatively trivial.
This pull-request is comprised of the following commits
This adds a new structure member
mime_type
tonxt_http_response_t
which is used to store the MIME tpe of the response. This is used to determine if the response should be compressed or not.This adds a new enum value to represent the "Not Acceptable" status code. This is used to indicate the requested compression scheme wasn't available and we've been told not to send the response uncompressed.
This adds the core compression code.
This doesn't include any of the actual compressors just the core infrastructure.
These commits add support for deflate, gzip, zstd and brotli compression.
This adds the needed configure scripts to enable support for the above. Any or all of the above compressors can be enabled.
This wire the above into the config system.
This adds a couple of helper functions that will be used to doing the actual compression.
This adds support for compressing static responses.