### Motivation:
zlib stores the address of `z_stream` in its internal state and expects
that
address to remain stable across calls. Storing `z_stream` inline in
movable
Swift value types does not provide that guarantee and can cause
`Z_STREAM_ERROR` during cleanup.
Fixes#308.
### Modifications:
- Make the internal compressor and decompressor reference owners of
separately
allocated `z_stream` storage.
- Pass the stable pointer to every zlib initialization, processing, and
cleanup
call.
- Keep lifecycle state with the pointer owner and make conditional
cleanup
idempotent.
- Add regression tests covering failed initialization recovery, multiple
compression and decompression calls, repeated lifecycles, and
deallocation
cleanup.
### Result:
zlib observes the same `z_stream` address for each complete lifecycle,
while
stream cleanup remains single-owner and safe against repeated shutdown.
### Motivation:
`NIOHTTPRequestDecompressor` / `NIOHTTPResponseDecompressor` keep their
state in a
stored `var decompressor` (a `struct`) and forwarded downstream
(`fireChannelRead` /
`fireErrorCaught`) from inside that struct's `mutating` `channelRead`,
i.e. while an
exclusive (modify) access to the stored property was still open. If
firing downstream
synchronously re-enters the handler's `channelRead`, a second exclusive
access is
opened and the program traps with `Fatal access conflict detected`
(SIGABRT).
This reproduces in production with AsyncHTTPClient over HTTP/2 and
`decompression = .enabled`: a compressed response is mid-decompress when
a
decompression error closes the stream, and the buffered read
re-delivered on the
stream-close path re-enters `channelRead` on the same decompressor.
### Modifications:
- The body decompression is now expressed as a
`NIOSingleStepByteToMessageDecoder`,
driven by a `NIOSingleStepByteToMessageProcessor`. The handler pulls one
decompressed
buffer at a time and forwards it only after `decode` has returned, so
the decoder
relinquishes its exclusive access before anything is fired downstream.
- The shared driver is now a `class`, so the head/end forwarding paths
are re-entrancy
safe for the same reason.
- The zlib stream is torn down in the decoder's `deinit`, which
additionally fixes a
leak when a message ends without a final `.end`.
- No public API changes.
### Result:
Synchronous re-entry of `channelRead` no longer traps. Adds regression
tests covering
the forward and error re-entry paths on both decompressors; all existing
decompression
tests (limits, trailing data, truncation, multi-message reuse) continue
to pass.
Fixes#314
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
Motivation:
Certain malformed q-values can trigger a crash because they don't
validate that a string split results in the expected number of parts.
Modifications:
- validate that the number of split parts is as expected, return a
q-value of zero otherwise (i.e. "not acceptable")
Result:
Safer code
### Motivation
We want to maintain less code.
### Changes
- Unifi the request and response decompression by extracting the decompression code into a `Decompressor` struct
- `NIOHTTPRequestDecompressor` calls out to `Decompressor` internally
- `NIOHTTPResponseDecompressor` calls out to `Decompressor` internally
### Result
- Less code to maintain
- Less subtle bugs
### Motivation:
Relying on the system zlib causes issues if that library (or its
headers) doesn't ship with the system.
### Modifications:
Vendor it.
### Result:
- Fewer dependencies.
- Binary size increase negligible. The _sum of the debug builds_ of all
`.o` files is less than 200kB. A linked release build will make this
even smaller:
```
$ du -k .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/*.o
12 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/adler32.c.o
12 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/crc32.c.o
60 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/deflate.c.o
16 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/inffast.c.o
40 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/inflate.c.o
16 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/inftrees.c.o
40 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/trees.c.o
8 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/zutil.c.o
```
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
### Motivation:
To migrate to GitHub actions and centralised infrastructure.
### Modifications:
Changes of note:
* Adopt swift-format using rules from SwiftNIO
* Remove scripts and docker files which are no longer needed
### Result:
Feature parity with old CI.
* Added support for conditional response compression
* Updated expectations to be fulfilled via a defer
* Updated compression response predicate to return an intent enum rather than a boolean
Motivation:
`NIOHTTPDecompression` erroneously accumulates decompressed data sizes across multiple requests in a single channel, leading to unwarranted `DecompressionError.limit` errors. This affects applications using persistent connections, as the decompression limits are improperly enforced. This change aims to address and rectify this issue.
Modifications:
- Add initialization of `inflated` within `Decompressor.initializeDecoder`.
- Introduced new tests to validate decompression functionality across multiple requests on the same channel
Result:
This fix ensures each request's decompression size is independently considered, eliminating incorrect limit errors, and enhancing reliability for applications using HTTP compression with persistent connections.
Motivation
Per SwiftNIO's formal version policy, we are ready to drop support for
Swift 5.5.
Modifications
This patch removes the support for 5.5 and all supporting
infrastructure. This includes the test generation functionality, which
is no longer required, as well as the files generated by that
functionality. It updates the dockerfile for 5.8, and it removes all
conditional compilation checks that are now definitionally true.
Result
A nice, clean, 5.6+ codebase
Motivation
Currently we don't confirm that the decompression has completed
successfully. This means that we can incorrectly spin forever attempting
to decompress past the end of a message, and that we can fail to notice
that a message is truncated. Neither of these is good.
Modifications
Propagate the message zlib gives us as to whether or not decompression
is done, and keep track of it.
Add some tests written by @vojtarylko to validate the behaviour.
Result
Correctly police the bounds of the messages.
Resolves#175 and #176.
Motivation:
An index page ties all the other documentation together
Modifications:
Add index pages for the library targets.
Correct a few minor errors in the main docs.
Result:
A more joined up documentation experience.
* Improve documentation for NIOExtras
Motivation:
Docs will help users do things correctly.
Modifications:
Add missing comments, improve links.
Result:
Better docc documentation
* Docc in NIOHTTPCompression
* NIOSOCKS docc
* Correct bad symbol
* Minor typo
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
With NIO 2.32.0 we broke the core NIO module up into modules that split
apart the POSIX layer and the core abstractions. As a result, this
package no longer needs to express a hard dependency on the POSIX layer.
Modifications:
- Rewrote imports of NIO to NIOCore.
- Added NIOEmbedded and NIOPosix imports where necessary in tests.
- Extended soundness script to detect NIO imports.
- Note that the main modules still depend on NIO, which is necessary
for backwards-compatibility reasons. This dependency is unused.
Result:
No need to use NIOPosix.
Motivation:
Previously, when using the response compressor, doing a flush() right before finishing the
response data would cause the final compression chunk to be omitted. Some strict decompressors
(such as gzip or the zlib functionality exported in nodejs) would refuse to decompress the
incomplete response.
With this change, the generated compressed response is properly finalized.
Modifications:
In HTTPResponseCompressor.swift, a channel write is now also generated if no body data is
added, but a flush is required.
Result:
The response is now correct for this edge case, enabling gzip, nodejs, et al., to
decompress it without errors.
Motivation:
NIOHTTPRequestDecompressor and HTTPResponseDecompressor are both affected by an issue where the decompression limits defined by their DecompressionLimit property wasn't correctly checked when is was set with DecompressionLimit.size(...), allowing denial of service attacks.
Modifications:
- Update DecompressionLimit.size(...) to correctly check the size of the decompressed data.
- Update test cases to avoid future regressions regarding the size checks.
Result:
Prevents DoS attacks though maliciously crafted compressed data.
* Added NIOHTTPRequestCompressor to compress requests
Also moved common code from request and response compressor into separate NIOHTTPCompression enum.
* Updates after comments from @weissi
Also reinstated public enum HTTPResponseCompressor.CompressionError
* algorithms are now let not var
* Catch situation where head is flushed before anything else comes through
Content-encoding was not being set
Added additional tests for header values
* Added documentation around 5 bytes added to buffer size and add them
* Renaming NIOHTTPCompressionSetting to NIOCompression
Also
NIOHTTPCompressionSetting.CompressionAlgorithm is NIOCompression.Algorithm
NIOHTTPCompressionSetting.CompressionError is NIOCompression.Error
Algorithm now conforms to Equatable
* Forgot to run generate_linux_tests
* Fix typos
### Motivation:
There will be times when a client wishes to send larger requests with gzipped bodies to save on network traffic. This PR adds a `NIOHTTPRequestDecompressor` which can be added to the server's channel pipeline so those requests are automatically inflated.
### Modifications:
- Added a `CNIOExtrasZlib_voidPtr_to_BytefPtr` C method.
- Added a `NIOHTTPRequestDecompressor` type.
- Added a `HTTPResponseDecompressorTest` test case.
### Result:
Now you don't have to manually check the `Content-Encoding` header and decompress the body on each incoming request.
* extract common classes for server request decompressor
* review fix: make fields private and make state part of the handler
* review fixes
* review fix: reserve capacity before inflating
### Motivation:
Many HTTP servers can send compressed responses to clients and it would be a great feature for `AsyncHTTPClient` to support it. But since we want to minizime usage of unsafe APIs in SSWG projects, I propose to consolidate interfacing with zlib to `nio-extras` since it already supports `zlib` compression.
### Modifications:
Added `HTTPResponseDecompressor` and accompanying tests
### Result:
Users (primarily `AsyncHTTPClient`) can now provide automatic response decompression support.
Motivation:
HTTPResponseCompressor is trivially removable, so mark it.
Modifications:
make HTTPResponseCompressor implement RemovableChannelHandler
Result:
HTTPResponseCompressor can be removed