Skip to content

Commit

Permalink
Add configuration to http2Upgrade in http2 example (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Nov 15, 2024
1 parent 0ad2d6e commit 0730d5e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion http2/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.4.0"),
.package(url: "https://github.com/apple/swift-certificates.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
],
Expand Down
36 changes: 20 additions & 16 deletions http2/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Hummingbird
import HummingbirdHTTP2
import Logging
import NIOCore
import NIOHTTPTypesHTTP2
import NIOHTTP2

public protocol AppArguments {
var tlsConfiguration: TLSConfiguration { get throws }
Expand All @@ -14,31 +14,35 @@ struct ChannelRequestContext: RequestContext {
self.channel = source.channel
}

var hasHTTP2Handler: Bool {
get async {
if let channel = self.channel {
return (try? await channel.pipeline.handler(type: HTTP2FramePayloadToHTTPServerCodec.self).get()) != nil
}
return false
}
var isHTTP2: Bool {
// Using the fact that HTTP2 stream channels have a parent HTTP2 connection channel
// as a way to recognise an HTTP/2 channel vs an HTTP/1.1 channel
self.channel.parent?.parent != nil
}

var coreContext: CoreRequestContextStorage
let channel: Channel?
let channel: Channel
}

import Hummingbird

func buildApplication(arguments: some AppArguments, configuration: ApplicationConfiguration) throws -> some ApplicationProtocol {
func buildApplication(arguments: some AppArguments, configuration: ApplicationConfiguration) throws
-> some ApplicationProtocol
{
let router = Router(context: ChannelRequestContext.self)
router.get("/http") { _, context in
// return "Using http v\(request.head. == "h2" ? "2.0" : "1.1")"
return "Using http v\(await context.hasHTTP2Handler ? "2.0" : "1.1")"
router.add(middleware: FileMiddleware(searchForIndexHtml: true))
router.get("/http") { request, context in
return "Using http v\(context.isHTTP2 ? "2.0" : "1.1")"
}

let app = try Application(
router: router,
server: .http2Upgrade(tlsConfiguration: arguments.tlsConfiguration),
server: .http2Upgrade(
tlsConfiguration: arguments.tlsConfiguration,
configuration: .init(
idleTimeout: .seconds(30),
gracefulCloseTimeout: .seconds(30),
maxAgeTimeout: .seconds(2 * 60 * 60)
)
),
configuration: configuration
)
return app
Expand Down

0 comments on commit 0730d5e

Please sign in to comment.