Skip to content

Commit

Permalink
Remove '#if canImport(NIOSSL)' (#22)
Browse files Browse the repository at this point in the history
Motivation:

Forks of this transport to remove NIOSSL can be done by removing the
Posix module rather then removing NIOSSL. This means we no longer need
the can-imports for NIOSSL.

Modifications:

Remove all instances of '#if canImport(NIOSSL)'

Result:

Simpler code
  • Loading branch information
glbrntt authored Nov 8, 2024
1 parent a2e1ea8 commit 237bcc4
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public import GRPCCore
public import GRPCNIOTransportCore // should be @usableFromInline
public import NIOCore // has to be public because of EventLoopGroup param in init
public import NIOPosix // has to be public because of default argument value in init

#if canImport(NIOSSL)
private import NIOSSL
#endif

extension HTTP2ClientTransport {
/// A `ClientTransport` using HTTP/2 built on top of `NIOPosix`.
Expand Down Expand Up @@ -129,23 +126,20 @@ extension HTTP2ClientTransport.Posix {
private let config: HTTP2ClientTransport.Posix.Config
private let eventLoopGroup: any EventLoopGroup

#if canImport(NIOSSL)
private let nioSSLContext: NIOSSLContext?
private let sslContext: NIOSSLContext?
private let serverHostname: String?
#endif

init(eventLoopGroup: any EventLoopGroup, config: HTTP2ClientTransport.Posix.Config) throws {
self.eventLoopGroup = eventLoopGroup
self.config = config

#if canImport(NIOSSL)
switch self.config.transportSecurity.wrapped {
case .plaintext:
self.nioSSLContext = nil
self.sslContext = nil
self.serverHostname = nil
case .tls(let tlsConfig):
do {
self.nioSSLContext = try NIOSSLContext(configuration: TLSConfiguration(tlsConfig))
self.sslContext = try NIOSSLContext(configuration: TLSConfiguration(tlsConfig))
self.serverHostname = tlsConfig.serverHostname
} catch {
throw RuntimeError(
Expand All @@ -155,7 +149,6 @@ extension HTTP2ClientTransport.Posix {
)
}
}
#endif
}

func establishConnection(
Expand All @@ -165,16 +158,14 @@ extension HTTP2ClientTransport.Posix {
group: self.eventLoopGroup
).connect(to: address) { channel in
channel.eventLoop.makeCompletedFuture {
#if canImport(NIOSSL)
if let nioSSLContext = self.nioSSLContext {
if let sslContext = self.sslContext {
try channel.pipeline.syncOperations.addHandler(
NIOSSLClientHandler(
context: nioSSLContext,
context: sslContext,
serverHostname: self.serverHostname
)
)
}
#endif

return try channel.pipeline.syncOperations.configureGRPCClientPipeline(
channel: channel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ internal import NIOCore
internal import NIOExtras
internal import NIOHTTP2
public import NIOPosix // has to be public because of default argument value in init
private import NIOSSL
private import Synchronization

#if canImport(NIOSSL)
import NIOSSL
#endif

extension HTTP2ServerTransport {
/// A `ServerTransport` using HTTP/2 built on top of `NIOPosix`.
///
Expand Down Expand Up @@ -63,7 +60,6 @@ extension HTTP2ServerTransport {
address: GRPCNIOTransportCore.SocketAddress,
serverQuiescingHelper: ServerQuiescingHelper
) async throws -> NIOAsyncChannel<AcceptedChannel, Never> {
#if canImport(NIOSSL)
let sslContext: NIOSSLContext?

switch self.config.transportSecurity.wrapped {
Expand All @@ -80,7 +76,6 @@ extension HTTP2ServerTransport {
)
}
}
#endif

let serverChannel = try await ServerBootstrap(group: eventLoopGroup)
.serverChannelOption(.socketOption(.so_reuseaddr), value: 1)
Expand All @@ -90,13 +85,11 @@ extension HTTP2ServerTransport {
}
.bind(to: address) { channel in
channel.eventLoop.makeCompletedFuture {
#if canImport(NIOSSL)
if let sslContext {
try channel.pipeline.syncOperations.addHandler(
NIOSSLServerHandler(context: sslContext)
)
}
#endif

let requireALPN: Bool
let scheme: Scheme
Expand Down
3 changes: 1 addition & 2 deletions Sources/GRPCNIOTransportHTTP2Posix/NIOSSL+GRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(NIOSSL)

import NIOSSL

extension NIOSSLSerializationFormats {
Expand Down Expand Up @@ -159,4 +159,3 @@ extension TLSConfiguration {
self.applicationProtocols = ["grpc-exp", "h2"]
}
}
#endif
4 changes: 0 additions & 4 deletions Sources/GRPCNIOTransportHTTP2Posix/TLSConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,10 @@ extension HTTP2ServerTransport.Posix.Config {
/// This connection is plaintext: no encryption will take place.
public static let plaintext = Self(wrapped: .plaintext)

#if canImport(NIOSSL)
/// This connection will use TLS.
public static func tls(_ tls: TLS) -> Self {
Self(wrapped: .tls(tls))
}
#endif
}

public struct TLS: Sendable {
Expand Down Expand Up @@ -261,12 +259,10 @@ extension HTTP2ClientTransport.Posix.Config {
/// This connection is plaintext: no encryption will take place.
public static let plaintext = Self(wrapped: .plaintext)

#if canImport(NIOSSL)
/// This connection will use TLS.
public static func tls(_ tls: TLS) -> Self {
Self(wrapped: .tls(tls))
}
#endif
}

public struct TLS: Sendable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
import GRPCCore
import GRPCNIOTransportCore
import GRPCNIOTransportHTTP2Posix
import XCTest

#if canImport(NIOSSL)
import NIOSSL
#endif
import XCTest

final class HTTP2TransportNIOPosixTests: XCTestCase {
func testGetListeningAddress_IPv4() async throws {
Expand Down Expand Up @@ -191,7 +188,6 @@ final class HTTP2TransportNIOPosixTests: XCTestCase {
XCTAssertEqual(grpcConfig.backoff, HTTP2ClientTransport.Config.Backoff.defaults)
}

#if canImport(NIOSSL)
static let samplePemCert = """
-----BEGIN CERTIFICATE-----
MIIGGzCCBAOgAwIBAgIJAJ/X0Fo0ynmEMA0GCSqGSIb3DQEBCwUAMIGjMQswCQYD
Expand Down Expand Up @@ -478,5 +474,4 @@ final class HTTP2TransportNIOPosixTests: XCTestCase {
XCTAssertEqual(nioSSLTLSConfig.trustRoots, .default)
XCTAssertEqual(nioSSLTLSConfig.applicationProtocols, ["grpc-exp", "h2"])
}
#endif
}

0 comments on commit 237bcc4

Please sign in to comment.