80 Commits
Author SHA1 Message Date
Cory Benfield c2c39c63f6 Basic PRIORITY frame validation. (#71)
Motivation:

In the future we'll want to use PRIORITY frames to provide feedback on
how we emit data. Additionally, RFC 7540 forbids certain kinds of
PRIORITY frame, which right now we tolerate receiving. We can design a
minor PRIORITY shim to ensure that we have a code path for PRIORITY data
to be passed around, and then use this shim to enforce RFC 7540's
requirements.

Modifications:

- Added support for PRIORITY frame data inbound.
- Enforce that frames may not establish self-dependency.

Result:

Better policing of RFC 7540 constraints.
2019-03-25 18:09:46 +00:00
Cory Benfield ed9244267b Allow changing MAX_FRAME_SIZE. (#69)
Motivation:

SETTINGS_MAX_FRAME_SIZE is changeable: we should allow that change to
occur.

Modifications:

- Added fields for communicating MAX_FRAME_SIZE changes.
- Update the encoders/decoders as those settings change.
- Throw errors from the encoders/decoders if violated.

Result:

Users and remote peers can change MAX_FRAMES_SIZE.
2019-03-25 18:31:47 +02:00
Cory Benfield 55daa4cd4e Correctly notify StreamMultiplexer of SETTINGS changes. (#67)
Motivation:

The StreamMultiplexer and StreamChannels are responsible for sending
WINDOW_UPDATE frames, but they were never told if the user changed the
value of SETTINGS_INITIAL_WINDOW_SIZE. They should be.

Modifications:

- Send user events when SETTINGS_INITIAL_WINDOW_SIZE changes.
- Use those window events to change the target window size in
    HTTP2StreamChannel.
- Keep track of the last stream window size we knew.

Result:

Less chance of flow control mess.
2019-03-15 17:37:00 +00:00
Cory Benfield 1138881931 CONTINUATION frames must be for the correct stream. (#66)
Motivation:

We mustn't allow CONTINUATION frames to be sent on invalid stream IDs.

Modifications:

- Police that the CONTINUATION frame raw stream ID matches the previous
    frame's raw stream ID.

Result:

Better conformance to the RFC
2019-03-15 15:45:45 +00:00
Cory Benfield d9f1131749 Be emphatic about remote peers making mistakes. (#65)
Motivation:

Receiving a CONTINUATION frame at the wrong stage of a connection is
a PROTOCOL_ERROR, but out of an abundance of caution we were categorising
it as an INTERNAL_ERROR. We should be more confident about our error
categories.

Modifications:

- Changed INTERNAL_ERROR to PROTOCOL_ERROR.
- Added a test for this error case.

Result:

More confidence, more style, more pizazz.
2019-03-15 14:37:17 +00:00
Cory Benfield 9e07639b44 Respect changes to INITIAL_WINDOW_SIZE in outbound buffer. (#64)
Motivation:

When the remote peer changes SETTINGS_INITIAL_WINDOW_SIZE, this change
affects the flow control window sizes of all existing streams (as though
they were created with this new initial window size). We should respect
those flow control changes.

Modifications:

- Propagated changes in SETTINGS_INITIAL_WINDOW_SIZE.
- Made NIOHTTP2ConnectionStateChange internal.
- Added support for changing initial window size.

Result:

SETTINGS frames that change initial window size now work properly.
2019-03-15 14:29:53 +00:00
George Barnett 348a10ee19 Ensure preamble only gets sent once. (#61)
Motivation:

In certain cases it was possible for preamble to be sent once; this
would lead to frames getting sent more than once which in turn would
break state machines.

Modifications:

- Check the state of the connection before sending preamble.
- Include a test to check.

Result:

More correct implementation
2019-03-15 12:40:50 +00:00
George Barnett ddf2c29493 Support DATA frames with zero-length application data (#51)
Motivation:

DATA frames with zero-length application data would cause the state
machine to wait for more bytes to continue. This could cause consuming
code to deadlock.

Modifications:

Updated the frame parser to only require more data when there are
remaining bytes in the payload and there are zero readable bytes.

Result:

DATA frames with zero-length application data can be parsed.
2019-03-15 12:34:29 +00:00
George Barnett 2bec00f11c Remove flushability of streams when stream window size is set to zero. (#55)
Motivation:

When setting the stream window size for a stream which had data but
couldn't write it (because of a zero window size) the stream would be
marked as flushable. Setting the window size to zero again would not
mark it as unflushable. On the following window (non-zero) resize it
would be marked as flushable again. However, since this is preceeded by
an assertion that it wasn't already marked as flushable it would cause a
crash.

Modifications:

- Mark the stream as unflushable if it previously had data which it
  couldn't write.

Result:

Fewer crashes
2019-03-15 12:27:22 +00:00
Cory Benfield 03e8e1da3c Deliver PRIORITY frames to the parent stream. (#62)
Motivation:

PRIORITY frames aren't really sent on a single stream, so we shouldn't
attempt to deliver them to the child channels. Instead, we deliver them on
the parent channel.

Modifications:

Moved PRIORITY frames to the parent channel.

Result:

Less confusion about where PRIORTY frames go
2019-03-15 11:46:10 +00:00
Cory Benfield b946acb9a2 Remove now-unnecessary data path types and diagram. (#60)
Motivation:

We had bold plans, but they didn't work out, so these types are now
vestigial.

Modifications:

Removed them.

Result:

Fewer types
2019-03-14 16:18:28 +00:00
Cory Benfield 273e6deaa8 Remove crashers from HTTP2ToHTTP1Codecs. (#57)
Motivation:

When we spiked out these codecs we were missing a number of error handling
paths, which meant that erroneous input would lead to crashes. While we
don't expect these handlers to have to face that kind of input, we should
still aim to tolerate it where doing so is reasonable.

Modifications:

- Replaced all force unwraps and preconditions with throwing.

Result:

Fewer crashes on bad input.
2019-03-13 18:06:14 +00:00
Cory Benfield 38516fe870 Collapse HTTP/2 channel handlers. (#52)
Motivation:

We attempted to use the NIO channel pipeline to spread out the HTTP/2
implementation across a number of channel handlers. Unfortunately, due
to the concerns of reentrancy it's not really safely possible to do this:
it's too easy for the pipeline ordering to become so confused that the
implementation ends up incorrect.

For this reason, we are going to squash together the three handlers that
provide a complete HTTP/2 protocol implementation into just one.

Modifications:

- Move the ConcurrentStreamsHandler and FlowControlHandler into the
    HTTP2Handler.
- Update setup and test code accordingly.

Result:

More correct implementation
2019-03-13 16:04:38 +00:00
Cory Benfield d2697cf0d4 Clean up new NIO breaking changes. (#56)
Motivation:

Code should be able to compile.

Modifications:

- Add necessary force-unwraps to ByteBufferViews.
- Remove use of now-dead HTTPHeaders storage and indices API.

Result:

No compile errors.
2019-03-13 14:19:16 +00:00
Tanner bfc23b5540 add source method (#46) 2019-02-26 21:12:47 +00:00
Cory Benfield 24f67d855e New HTTP/2 currency types. (#44)
Motivation:

As discussed in our HTTP/2 types manifesto, it is helpful to run more
metadata through the HTTP/2 pipeline. This makes it easier to write
ChannelHandlers that fully participate in the HTTP/2 state machine
while avoiding needing to too tightly couple the handlers.

Modifications:

- Added new HTTP/2 pipeline data types.
- Removed HTTP2Frame.FrameFlags, moved flags into relevant payloads.

Result:

Right now, not much. All tests pass with new payload types. The full
data path rewrite will follow in a separate patch, as this one is large
enough.
2019-02-26 14:04:58 +00:00
Johannes Weiss ffe5102b7e update to latest NIO 2 code (#45)
Motivation:

Code should compile and have the latest NIO 2 API.

Modifications:

- rename `ctx` to `context`

Result:

compiles again
2019-02-26 12:57:39 +00:00
Cory Benfield 9492473e9a Add new HTTP/2 pipeline configurator. (#42)
Motivation:

Rather than rely on users to get a bunch of stuff right when configuring
their HTTP/2 pipelines, it's helpful to insert the handlers in the right
order in a helper function.

Modifications:

Provide a helper configurator that inserts handlers correctly.

Result:

Easier to configure HTTP/2 pipelines.
2019-02-22 09:55:51 +00:00
Cory Benfield 8d3040d9e2 Avoid warnings when adding handlers. (#43) 2019-02-21 18:55:13 +00:00
Cory Benfield 3c90345c36 Pure-Swift HTTP/2 implementation (#40)
* Updated to use SwiftNIO 1.10+ to gain access to HTTPHeaders internals.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Initial basic parser code.

* Adding unit tests.

* Add NIOHTTP2FlowControlHandler

Motivation:

One of the things that nghttp2 used to do for us was to handle buffering
DATA frames until flow control allowed them to go out. For users that
want that behaviour, we need to replace the nghttp2 code with a new
ChannelHandler that can do this work.

Modifications:

- Added NIOHTTP2FlowControlHandler.

Result:

Users will have the same feature set as they did with nghttp2.

* Generate Linux test information.

* Documentation comment.

* Updated new tests from master to use new payload types.

* Updated following review.

Notable changes:

- Implemented unit tests for the ALTSVC (0xa) and ORIGIN (0xc) frame types.
- Additional DATA frames are synthesized to wrap individual payload chunks as they arrive. If the payload of a DATA frame with 4MB data arrives in 4 separate 1MB chunks, the decoder will emit 4 DATA frames, with only the last containing the END_STREAM flag (if that was set on the incoming frame).
- HEADERS / PUSH_PROMISE frames and any following CONTINUATION frames are now handled properly: header fragments from all frames are gathered up into a single buffer. Once a frame with END_HEADERS is encountered, the resultant accumulated buffer is sent for decoding along with the flags and stream ID of the original HEADERS / PUSH_PROMISE frame.
- HPACK encoders/decoders now live in the frame encoder/decoder types, rather than being attached to a stream.

* Updated Linux tests.

* Headers placed in relevant files.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Revamped frame decoder state machine and API. Now uses separate input and output methods to manage multiple frames in a single buffer.

* Updated Linux tests.

* Bumped swift-nio version to 1.12.0 and updated `HTTPHeaders` -> `HPACKHeaders` conversion initializer to directly copy the buffer and map the header name/value indices.

* Set up HTTP2Parser in handlerAdded if the channel had been previously activated. (#36)

By setting up the handler in the handlerAdded hook, HTTP2Parser supports
being added dynamically in a channelRead event, after the channel has sent
the channelActive event.

(cherry picked from commit 88bd6d369a)

* Minor tweaks to get the tests passing

* Initial integration here

* Minor clearing up of the handler

* Get the tests compiling

* Fix header issues.

We now pass the frame parser to the various setting operations to let it
update the state. We also fixed some issues with specific settings.

* Remove ngthttp2-based impl, clean up stream ID

* Tests with new HTTP2StreamID

* Simple test cleanup

* Get remaining non-core tests passing

* Allow GOAWAY even when fully quiesced

* We don't buffer outbound frames any more

* Emit StreamClosed events on GoAway frame

* Notify stream creation and closure

* This test started failing

* Goodbye, C code! 🎉

* Send PING ACK automatically

* Updated based on PR feedback.

* Correctly handle 1XX responses

* Add SETTINGS_MAX_FRAME_SIZE support to flow controller

* Correctly account for flow control

* Define ConcurrentStreamsHandler

* Prepare for signalling connection state changes

* Better state change message propagation

* Refactor to use result with effect everywhere

* Don't lose flow control info on stream error

* Add basic flow control management to stream multiplexer

* Better pipeline in sample server

* Clean up some reentrancy concerns

* Move to Swift 5 and NIO 2

* Move to new Swift 5 for CI

* Allow wider range of choices of years

* Catch up to NIO 2 again
2019-02-21 11:52:49 +00:00
George Barnett ac7a0d41cf Clarify cleanup docs for stream channel creation (#38)
Motivation:

The documentation for creating a new stream channel didn't indicate
whether the user had to do any cleanup on the multiplexer once a stream
was no longer in use.

Modifications:

Added a note to the documentation indicating that no additional action
is required by the user beyond closing the stream.

Result:

The documentation is less ambiguous.
2019-01-23 14:07:12 +00:00
Cory Benfield f1d875433f Add support for Ubuntu 16.04's packaged nghttp2. (#29)
Motivation:

We'd done all our development against newer copies of nghttp2, that
behaved a bit differently. We should support Ubuntu's copy, at least
while nghttp2 is relevant to us.

Modifications:

- Shimmed over a version difference.
- Fixed the tests to stop relying on differences.
- Removed the Swift 4.0.3 dockerfiles as we don't support Swift
  4.0 anyway.

Result:

Users can build against Ubuntu 16.04's nghttp2 package.
2018-12-04 14:20:24 +00:00
Cory Benfield 38b8235868 Correctly activate child channels. (#27)
Motivation:

Child channels should not activate before their parents.

Modifications:

Delay activation if the parent channel is not currently active.
Activate during channelActive, if needed.

Result:

Easier to start HTTP/2 requests
2018-11-26 11:44:53 +00:00
Cory Benfield c0951893c7 Trailers sent on idle streams deserve to be sent too. (#25)
Motivation:

Due to nghttp2's weird data provider model, all trailers go through a
similar send path as data frames. This includes the fact that the data
sender can go "idle". If the data sender is idle, and the user sends
trailers, we don't bother to tell nghttp2 to ask us for more data,
so the trailers sit there forever, feeling sad.

Modifications:

- Correctly restart the data provider when sending data.

Result:

Trailers will be emitted when the stream is idle.
2018-11-21 11:42:22 +00:00
Tobias 4f8c857e5d Removes wrong return type documentation (#18) 2018-09-06 08:52:23 -04:00
Jim Dovey f2c2534d4c More Swift-y types for HTTP/2 frame, flags, and settings. (#16)
Motivation:

Prior to bringing in my HTTP/2 encoder/decoder, I think my implementations of HTTP/2 frame flags as `OptionSet`s and settings as an enum (i.e. existentials) make for a nicer API, and provide somewhat more information for the compiler to work with when optimizing.

Modifications:

- `HTTP2Frame.FrameFlags` is now an `OptionSet`.
- `HTTP2Frame.FramePayload` now supports all current HTTP/2 frame types, can return their on-wire identifier, and can return the set of `HTTP2Frame.FrameFlags` that the frame allows.
- `HTTP2Setting` is now an enum with associated values, and includes the SETTINGS_ENABLE_CONNECT_PROTOCOL value.
- Added `NIOHTTP2Errors.InvalidSettings` error, used when bad settings are encountered by decoder.
- Updated tests to use the new `HTTP2Setting` type.

Result:

Not a lot changes, beyond the in-memory layout of some things. All these types still have nghttp2 conversion methods, and all tests still run successfully.
2018-08-23 14:56:20 +01:00
Johannes Weiss 346af5be0e don't crash when client sends bad magic (#12)
Motivation:

We shouldn't just crash if the client sends bad magic.

Changes:

Send an error through the pipeline instead of crashing on bad client
magic.

Result:

fewer crashes
2018-08-16 15:29:14 +01:00
Cory Benfield 392b2e3228 Avoid crashing when MAX_CONCURRENT_STREAMS exceeds old cache size (#13)
Motivation:

Our dead stream cache should not cause us to crash when we can have more
active streams than the entire size of the cache. These things are
at least nominally unrelated.

Modifications:

Rewrote the code to avoid asserting that we'll always be able to shrink
down to the dead cache size.

Result:

Fewer crashes
2018-08-16 13:19:04 +01:00
Cory Benfield b7085b6c0d Correctly discard only inactive streams. (#11)
Motivation:

Long-lived connections that have many inactive streams would cause
crashes. Crashes are, in my professional opinion, less than ideal
when they occur under expected usage cases.

Modifications:

- Fixed an inverted boolean check that would discard only *active*
  streams.
- Made the cached stream count configurable, removing a TODO.
- Added tests.

Result:

Fewer crashes, better software.
2018-08-15 21:26:50 +01:00
Cory Benfield 36321d080e Clean up tests. (#9)
Motivation:

Our tests were failing as a result of the previous commit changing the
HTTP/2 lifecycle.

Modifications:

Fired channelActive in a few tests, correctly propagated
channelInactive.

Result:

Tests should pass.
2018-05-20 11:18:25 +01:00
Cory Benfield d17462f586 Fix example server.
Motivation:

The example server had slowly bitrotted over time: some minor fixes were
needed.

Modifications:

I fixed it!

Result:

The example server works against a basic curl command.
2018-05-18 17:53:21 +01:00
Cory Benfield aee602c435 Add interface for creating channels from the client side. (#6)
Motivation:

While the HTTP2StreamMultiplexer could create channels in response to
inbound request streams on the server side, there was no interface for
creating channels locally.

That would make having a HTTP/2 client kinda tricky!

Modifications:

Added HTTP2StreamMultiplexer.createStreamChannel.

Result:

Possible to write HTTP/2 clients.
2018-05-18 17:26:12 +01:00
Cory Benfield 0a271b1cef Remove handlers from stream channel pipelines on shutdown. (#5)
Motivation:

When closing a stream channel, it must remove its channel handlers.

Modifications:

Call ChannelCore.removeHandlers on shutdown.

Result:

Handlers are removed on channel shutdown.
2018-05-18 14:05:12 +01:00
Cory Benfield c2d4a88b24 Flesh out HTTP2 to HTTP1 codecs. (#4)
Motivation:

Many users of HTTP/2 will not want to interact with the HTTP/2 protocol
directly, but instead to write generic HTTP applications on top of an
abstraction layer. These codecs provide part of that abstraction layer.

Modifications:

Added more codecs, fleshed them out, wrote many tests.

Result:

Easier application writing.
2018-05-18 11:35:26 +01:00
Cory Benfield e2ea2b88b9 Support read() 2018-05-17 11:58:25 +01:00
Cory Benfield bbcb593a6d Support buffered writes 2018-05-16 17:53:26 +01:00
Cory Benfield 5c5987e6ad Manage delayed stream channel setup 2018-05-16 15:37:41 +01:00
Cory Benfield 87cfe4897f Test stream-initiated closure. 2018-05-16 14:24:22 +01:00
Cory Benfield 861efdcd75 Remove closed channels from the map 2018-05-16 11:28:49 +01:00
Cory Benfield 59611cd174 Initial multiplexer testing 2018-05-15 17:36:43 +01:00
Cory Benfield 4acc76bfd7 Fire user events when streams are closed 2018-05-14 17:57:10 +01:00
Cory Benfield c7671bc982 Suppress PRIORITY frames 2018-05-14 15:35:09 +01:00
Cory Benfield 986922e01f Minor fixes 2018-05-14 14:52:28 +01:00
Cory Benfield e5d470daf6 Allow sending trailers 2018-05-14 13:39:15 +01:00
Cory Benfield 91349458ff Don't attach data for informational responses 2018-05-14 13:24:53 +01:00
Cory Benfield a5854839c7 Refactor state tracking for streams 2018-05-14 12:53:58 +01:00
Cory Benfield b7813bf908 Add hooks to data provider for trailers 2018-05-11 14:17:57 +01:00
Cory Benfield 493ba60314 CustomDebugStringConvertible for HTTP2StreamID 2018-05-11 10:56:34 +01:00
Cory Benfield 95b557d617 Fail writes when stream closed by rststream 2018-05-11 10:04:32 +01:00
Cory Benfield 7aa92fe7e2 Refactor error code to struct instead of enum 2018-05-10 17:35:46 +01:00