-
Notifications
You must be signed in to change notification settings - Fork 10
Validate configuration at initialization #107
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
Merged
aryan-25
merged 19 commits into
swift-server:main
from
aryan-25:validate-config-at-init
Aug 13, 2026
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
043c39d
Validate configuration at initialization
aryan-25 f933d14
Refactor
aryan-25 f6b994d
Refactor
aryan-25 9aa1c85
Merge branch 'main' into validate-config-at-init
aryan-25 8da01f8
Format argument declaration
aryan-25 6605bc2
Set default value for HTTP/2 and HTTP/3 configuration
aryan-25 bd7757d
Use `CaseIterable`
aryan-25 f20d89f
Add `Validated` prefix to context types
aryan-25 3d44436
Merge branch 'main' into validate-config-at-init
aryan-25 b32144d
Add static var for default `http2` and `http3` configurations
aryan-25 54b6357
Remove validated types; store validated contexts inline
aryan-25 539385f
Fix DocC references
aryan-25 a1658a9
Refactor
aryan-25 d970498
Reduce duplication in tests
aryan-25 0ece720
Empty commit to re-trigger CI checks
aryan-25 a8abebb
Empty commit to re-trigger CI checks
aryan-25 5c29ae1
Replace try! with preconditionFailure
aryan-25 cad591a
Merge branch 'main' into validate-config-at-init
aryan-25 8d224c0
Empty commit to re-trigger CI checks
aryan-25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
Sources/NIOHTTPServer/Configuration/NIOHTTPServerConfiguration+Validation.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift HTTP Server open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift HTTP Server project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift HTTP Server project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import NIOSSL | ||
|
|
||
| #if HTTP3 | ||
| import NIOQUIC | ||
| #endif | ||
|
|
||
| @available(anyAppleOS 26.0, *) | ||
| extension NIOHTTPServerConfiguration { | ||
| /// Validates the compatibility of the `supportedHTTPVersions` and `transportSecurity` configurations, and stores | ||
| /// the TLS resources required to set up the server channels. | ||
| mutating func validateTransportConfiguration() throws { | ||
| #if HTTP3 | ||
| (self.quicAuthenticationConfiguration, self.quicAuthenticator) = try self.makeQUICAuthentication() | ||
| #endif | ||
|
|
||
| self.sslContext = try self.makeSSLContext() | ||
| } | ||
|
|
||
| /// Creates the `NIOSSLContext` used by the secure upgrade channel(s), or `nil` if the configuration does not | ||
| /// specify a secure upgrade channel. | ||
| private func makeSSLContext() throws -> NIOSSLContext? { | ||
| #if HTTP3 | ||
| if self.supportedHTTPVersions.http3ConfigIfSupported != nil, self.supportedHTTPVersions.count == 1 { | ||
| // Only HTTP/3 was specified. As such, `NIOSSLContext` is not needed because a secure upgrade channel won't | ||
| // be set up. We can just return `nil` here. | ||
| return nil | ||
| } | ||
| #endif | ||
|
|
||
| switch self.transportSecurity.backing { | ||
| case .plaintext: | ||
| // Only HTTP/1.1 can be served over plaintext. To serve HTTP/2, `transportSecurity` must be set to `.tls` or | ||
| // `.mTLS`. | ||
| guard self.supportedHTTPVersions == [.http1_1] else { | ||
| throw NIOHTTPServerConfigurationError.incompatibleTransportSecurity | ||
| } | ||
| return nil | ||
|
|
||
| case .tls, .mTLS: | ||
| return try .makeServerContext( | ||
| transportSecurity: self.transportSecurity, | ||
| alpnIdentifiers: self.supportedHTTPVersions.alpnIdentifiers | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #if HTTP3 | ||
| /// Creates the QUIC authentication resources used by the HTTP/3 channel(s). | ||
| /// | ||
| /// Both are `nil` if HTTP/3 is not among ``supportedHTTPVersions``. | ||
| private func makeQUICAuthentication() throws -> ( | ||
| configuration: NIOQUIC.AuthenticationConfiguration?, | ||
| authenticator: NIOQUIC.Authenticator? | ||
| ) { | ||
| guard self.supportedHTTPVersions.http3ConfigIfSupported != nil else { return (nil, nil) } | ||
|
|
||
| switch self.transportSecurity.backing { | ||
| case .plaintext: | ||
| // Only HTTP/1.1 can be served over plaintext. To serve HTTP/3, `transportSecurity` must be set to `.tls`. | ||
| throw NIOHTTPServerConfigurationError.incompatibleTransportSecurity | ||
|
|
||
| case .tls(let tlsCredentials): | ||
| // We unfortunately need to pass forward both an `AuthenticationConfiguration` and an `Authenticator`: | ||
| // | ||
| // - RPK credentials are read from `AuthenticationConfiguration`; | ||
| // - X509 certificates (in-memory or PEM files on disk) are read from `Authenticator`. | ||
| // | ||
| // The problem is that `QUICConfiguration` requires the `AuthenticationConfiguration` argument, *even* | ||
| // when the TLS credentials are X509 certificates. Moreover, `AuthenticationConfiguration` can only be | ||
| // created with *PEM-file backed X509 credentials* (or RPKs), *even though* `Authenticator` supports | ||
| // `swift-certificates` objects as the source. | ||
| return (configuration: try .init(tlsCredentials), authenticator: try .init(tlsCredentials)) | ||
|
|
||
| case .mTLS: | ||
| throw NIOHTTPServerConfigurationError.mTLSNotCurrentlySupportedOverHTTP3 | ||
| } | ||
| } | ||
| #endif // HTTP3 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, but I think additionally having a
static varfor h2 and h3 that defaults the config to.defaultsmay make the API slightly nicer since we can avoid the empty brackets (.http2instead of.http2())There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in commit b32144d.