Skip to content

Fixes propagation of errors during TLS handshake #316

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
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
13 changes: 3 additions & 10 deletions Sources/AsyncHTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -935,16 +935,9 @@ class TLSEventsHandler: ChannelInboundHandler, RemovableChannelHandler {
}

func errorCaught(context: ChannelHandlerContext, error: Error) {
if let sslError = error as? NIOSSLError {
switch sslError {
case .handshakeFailed:
self.completionPromise?.fail(error)
self.completionPromise = nil
context.pipeline.removeHandler(self, promise: nil)
default:
break
}
}
self.completionPromise?.fail(error)
self.completionPromise = nil
context.pipeline.removeHandler(self, promise: nil)
context.fireErrorCaught(error)
}

Expand Down
1 change: 1 addition & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extension HTTPClientTests {
("testBodyUploadAfterEndFails", testBodyUploadAfterEndFails),
("testNoBytesSentOverBodyLimit", testNoBytesSentOverBodyLimit),
("testDoubleError", testDoubleError),
("testSSLHandshakeErrorPropagation", testSSLHandshakeErrorPropagation),
]
}
}
37 changes: 37 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2675,4 +2675,41 @@ class HTTPClientTests: XCTestCase {
// we need to verify that second error on write after timeout does not lead to double-release.
XCTAssertThrowsError(try self.defaultClient.execute(request: request, deadline: .now() + .milliseconds(2)).wait())
}

func testSSLHandshakeErrorPropagation() throws {
class CloseHandler: ChannelInboundHandler {
typealias InboundIn = Any

func channelActive(context: ChannelHandlerContext) {
context.close(promise: nil)
}
}

let server = try ServerBootstrap(group: self.serverGroup)
.childChannelOption(ChannelOptions.autoRead, value: false)
.childChannelInitializer { channel in
channel.pipeline.addHandler(CloseHandler())
}
.bind(host: "127.0.0.1", port: 0)
.wait()

defer {
XCTAssertNoThrow(try server.close().wait())
}

let request = try Request(url: "https://localhost:\(server.localAddress!.port!)", method: .GET)
let task = self.defaultClient.execute(request: request, delegate: TestHTTPDelegate())

XCTAssertThrowsError(try task.wait()) { error in
#if os(Linux)
XCTAssertEqual(error as? NIOSSLError, NIOSSLError.uncleanShutdown)
#else
if isTestingNIOTS() {
XCTAssertEqual((error as? AsyncHTTPClient.HTTPClient.NWTLSError).map { $0.status }, errSSLClosedNoNotify)
} else {
XCTAssertEqual((error as? IOError).map { $0.errnoCode }, 54)
}
#endif
}
}
}