Skip to content
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

Add configuration to http2Upgrade in http2 example #132

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
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
Loading