-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 sending end stream in http2 web socket stream #73222
Merged
greenEkatherine
merged 7 commits into
dotnet:main
from
greenEkatherine:fix-end-stream-ws-h2
Aug 5, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
796617c
Fix sending end stream for websocket over H/2
d9b1858
Merge branch 'main' into fix-end-stream-ws-h2
c88e46b
feedback
92c8214
Update src/libraries/System.Net.WebSockets.Client/tests/SendReceiveTe…
0d78ed8
default usessl in tests
1641793
Update src/libraries/System.Net.WebSockets.Client/tests/ConnectTest.H…
d2d3ff8
disable new tests on browser because sockets not supported
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/libraries/System.Net.WebSockets.Client/tests/SendReceiveTest.Http2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Sockets; | ||
using System.Net.Test.Common; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
using static System.Net.Http.Functional.Tests.TestHelper; | ||
|
||
namespace System.Net.WebSockets.Client.Tests | ||
{ | ||
public class SendReceiveTest_Http2 : ClientWebSocketTestBase | ||
{ | ||
public SendReceiveTest_Http2(ITestOutputHelper output) : base(output) { } | ||
|
||
[Theory] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
[SkipOnPlatform(TestPlatforms.Browser, "System.Net.Sockets is not supported on this platform")] | ||
public async Task ReceiveNoThrowAfterSend_NoSsl(bool useHandler) | ||
{ | ||
var serverMessage = new byte[] { 4, 5, 6 }; | ||
await Http2LoopbackServer.CreateClientAndServerAsync(async uri => | ||
{ | ||
using (var cws = new ClientWebSocket()) | ||
using (var cts = new CancellationTokenSource(TimeOutMilliseconds)) | ||
{ | ||
cws.Options.HttpVersion = HttpVersion.Version20; | ||
cws.Options.HttpVersionPolicy = Http.HttpVersionPolicy.RequestVersionExact; | ||
if (useHandler) | ||
{ | ||
var handler = new SocketsHttpHandler(); | ||
await cws.ConnectAsync(uri, new HttpMessageInvoker(handler), cts.Token); | ||
} | ||
else | ||
{ | ||
await cws.ConnectAsync(uri, cts.Token); | ||
} | ||
|
||
await cws.SendAsync(new byte[] { 2, 3, 4 }, WebSocketMessageType.Binary, true, cts.Token); | ||
|
||
var readBuffer = new byte[serverMessage.Length]; | ||
await cws.ReceiveAsync(readBuffer, cts.Token); | ||
Assert.Equal(serverMessage, readBuffer); | ||
} | ||
}, | ||
async server => | ||
{ | ||
Http2LoopbackConnection connection = await server.EstablishConnectionAsync(new SettingsEntry { SettingId = SettingId.EnableConnect, Value = 1 }); | ||
(int streamId, HttpRequestData requestData) = await connection.ReadAndParseRequestHeaderAsync(readBody: false); | ||
// send status 200 OK to establish websocket | ||
await connection.SendResponseHeadersAsync(streamId, endStream: false).ConfigureAwait(false); | ||
|
||
// send reply | ||
byte binaryMessageType = 2; | ||
var prefix = new byte[] { binaryMessageType, (byte)serverMessage.Length }; | ||
byte[] constructMessage = prefix.Concat(serverMessage).ToArray(); | ||
await connection.SendResponseDataAsync(streamId, constructMessage, endStream: false); | ||
|
||
}, new Http2Options() { WebSocketEndpoint = true, UseSsl = false } | ||
); | ||
} | ||
|
||
[Fact] | ||
[SkipOnPlatform(TestPlatforms.Browser, "Self-signed certificates are not supported on browser")] | ||
public async Task ReceiveNoThrowAfterSend_WithSsl() | ||
{ | ||
var serverMessage = new byte[] { 4, 5, 6 }; | ||
await Http2LoopbackServer.CreateClientAndServerAsync(async uri => | ||
{ | ||
using (var cws = new ClientWebSocket()) | ||
using (var cts = new CancellationTokenSource(TimeOutMilliseconds)) | ||
{ | ||
cws.Options.HttpVersion = HttpVersion.Version20; | ||
cws.Options.HttpVersionPolicy = Http.HttpVersionPolicy.RequestVersionExact; | ||
|
||
var handler = CreateSocketsHttpHandler(allowAllCertificates: true); | ||
await cws.ConnectAsync(uri, new HttpMessageInvoker(handler), cts.Token); | ||
|
||
await cws.SendAsync(new byte[] { 2, 3, 4 }, WebSocketMessageType.Binary, true, cts.Token); | ||
|
||
var readBuffer = new byte[serverMessage.Length]; | ||
await cws.ReceiveAsync(readBuffer, cts.Token); | ||
Assert.Equal(serverMessage, readBuffer); | ||
} | ||
}, | ||
async server => | ||
{ | ||
Http2LoopbackConnection connection = await server.EstablishConnectionAsync(new SettingsEntry { SettingId = SettingId.EnableConnect, Value = 1 }); | ||
(int streamId, HttpRequestData requestData) = await connection.ReadAndParseRequestHeaderAsync(readBody: false); | ||
// send status 200 OK to establish websocket | ||
await connection.SendResponseHeadersAsync(streamId, endStream: false).ConfigureAwait(false); | ||
|
||
// send reply | ||
byte binaryMessageType = 2; | ||
var prefix = new byte[] { binaryMessageType, (byte)serverMessage.Length }; | ||
byte[] constructMessage = prefix.Concat(serverMessage).ToArray(); | ||
await connection.SendResponseDataAsync(streamId, constructMessage, endStream: false); | ||
|
||
}, new Http2Options() { WebSocketEndpoint = true } | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe I'm doing something wrong, or misunderstanding the change, but if I'm reading this correctly and testing the new feature correctly, this check does nothing because
ConnectProtocolEstablished
is only set after the request has already been sent and a response with a 200 was received.runtime/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs
Lines 640 to 643 in 08a11e4
Which means every time an HTTP2 WebSocket request is made it will still send the END_STREAM flag.
Did you test this change with Kestrel before merging? Am I holding the feature wrong?
cc @karelz
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.
Sorry, I will double check. I tested it with Kestrel but I probably miss to check state on the server side. Using receive before send works so I thought that the end stream flag causes an issue only on established connection. Also connect without end stream is hanging, I should try different approach.
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.
I see, I have to switch on flush correctly for both connect and send