79 Commits

Author SHA1 Message Date
Guoye Zhang
fde9d65d2e
Import HTTP resumable upload sample code (#203)
Support HTTP resumable upload.

### Motivation:

Supporting HTTP resumable upload protocol defined in
https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-resumable-upload-05

* Interop version 3: iOS 17.0, macOS 14.0
* Interop version 5: iOS 18.0, macOS 15.0
* Interop version 6: iOS 18.1, macOS 15.1

### Modifications:

2 new public classes, `HTTPResumableUploadHandler` and
`HTTPResumableUploadContext`, and a few other supporting objects to
manage resumable uploads and translate them into regular uploads.

---------

Co-authored-by: Jonathan Flat <jflat@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-12-11 07:39:13 +00:00
Rick Newton-Rogers
3f776e9aaf
Migrate CI to use GitHub Actions. (#234)
### 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.
2024-10-28 14:00:57 +00:00
Rick Newton-Rogers
2e9746cfc5
LineBasedFrameDecoderTest cleanup (#230)
Remove extraneous `print` statement.

Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
2024-09-11 17:04:50 +01:00
Rick Newton-Rogers
4f888611eb
LineBasedFrameDecoder.decodeLast handle more frames (#229)
### Motivation:

`LineBasedFrameDecoder.decodeLast` throws an error if there is more than
one frame’s worth of bytes remaining in the buffer.

This is in violation of the `NIOSingleStepByteToMessageDecoder`protocol
requirement that this method be called in a loop until all bytes are
decoded.

### Modifications:

* The method now only throws if the decode operation could not return a
frame and there are bytes left in the buffer (previously only the latter
criterion applied).
* Method documentation is updated.
* Test added.

### Result:

`LineBasedFrameDecoder.decodeLast` can cope with more edge cases.

Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
2024-09-11 16:44:42 +01:00
Dimitri Bouniol
d1ead62745
Conditional Response Compression (#225)
* 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
2024-07-18 16:19:20 +01:00
Dimitri Bouniol
05c36b5745
Added a test for responding with 304 Not Modified with HTTP compression (#224) 2024-06-18 04:51:32 -07:00
Johannes Weiss
efd416ee15
clean up @retroactive conformances (#222) 2024-05-09 13:02:05 +01:00
David Nadoba
8b9030df7c
Enable automatic compression format detection (#208)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-03-26 02:57:29 -07:00
Iceman
3bbec9883a
Fix NIOHTTPDecompression bug for multi-request channels (#221)
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.
2024-03-20 01:58:49 -07:00
Johannes Weiss
cdd1580a03
LineBasedFrameDecoder: can be a NIOSingleStepByteToMessageDecoder (#217)
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2024-02-06 16:31:09 +00:00
Si Beaumont
363da63c19
Add missing availability guards in tests (#214) 2023-12-15 08:05:29 -08:00
Cory Benfield
7264a5a0d7
Add retroactive marker to test conformances (#212)
Motivation

Nightly CI builds require annotations on retroactive conformances.
We have a few used only in tests, which are totally safe.

Modifications

Add retroactive conformance marker.

Result

Nightly CI works again
2023-11-28 17:53:58 +00:00
George Barnett
df34965cd7
Don't reserve capacity for NIOPCAPRingBuffer (#209)
Motivation:

The NIOPCAPRingBuffer can limit the number of fragment or the total
number of bytes in its buffer or both. When configuring the buffer to
limit only the maximum number of bytes it sets the maximum number of
fragments allowed to `.max`. On `init` the buffer has enough capacity
reserved to store tha maximum number of fragments.

This would be a large and potentially totally unnecessary allocation.
That is, if it didn't crash at runtime. It crashes at runtime as
`CircularBuffer` converts the requested capacity to a `UInt32` which
traps if you pass it an `Int.max`.

Modifications:

- Don't reserve capacity on init
- Adjust the test which tests the byte limit to not set a capacity as
  well

Result:

- `NIOPCAPRingBuffer(maximumBytes:)` doesn't crash
2023-11-03 10:04:31 +00:00
Guoye Zhang
798c962495
Add HTTP types adapter for SwiftNIO (#202)
* Add HTTP types adapter for SwiftNIO

* swiftformat

* Guard on Swift 5.8

* Review comments

* Update swift-http-types to 0.1.1

* Update swift-http-types to 1.0.0

* Review feedback

* Review feedback

* Bump minimum Swift version to 5.7.1

* Allow Host in any order
2023-10-20 17:55:23 +01:00
Cory Benfield
9cdb93e321
Drop Swift 5.5 (#197)
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
2023-04-13 16:47:28 +01:00
Cory Benfield
0e0d0aab66
Fix documentation and add support for CI-ing it (#196)
* Fix documentation and add support for CI-ing it

* Soundness cleanup
2023-04-11 11:25:17 +01:00
Johannes Weiss
df2810507b
TaggedRequestResponseHandler (#157) 2023-03-22 13:02:05 +00:00
Johannes Weiss
4569c6911b
add the NFS3 protocol (#155)
* add the NFS3 protocol

* make writes return the amount of bytes written

* nits, docs, removes

* compiler errors on old swifts

* Hashable everything

* delay errors

* remove typealiases

* Sendables

* implicit endiannes

* more changes
2023-03-21 17:10:58 +00:00
Cory Benfield
cc1e527507
Make SynchronizedFileSink.close unavailable from async (#195)
Motivation

syncClose will block whatever thread it's on indefinitely. That makes it
unsafe to call in async contexts.

Modifications

Add a new close() method that's async.
Make the existing method unavailable from async.
Add some tests.

Results

Easier to close these from async contexts
2023-03-03 05:48:20 -08:00
Franz Busch
d75ed708d0
Refactor QuiescingHelper to exhaustively iterate state (#193)
# Motivation
Currently the `QuiescingHelper` is crashing on a precondition if you call shutdown when it already was shutdown. However, that can totally happen and we should support it.

# Modification
Refactor the `QuiescingHelper` to exhaustively switch over its state in every method. Furthermore, I added a few more test cases to test realistic scenarios.

# Result
We are now reliable checking our state and making sure to allow most transitions.
2023-02-24 09:24:37 -08:00
carolinacass
6bd9bf5c29
ServerQuiescingHelper no longer leaking promises (#192)
* ServerQuiescingHelper no longer leaking promises

Motivation:
ServerQuiescingHelper leaked promises when promise left scope and not succeeded

Modifications:
Failing promise within a deinit

* Minor fixes

* generating linux tests

* mini update
2023-02-13 11:45:10 +00:00
David Nadoba
e4efedbf47
Add Swift 5.8 CI and update nightly CI to Ubuntu 22.04 (#190)
* Add Swift 5.8 CI
@dnadoba

* Update nightly CI to Ubuntu 22.04

* Run `generate_linux_tests.rb`
2023-01-18 09:15:17 +01:00
Rick Newton-Rogers
5ff8cc59a6
Migrate http1 proxy connect handler (#185)
Motivation:

Moving the HTTP1ProxyConnectHandler into swift-nio-extras will make the
code which is generally useful when dealing with HTTP1 proxies available
more easily to a wider audience.

Modifications:

The code and tests are copied over from 0b5bec741b/Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/HTTP1ProxyConnectHandler.swift.

Result:

HTTP1ProxyConnectHandler will be surfaced via the NIOExtras library
2023-01-12 13:56:04 +00:00
Rick Newton-Rogers
54def83a52
update generated test script from swift-nio (#188)
The newer version of the script automatically calculates accurate
copyright date statements.
2022-12-02 15:17:43 +00:00
carolinacass
91dd2d61fb
Use #fileID/#filePath instead of #file (#184)
Motivation:

Modifications:

Changed #file to #filePath or #fileID depending on situation
2022-10-28 13:56:39 +01:00
George Barnett
55f37f388c
Raise minimum supported Swift version from 5.4 to 5.5 (#180)
Motivation:

SwiftNIO periodically drops support for older Swift versions. Now that
5.7 has been released, 5.4 will be dropped.

Modifications:

- Remove 5.4 specific Package.swift and docker-compose
- Update the 5.7 docker-compose to use the released 5.7 and move from
  focal (2004) to jammy (2204)
- Update docs

Results:

Minimum Swift version is 5.5
2022-09-29 09:21:17 +01:00
Cory Benfield
6c84d24775
Correctly validate the bounds of decompression (#177)
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.
2022-09-16 08:22:42 +01:00
Cory Benfield
d66ae0557e
Clean up imports and dependencies. (#144)
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.
2021-09-14 16:30:39 +01:00
Fabian Fett
79c0d26ba8
Add SOCKSProxyEstablishedEvent (#136)
* Add SOCKSProxyEstablishedEvent

* Ensuring Handler removal works

* Code review
2021-06-22 16:56:35 +01:00
David Evans
29e4c0a2b4
Fix writing domains (#135) 2021-06-18 11:58:57 +01:00
David Evans
b03d835bca
Add SOCKS authentication fast path (#134)
Add a fast path to SOCKS auto-authenticate when the server selects noneRequired as the SOCKS server authentication method.
2021-06-17 14:56:32 +01:00
David Evans
548e0d4893
Fix incorrect SOCKS client flushing behaviour (#133)
* Add buffering test

* Convert to marked buffer

* Soundness

* Re-add fastpath

* Fix
2021-06-17 11:01:54 +01:00
David Evans
93fc12bdb7
Implement SOCKSv5 server handshake (#132)
* SOCKS handshake handler implementation

* Soundness

* Remove placeholder text

* Sad path tests

* Soundness

* Docs

* Fix workflow tests

* Make handler removable

* Protect methods

* Prevent test crashes

* Cleanup public types

* Add test that writing after auth fails

* Add force handler removal tests

* Remove client and server state from public api

* Explicitly handle states

* Remove promises

* Fix test

* Add data to authentication complete

* Refactor to add authentication complete flag
2021-06-16 15:51:23 +01:00
David Evans
d861f305a1
Fix SOCKS client sending greeting and port/address endianness (#131)
* Guard channel is active

* Add delayed channel connection test

* Soundness

* Fix endianness

* Add delayed added test

* Soundness

* Apply suggestions from code review

Co-authored-by: George Barnett <gbarnett@apple.com>

Co-authored-by: George Barnett <gbarnett@apple.com>
2021-06-11 15:21:24 +01:00
David Evans
806a0ead2c
Implement a SOCKSv5 client (#128)
Implement a SOCKSv5 client according to RFC 1928. Server implementation will be added in another PR to keep sizes down.

https://datatracker.ietf.org/doc/html/rfc1928

A few meaningful changes:

Add all relevant types used across SOCKS clients and servers
Add a state machine used to manage a connection from a clients side
Add a channel handler that should be added at the very start of a channel pipeline
2021-06-10 12:12:30 +01:00
David Nadoba
de1c80ad1f
fix crash in LengthFieldBasedFrameDecoder for malicious length values (#115)
* fix crash in LengthFieldBasedFrameDecoder for malicious length values

Motivation:

LengthFieldBasedFrameDecoder will cause a fatal error if the length value does not fit into an `Int`.
This can happen if `lengthFieldLength` is set to `.eight` and we are on a 64 bit platform or if `lengthFieldLength` is set to `.four` and we are on a 32-bit platform.
If we then receive a length field value which is greater than `Int.max` the conversion from `UInt` to `Int` will cause a fatal error.
This could be abused to crash a server by only sending 4 or 8 bytes.

Modifications:

safely convert UInt64 & UInt32 to Int and throw an error if they can't be represented as an Int

Result:

- LengthFieldBasedFrameDecoder with lengthFieldLength set to `.eight` can no longer crash the server on a 64-bit platform
- LengthFieldBasedFrameDecoder with lengthFieldLength set to `.four` can no longer crash the server on a 32-bit platform

* use early exit instead of XCTSkipIf

* add support for `.eight` on 32-bit platforms

* limit frame length to `Int32.max`

* change test names

* throw correct error

* fix compilation for Swift 5.0 and add NIO prefix to error enum

* add test for maximum allowed length and one above the maximum allowed length

Signed-off-by: David Nadoba <dnadoba@gmail.com>

* run XCTest script

Signed-off-by: David Nadoba <dnadoba@gmail.com>

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2021-02-18 12:37:41 +00:00
David Nadoba
3d14afbe3f
add support for a 24 bit (3 byte) length field (#114)
Motivation:

The RSocket protocol uses a 24 bit length field

Modifications:

- add two new methods readInteger and writeInteger on ByteBuffer that support reading and writing integers of any size.
- add a new case (.three) to ByteLength

Result:

LengthFieldBasedFrameDecoder & LengthFieldPrepender do now support a 24 bit length field

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2021-02-17 09:04:24 +00:00
George Barnett
a8e195bdf8
Fail outstanding promises in channelInactive in the RequestResponseHandler (#106)
Motivation:

It's possible for channels to be closed without an error; and the
`RequestResponseHandler` should tolerate that by failing any promises
for which it does not have a response for.

Modifications:

- Add `ClosedBeforeReceivingResponseError`
- Fail outstanding promises with `ClosedBeforeReceivingResponseError` in
  `RequestResponseHandler.channelInactive`
- Add a test.

Result:

Outstanding request promises are failed when the channel becomes inactive.
2020-08-24 07:35:13 +01:00
Tanner
0b9eb87b22
Skip HTTPResponseCompressor logic if response is 204 (no content) (#105)
* Skip HTTPResponseCompressor logic if response is 204 (no content)

* update test manifests

* use mayHaveResponseBody
2020-08-14 17:10:00 +01:00
Karl
f700f5b355
Rename startsWithSameUnicodeScalars -> startsWithExactly, switch to comparing UTF8 bytes. (#104) 2020-08-05 10:47:25 +01:00
gkaindl
d525d3bbd1
Ensures gzip/deflate-compressed responses are properly finalized in all cases (#100)
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.
2020-07-28 17:33:35 +01:00
David Evans
6740bf98c2
Silence #file warnings (#97)
* Wrap in parentheses

* Revert previous changes
2020-07-24 16:54:32 +01:00
David Evans
1cb9e9e24b
Fix #file warnings (#96)
* Fix #file warnings
2020-07-24 14:54:39 +01:00
Peter Adams
fe17f53108
Capture packets leading upto a user triggered event. (#94)
Motivation:

Capturing all packets is expensive. Recording to a ring buffer and
then outputting on a triggering event allows this cost to be reduced.

Modifications:

Add a new handler - NIOPCAPRingCaptureHandler.
This derives from the existing NIOWritePCAPHandler and generates PCAP recordings.
A ring buffer contained in this handler stores the captured packets until RecordPreviousPackets
is received as a user message at which point they are flushed to the sink.

Result:

There is a new handler capable of outputting packet captured data only in the build up to
a known event.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: George Barnett <gbrntt@gmail.com>
2020-07-24 13:55:12 +01:00
Fabian Fett
b8fd38c1db
Support decompression of HTTP responses that do not contain a Content… (#93)
Fixes #78

Co-authored-by: Trevör Anne Denise <trevor.annedenise@icloud.com>
2020-07-24 11:12:09 +01:00
Johannes Weiss
7cd24c0efc
WritePCAPHandler: support logging more than 4GiB of data (#85)
Motivation:

Previously, WritePCAPHandler would crash if more than 4GiB of data were
either received or sent through the same instance of the
WritePCAPHandler because of a UInt32 overflow representing the TCP
sequence/ACK numbers.

Modifications:

Make TCP sequence/ACK numbers wrap around correctly.

Result:

- now you can send/receive up to 16 EiB of data :P.
- fixes rdar://61887658
2020-05-18 11:37:20 +01:00
Trevör
f21a87da13
Merge pull request from GHSA-xhhr-p2r9-jmm7
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.
2020-05-02 09:29:33 +01:00
Adam Fowler
020e322a65
Added NIOHTTPRequestCompressor to compress requests (#88)
* 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
2020-04-30 17:28:49 +01:00
Shekhar Rajak
4a71e8ad6e
Get rid of do { ... } catch { ... } for expected errors (#84)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-18 07:53:09 +00:00
Johannes Weiss
a98eabea3f
NIOWritePCAPHandler: make pcap issuing configurable (#75) 2020-01-20 15:15:36 +00:00