Motivation:
NIO has a 'ChannelShouldQuiesceEvent' which channels can listen for in
order to know when they should quiesce. This is typically used to
initiate a graceful shutdown of an HTTP/2 server. However, child
channels aren't notified of this event so HTTP/2 servers must keep track
of streams separately in order to notify them when the server is
quiescing.
Modifications:
- Propagate the `ChannelShouldQuiesceEvent` to child channels
Result:
Child channels can watch for `ChannelShouldQuiesceEvent`s
* Reject long sequences of CONTINUATION frames
Motivation:
Long sequences of CONTINUATION frames can be used to mount attacks by attempting to get a remote peer to consume large amounts of memory.
Modifications:
- Add a limit to the number of sequential CONTINUATION frames that can be received. This limit is configurable by users at the NIOHTTP2Handler level and has a default value of 5. When this limit is exceeded, the recipient responds with a GOAWAY frame and an "Enhance Your Calm" error of a newly created type `ExcessiveContinuationFrames`.
Result:
Long sequences of CONTINUATION frames are now rejected by the recipient.
* Refactor implementation
* Minor adjustments
* Implement adjustments
- Refactor implementation
- Revert unnecessary diffs
* Implement adjustments
- Change `public let maximumSequentialContinuationFrames` to `public var`.
- Refactor implementation.
* Implement minor adjustments
* Store the default `maximumSequentialContinuationFrames` in a `private static let`
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
Motivation:
To address warnings revealed with
`.enableExperimentalFeature("StrictConcurrency=complete"),` in Swift 5.8
Modifications:
* Numerous protections of state in tests
* Make the multiplexer instances explicitly loop bound
* Update channel initializers to be marked as Sendable
* Mark error backing storage types as unchecked sendable
Result:
Better thread safety
* Limit rate of permitted RST frames
Motivation:
Large number of stream reset frames may be used as a DoS (Denial of
Service) vector in an attempt to overload the CPU of the handling server.
Modifications:
Introduce an additional DoS heuristic which evaluates the rate of
incoming stream reset frames. If the rate exceeds that which is
permitted then the connection is closed and a `GOAWAY` issued.
The allowed rate is configurable but defaults to 200 resets within 30
seconds. This should be acceptable for most applications.
Result:
Excessive reset frames result in the connection being closed.
* review comments
* further review comments
* add integration test
Motivation:
When adding in the SPI methods for exposing async sequences of HTTP/2
streams we moved the stream initialization to a subtly different
location so that it was easier to exfiltrate the outputs of those
initialization functions (such as protocol negotiation outputs).
In some cases this broke ordering expectations.
Modifications:
Yield the (optionally wrapped) channels as initializer outputs to the async stream
Result:
Changes only exist within SPI. Async inbound stream channel
initialization now matches previous behavior.
Motivation:
The stream channel updates its writability manager when issuing a write
to the parent channel and when the write promise has been succeeded.
If the write promise has already been succeeded then the writability
manager can be updated in the wrong order which can lead to underflow.
Modifications:
- Use signed arithmetic in the writability manager and allow pending
bytes to become (briefly) negative.
Result:
The writability manager's pending bytes shouldn't underflow.
Motivation:
We don't currently support ALTSVC and ORIGIN frames. At the moment when
receiving or attempting to write these frames we trap. Trapping when
receiving an unsupported frame can lead to DoS attacks. Moreover these
frames are non critical so can safely be ignored.
Modifications:
- Forward inbound and outbound ALTSVC and ORIGIN frames to the
connection state machine
- The connection state machine now ignores inbound frames of this type
- The connection state machine traps on outbound frames of this type (as
per the current behaviour)
- Document this behaviour on `HTTP2Frame.FramePayload`
- Tests and fuzz testing failure case
Result:
We don't trap when receiving unsupported frames.
Motivation:
The `wroteFrame` flag in `HTTP2ChannelHandler` indicates whether a frame
has been written. This flag is checked before flushing in order to avoid
unnecessary flushes. Unfortunately this is only ever set to `true`.
Modifications:
- Add a `flushIfNecessary` to check and toggle `wroteFrame` and flush if
necessary
- Add a test
Result:
- Few unnecessary flushes
- Resolves https://github.com/apple/swift-nio-http2/issues/245
Motivation:
When we hit a stream error on an outbound frame, we don't currently
distinguish between them and channel errors. This makes it impossible
for anyone to tell them apart. It also makes it impossible for the
multiplexer to deliver these errors into the relevant stream channels
that triggered them, making it essentially certain they'll be lost.
Better diagnostics would be good, so we are providing them.
Modifications:
- Add new StreamError type that carries a stream ID and an underlying
error.
- Emit this StreamError type for outbound stream errors.
- Deliver this StreamError type into the relevant child channels when
possible.
Result:
Better diagnostics for stream errors.
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 imports where necessary in tests.
- 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:
The OutboundFlowControlBuffer is responsible for managing DATA and
HEADERS frames for all streams in order to ensure that we obey HTTP/2
flow control rules, even when users don't. This object is therefore on
the data path for the vast majority of frames, and is extremely
performance sensitive.
Unfortunately, the current algorithm has some nasty performance cliffs.
Right now it has the awkward property of making flushes take an amount
of time linear in the number of streams on the connection, not the
number of streams that had had frames sent on them. The result is that
connections with highly parallel numbers of streams see extremely poor
performance when flushing, a problem that is exacerbated if other parts
of the HTTP/2 stack fail to coalesce those flushes.
We can clean this up in a few ways, but the easiest way is to just keep
track of which streams have done I/O since the last flush. While we're
there, we can make a few other optimisations to ensure we only flush
data on streams we are actually processing, to further reduce the risk
of wasting our time processing unnecessary work.
Modifications:
- Add a Set to keep track of where we've done writes since the last
flush.
- Streams whose flow control window goes to zero stop being writable.
- Cheaper and more accurate calculation of zero-length writes, which
avoids the possibility of awkward stalls with zero-length DATA frames
and END_STREAM.
- Stop doing math about the number of flushed bytes, we don't use that
information any more.
Results:
An approximately 10% performance gain on the 100 concurrent streams
server-only benchmark. The relative performance gain shifts with the
number of active streams, but it should be positive in all cases.
Motivation:
Without knowing where an error was thrown it can be unnecessarily
difficult to work out the reasons an application ended up in an error
state.
Modifications:
- Add a 'file' string to each of the 'NIOHTTP2Errors' denoting the file
and line where an error was created
- Add static methods to create errors to 'NIOHTTP2Errors' defaulting
file and line, this is unfortunately necessary as defaulting file and
line in the `init` makes the defaulted and non-defaulted errors
indistinguishable, and the compiler will pick the one with fewest
defaulted values.
- Add equatable conformance manually so as to ignore 'file'
- Add backing storage to errors which no longer be stored in an existential
container
- Add 'CustomStringConvertible' conformance to the errors using backing
storage so that the 'file' is actually visible when converting the
error
- Deprecate the old `init`s
- Fix all deprecation warnings
Result:
- NIOHTTP2Errors carry a 'file' with them indicating where they were
created
Motivation:
We made a bunch of changes to resolve#214. We should add a test to
ensure we actually fixed the issue.
Modifications:
- Add a test verifying that we can write on streams in a different order
to the order in which we create them
- Add a corresponding test for frame-based streams verifying that the
first write on each stream must match the order in which the streams
were created
- Remove an unnecessary `throws` in the base HTTP2 to HTTP1 server codec
Result:
- No functionality change, just more tests.
Motivation:
As part of #214 we need to introduce new pipeline helpers to configure
the channel pipeline with the new handlers. This PR adds those helpers
and deprecates the old paths.
Modifications:
- Add pipeline helpers with the new streamID-less initializer
- Switch old pipeline helpers which provided codecs to use the new path
and codecs
- Duplicate SimpleClientServerTests and ConfiguringPipelineTests to add
frame payload based counterparts
- Deprecate older helpers and multiplexer init, delete tests which don't
hit deprecated paths (note: these still exist in the payload based
counterparts)
- Update Bench1Conn10kRequests to be payload based
Result:
- More 'FramePayload' based Tests
- Paths for creating an 'HTTP2Frame' based stream via
'HTTP2StreamMultiplexer' are now deprecated