-
Notifications
You must be signed in to change notification settings - Fork 652
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
Fix for Websocket client upgrade failure #2659
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,26 @@ extension ChannelPipeline { | |
} | ||
} | ||
|
||
private func interactInMemory(_ first: EmbeddedChannel, | ||
_ second: EmbeddedChannel, | ||
eventLoop: EmbeddedEventLoop) throws { | ||
var operated: Bool | ||
|
||
repeat { | ||
eventLoop.run() | ||
operated = false | ||
|
||
if let data = try first.readOutbound(as: ByteBuffer.self) { | ||
operated = true | ||
try second.writeInbound(data) | ||
} | ||
if let data = try second.readOutbound(as: ByteBuffer.self) { | ||
operated = true | ||
try first.writeInbound(data) | ||
} | ||
} while operated | ||
} | ||
|
||
private func setUpClientChannel(clientHTTPHandler: RemovableChannelHandler, | ||
clientUpgraders: [NIOHTTPClientProtocolUpgrader], | ||
_ upgradeCompletionHandler: @escaping (ChannelHandlerContext) -> Void) throws -> EmbeddedChannel { | ||
|
@@ -591,7 +611,7 @@ final class TypedWebSocketClientEndToEndTests: WebSocketClientEndToEndTests { | |
requestKey: "OfS0wDaT5NoxF2gqm7Zj2YtetzM=", | ||
upgradePipelineHandler: { (channel: Channel, _: HTTPResponseHead) in | ||
channel.pipeline.addHandler(handler) | ||
}) | ||
}) | ||
|
||
// The process should kick-off independently by sending the upgrade request to the server. | ||
let (clientChannel, upgradeResult) = try setUpClientChannel( | ||
|
@@ -615,5 +635,79 @@ final class TypedWebSocketClientEndToEndTests: WebSocketClientEndToEndTests { | |
|
||
return (clientChannel, handler) | ||
} | ||
|
||
func testSimpleUpgradeRejectedWhenServerSendsUpgradeNil() throws { | ||
|
||
enum TestUpgradeResult: Int { | ||
case successfulUpgrade | ||
case notUpgraded | ||
} | ||
|
||
let serverRecorder = WebSocketRecorderHandler() | ||
let clientRecorder = WebSocketRecorderHandler() | ||
let loop = EmbeddedEventLoop() | ||
let serverChannel = EmbeddedChannel(loop: loop) | ||
let clientChannel = EmbeddedChannel(loop: loop) | ||
|
||
let serverUpgrader = NIOTypedWebSocketServerUpgrader( | ||
shouldUpgrade: { (channel, head) in | ||
channel.eventLoop.makeSucceededFuture(nil) | ||
}, | ||
upgradePipelineHandler: { (channel, req) in | ||
channel.pipeline.addHandler(serverRecorder) | ||
} | ||
) | ||
|
||
XCTAssertNoThrow(try serverChannel.pipeline.syncOperations.configureUpgradableHTTPServerPipeline( | ||
configuration: .init( | ||
upgradeConfiguration: NIOTypedHTTPServerUpgradeConfiguration<Void>( | ||
upgraders: [serverUpgrader], | ||
notUpgradingCompletionHandler: { $0.eventLoop.makeSucceededVoidFuture() } | ||
) | ||
) | ||
)) | ||
|
||
let basicClientUpgrader = NIOTypedWebSocketClientUpgrader<TestUpgradeResult>( | ||
upgradePipelineHandler: { (channel: Channel, _: HTTPResponseHead) in | ||
channel.eventLoop.makeCompletedFuture { | ||
return TestUpgradeResult.successfulUpgrade | ||
} | ||
}) | ||
|
||
var headers = HTTPHeaders() | ||
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 config = NIOTypedHTTPClientUpgradeConfiguration<TestUpgradeResult>( | ||
upgradeRequestHead: requestHead, | ||
upgraders: [basicClientUpgrader], | ||
notUpgradingCompletionHandler: { channel in | ||
channel.eventLoop.makeCompletedFuture { | ||
return TestUpgradeResult.notUpgraded | ||
} | ||
} | ||
) | ||
let updgradeResult = try clientChannel.pipeline.syncOperations.configureUpgradableHTTPClientPipeline(configuration: .init(upgradeConfiguration: config)) | ||
|
||
XCTAssertNoThrow(try interactInMemory(clientChannel, serverChannel, eventLoop: loop)) | ||
XCTAssertNoThrow(try clientChannel.finish()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this test hits |
||
updgradeResult.whenComplete { result in | ||
switch result { | ||
case .success(let value): | ||
XCTAssertTrue(value == .notUpgraded) | ||
case .failure(let error): | ||
XCTFail("There should be no failure here \(error)") | ||
} | ||
} | ||
XCTAssertNoThrow(try serverChannel.finishAcceptingAlreadyClosed()) | ||
XCTAssertEqual(clientRecorder.errors.count, 0) | ||
XCTAssertEqual(serverRecorder.errors.count, 0) | ||
XCTAssertNoThrow(try loop.syncShutdownGracefully()) | ||
} | ||
} | ||
#endif |
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.
This We cannot run the
notUpgradingCompletionHandler
in all cases where the state machine returnsfailUpgradePromise
right now. What we should rather do is add a case to theHandlerRemovedAction
from the state machine and return that from the states where we know that running thenotUpgradingCompletionHandler
is correct. In detail, running thenotUpgradingCompletionHandler
while we are in the stateupgrading
orunbuffering
doesn't seem correct to me.