Bug 215456 - Set Sec-WebSocket-Protocol for WebSocket NSURLSession code path
Summary: Set Sec-WebSocket-Protocol for WebSocket NSURLSession code path
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: youenn fablet
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-08-13 09:30 PDT by youenn fablet
Modified: 2020-08-17 05:21 PDT (History)
3 users (show)

See Also:


Attachments
Patch (2.52 KB, patch)
2020-08-14 10:10 PDT, youenn fablet
no flags Details | Formatted Diff | Diff
Patch (2.59 KB, patch)
2020-08-14 10:49 PDT, youenn fablet
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description youenn fablet 2020-08-13 09:30:50 PDT
Set Sec-WebSocket-Protocol for WebSocket NSURLSession code path
Comment 1 Radar WebKit Bug Importer 2020-08-13 09:31:43 PDT
<rdar://problem/66996369>
Comment 2 youenn fablet 2020-08-14 10:10:13 PDT
Created attachment 406601 [details]
Patch
Comment 3 Alex Christensen 2020-08-14 10:12:06 PDT
Comment on attachment 406601 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=406601&action=review

> Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm:1571
> +        [requestWithProtocols addValue: StringView(protocol).createNSStringWithoutCopying().get() forHTTPHeaderField:@"Sec-WebSocket-Protocol"];

This seems unsafe.  Why not just make a copy?
Comment 4 Alex Christensen 2020-08-14 10:25:22 PDT
Comment on attachment 406601 [details]
Patch

This seems like a good workaround for the fact that there is webSocketTaskWithURL:protocols: but no webSocketTaskWithRequest:protocols:.  Let's introduce that API and do this on operating system versions that don't have it.
Comment 5 youenn fablet 2020-08-14 10:33:35 PDT
(In reply to Alex Christensen from comment #4)
> Comment on attachment 406601 [details]
> Patch
> 
> This seems like a good workaround for the fact that there is
> webSocketTaskWithURL:protocols: but no webSocketTaskWithRequest:protocols:. 
> Let's introduce that API and do this on operating system versions that don't
> have it.


Actually there is, let's use that.
Comment 6 Alex Christensen 2020-08-14 10:34:18 PDT
There isn't on Catalina.
Comment 7 youenn fablet 2020-08-14 10:35:39 PDT
(In reply to Alex Christensen from comment #6)
> There isn't on Catalina.

We might actually want to bump HAVE_NSURLSESSION_WEBSOCKET to Big Sur.
Comment 8 youenn fablet 2020-08-14 10:43:14 PDT
(In reply to Alex Christensen from comment #6)
> There isn't on Catalina.

I looked too quickly, there is no such thing given we also want to set other headers like User-Agent.
Comment 9 youenn fablet 2020-08-14 10:49:07 PDT
Created attachment 406606 [details]
Patch
Comment 10 jmehta 2020-08-14 10:52:52 PDT
There is no variant of WebSocket task with request and protocols. The only variant with protocols is the one that takes a url. 

The client can add the protocol headers manually on the request which would achieve the same result.

I think in WebKit's case we want to pass along the request since other information about main document etc will also be needed to lookup cookies etc, so moving to the url variant might not work.
Comment 11 youenn fablet 2020-08-14 10:54:38 PDT
> I think in WebKit's case we want to pass along the request since other
> information about main document etc will also be needed to lookup cookies
> etc, so moving to the url variant might not work.

Right, I do not think offering webSocketTaskWithRequest:protocols: provides much really.
If you are using webSocketTaskWithRequest, you are already out of WebSocket standard API.
Comment 12 jmehta 2020-08-14 10:56:59 PDT
(In reply to youenn fablet from comment #11)
> > I think in WebKit's case we want to pass along the request since other
> > information about main document etc will also be needed to lookup cookies
> > etc, so moving to the url variant might not work.
> 
> Right, I do not think offering webSocketTaskWithRequest:protocols: provides
> much really.
> If you are using webSocketTaskWithRequest, you are already out of WebSocket
> standard API.

Yup!
Comment 13 Alex Christensen 2020-08-14 11:06:19 PDT
Comment on attachment 406606 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=406606&action=review

> Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm:1569
> +    auto *nsRequest = request.nsURLRequest(WebCore::HTTPBodyUpdatePolicy::DoNotUpdateHTTPBody);

I think this returns an NSMutableURLRequest, so we could avoid a copy.  Probably not too important here.

> Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm:1571
> +        NSMutableURLRequest *requestWithProtocols = [[nsRequest mutableCopy] autorelease];

adoptNS could avoid putting a reference in the autorelease pool.
Comment 14 Alex Christensen 2020-08-14 14:04:37 PDT
Comment on attachment 406606 [details]
Patch

ok
Comment 15 youenn fablet 2020-08-17 05:17:00 PDT
> > Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm:1569
> > +    auto *nsRequest = request.nsURLRequest(WebCore::HTTPBodyUpdatePolicy::DoNotUpdateHTTPBody);
> 
> I think this returns an NSMutableURLRequest, so we could avoid a copy. 
> Probably not too important here.

It does not seem so from ResourceRequest::nsURLRequest.
Comment 16 EWS 2020-08-17 05:21:20 PDT
Committed r265752: <https://trac.webkit.org/changeset/265752>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 406606 [details].