Cory Benfield ac2a5afce3 Merge pull request from GHSA-q36x-r5x4-h4q6
Motivation

The HTTP2FrameDecoder is a complex object that was written early in the
development of swift-nio-http2. Its logical flow is complex, and it
hasn't been meaningfully rewritten in quite some time, so it's difficult
to work with and understand.

Annoyingly, some bugs have crept in over the years. Because of the
structure of the code it can be quite difficult to understand how the
parser actually works, and fixing a given issue can be difficult.

This patch aims to produce a substantial change to the HTTP2FrameDecoder
to make it easier to understand and maintain in the long run.

Modifications

This patch provides a complete rewrite of HTTP2FrameDecoder. It doesn't
do this by having a ground-up rewrite: instead, it's more like a
renovation, with the general scaffolding kept. The rewrite was performed
incrementally, keeping the existing test suite passing and writing new
tests when necessary.

The following major changes were made:

1. New states and edges were added to the state machine to handle
   padding.

   Prior to this change, padding was handled as part of frame payload
   decoding. This is not totally unreasonable, but it dispersed padding
   management widely and made it easy to have bugs slip in. This patch
   replaces this with a smaller set of locations.

   Padding is now handled in two distinct ways. For HEADERS and
   PUSH_PROMISE frames, trailing padding is still stripped as part of
   frame payload decode, but it's done so generically, and the padding
   bytes are never exposed to the individual frame parser. For DATA,
   there is a new state to handle trailing padding removal, which
   simplifies the highly complex logic around synthesised data frames.

   For all frames, the leading padding byte is handled by a new
   dedicated state which is used unconditionally, instead of attempting
   to opportunistically strip it. This simplifies the code flow.

   As a side benefit, this change means we can now accurately report the
   padding used on HEADERS and PUSH_PROMISE frames, even when they are
   part of a CONTINUATION sequence.

2. The synthesised DATA frame logic has been substantially reworked.

   With the removal of the padding logic from the state, we now know
   that so long as we have either got a byte of data to emit _or_ the
   DATA frame is zero length, we will always emit a frame. This has made
   it simpler to understand the control flow when synthesising DATA
   frames.

3. The monolithic state switch has been refactored into per-state
   methods.

   This helps manage the amount of state that each method can see, as
   well as to logically split them up. In addition, it allows us to
   recast state transformations as (fairly) pure functions.

   Additionally, this allowed the larger methods to be refactored with
   smaller helpers that are more obviously correct.

4. The frame payload parsers have been rewritten.

   The main goal here was to remove preflight length checks and unsafe
   code. The preflight length checks cause trouble when they disagree
   with the parsing code, so we now rely on the parsing code being
   correct with regard to length.

   Relatedly, we previously had two separate places where we
   communicated length: a frame header length and a ByteBuffer length.
   This was unnecessary duplication of information, so we instead use a
   ByteBuffer slice to manage the length. This ensures that we cannot
   over-parse a message.

   Finally, in places that used unsafe code or separate integer reads,
   we have refactored to stop using that unsafe code and to use combined
   integer reads.

5. Extraneous serialization code has been extracted.

   The HTTP2FrameEncoder was unnecessarily in this file, which took a
   large file and made it larger. I moved this out.

Result

The resulting parser is clearer and safer. Complex logic has been broken
out into smaller methods with less access to global data. The code
should be generally clearer.
2022-03-10 10:16:17 +00:00
2022-01-18 02:19:40 -08:00
2022-01-18 02:19:40 -08:00
2017-12-04 12:13:14 +00:00
2021-03-09 14:11:19 +00:00

SwiftNIO HTTP/2

This project contains HTTP/2 support for Swift projects using SwiftNIO. To get started, check the API docs.

Building

swift-nio-http2 is a SwiftPM project and can be built and tested very simply:

$ swift build
$ swift test

Versions

Just like the rest of the SwiftNIO family, swift-nio-http2 follows SemVer 2.0.0 with a separate document declaring SwiftNIO's Public API.

swift-nio-http2 1.x

swift-nio-http2 versions 1.x are a pure-Swift implementation of the HTTP/2 protocol for SwiftNIO. It's part of the SwiftNIO 2 family of repositories and does not have any dependencies besides swift-nio and Swift 5. As the latest version, it lives on the main branch.

To depend on swift-nio-http2, put the following in the dependencies of your Package.swift:

.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.19.2"),

SwiftNIO HTTP2 1.18.x and later support Swift 5.2 and above. 1.17.x and earlier also support Swift 5.0 and 5.1.

swift-nio-http2 0.x

The legacy swift-nio-http 0.x is part of the SwiftNIO 1 family of repositories and works on Swift 4.1 and newer but requires nghttp2 to be installed on your system. The source code can be found on the nghttp2-support-branch.

Developing SwiftNIO HTTP/2

For the most part, SwiftNIO development is as straightforward as any other SwiftPM project. With that said, we do have a few processes that are worth understanding before you contribute. For details, please see CONTRIBUTING.md in this repository.

S
Description
Mirror of github.com/apple/swift-nio-http2
Readme Apache-2.0
8.7 MiB
0 Stars 1 Watchers 0 Forks
Languages
Swift 98.8%
Shell 0.8%
Python 0.2%
Ruby 0.2%