Skip to content

How to enable HTTP2 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let package = Package(
name: "Articles",
dependencies: [
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "HummingbirdHTTP2", package: "hummingbird"),
.product(name: "HummingbirdRouter", package: "hummingbird"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
Expand Down
30 changes: 30 additions & 0 deletions Snippets/hummingbird-2-http-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// snippet.imports
import Hummingbird
import HummingbirdHTTP2
import NIOCore
import NIOHTTPTypesHTTP2
import NIOSSL
// snippet.end

// snippet.tls
let certificateChainFilePath = "/path/to/server.crt"
let privateKeyFilePath = "/path/to/private-key.pem"

let certificateChain = try NIOSSLCertificate.fromPEMFile(certificateChainFilePath)
let privateKey = try NIOSSLPrivateKey(file: privateKeyFilePath, format: .pem)
let tlsConfiguration = TLSConfiguration.makeServerConfiguration(
certificateChain: certificateChain.map { .certificate($0) },
privateKey: .privateKey(privateKey)
)
// snippet.end

let router = Router()

// snippet.application
let app = try Application(
router: router,
server: .http2Upgrade(tlsConfiguration: tlsConfiguration)
)

try await app.runService()
// snippet.end
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Enabling HTTP/2 in Hummingbird

[Hummingbird](https://hummingbird.codes) features a flexible architecture that allows swapping various components in and out. This flexibility extends to the networking layer, where you can easily swap out the default HTTP/1 networking code for one supporting HTTP/2.

## Package Manifest

As part of your code, you'll have a target that depends on Hummingbird.

```swift
.executableTarget(
name: "App",
dependencies: [
.product(name: "Hummingbird", package: "hummingbird"),
...
]
),
```

Because Hummingbird aims to only compile what you need, Hummingbird doesn't enable HTTP/2 support by default. In order to use HTTP/2, you'll first need to depend on the `HummingbirdHTTP2` module within the Hummingbird package.

Add the following product to your target's dependencies:

```swift
.product(name: "HummingbirdHTTP2", package: "hummingbird"),
```

## Enabling HTTP/2

First, in your application, you'll need to import the relevant dependencies:

@Snippet(path: "site/Snippets/hummingbird-2-http-2", slice: "imports")

HTTP/2 can only be enabled if your server already supports TLS. In order to support TLS, you'll need to create a ``TLSConfiguration`` from ``NIOSSL``.

You'll need to obtain this certificate through a certificate authority (CA) or generate a self-signed certificate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a short section on how to generate a self-signed certificate and key? That would be really useful, in my opinion.

FYI: https://theswiftdev.com/a-simple-http2-server-using-vapor-4/#certificate-generation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tib would you want to write that section? I'd prefer not using RSA in my the recommended certificates of course.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that, what's your recommended algo? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any elliptic curve


Then, using this certificate, you can create a ``TLSConfiguration`` object that Hummingbird can use to serve HTTP/1.1 and HTTP/2 traffic.

@Snippet(path: "site/Snippets/hummingbird-2-http-2", slice: "tls")

Finally, when creating your server, you can pass this configuration to the server's initializer.

@Snippet(path: "site/Snippets/hummingbird-2-http-2", slice: "application")

With this configuration, your server will now support HTTP/2 traffic.

## Conclusion

Enabling HTTP/2 in Hummingbird is straightforward. By adding the `HummingbirdHTTP2` module to your target's dependencies and loading your server's TLS certificate, you can easily enable HTTP/2 support in your Hummingbird application.

The pluggable architecture of Hummingbird makes it easy to swap out components, such as the networking layer, to support new features like HTTP/2.