Repository navigation
Use ManagedHandler in managed ClientWebSocket - #24288
Conversation
It takes a input array anyway and uses that rather than allocating if provided? (i.e. ArrayPool isn't required for buffer allocation avoidance) |
Correct |
d663e09 to
686217c
Compare
|
@dotnet-bot test OSX x64 Debug Build please: |
|
|
||
| internal static bool IsSupportedSecureScheme(string scheme) => | ||
| string.Equals(scheme, "https", StringComparison.OrdinalIgnoreCase) || | ||
| string.Equals(scheme, "wss", StringComparison.OrdinalIgnoreCase); |
There was a problem hiding this comment.
What if the websocket schemes are used directly from HttpClient?
There was a problem hiding this comment.
Is this what we want? e.g. is this what the WinHttpHandler does?
I would have expected the ClientWebSocket logic to construct a Uri with an http scheme (http/https as appropriate) and then pass this down to HttpClient.
There was a problem hiding this comment.
I would have expected the ClientWebSocket logic to construct a Uri with an http scheme (http/https as appropriate) and then pass this down to HttpClient.
How does that work when going through a proxy? Wouldn't the proxy then forward the wrong scheme as part of the request?
e.g. is this what the WinHttpHandler does?
WinHttpHandler doesn't support web sockets at all.
There was a problem hiding this comment.
How does that work when going through a proxy? Wouldn't the proxy then forward the wrong scheme as part of the request?
That said, if it works fine with proxies, changing the scheme was my original plan and is certainly easier.
| return bytesRead; | ||
| } | ||
|
|
||
| public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
There was a problem hiding this comment.
Doesn't this mean that we will allow writes on any Connection: close connection? This seems like it's too general. Can't we restrict this to cases where we are actually using the Upgrade mechanism?
There was a problem hiding this comment.
Doesn't this mean that we will allow writes on any Connection: close connection?
Yes
Can't we restrict this to cases where we are actually using the Upgrade mechanism?
I guess? So you want to check whether there was an Upgrade request and Upgrade response header and allow writes only when both are there?
There was a problem hiding this comment.
I assume we'd look for a 101 response code and then do something special with it (i.e. give back the duplex stream).
There was a problem hiding this comment.
This does seem useful in other cases than WebSocket, btw.
Basically we are saying that we support the Upgrade mechanism, and we have a way to expose the resulting raw stream back to the user.
This also gets us part way to supporting CONNECT tunneling for proxies...
There was a problem hiding this comment.
This does seem useful in other cases than WebSocket, btw.
Yeah, that's why I didn't constrain it. But I can see the argument in constraining it only to a switching protocols response code, so I'll do so.
| public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); | ||
|
|
||
| public override void SetLength(long value) => throw new NotSupportedException(); | ||
| public override void Flush() { } |
There was a problem hiding this comment.
Is this the expected behavior? Seems like Flush should throw if the stream is not writable.
There was a problem hiding this comment.
Is this the expected behavior? Seems like Flush should throw if the stream is not writable.
There's little documentation on what Stream.Flush should do on non-writable streams, and thus our streams are inconsistent, but mostly err on the side of nop'ing. MemoryStream, FileStream, NetworkStream, and DeflateStream for example all treat Flush on a non-writable stream as a nop. PipeStream.Flush on a non-writable stream throws.
|
@geoffkizer, thanks for reviewing. I pushed a commit with tweaks in response. |
|
@dotnet-bot test Outerloop Linux x64 Release Build please |
| { | ||
| internal sealed partial class HttpConnection : IDisposable | ||
| { | ||
| private sealed class ConnectionCloseDuplexStream : HttpContentDuplexStream |
There was a problem hiding this comment.
Can we call this something like "ConnectionUpgradeStream" -- that seems more descriptive.
There was a problem hiding this comment.
Actually, since I think we want to use this for CONNECT as well, can we call it something like RawConnectionStream?
|
Re proxies, I'm not entirely sure how this is supposed to work. I'm looking at the RFC (https://tools.ietf.org/html/rfc6455) and it looks like non-secure connections are supposed to use the CONNECT verb to try to establish an end-to-end connection. See section 4.1. That said, WebSockets is one of those things where practice may be different than the RFC... Secure connections use standard proxy tunneling, which establishes an end-to-end connection using CONNECT under the covers, but this should be transparent to the ClientWebSocket code (mostly?). Of course, we haven't implemented proxy tunneling yet. I'm not quite sure how to implement the non-secure proxy support in ClientWebSocket. It seems straightforward to add CONNECT support to HttpClient; we need this for SSL proxy tunneling anyway. However, we then need to take the resulting connection and make HttpClient use it for the Upgrade request. There's not really a way to make this work today. We could add some sort of mechanism for this, and it might be generally useful, but it's not trivial. It also looks like the ws/wss scheme should be used when querying for a proxy. Thoughts? |
|
Other than the proxy issue above, this all looks good to me. |
The .NET Framework always uses CONNECT tunneling for WebSocket when going thru a proxy even if it is for non-secure protocol (ws). This was to ensure maximum reliability when going thru proxies. Many proxies like to block websocket traffic. So, the decision at the time when .NET Framework ClientWebSocket was done was to always use CONNECT tunneling. I think other implementations such as browsers do the same thing. I know that Internet Explorer and Edge browsers do that also (I was on the team that did that implementation). |
|
@davidsh Yeah, that's how I read the RFC as well. |
…s test 60 seconds is annoyingly long when trying to iterate. Reduced to 10.
Currently if a web socket message arrives but not all of its data is there, ManagedWebSocket.ReceiveAsync will manufacture a non-EndOfMessage WebSocketReceiveResult with the available data. This means for larger messages, we end up artificially splitting the messages, which in turns leads to all of the async methods up the stack doing extra allocations due to needing to call back in to get the remainder of the message. While the approach does allow for the consumer to start processing the data from the message immediately, in most cases the consumer needs the full message anyway. So we can avoid these costs by being more aggressive about reading the full message size, up to whatever receive buffer we have to work with.
We end up draining the relevant bucket in the pool very quickly, such that we still end up doing a lot of allocating while also harming other users of the same bucket size.
…ete message There's no guarantee that WebSocket.ReceiveAsync will return a complete message; in the extreme, a message can contain long.MaxValue bytes but the API only allows you to retrieve ~int.MaxValue at a time, but even way below that, it's common for ReceiveAsync to return the data it has available rather than gathering up all of the data from the message. However, several of the ConnectTest tests were depending on the full-message behavior. This commit fixes that.
…cket - Allow ws:// and wss:// schemes - Add an HttpContentDuplexStream base class to parallel the existing HttpContentReadStream and HttpContentWriteStream base classes - Move more shared functionality down to the base HttpContentStream class - Make ConnectionCloseStream duplex instead of read-only
ClientWebSocket's managed implementation currently has its own basic Socket-based code for establishing a websocket connection. This change switches it to use ManagedHandler instead, and in doing so benefits from improvements made to ManagedHandler thus far and in the future. For example, currently ManagedWebSocket doesn't support ClientWebSocketOptions.Proxy or ClientWebSocketOptions.Credentials, but since ManagedHandler does, now ManagedWebSocket does, too.
- Have ClientWebSocket change the scheme from http{s} -> ws{s} instead of having HttpClient accept ws{s} urls.
- Use a duplex response stream only when the status code is 101
- Disable a test that relies on connection opening being cancelable
393e037 to
518ea1c
Compare
|
@dotnet-bot test this please |
|
@dotnet-bot test Outerloop Linux x64 Release Build please |
|
@dotnet-bot test Linux x64 Release Build please (ConnectAsync_DnsEndPoint_Success failure "No such device or address") |
|
@dotnet/dnceng, can you help me determine what's wrong with https://ci3.dot.net/job/dotnet_corefx/job/master/job/linux-TGroup_netcoreapp+CGroup_Release+AGroup_x64+TestOuter_false_prtest/3898/ ? It shows as a red X, and when I click through it shows there's a failure on RedHat.73.Amd64.Open, but then drilling in further, there aren't any rows listed in the tests results view, it says there are 0 failures, etc. |
|
It looks like two builds were started relatively near each other. The original build, and the one re-triggered by your request of "test this please". It seems like our logic of which of these builds is more relevant isn't entirely consistent (mc.dot.net is picking the successful one, and Jenkins/github status API, the failed one). I will check the heuristics of mc.dot.net to make sure that the one it's picking is the most recent one. |
|
Thanks, @ChadNedzlek. |
|
@stephentoub re: the weirdness of reporting. 5 identical copies of the RedHat run were initiated. The 2nd out of 5 run failed, and the others all passed. So it seems like the GitHub status is what is being incorrectly reported. |
|
@stephentoub This is likely due to some instability on the Jenkins side that's being actively investigated. Mission control is accurately reflecting the most recent state of the PR, so you should be able to trust it in the meantime, though the status on the GitHub side might fail to update. |
|
Thanks, @ChadNedzlek. |
…agedhandler Use ManagedHandler in managed ClientWebSocket Commit migrated from dotnet/corefx@ed12fd8
ClientWebSocket's managed implementation currently has its own basic Socket-based code for establishing a websocket connection. This change switches it to use ManagedHandler instead, and in doing so benefits from improvements made to ManagedHandler thus far and in the future. For example, currently the managed ClientWebSocket doesn't support ClientWebSocketOptions.Proxy or ClientWebSocketOptions.Credentials, but since ManagedHandler does, now the managed ClientWebSocket does, too.
As part of this, there are several additional commits:
cc: @geoffkizer, @Priya91, @wfurt, @anurse
Fixes https://github.com/dotnet/corefx/issues/23152