I'm trying to use the new typed WebSocket upgraders in NIO, and I can't figure out how to retrieve the response headers sent by the server during the initial HTTP phase of the WebSocket upgrade process.
I adapted my client code from the sample in NIO. It currently looks like:
private func openClientChannel(host: String, port: Int) async throws -> ServerConnection {
let bootstrap = PlatformBootstrap(group: ClientManager.group)
let upgradeResult = try await bootstrap.connect(host: host, port: port) { channel in
channel.eventLoop.makeCompletedFuture {
let upgrader = NIOAsyncWebSockets.NIOTypedWebSocketClientUpgrader<UpgradeResult> { channel, responseHead in
self.system.logger.trace("upgrading client channel to server on \(TaskPath.current)")
self.system.logger.trace("responseHead = \(responseHead)")
return channel.eventLoop.makeCompletedFuture {
let asyncChannel = try WebSocketAgentChannel(wrappingChannelSynchronously: channel)
return UpgradeResult.websocket(ServerConnection(channel: asyncChannel, nodeID: NodeIdentity("bogus")))
}
}
var headers = HTTPHeaders()
headers.add(name: "ActorSystemNodeID", value: self.system.nodeID.id)
headers.add(name: "Content-Type", value: "text/plain; charset=utf-8")
headers.add(name: "Content-Length", value: "0")
let requestHead = HTTPRequestHead(
version: .http1_1,
method: .GET,
uri: "/",
headers: headers
)
let clientUpgradeConfiguration = NIOTypedHTTPClientUpgradeConfiguration(
upgradeRequestHead: requestHead,
upgraders: [upgrader],
notUpgradingCompletionHandler: { channel in
channel.eventLoop.makeCompletedFuture {
return UpgradeResult.notUpgraded
}
}
)
let negotiationResultFuture = try channel.pipeline.syncOperations.configureUpgradableHTTPClientPipeline(
configuration: .init(upgradeConfiguration: clientUpgradeConfiguration)
)
return negotiationResultFuture
}
}
switch try await upgradeResult.get() {
case .websocket(let serverConnection):
return serverConnection
case .notUpgraded:
throw WebSocketActorSystemError.failedToUpgrade
}
}
}
Somewhere in here, I need to access the HTTP response headers from the initial HTTP connection. I had hoped they would be in the second argument to the NIOTypedWebSocketClientUpgrader upgradePipelineHandler, but those seem to be for a later phase of the upgrade process.
Is there some way to access those response headers?