251 Commits
Author SHA1 Message Date
Aryan Shah 2f58d8ef84 Revert configuration type for HTTP/1 server pipeline setup (#3775)
### Motivation:

We introduced a new configuration type in #3746 to make further
extensions to the server pipeline (#3745 for example) easier to
introduce.

The plan was to merge #3745 soon after #3746 was merged, because #3745
added a breaking change to the new config type.

However, #3745 needs some more consideration. We should therefore revert
the change in #3746 to unblock new releases from being made until we
come up with a better solution.
 
### Modifications:

A clean revert of PR #3746.

### Result:

Unblocks new releases.
2026-11-02 13:00:48 +00:00
Aryan Shah 108daca35d Add configuration type for HTTP/1 server pipeline setup (#3746)
### Motivation:

`NIOHTTP1.HTTPPipelineSetup` contains helper methods which add HTTP/1
client/server handlers to the provided channel pipeline.

Over time, we have had to duplicate these methods when introducing a new
configuration option in order to not break API.

As a result, there is now lots of duplication in
`NIOHTTP1.HTTPPipelineSetup` and introducing more configuration options
is very cumbersome. We should instead add a configuration type to solve
this problem.

### Modifications:

- Added new configuration types named
`NIOHTTPServerPipelineConfiguration`, and
`NIOHTTPServerPipelineSynchronousConfiguration`.
- Added a new pipeline setup helper method on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations` that requires the configuration
type as an argument.
- Deprecated all existing helper methods on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations`.

### Result:

Better API.
2026-10-19 13:23:19 +01:00
Anton Karpov 14059d6536 Name the documented arguments the way the declarations name them (#3698)
### Motivation:

Eight doc comments name an argument that the declaration below them does
not have, so DocC has nothing to bind the description to and quietly
drops it. Nothing warns about this: Swift has no equivalent of
`-Wdocumentation`, and DocC stays silent rather than complaining, so
these sit until somebody reads the generated page and wonders where the
description went

| file | the comment says | the declaration says |
|---|---|---|
| `NIOPosix/ServerSocket.swift` | `descriptor` | `socket` |
| `NIOPosix/Socket.swift` ×2 | `descriptor` | `socket` |
| `NIOHTTP1/ByteCollectionUtils.swift` | `bytes` | `to` |
| `NIOHTTP1/HTTPTypes.swift` | `buffer` | `headers` |
| `NIOFS/BufferedStream.swift` ×2 | `sequence` | `element` |
| `NIOFS/SystemFileHandle.swift` | `executor` | `threadPool` |

The socket ones are worth a word. Each of those files has a pair: a
deprecated `init(descriptor:)` and its replacement `init(socket:)`, and
the rename is recorded right there in `@available(*, deprecated,
renamed: "init(socket:setNonBlocking:)")`. The replacement inherited the
wording of the one it replaced, so the doc still says `descriptor`. The
deprecated ones are correct and I left them alone

And the last two are the same defect in two copies of one module,
`NIOFS` and `_NIOFileSystem`, which is what a verbatim copy of a file
does to a mistake inside it. Fixing one and not the other would leave
them out of sync, so both are in here

### Modifications:

Renamed the documented argument to the one in the declaration, eleven
lines across eight files

One is slightly more than a rename: in `ByteBuffer.write(headers:)` the
entry described the buffer the method is called on rather than its
argument, so it now describes `headers` and keeps the note about the
writer index

### Result:

The parameter descriptions reach the generated documentation. No API
change, no behaviour change, comments only

btw these came out of a checker I wrote for exactly this, run over the
tree and then read by hand one by one. Six of the things it flagged
turned out to be my parser rather than your code, and those are fixed on
my side rather than sent to you 🙌🏼

all of it on a MacBook Air M3, of course
2026-09-01 11:10:23 +00:00
Gus Cairo 0b18836bd8 Don't write any bytes for H1 responses that shouldn't include a body (#3651)
### Motivation:

On the server side, `HTTPResponseEncoder` chooses a response's body
framing only from the response status
(`HTTPResponseStatus.mayHaveResponseBody`): it has no knowledge of the
request method as it's an outbound-only handler. But HTTP requires that
a response to a HEAD request (and a 2xx response to CONNECT) carry no
message body, regardless of status or framing headers.

As a result, a response to a HEAD request with a status that allows a
body (like 200) and no `Content-Length` gets framed as
`Transfer-Encoding: chunked`, and the encoder emits the chunked
terminator (`0\r\n\r\n`). This is illegal.
A conformant client won't read those bytes when receiving a HEAD
response, so on a keep-alive connection they get misinterpreted as the
start of the next response, corrupting the connection. This surfaces as
intermittent, hard-to-diagnose failures (e.g. "I/O on closed channel")
on the following request.


### Modifications:

Made the server response framing method-aware without changing any
public API:
  
- The request decoder now hands each decoded request's method to the
response encoder.
- `HTTPResponseEncoder` consults the method and, for responses to
HEAD/CONNECT requests, omits the body and the terminator.
- This is wired up in `configureHTTPServerPipeline`. When the encoder is
constructed and used on its own, its behaviour is unchanged (framing is
still derived solely from the status), so there is no behavioural or API
change for users who may be creating this handler manually.


### Result:

Responses to `HEAD` and `CONNECT` requests are no longer emitted with a
message body, keeping keep-alive connections well-formed and
RFC-compliant. No public API changes.
2026-07-15 14:55:52 +01:00
Johannes Weiss eeb20e7f95 HTTPDecoder: restore old limit of 80 kB headers (#3621)
### Motivation:

NIO's historic limit was that the maximum header block was limited to 80
kB which is a sensible limit and originates from Nginx's http_parser.c.

The history of the changes:
- commit e67fc995: Introduces the 80 kB limit
- commit 69180342: Accidentally removes the limit entirely
- commit b24872d3: Introduced the 2 MB / 256 headers limit

I would argue that 2MB & 256 headers is a bad limit:
- 2MB is way too big, that allows pretty powerful slowloris-style
attacks forcing NIO to hold on to 2 MB per connection
- 256 headers is too little for software that may be a proxy
  - in HTTP, the number of headers is _non-semantic_
- A: `a: 1\r\n a: 2\r\n a: 3\r\n a: 4\r\n a: 5\r\n a: 6\r\n[...]\r\na:
257\r\n`
    - B: `a: 1, 2, 3, 4, 5, ..., 257\r\n`
- A & B are _equivalent_. Yet, A would be rejected and B accepted.
That's not right.
 
### Modifications:

Restore the old limits.

### Result:

- More compatible (because we allow more than 256 headers)
- More secure (because the default goes back to 80kB)
2026-06-30 09:22:19 +00:00
Kumar TanayandGeorge Barnett a8e036cb86 Fix incorrect status lines in HTTPResponseEncoder fast paths (#3631)
### Motivation

`HTTPResponseEncoder` writes hand-optimised status lines for the
well-known `HTTPResponseStatus` values instead of going through the
general `default` path that formats `HTTP/<version> <code>
<reasonPhrase>`. Several of those fast paths had drifted out of sync
with `HTTPResponseStatus.reasonPhrase`, so the exact bytes on the wire
depended on whether a status happened to have a fast path:

* **HTTP/1.0 + 511** emitted the wrong HTTP version (`HTTP/1.1 511 ...`)
— a wire-format correctness bug, not just cosmetic.
* **HTTP/1.1 + 431** emitted the reason phrase `Range Not Satisfiable`
instead of `Request Header Fields Too Large`.
* **HTTP/1.1 + 416** emitted `Request Range Not Satisified` (also
misspelled) instead of `Range Not Satisfiable`.
* **HTTP/1.0 and HTTP/1.1 + 307** emitted `Tempory Redirect`
(misspelled) instead of `Temporary Redirect`.

Resolves #3630.

### Modifications

* Correct the five diverging status lines in `HTTPResponseEncoder` so
they match `HTTPResponseStatus.reasonPhrase` and carry the correct HTTP
version.
* Add a regression test that, for every well-known status at both
HTTP/1.0 and HTTP/1.1, asserts the encoded status line equals the
canonical `HTTP/<version> <code> <reasonPhrase>` form, keeping the fast
paths in lockstep with `reasonPhrase`.

### Result

The encoder now produces correct, consistent status lines for all
well-known statuses regardless of whether they use a fast path. The new
test fails on the pre-fix code (the 511 case fails on the version, the
431/416/307 cases on the reason phrase) and passes after the fix.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-06-23 12:33:49 +00:00
Adam FowlerandCory Benfield 9670fe1d68 Don't fatalError if NIOTypedHTTPServerUpgraderStateMachine is in state finished when finding upgrader (#3597)
Don't fatalError in
NIOTypedHTTPServerUpgraderStateMachine.findingUpgraderCompleted` when
the state is `.finished`.

### Motivation:

There is a race where the connection can be closed and the
NIOTypedHTTPServerUpgraderHandler can be removed setting the state to
`.finished` while we are still finding upgraders. And then when
`findingUpgraderCompleted` gets called it would fatalError.

### Modifications:

- Return `nil` as an action if the state is `.finished` when
`findingUpgraderCompleted` is called.
- Added a new set of tests which test the
`NIOTypedHTTPServerUpgraderStateMachine` directly instead of relying on
`HTTPServerUpgraderTests`. I used Swift Testing for these, not sure what
NIO policy on new tests are?

### Result:

No more crashes and better testing for
NIOTypedHTTPServerUpgraderStateMachine

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-05-21 13:41:50 +00:00
Fabian Fett 57c0a08a33 Speed up the tests (#3601)
### Motivation

We want our tests to pass in reasonable time.

### Changes

- Disable test
`ByteBufferCrashTests.setBytesWithoutContigiousStorageMoreThanUInt32maxBytes`
- Do not initialize data in `ByteBufferCrashTests.
takingOwnershipOfPointerThatsToLarge`

### Result

Tests are faster again.
2026-05-20 10:12:53 +00:00
Fabian Fett b24872d3aa Merge commit from fork
Add configurable HTTP/1 decoder parsing limits

### Motivation:

The HTTP/1 decoder previously enforced a single hardcoded limit (80 KB per header field) and placed no cap on the total header-block size or the number of header fields per message. That made resource use unpredictable on requests with very many or very large headers, and there was no way for users to tune the limits for their workload.

### Modifications:

- Introduce a new public `NIOHTTPDecoderLimitConfiguration` exposing three parsing limits:
  - maxHeaderFieldSize — maximum size of a single header field (name + value). Default: 80 KB.
  - maxHeaderListSize — maximum total bytes of all header field names and values combined per message. Default: 2 MB.
  - maxHeaderFieldCount — maximum number of header fields per message, including trailers. Default: 256.
- Plumb the configuration through `HTTPDecoder` / `HTTPRequestDecoder` / `HTTPResponseDecoder` initializers. The existing initializers continue to work unchanged via convenience overloads that fall back to
  default limits.
- Expose the configuration on `NIOUpgradableHTTPServerPipelineConfiguration` and `NIOUpgradableHTTPClientPipelineConfiguration` so users of the typed pipeline setup can override the defaults.
- Track per-message totals in `BetterHTTPParser` and reject with `HTTPParserError.headerOverflow` when a limit is exceeded; counters reset at each new message so HTTP/1 pipelined connections behave
  correctly.
- Add tests covering the success and error paths for each limit, the default behaviors, and that trailer fields count toward the field-count cap.

### Result:

Users can configure the HTTP/1 decoder's per-field-size, total-list-size, and field-count limits. Existing call sites continue to compile and run unchanged, now with bounded defaults across all three dimensions instead of just one.
2026-05-20 10:00:56 +01:00
Fabian Fett dd16365724 Merge commit from fork
- Ensure HTTP Request Uri and Method only contain allowed bytes
- Ensure HTTP Response Reasons only contain allowed bytes
2026-05-20 09:56:46 +01:00
Fabian Fett 37e191d35b Strip Transfer-Encoding when Content-Length is present in HTTPEncoder (#3583)
Strip Transfer-Encoding when Content-Length is present in HTTPEncoder

### Motivation

When `correctlyFrameTransportHeaders()` detected a `Content-Length`
header, it returned `.contentLength` without removing any existing
`Transfer-Encoding` header.

### Changes

- Remove `Transfer-Encoding` header, if a `Content-Length` header is
present

### Result

NIO sends either a `Content-Length` header or a `Transfer-Encoding`
header, but not both.
2026-05-06 10:14:09 +00:00
Adam FowlerandCory Benfield 61053a00bb NIOTypedHTTPClientUpgradeHandler will write Upgrade request on handlerAdded if active (#3473)
`NIOTypedHTTPClientUpgradeHandler.handlerAdded` will write upgrade
request if the channel is already active

### Motivation:

This has been added to make it easier to implement a websocket client
that supports proxies. If the upgrade handler is added after the proxy
connect has been processed previously nothing would happen.

### Modifications:

- Added new function
`NIOTypedHTTPClientUpgradeHandler.writeUpgradeRequest()` function. Call
this from `channelActive` and also from `handlerAdded` if the channel is
already active.
- Added test `testUpgradeHappensAfterHandlerAdded`

### Result:

NIOTypedHTTPClientUpgradeHandler now also works if it is added to a
channel after the channel is active

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-01-14 11:40:00 +00:00
aryan-25 a1605a3303 Use explicit class name in all Self.[un]wrap{In}{Out}bound{In}{Out} calls (#3463)
### Motivation:

Swift does not currently generic specialize static methods on final
classes that have no parent classes when the method is implemented in a
protocol. This means that calling such methods through `Self` will not
involve a generic specialization, whereas using the explicit type name
will.

This pattern manifests in the `[un]wrap{In}{Out}bound{In}{Out}` static
methods defined in the
[`ChannelInboundHandler`](https://github.com/apple/swift-nio/blob/27146d484478b1bb0f150e848758f3a34ed9cbd0/Sources/NIOCore/TypeAssistedChannelHandler.swift#L60)
and
[`ChannelOutboundHandler`](https://github.com/apple/swift-nio/blob/27146d484478b1bb0f150e848758f3a34ed9cbd0/Sources/NIOCore/TypeAssistedChannelHandler.swift#L95)
protocols and their use from all channel handler classes. As such, we
should replace the `Self` part in all
`Self.[un]wrap{In}{Out}bound{In}{Out}` calls with the explicit class
name.

### Modifications:

Replaced all `Self.[un]wrap{In}{Out}bound{In}{Out}` calls to use the
explicit class name.

### Result:

Eliminates unnecessary overhead.
2025-12-15 17:36:11 +00:00
George BarnettandCory Benfield a65e9735ac Drop all server response parts after an invalid header (#3339)
Motivation:

NIOHTTPResponseHeadersValidator drops head/end response parts if they
contain invalid header fields. If a head part is dropped and the server
allows pipelining then the subsequent body/end part will reach the
pipelining handler and result in an assertion failure in debug builds.
In release builds, some parts may be incorrectly written out leading to
protocol violations.

Modifications:

- Drop all response parts after an invalid response part

Result:

- Fewer bugs
- Resolves #3326

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-08-12 09:33:24 +00:00
Fabian FettandCory Benfield 2c9b7c6061 [Windows] Ensure NIOHTTP1 compiles (#3338)
This only needs one `numericCast`.

Previous error:
```
C:\Users\fat\Developer\apple\swift-nio\Sources\NIOHTTP1\HTTPDecoder.swift:113:49: error: cannot convert value of type 'CUnsignedInt' (aka 'UInt32') to expected argument type 'Int32'
 111 |                 statusCode: Int(opaque!.pointee.status_code),
 112 |                 isUpgrade: opaque!.pointee.upgrade != 0,
 113 |                 method: llhttp_method(rawValue: CUnsignedInt(opaque!.pointee.method)),
     |                                                 `- error: cannot convert value of type 'CUnsignedInt' (aka 'UInt32') to expected argument type 'Int32'
 114 |                 keepAliveState: opaque!.keepAliveState
 115 |             ) {
```

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-08-11 17:20:12 +00:00
Rafael Cepeda 02be63c7f4 Fixes all warnings when -require-explicit-sendable flag is enabled (#3320)
Fixes all warnings when `-require-explicit-sendable` flag is enabled and
enables the flag on macOS CI.

### Motivation:

We want to ensure our public API is either explicitly marked as
`Sendable` or not.

### Modifications:

Marked appropriate public types as `Sendable`, or explicitly defined
their conformance to the `Sendable` protocol as unavailable.

### Result:

We can now enable `-require-explicit-sendable` compiler flag in our
codebase.
2025-07-30 11:50:28 +01:00
Rick Newton-Rogers 57658e12ff Drop Swift 5.9 (#3228)
Motivation:

Swift 5.9 is no longer supported, we should bump the tools version and
remove it from our CI.

Modifications:

* Bump the Swift tools version to Swift 5.10
* Remove Swift 5.9 jobs where appropriate in main.yml, pull_request.yml

Result:

Code reflects our support window.
2025-05-07 12:24:44 +00:00
Cory Benfield abe4f1bcb6 Replace almost all public static lets with computed vars (#3229)
Motivation:

Public static lets can serve a bunch of roles, but one of them is to
store simple constants: integers, and other trivial types, for example.
This is a nice pattern and for internal and private static lets it works
well, but for public ones it produces some inefficient code.

In particular, it has two downsides. First, it allocates storage for
that value. We don't actually need to allocate a few hundred extra
megabytes for the various integers we want to store.

Secondly, it forces calling code to access the address and call the
dispatch_once code in order to get hold of the value. For trivial types
we don't need that cost: they can just know what the value is directly.

Inlinable computed vars avoid all of these costs: they have no size
overhead for storage, and they are visible to all clients so their
values can be directly assembled.

While I'm here, I added a bunch of other inlinable annotations for a few
trivial data types I stumbled onto.

Modifications:

- Added loads of inlinables. Like, loads.
- Swapped many static lets to static vars.

Result:

Better codegen, smaller memory footprints, more attributes.
2025-05-02 16:08:10 +01:00
Adam FowlerandCory Benfield d7a8ff6e36 Add errorCaught to NIOTypedHTTPClientUpgradeHandler (#3210)
To handle errors fired by previous ChannelHandlers. eg
NIOSSLClientHandler

### Motivation:

Otherwise if a ChannelHandler previous to the upgrade ChannelHandler
fails the error returned is `inappropriateOperationForState` and the
error fired by the original ChannelHandler is lost.

Added `testReturnsErrorsFromPriorChannelHandlers`. Also had to
reorganise `setUpClientChannel` slightly so I could get the result of
the upgrade.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-04-16 14:24:42 +00:00
Rick Newton-Rogers 5f60ceeca0 Enable strict concurrency checking for NIOHTTP1 (#3115)
### Motivation:

To ensure NIOHTTP1 concurrency safety.

### Modifications:

* Enable strict concurrency checking in the package manifest.
* Mark several objects `Sendable` with `@preconcurrency` annotations
where they are returned in futures which may execute in arbitrary
concurrency domains.
  * `NIOTypedHTTPClientProtocolUpgrader`
  * `NIOTypedHTTPClientUpgradeConfiguration`
  * `NIOUpgradableHTTPServerPipelineConfiguration`
  * `NIOUpgradableHTTPClientPipelineConfiguration`
  * `NIOTypedHTTPServerProtocolUpgrader`
  * `NIOTypedHTTPServerUpgradeConfiguration`
* Mark handlers as explicitly not sendable
  * `NIOTypedHTTPClientUpgradeHandler`
  * `NIOTypedHTTPServerUpgradeHandler`
* Added new Sendable type aliases:
  * `NIOHTTPClientUpgradeSendableConfiguration`
  * `NIOHTTPServerUpgradeSendableConfiguration`

### Result:

No more concurrency warnings. Builds will warn and CI will fail if
regressions are introduced.
2025-02-19 15:04:51 +00:00
Tim CondonandCory Benfield ac79370dee Add ChannelPipeline.SynchronousOperations.Position (#3065)
Adds a new `ChannelPipeline.SynchronousOperations.Position` type to fill
a hole in the API that prevented non-sendable Channel handlers being
added at a position to the pipeline via the synchronous operations.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-02-04 10:31:29 +00:00
bf41ad1595 Improve HTTPHeaders description performance (#3063)
### Motivation:

As outlined in this
[issue](https://github.com/apple/swift-nio/issues/2930) by @weissi, the
current performance of calling `description` on `HTTPHeaders` is
undermined by the dynamism of Array.

### Modifications:

The proposed solution replaces the implementation of `description` to
iterate over the items and print them out manually. This provides a
faster solution since we bypass the cost of calling into `description`
of an Array.


### Result:

A more performant implementation of `HTTPHeaders` description.

---------

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-01-20 16:39:21 +00:00
Cory BenfieldandJohannes Weiss 96877aff7f Fix compiling on 6.1 (#3057)
Motivation:

Swift 6.1 nightlies have added many new warnings and errors to NIO. This
patch resolves all of them.

Modifications:

- Clean up missing imports
- Move away from @_implementationOnly where necessary
- Fix a few things where Sendable mismatches were present.

Result:

Clean compile again

---------

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2025-01-14 16:36:17 +00:00
Johannes Weiss 6ba8f4f04a fix almost all Sendable warnings (#2994)
### Motivation:

Opening the `swift-nio` repository made me warning blind because there
were always so many trivially fixable warnings about things that were
correct but cannot be understood by the compiler.

### Modifications:

Fix all the sendable warnings that popped up, except for one test where
`NIOLockedValueBox<Thread?>` isn't sendable because `Foundation.Thread`
seemingly isn't `Sendable` which is odd. Guessing that'll be fixed on
their end.

### Result:

- Fewer warnings
- Less warning-blindness
- More checks
2024-11-25 17:56:44 +00:00
Adam FowlerandFranz Busch a026de35e0 Close channel during upgrade, if client closes input (#2756)
Check for half closure during server upgrade and close channel if client
closes the channel

### Motivation:

This is to fix #2742 

### Modifications:

Add `userInboundEventTriggered` function to
`NIOTypedHTTPServerProtocolUpgrader` which checks for
`ChannelEvent.inputClosed`

### Result:

Negotiation future now errors when client closes the connection instead
of never completing

---------

Co-authored-by: Franz Busch <privat@franz-busch.de>
2024-11-14 18:15:12 +00:00
Rick Newton-Rogers adfd61adc5 Use Swift 6.0 docs pipeline (#2966)
### Motivation:

Documentation checking catches more issues in Swift 6.0.

### Modifications:

Adopt the Swift 6.0 image and fix the errors.

### Result:

More accurate docs.
2024-11-07 13:03:47 +00:00
Cory Benfield 411c2c553c Handle Sendability of RemovableChannelHandler (#2953)
Motivation:

RemovableChannelHandlers have a large API surface in NIOCore. That API
surface is a bit awkward with regard to strict concurrency, and needs
some cleanup.

Modifications:

This patch adds some new API that is necessary to safely work with
RemovableChannelHandlers, deprecates some API that cannot plausibly be
used, and cleans up some other parts of the API.

Result:

Easier to work with RemovableChannelHandlers
2024-10-29 10:40:47 +00:00
Franz Busch c9756e1083 Adopt swift-format (#2794)
* Apply formatting

* Apply no block comments rule

* Apply OmitExplicitReturns

* Apple OnlyOneTrailingClosureArgument

* Apply NoAssignmentInExpressions

* Fix up DontRepeatTypeInStaticProperties lint errors

* Apply `OrderedImports`

* Apply `ReplaceForEachWithForLoop`

* format file

* Enable the formatting pipeline

* Adopt `AmbiguousTrailingClosureOverload`

* Fix license header

* Fix format check

* Fix `EndOfLineComment`

* Fix CI

* Adapt CI script to check if changes when running formatting

* Separate lint and format into to steps

* Fix format

* Adopt `UseEarlyExits`

* Revert "Adopt `UseEarlyExits`"

This reverts commit d1ac5bbe12.
2024-07-19 11:48:17 +02:00
Johannes Weiss 7948ed2104 ChannelHandler: provide static (un)wrap(In|Out)bound(In|Out) (#2791) 2024-07-18 11:55:48 +01:00
Dimitri Bouniol 5e9a9ccc10 Improved documentation for HTTP Parts to clarify how often each part is received (#2775) 2024-07-12 02:20:14 -07:00
Dimitri Bouniol 72cab7d9a4 Correctly mark 304 as not having a response body (#2737)
* Fixed an issue where HTTP/2 connections would disconnect if response compression were used and a 304 Not Modified response was returned

* Added HTTP encoder tests for 304 Not Modified responses
2024-06-12 18:30:58 +01:00
Franz BuschandCory Benfield 63e8c4e1aa Migrate to syncOperations in more places (#2661)
# Motivation

Adding a handler via the regular `pipeline.addHandler` APIs can lead to transferring the handler over isolation regions. To avoid running into `Sendable` warnings here we can use the `syncOperations` of the pipeline to avoid transferring the handlers.

# Modification

This PR is mostly changing code to avoid transferring handlers to and from the event loops in tests. This should be a no-op mostly.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-03-01 15:05:22 +00:00
Franz Busch 702cd7c56d Fix the typed HTTP upgrade compiler guards (#2594)
The compiler guards were unnecessarily complex and I also didn't cover 3 methods where we used the types.
2023-11-16 03:30:21 -08:00
Franz Busch c46199386c Revert "Back out new typed HTTP protocol upgrader (#2579)" (#2593)
* Revert "Back out new typed HTTP protocol upgrader (#2579)"

# Motivation
We have reverted the typed HTTP protocol upgrader pieces since adopters were running into a compiler bug (https://github.com/apple/swift/pull/69459) that caused the compiler to emit strong references to `swift_getExtendedExistentialTypeMetadata`. The problem is that `swift_getExtendedExistentialTypeMetadata` is not available on older runtimes before constrained existentials have been introduced. This caused adopters to run into runtime crashes when loading any library compiled with this NIO code.

# Modifications
This PR reverts the revert and guard all new code in a compiler guard that checks that we are either on non-Darwin platforms or on a new enough Swift compiler that contains the fix.

# Result
We can offer the typed HTTP upgrade code to our adopters again.

* Add compiler guards
2023-11-15 18:58:27 +00:00
Franz Busch 8c238f2cc1 Back out new typed HTTP protocol upgrader (#2579)
# Motivation
We got reports in https://github.com/apple/swift-nio/issues/2574 that our new typed HTTP upgrader are hitting a Swift compiler bug which manifests in a runtime crash on older iOS/macOS/etc.

# Modification
This PR backs out the new typed HTTP protocol upgrader APIs so that we can unblock our users until the Swift compiler bug is fixed.

# Result
No more crashes for our users.
2023-10-27 15:48:23 +01:00
hamzahrmalikandCory Benfield a41b02c580 Avoid terminating when a precondition is not met in HTTPServerPipelineHandler (#2550)
* Avoid terminating when a precondition is not met in HTTPServerPipelineHandler

* Address review comments

* rename precondition to assertion

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-10-18 06:26:43 -07:00
Franz Busch f38b7fd38a Remove SPI from NIOAsyncChannel, new bootstrap methods, protocol negotiation and HTTP upgrade. (#2548)
# Motivation
Over the past months, we have been working on new async bridges to make using NIO's `Channel` from Swift Concurrency possible. Since this work was far reaching we have opted to land all of it as SPI. Now the time has come and we feel confident enough to make the SPI official API. This comes after testing the new APIs in various scenarios such as HTTP 1&2, HTTP upgrades, protocol negotiation and in benchmarks.

# Modification
This PR removes the SPI from the `NIOAsyncChannel`, the bootstrap methods, protocol negotiation and HTTP upgrade.

# Result
Everyone can use the our new APIs🚀
2023-10-17 14:14:02 +01:00
Franz Busch 0fb8cb7947 Add docs for the async NIO APIs (#2549) 2023-10-16 15:02:43 +01:00
Franz Busch 4ab8b98f9d Introduce new typed HTTPClientUpgrader and WebSocketClientUpgrader (#2526)
* Introduce new typed `HTTPClientUpgrader` and `WebSocketClientUpgrader`

# Motivation
In my previous PR https://github.com/apple/swift-nio/pull/2517, I added a new typed `HTTPServerUpgrader` and corresponding implementation for the `WebSocketServerUpgrader`. The goal of those is to carry type information across HTTP upgrades which allows us to build fully typed pipelines.

# Modification
This PR adds a few things:
1. A new `NIOTypedHttpClientUpgradeHandler` + `NIOTypedHttpClientProtocolUpgrader`. I also moved the state handling to a separate state machine. Similar to the server PR I did not unify the state machine between the newly typed and untyped upgrade handlers since they differ in logic.
2. A new `NIOTypedWebSocketClientUpgrader`
3. An overhauled WebSocket client example.

# Result
This is the last missing piece of dynamic pipeline changing where we did not carry around the type information. After this PR lands, we can finalize the `AsyncChannel` and async typed NIO pieces.

* Remove availability on the protocols
2023-10-06 16:33:25 +01:00
David Nadoba d367dc0a87 Tolerate empty HTTP response body parts (#2531)
* Tolerate empty HTTP response body parts

### Motivation

Empty HTTP response body parts currently eagerly end a response. This is unexpected and against NIOs documented behaviour.

### Modification

- add test which send an empty response body part and would previously fail
- skip empty response body parts

### Result

Users can send empty body parts without ending the response.

* add comment explaining why we do it here instead of fixing it at the source

* move empty body part logic to `writeChunk`
2023-10-04 06:42:26 -07:00
Franz Busch de07e573d6 Introduce new typed HTTPServerUpgrader and WebSocketServerUpgrader (#2517)
* Introduce new typed `HTTPServerUpgrader` and `WebSocketServerUpgrader`

# Motivation
With our new `NIOAsyncChannel` and typed bootstrap APIs we want to be able to let users spell out their pipeline in a typed way. Pipelines can contain handlers that have to make a forking decision such as HTTP upgrading. Our current `HTTPServerUpgradeHandler` is one of those handlers but it lacks strict typing. To interact nicely with our new typed APIs we need to have a new variant of the `HTTPServerUpgradeHandler` that can carry type information.

# Modification
This PR adds a few things:
1. A new `NIOTypedHTTPServerUpgradeHandler` + `NIOTypedHTTPServeProtocolUpgrader`. I also moved the state handling logic to a separate state machine. I thought about unifying the state machines of the _old_ handler and the new one but they differ in behaviour which makes the state machine more complicated.
2. A new `NIOTypedWebSocketServerUpgrader` that conforms to `NIOTypedHTTPServerProtocolUpgrader`
3. An overhauled WebSocket server example that fully uses Concurrency.

# Result
We now have a way to fully type the server side of HTTP protocol upgrading.

Code review

Update parameter names for new API and fix example

* Introduce new configuration struct and rename to `UpgradablePipeline`

* Review comments
2023-10-02 07:26:16 -07:00
Rick Newton-Rogers 67553a7d6d Bump minimum Swift version to 5.7 (#2524)
* Bump minimum Swift version to 5.7

Motivation:

Now that Swift 5.9 is GM we should update the supported versions and
remove 5.6

Modifications:

* Update `Package.swift`
* Remove `#if swift(>=5.7)` guards
* Delete the 5.6 docker compose file and make a 5.10 one
* Update integration test script
* Update docs

Result:

Remove support for Swift 5.6, add 5.10

* fix indentation issues

* 5.9 docker image use release image
2023-10-02 11:38:07 +01:00
Alex Taffe e0d85543d7 Simplify header separator and crlf (#2513)
Everywhere else in the file uses `crlf` and `headerSeperator` when writing those static strings, we shouldn't be using string literals here
2023-09-06 14:52:25 +01:00
Cory Benfield 7d9f892d83 Support users opting-out of us setting framing headers. (#2508)
Motivation:

Right now we always set the framing headers in HTTP requests and
responses that we send. This is a good practice for most users, but
it can cause issues, most notably in responses to CONNECT requests
which requires that we do not set framing headers.

Unfortunately, in NIO's current HTTP/1.1 design it is not possible
for us to suppress these headers. This is because the HTTP encoders
come _earlier_ in the pipeline than the decoders, so the HTTP
encoders do not know structurally what requests they are responding
to.

While we could merge the encoders/decoders, that's a fairly
substantial change. A better short-term change is to offer users
the ability to turn off the NIO header manipulation feature. In
this circumstance, users take on full responsibility for
appropriately framing their HTTP messages.

Modifications

- Add config to HTTPRequestEncoder and HTTPResponseEncoder.
- Plumb this config through.
- Add a bunch of tests.

Result

Users have way more control of HTTP/1.1 messages.
2023-08-22 02:51:05 -07:00
Cory Benfield 2d8e6ca36f Tolerate sending data after close(mode: .output) (#2421)
Motivation

We shouldn't crash on somewhat likely user error.

Modifications

Pass on writes after close(mode: .output) instead of crashing.

Result

User code is more robust to weird edge cases.
2023-05-09 10:52:48 +01:00
Cory Benfield 5f8b0647e4 Handle close(output) in the pipeline handler. (#2414)
Motivation:

Currently the server pipeline handler ignores close. This generally works,
except in the rare case that the user calls close(mode: .output). In this
instance they have signalled that they'll never write again, and they're
likely expecting a final close shortly after.

However, it is possible that the pipeline handler has suspended reads
at the same time. On Linux this isn't an issue, because we'll still be told
about the eventual socket close. However, on Apple platforms we won't: we've
masked off the reads, and we can't listen to EVFILT_EXCEPT due to some
other issues. This means that on Apple platforms the server pipeline handler
can accidentally wedge the Channel open and prevent it from closing.

We should take this opportunity to have the server pipeline handler be smart
about close(mode: .output). What _should_ happen here is that the pipeline
handler should immediately refuse to deliver further requests on the Channel.
If one is in-flight, it can continue, but everything else should be dropped.
This is because the server cannot possibly respond to further requests.

Modifications:

- Add new states to the server pipeline handler
- Drop buffered requests and new data after close(mode: .output)
- Add tests

Result:

Server pipeline handler behaves way better.
2023-04-28 06:22:07 -07:00
David Nadoba 6720612111 Drop Swift 5.5 (#2406)
* Drop Swift 5.5

* Use `swift-atomics` 1.1.0 with `Sendable` adoption
2023-04-17 08:40:35 +01:00
George Barnett 73eb597aa3 Don't unconditionally remove the HTTPServerUpgradeHandler (#2303)
Motivation:

The `HTTPServerUpgradeHandler` removes itself from the pipeline after an
upgrade and unbuffers any unconsumed reads. If an upgrade starts but
does not complete successfully then the pipeline may be left in an
unknown state.

If, for example, the failure occurs before the user provided upgrade
handler is run then unbuffered writes may be unwrapped by the wrong
channel handler as the wrong type leading to a crash.

Modifications:

- Only remove the upgrade handler and forward buffered writes if all
  parts of the upgrade complete successfully.
- If part of the upgrade fails then fire an error into the channel
  pipeline without removing the server upgrade handler.
- Remove a few unnecessary `map`s.
- Make `httpEncoder` non-optional since and remove associated dead code
  since it can never be `nil`.

Result:

Upgrade handling is safer.
2022-10-28 07:32:43 -07:00
David Nadoba 16b5b2b793 Replace NIOSendable with Sendable (#2291) 2022-10-13 15:56:27 +01:00
David Nadoba c7b4989b02 Remove #if compiler(>=5.5) (#2292)
### Motivation
We only support Swift 5.5.2+.

### Modification
Remove all `#if swift(>=5.5)` conditional compilation blocks.

### Result
less branching
2022-10-13 07:17:46 -07:00