27 Commits
Author SHA1 Message Date
Fabian Fett 1f64743098 Fix receiving empty body byte buffer before .end (#295) 2021-08-12 11:29:39 +01:00
George Barnett 94c35c6c45 Fix 'heder' typo (#252)
Motivation:

- Typos are bugs. Bugs are bad.

Modifications:

- s/heder/header

Result:

Fewer typos
2020-10-26 11:12:31 +00:00
George Barnett 440d64d99a Add a location to each of 'NIOHTTP2Errors' (#247)
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
2020-09-24 15:21:58 +01:00
Cory Benfield 5b0194aa63 Reduce allocations when peeking pseudo-headers. (#246)
Motivation:

The 2 to 1 codecs do a number of operations where they "peek" the
pseudo-headers. Each of these operations uses the HPACKHeaders
subscript, meaning that they allocate a temporary array for a field that
can, in a correct request/response, contain only one element. That's
very wasteful!

Modifications:

- Rewrite peekPseudoHeader to iterate instead, avoiding the array.
- Add allocaton counter test.

Result:

4 fewer allocations per HTTP request transformed on the server side,
2 fewer allocations per HTTP response transformed on the client side.
2020-09-24 09:57:47 +01:00
George Barnett e9627350bd Verify payload-based stream channel fixes #214 (#228)
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.
2020-08-05 10:19:05 +01:00
George Barnett 38afdfce3e Deprecate the HTTP2 to HTTP1 codecs (#227)
Motivation:

We added HTTP2FramePayload-to-HTTP1 codecs as part of #214 but are yet
to deprecate their HTTP2-to-HTTP1 counterparts.

Modifications:

- Deprecate `HTTP2ToHTTP1ClientCodec`
- Deprecate `HTTP2ToHTTP1ServerCodec`
- Deprecate tests containing them
- (We already have tests for HTTP2FramePayload-to-HTTP1 codecs)

Result:

HTTP2-to-HTTP1 codecs are deprecated
2020-08-04 14:25:28 +01:00
George Barnett 3c668f5c7e Add HTTP2Frame.FramePayload codecs (#222)
Motivation:

As part of the work for #214 we need new codecs which transform
`HTTP2Frame.FramePayload` to and from the appropriate request and
response parts.

Modifications:

- Add `HTTP2FramePayloadToHTTP1ClientCodec`
- Add `HTTP2FramePayloadToHTTP1ServerCodec`
- Duplicate the HTTP2ToHTTP1CodecTests and update the relevant parts to
  use payloads instead of frames.
- Add relevant test helpers.
- Note: the HTTP2 to HTTP1 (frame based) codecs haven't been deprecated:
  doing so without warnings depends on #221.

Result:

- We can transform `HTTP2Frame.FramePayload` types to the relevant HTTP
  client and server request and response types.
2020-07-31 10:29:42 +01:00
Cory Benfield 04706e7df5 Refactor the codecs for extension. (#219)
Motivation:

As part of the work in #214 we're going to need to update the
HTTP2ToHTTP1 codecs. These need to be replaced for the new channel
pipelines. The core of the logic will be identical in both cases, so
let's start by factoring that logic out into some nice standalone
objects that we can reuse.

Modifications:

- Pull out the base codecs into structures.
- Rewrite the main codecs in terms of these new structures.

Result:

Easier extension points.
2020-07-29 09:26:49 +01:00
Johannes Weiss 114c4e0022 accept more valid-in-HTTP/1 but invalid-in-H2 messages (#153)
Motivation:

HTTP/2 is mostly just HTTP(/1) semantics with streams on a new wire
protocol. However, there are certain things that are legal in HTTP/1 but
which are illegal in HTTP/2.

Modification:

Accept more of those messages and transform them into their HTTP/2
equivalent.

Result:

- Easier to write HTTP/2 clients & servers in NIO.
- fixes #152
2019-09-09 16:15:46 +01:00
Johannes Weiss 7de397fda8 ignore frames that don't have a HTTP/1 equivalent (#146)
Motivation:

Previously, the H2to1 codec would crash if for example a push promise
came by.

Modifications:

Instead of crashing, ignore the frames without equivalent.

Result:

Fewer crashes.
2019-07-18 17:04:27 +01:00
Cory Benfield 144f9d2a0e Reduce intermediate header copies. (#112)
Motivation:

We were copying HTTP headers around quite a lot when transforming from HTTP/1 to HTTP/2 and
back. Due to the different underlying representations we cannot get away from needing to do
one copy, as the backing array storage is of a different type, but we were frequently incurring
CoWs as well that were just totally unnecessary.

Modifications:

- Refactored the header code to work more confidently on HPACKHeaders.
- Removed some mutating functions that triggered unnecessary CoWs.
- Improved HPACKHeaders(httpHeaders:) initializer to avoid unnecessary intermediate array
- Improved HTTPHeaders(requestHead:protocolString:) to avoid CoWs and reallocations
- Improved HTTPHeaders(responseHead:) to avoid CoWs and reallocations

Result:

Fewer heap allocations
2019-05-03 17:56:35 +01: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 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 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
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
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 c937029ad4 Remove CONTINUATION (and so endHeaders).
We treat CONTINUATION frames as an implementation detail, which means the
endStream flag is also redundant. Let's remove both.
2018-05-03 10:48:54 +01:00
Cory Benfield ca89364e1e Don't make HTTP/2 know about request/response head 2018-04-30 11:44:08 +01:00
Cory Benfield 1ef32a0d3d Correct handling of stream IDs. 2018-04-27 15:40:18 +01:00
Cory Benfield 2044c89450 Improve HTTP2StreamChannel state handling.
Motivation:

The HTTP2StreamChannel needs to be aware of the HTTP/2 state machine,
at least at some level. This is to ensure it can map that state
machine onto the (unfortunately implicit) NIO stream state machine
and appropriately clean up resources like stream promises.

Modifications:

Lots. Added stream state tracking, refactored the frame structure,
stopped creating stream 0 as a sub-stream, and more.

Result:

We no longer crash when the curl TCP connection goes away.
2018-04-23 17:15:17 +01:00
Cory Benfield bab2bb7cfc Removed debugging handlers 2018-04-18 16:28:12 +01:00
Cory Benfield 3cf74039d9 Fix many issues writing DATA frames 2018-04-16 16:09:13 +01:00
Cory Benfield e4de5788a5 Initial support for writing DATA frames 2018-04-16 15:08:08 +01:00
Johannes Weiß 504c24939e very rough first steps towards the first handled HTTP/2 request 2018-04-11 15:06:12 +01:00