Skip to content

Implements URLSessionWebSocketTask #4643

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 6 commits into from
Nov 16, 2022

Conversation

pseligman
Copy link
Contributor

Relies on currently-experimental web socket support in libcurl, which should land in 7.86.0. We should hold off on merging this until that version is tagged, to ensure there are no late-breaking API changes in libcurl that require changes to this patch.

@pseligman
Copy link
Contributor Author

libcurl 7.86.0 was tagged earlier this week, and no API changes necessary. I think we're okay to land this.

@pseligman
Copy link
Contributor Author

@jrflat Can I bug you for a review?

Update "WebServices" to "WebSockets" everywhere.
Update code to fail gracefully when libcurl is at >= 7.86.0, but lacks WebSockets support.
Update tests to bail when WebSockets support is disabled in libcurl.
@jrflat
Copy link
Contributor

jrflat commented Oct 28, 2022

@swift-ci please test

@pseligman
Copy link
Contributor Author

Looks like the Linux check was aborted, but I don't see any details why. Going to see if it'll let me kick off a test build.

@pseligman
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@jrflat jrflat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a pass and I think it looks good, just some nits and questions about various imports. Were some of these changes made specifically to help swift-corelibs-foundation run on Darwin? (i.e. not necessarily part of WebSocket implementation, but useful nonetheless)

@@ -5,7 +5,7 @@
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>SwiftFoundation</string>
<string>SwiftFoundationNetworking</string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes (SwiftFoundationXML, too) ones that should've been made on the repo earlier? (Not related to WebSocketTasks necessarily)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! As part of the e9e183c commit, these were misc changes that should have landed earlier to keep the Darwin build healthy.

@@ -1922,6 +2007,13 @@ class TestURLSession: LoopbackServerTest {
/* ⚠️ */ testExpectedToFail(test_noDoubleCallbackWhenCancellingAndProtocolFailsFast, "This test crashes nondeterministically: https://bugs.swift.org/browse/SR-11310")),
/* ⚠️ */ ("test_cancelledTasksCannotBeResumed", testExpectedToFail(test_cancelledTasksCannotBeResumed, "Breaks on Ubuntu 18.04")),
]
if #available(macOS 12.0, *) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was macOS 10.15 in the earlier #available() checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSocket support requires 10.15.
XCTestCase.asyncTest requires 12.0


guard
let location = response.value(forHeaderField: .location),
let targetURL = URL(string: location)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Location can be relative. Use string:relativeTo: initializer which can handle both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is existing behavior, the change here just moved redirectRequest out of an extension (see old line 656). I'd prefer to split this out into a separate issue.


open func webSocketTask(with url: URL, protocols: [String]) -> URLSessionWebSocketTask {
var request = URLRequest(url: url)
request.setValue(protocols.joined(separator: ", "), forHTTPHeaderField: "Sec-WebSocket-Protocol")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are Sec-WebSocket-Key and Sec-WebSocket-Version added by curl?

Copy link
Contributor Author

@pseligman pseligman Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. -Key and -Version are properties of the WebSocket protocol itself, and are injected by libcurl. -Protocol is meant for application-specific logic on top of the WebSocket protocol, which is why this belongs at our layer.

@pseligman
Copy link
Contributor Author

Were some of these changes made specifically to help swift-corelibs-foundation run on Darwin? (i.e. not necessarily part of WebSocket implementation, but useful nonetheless)

Yes, one of the 3 commits in this PR (e9e183c) contains misc fixes to clean up compilation and running on Darwin. If preferable, I can break that out into a separate PR. But the changes were small enough that they felt better coming along for the ride as part of the larger WebSocket PR.

@pseligman
Copy link
Contributor Author

@swift-ci please test

@jrflat
Copy link
Contributor

jrflat commented Nov 2, 2022

Yes, one of the 3 commits in this PR (e9e183c) contains misc fixes to clean up compilation and running on Darwin. If preferable, I can break that out into a separate PR. But the changes were small enough that they felt better coming along for the ride as part of the larger WebSocket PR.

Ok that makes sense I think it's fine to keep the changes in the WebSocket PR.

Comment on lines 10 to 12
import Foundation
import XCTest

Copy link
Contributor

@jrflat jrflat Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably do need to add these back to get the macOS build working again, but overall looks good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that's just embarrassing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries at all, tests look good!

@pseligman
Copy link
Contributor Author

@swift-ci please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants