142 Commits
Author SHA1 Message Date
George Barnett 8752535dd2 Ignore WINDOW_UPDATE on closed streams. (#109)
Motivation:

RFC 7540 Section 6.9: "WINDOW_UPDATE can be sent by a peer that has sent
a frame bearing the END_STREAM flag.  This means that a receiver could
receive a WINDOW_UPDATE frame on a "half-closed (remote)" or "closed"
stream. A receiver MUST NOT treat this as an error (see Section 5.1)."

Previously WINDOW_UPDATE frames received in this state would be treated
as an error.

Modifications:

- Allow frames to be ignored if they are for a stream which has been
  closed.
- Add a test to verify WINDOW_UPDATE on a closed stream.
- Update tests which relied on a stream error in this case.

Result:

More compliant code.
1.2.0
2019-04-29 15:02:41 +01:00
Johannes Weiss 8c991df0c0 Add 'Show on GitHub' link to API docs (#108)
Motivation:

Allowing readers of the API documentation to drill into the implementation of documented declarations can be educational and helpful for debugging, among several other reasons.

Modifications:

Add --github-file-prefix appending the current version in order to keep stable links. This means that documentation generated from any non-release revision may resolve incorrectly. This can be refined in the doc generation script later if it is deemed problematic by resolving to the commit sha if it doesn't exactly align with the version.

Result:

Users will be able to click a 'Show on GitHub' for all documented API declarations.
2019-04-29 10:42:20 +01:00
Cory Benfield 4cb9d07b54 Avoid unnecessary heap allocation when parsing frame headers. (#107)
Motivation:

As noted in #105, I spotted that we were heap-allocating an array
when parsing the frame header. That heap allocation isn't necessary, we
can just write directly into an Int, which should save us some heap spray.

Modifications:

- Rewrote code to avoid heap allocating array and instead to use Int.
- Elided some bounds checks by using an unsafe pointer, since I was
    already here and it saved about 1% performance in a hot loop.

Result:

Fewer heap allocations on the hot code path.
2019-04-18 17:32:17 +01:00
Cory Benfield ee31368790 Remove unnecessary state CoWs. (#104)
Motivation:

When benchmarking the HTTP/2 implementation I noticed that we were
performing a large number of CoW operations on the Dictionary that backs
the ConnectionStreamState structure: specifically, once per frame sent or
received.

This is substantial and unnecessary allocation overhead that drags down
the performance of the program, sprays the heap with small allocations, and
generally just leads to nasty performance costs with no gain.

Sadly, this turns out not to be the easiest CoW to get rid of, because
the ConnectionStreamState structure is stored in associated data on the
connection state machine state enumeration. This means that our usual tricks
for avoiding the CoW (such as swap()) no longer work.

The only solution is to add a new state that corresponds to no state at all
so that we can make the connection state go away while we mutate it. This is
all a bit gross, frankly. Unfortunately, the act of making this simple change
leads to a huge diff as we have to tweak basically every state machine
function in order to ensure that it does the right thing.

Modifications:

- Added a function to control CoWing the state machine.
- Used that function basically everywhere.
- Made sure that function had assertions.
- Annoyingly had to increase the visibility of a bunch of structs.

Result:

Fewer CoWs, better performance, sadder code reviewers.
2019-04-18 14:28:45 +01:00
Cory Benfield 429862635a Allow maxStreamID to be used in GOAWAY by either peer. (#102)
Motivation:

We would previously error if clients emitted a GOAWAY frame that had
a last stream ID set to the max value. This is a bit unnecessary, not
least given that we emitted those frames ourselves! In fact, this error
would mask real server protocol errors if we hit them.

Modifications:

- Allowed max stream ID as the last stream ID.
- Wrote a test to verify this.

Result:

Fewer unnecessary protocol errors.
2019-04-16 18:56:30 +01:00
Cory Benfield 492f375a14 Allow inserting overlarge indexed headers. (#99)
Motivation:

Inserting an indexed header field that is too large is not an error, and
we shouldn't treat it as one. This was tripping over an example application
that hit Wikipedia.

Modifications:

- Stop throwing errors when we try to insert an overlarge header to the
    dynamic table.
- Add a regression test.

Result:

We can speak to Wikipedia
2019-04-16 11:48:21 +01:00
Cory Benfield aaaeca77f9 Police correct content-length headers. (#98)
Motivation:

RFC 7540 requires that compliant specifications emit errors if content-length has been incorrectly
set. We should have this behaviour by default, though we should allow it to be disabled if necessary.

Modifications:

- Added content-length checking.
- Added flag to disable content-length checking.

Result:

Better RFC 7540 compliance.
2019-04-15 16:57:16 +01:00
Cory Benfield 08db9d8cd4 Resolve off-by-one errors in settings bounds. (#97)
Motivation:

When we're validating that settings are in their bounds, we should probably endeavour to
express those bounds correctly.

Modifications:

- Fixed the bounds checks on SETTINGS_INITIAL_WINDOW_SIZE and SETTINGS_MAX_FRAME_SIZE.
- Added regression tests.

Result:

Better correctness.
2019-04-15 12:06:10 +01:00
Cory Benfield ae57c2a0d8 Validate that connection-specific headers aren't sent. (#95)
Motivation:

RFC 7540 forbids the use of connection-specific header fields, except for TE sent
with the value "trailers". We should police that.

Modifications:

- Added extra policing around regular header fields.

Result:

Better validation
1.1.0
2019-04-12 16:01:43 +01:00
Cory Benfield b4a9ed3877 Forbid empty :path headers. (#94)
Motivation:

RFC 7540 does not allow empty :path pseudo-headers. We should police this.

Modifications:

- Added a check that :path isn't empty.

Result:

Better RFC 7540 validation.
2019-04-12 14:38:10 +01:00
Cory Benfield 7acb02befb Enforce mandatory and allowed pseudo-headers. (#91)
Motivation:

HTTP/2 header blocks have a certain set of pseudo-headers that are mandatory
and allowed on them. We currently don't enforce that, which makes it possible to send
malformed requests or responses, as well as to have to handle them. That's not great.

Modifications:

- Added enforcement of the allowed pseudo-header fields.

Result:

Better pseudo-header field enforcement.
2019-04-12 13:58:56 +01:00
Cory Benfield ddc982111e Move to String-backed HPACK representations. (#89)
Motivation:

When originally written our HPACK implementation targetted Swift 4.2,
which had performance issues when storing mostly-ASCII data in Strings.
For this reason we had a number of data types that used ByteBuffers as
their backing storage and reified Strings when necessary.

In Swift 5 this is no longer worthwhile: in fact, it imposes performance
penalties above and beyond simply using the Strings. For this reason, we
should move to a simpler model where we simply store Strings again.

Modifications:

- Rewrote all HPACK data structures to use Strings as their backing store.
- Removed now-redundant genericism.
- Removed now-unnecessary StringRing data structure and associated tests.

Result:

Faster performance in almost all cases.
2019-04-12 12:42:24 +01:00
Cory Benfield 80b243d8a5 Enforce pseudo-headers must be first in a header block. (#90)
Motivation:

RFC 7540 requires that HTTP/2 pseudo-headers must be first in a HTTP/2
header block, and must occur before any regular headers. We should enforce
that requirement.

Modifications:

- Added checking that pseudo-headers are first in a header block.

Result:

Better RFC 7540 validation.
2019-04-11 14:22:01 +01:00
George Barnett 99e35a8ebb Allow zero length SETTINGS frames to be parsed. (#88)
Motivation:

Zero length SETTINGS frames are permitted by RFC 7540 but the frame
parser currently throws a FRAME_SIZE_ERROR.

Modifications:

- Allow zero length SETTINGS frames to be parsed
- Added a test

Result:

Parsing zero length SETTINGS frames don't throw an error.
2019-04-10 17:20:24 +01:00
Cory Benfield 74b8f4f110 Validate no uppercase characters in header field names. (#84)
Motivation:

RFC 7540 forbids uppercase characters in header field names, but we
currently allow them. We shouldn't.

This is the first in a series of patches that will validate header blocks
when reasonable. As a result, it seems larger than necessary, as it adds
a bunch of scaffolding for extending with further validation code.

Modifications:

- Added header field validation scaffolding.
- Validates that header field names do not contain uppercase chars.
- Allows sending RST_STREAM frame before stream setup.

Result:

Better header field validation.
2019-04-05 18:01:22 +02:00
Cory Benfield 70ac404a42 Decrease allocations in encode loop. (#86)
While investigating performance of HTTP/2 I noticed a substantial number
of allocations occuring in a hot loop in header compression. Specifically
I set up a simple harness that encoded headers 100 times, and then ran
that harness 10k times. This harness ended up performing 17 million
allocations, which is frankly a bit too high!

The fact that the number of allocations was in the millions suggested that
we were allocating some resources per header encode. That's not good:
assuming the target `ByteBuffer` is big enough we should not need to
perform new allocations to encode headers.

The allocations came from two places. Firstly, we were allocating in the
`HeaderTableStorage.indices(matching:)` function. This was because that
operation was returning a lazy collection, which required multiple heap
allocations for the closure contexts. Those needed to be eliminated.

Secondly, we were triggering a CoW of the `ByteBuffer` passed in to
`encode()`. This is the result of a complex bit of control flow that
fundamentally meant that the buffer was held in two places in that
method.

Removing these two sources of allocations dropped the count in my test
from 17 million to 176k, a decrease of 100x. We also saw a speed boost
of nearly 66% in terms of runtime of the microbenchmark.

- Removed `HeaderTableStorage.indices(matching:)`
- Implemented `HeaderTableStorage.closestMatch(name:value:), containing
   the repeated logic of the body of the two loops over the header
   indices.
- Used swap() and an empty byte buffer to ensure that encode does not
   cause a CoW operation.

Much faster and more memory efficient header encoding.
2019-04-05 16:38:21 +01:00
Johannes Weiss 1a5c69c7f9 API docs: repo switcher (#83) 2019-03-28 16:03:38 +00:00
Johannes Weiss 096ee065b1 README: link to API docs (#82) 2019-03-28 14:41:08 +00:00
Cory Benfield a92923bcb7 Require END_STREAM to be set on trailers. (#81)
Motivation:

RFC 7540 requires that the END_STREAM bit be set on a HEADERS frame that
sends trailers. We should enforce that.

Modifications:

- Require that the END_STREAM bit is set on trailers.

Result:

We reject more incorrect flows.
1.0.1
2019-03-27 11:53:31 +00:00
Cory Benfield 055b9e534a Forward channelActive from stream multiplexer. (#80)
Motivation:

The stream multiplexer was incorrectly eating channelActive on its own
channel, and failing to pass them on to subsequent channel handlers, if
there were any.

Modifications:

- Passed channelActive on from stream multiplexer.

Result:

Easier to handle things on the parent channel.
2019-03-27 10:27:38 +00:00
Cory Benfield 85b0868379 Fixup typo in README (#78) 2019-03-27 09:53:05 +00:00
tomer doron 13d9b4852e add doc generation script (#77)
motivation: publish API docs

changes:
* amend docker file to include jazzy
* add doc generation script based on what we have in swift-nio and swift-log
2019-03-27 09:33:19 +00:00
Johannes Weiss 7adcb49d8a remove outdated information from readme (#76)
Motivation:

swift-nio-http2 is no longer in beta and does no longer require nghttp2.
We should state that.

Modifications:

Remove outdated and wrong information from the readme.

Result:

Fewer lies in the documentation.
2019-03-26 18:13:43 +00:00
Johannes Weiss a4a1d9c727 use Swift 5.0 release (#74) 2019-03-26 11:20:13 +00:00
Johannes Weiss a9fc7c17dd use SwiftNIO 2.0.0 (#72) 1.0.0 2019-03-26 09:25:02 +00:00
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
Johannes Weiss 7e17e72f41 update to latest EmbeddedChannel API (#68)
Motivation:

EmbeddedChannel's API has changed a litle bit, we should update.

Modifications:

update

Result:

code will continue to work
1.0.0-convergence.2
2019-03-22 14:05:10 +00: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.
1.0.0-convergence.1
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 4a490f2e04 Refuse to decode indexed header with index 0. (#63)
Motivation:

RFC 7541 is pretty clear that index 0 is no bueno in the indexed
header representation, so we should forbid it.

Modifications:

- Detected attempts to decode index 0.

Result:

Index 0 is now forbidden.
2019-03-15 12:01:09 +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
Tanner 049ba81fcc remove _NIOAPI1Shims since NIO 2 is in convergence (#59)
* remove _NIOAPI1Shims since NIO 2 is in convergence

* remove import in NIOHPACK
2019-03-13 19:45:28 +00:00
Tanner 48dccc54c3 depend on swift-nio 2.0.0 convergence (#58) 2019-03-13 18:48:09 +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
Cory Benfield 55a73ed12a Mark entire StringRing as readable. (#54)
Motivation:

We use a Bytebuffer as backing storage for a string ringbuffer to avoid
needing to reinvent that data structure ourselves. This used to work great
but NIO 2 recently changed the get* functions to trap if they accessed
memory outside the reader/writer indices.

We were never moving the writer index, instead treating the entire buffer
as valid. This led to preconditions, and sadness.

In an ideal world we'd only mark the bytes that are actually active as
readable to ensure that we have the defensive traps from bytebuffer. In
practice, that's a sufficiently complex thing to do that we're better
served just trusting our code and taking off the guard rails.

In future we should go back to seeing whether a CircularBuffer of strings
will work better, now that Strings are fast. I'll make that Johannes'
problem.

Modifications:

- Mark the entire backing buffer as readable.

Result:

Fewer crashes
2019-03-06 16:54:55 +00:00
Cory Benfield 0ac9fa6e30 Handle EmbeddedChannel throwing. (#48)
Motivation:

NIO has made EmbeddedChannel operations throwing. We need to handle that.

Modifications:

- Added assertNoThrowWithValue around all EmbeddedChannel operations.
- Suppressed some throwing errors after the assertNoThrowWithValue.

Result:

Compiles again.
2019-03-04 11:00:49 +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