Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
30da832
downgrade proposal for HttpClient
May 24, 2022
7b03bc8
HttpClient to handle ws over h2
Jun 7, 2022
e7b8332
ClientWebSocket to handle ws over h2
Jun 7, 2022
a5e991a
Add property for protocol header and remove it from known headers
Jun 17, 2022
264c802
Update src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestM…
Jun 18, 2022
08d1f8c
Address review feedback
Jun 18, 2022
71cc887
Rename HttpVersion and HttpVersionPolicy
Jun 20, 2022
8019a3c
Apply suggestions from code review
Jun 21, 2022
8ef8b4c
address review feedback
Jun 21, 2022
ac966a8
Merge branch 'main' into ws-h2-draft
Jun 23, 2022
f6de72a
address review feedback
Jun 24, 2022
9221483
address review feedback
Jun 28, 2022
fafc84b
Apply suggestions from code review
Jun 28, 2022
0d8f8f7
fix race condition on setting enable connect
Jun 29, 2022
9be5b95
inherit h2 read and writes streams
Jun 29, 2022
502b051
Apply suggestions from code review
Jun 30, 2022
e635439
fix H2PACK encoding issue
Jun 30, 2022
ad66857
add timeout for waiting settings task
Jun 30, 2022
c58d993
generalized settings received task
Jun 30, 2022
76c9e20
Update src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpH…
Jul 1, 2022
2467449
Fixing HttpStream tests
Jul 1, 2022
d9248a6
Apply suggestions from code review
Jul 4, 2022
baf52d4
address review feedback
Jul 4, 2022
afaf5be
Apply suggestions from code review
Jul 8, 2022
918c6a8
Address review feedback
Jul 8, 2022
9734075
Adapt test to ValueTask.FromException
Jul 8, 2022
efdcdba
Apply suggestions from code review
Jul 10, 2022
6f7b101
Adding connect tests
Jul 10, 2022
595642c
Apply suggestions from code review
Jul 10, 2022
e05db11
feedback + skip tests on browser
Jul 10, 2022
54d0b08
Merge branch 'main' into ws-h2-draft
Jul 10, 2022
b7f543b
Feedback + test for websocket stream
Jul 12, 2022
73968ec
Update src/libraries/System.Net.Http/src/Resources/Strings.resx
Jul 12, 2022
4dbe1c2
Address review feedback
Jul 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libraries/System.Net.Http/ref/System.Net.Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public HttpMethod(string method) { }
public static System.Net.Http.HttpMethod Post { get { throw null; } }
public static System.Net.Http.HttpMethod Put { get { throw null; } }
public static System.Net.Http.HttpMethod Trace { get { throw null; } }
public static System.Net.Http.HttpMethod Connect { get { throw null; } }
public bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] System.Net.Http.HttpMethod? other) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) { throw null; }
public override int GetHashCode() { throw null; }
Expand Down Expand Up @@ -656,6 +657,7 @@ internal HttpRequestHeaders() { }
public System.DateTimeOffset? IfUnmodifiedSince { get { throw null; } set { } }
public int? MaxForwards { get { throw null; } set { } }
public System.Net.Http.Headers.HttpHeaderValueCollection<System.Net.Http.Headers.NameValueHeaderValue> Pragma { get { throw null; } }
public string? Protocol { get { throw null; } set { } }
public System.Net.Http.Headers.AuthenticationHeaderValue? ProxyAuthorization { get { throw null; } set { } }
public System.Net.Http.Headers.RangeHeaderValue? Range { get { throw null; } set { } }
public System.Uri? Referrer { get { throw null; } set { } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public int? MaxForwards
set { SetOrRemoveParsedValue(KnownHeaders.MaxForwards.Descriptor, value); }
}

public string? Protocol
{
get { return (string?)GetSingleParsedValue(new HeaderDescriptor(":protocol")); }
set { SetOrRemoveParsedValue(new HeaderDescriptor(":protocol"), value); }
}


public AuthenticationHeaderValue? ProxyAuthorization
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static HttpMethod Patch
// Don't expose CONNECT as static property, since it's used by the transport to connect to a proxy.
// CONNECT is not used by users directly.

internal static HttpMethod Connect
public static HttpMethod Connect
{
get { return s_connectMethod; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ public override string ToString()

internal bool WasRedirected() => (_sendStatus & MessageIsRedirect) != 0;

internal bool IsWebSocketH2Request() => _version.Major == 2 && HasHeaders && Headers.Protocol == "websocket";

#region IDisposable Members

protected virtual void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,13 @@ private void ProcessSettingsFrame(FrameHeader frameHeader, bool initialFrame = f
// We don't actually store this value; we always send frames of the minimum size (16K).
break;

case SettingId.EnableConnect:
if (settingValue == 1)
{
IsConnectEnabled = true;
}
break;

default:
// All others are ignored because we don't care about them.
// Note, per RFC, unknown settings IDs should be ignored.
Expand Down Expand Up @@ -1446,6 +1453,15 @@ private void WriteHeaders(HttpRequestMessage request, ref ArrayBuffer headerBuff
WriteIndexedHeader(H2StaticTable.PathSlash, pathAndQuery, ref headerBuffer);
}

if (request.HasHeaders && request.Headers.Protocol != null)
{
WriteBytes(":protocol"u8, ref headerBuffer);
Encoding? protocolEncoding = _pool.Settings._requestHeaderEncodingSelector?.Invoke(":protocol", request);
WriteLiteralHeaderValue(request.Headers.Protocol, protocolEncoding, ref headerBuffer);

request.Headers.Protocol = null;
}

if (request.HasHeaders)
{
WriteHeaderCollection(request, request.Headers, ref headerBuffer);
Expand Down Expand Up @@ -1888,9 +1904,12 @@ private enum SettingId : ushort
MaxConcurrentStreams = 0x3,
InitialWindowSize = 0x4,
MaxFrameSize = 0x5,
MaxHeaderListSize = 0x6
MaxHeaderListSize = 0x6,
EnableConnect = 0x8
}

internal bool IsConnectEnabled { get; private set; }

// Note that this is safe to be called concurrently by multiple threads.

public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private sealed class Http2Stream : IValueTaskSource, IHttpStreamHeadersHandler,
private StreamCompletionState _requestCompletionState;
private StreamCompletionState _responseCompletionState;
private ResponseProtocolState _responseProtocolState;
private bool _webSocketEstablished;

// If this is not null, then we have received a reset from the server
// (i.e. RST_STREAM or general IO error processing the connection)
Expand Down Expand Up @@ -531,6 +532,7 @@ void IHttpStreamHeadersHandler.OnStaticIndexedHeader(int index)

if (index <= LastHPackRequestPseudoHeaderId)
{
// add protocol and others pseudoheaders
if (NetEventSource.Log.IsEnabled()) Trace($"Invalid request pseudo-header ID {index}.");
throw new HttpRequestException(SR.net_http_invalid_response);
}
Expand Down Expand Up @@ -629,6 +631,11 @@ private void OnStatus(int statusCode)
}
else
{
if (_response.RequestMessage != null && _response.RequestMessage.IsWebSocketH2Request() && statusCode == 200)
{
_webSocketEstablished = true;
}

_responseProtocolState = ResponseProtocolState.ExpectingHeaders;

// If we are waiting for a 100-continue response, signal the waiter now.
Expand Down Expand Up @@ -1036,6 +1043,10 @@ public async Task ReadResponseHeadersAsync(CancellationToken cancellationToken)
MoveTrailersToResponseMessage(_response);
responseContent.SetStream(EmptyReadStream.Instance);
}
else if (_webSocketEstablished)
{
responseContent.SetStream(new Http2ReadWriteStream(this));
}
else
{
responseContent.SetStream(new Http2ReadStream(this));
Expand Down Expand Up @@ -1584,6 +1595,131 @@ public override Task FlushAsync(CancellationToken cancellationToken)
return http2Stream._connection.FlushAsync(cancellationToken);
}
}

public sealed class Http2ReadWriteStream : HttpBaseStream
{
private Http2Stream? _http2Stream;
// should be removed
private readonly HttpResponseMessage _responseMessage;
// need to handle data flow

public Http2ReadWriteStream(Http2Stream http2Stream)
{
Debug.Assert(http2Stream != null);
Debug.Assert(http2Stream._response != null);
_http2Stream = http2Stream;
_responseMessage = _http2Stream._response;
}

~Http2ReadWriteStream()
{
if (NetEventSource.Log.IsEnabled()) _http2Stream?.Trace("");
try
{
Dispose(disposing: false);
}
catch (Exception e)
{
if (NetEventSource.Log.IsEnabled()) _http2Stream?.Trace($"Error: {e}");
}
}

protected override void Dispose(bool disposing)
{
Http2Stream? http2Stream = Interlocked.Exchange(ref _http2Stream, null);
if (http2Stream == null)
{
return;
}

// Technically we shouldn't be doing the following work when disposing == false,
// as the following work relies on other finalizable objects. But given the HTTP/2
// protocol, we have little choice: if someone drops the Http2ReadStream without
// disposing of it, we need to a) signal to the server that the stream is being
// canceled, and b) clean up the associated state in the Http2Connection.

http2Stream.CloseResponseBody();

base.Dispose(disposing);
}

public override bool CanRead => _http2Stream != null;
public override bool CanWrite => _http2Stream != null;

public override int Read(Span<byte> destination)
{
Http2Stream http2Stream = _http2Stream ?? throw new ObjectDisposedException(nameof(Http2ReadStream));

return http2Stream.ReadData(destination, _responseMessage);
}

public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken)
{
Http2Stream? http2Stream = _http2Stream;

if (http2Stream == null)
{
return ValueTask.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2ReadStream))));
}

if (cancellationToken.IsCancellationRequested)
{
return ValueTask.FromCanceled<int>(cancellationToken);
}

return http2Stream.ReadDataAsync(destination, _responseMessage, cancellationToken);
}

public override void CopyTo(Stream destination, int bufferSize)
{
ValidateCopyToArguments(destination, bufferSize);
Http2Stream http2Stream = _http2Stream ?? throw ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2ReadStream)));
http2Stream.CopyTo(_responseMessage, destination, bufferSize);
}

public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
ValidateCopyToArguments(destination, bufferSize);
Http2Stream? http2Stream = _http2Stream;
return
http2Stream is null ? Task.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2ReadStream)))) :
cancellationToken.IsCancellationRequested ? Task.FromCanceled<int>(cancellationToken) :
http2Stream.CopyToAsync(_responseMessage, destination, bufferSize, cancellationToken);
}

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
{

Http2Stream? http2Stream = _http2Stream;

if (http2Stream == null)
{
return ValueTask.FromException(new ObjectDisposedException(nameof(Http2WriteStream)));
}

return http2Stream.SendDataAsync(buffer, cancellationToken);
}

public override Task FlushAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

Http2Stream? http2Stream = _http2Stream;

if (http2Stream == null)
{
return Task.CompletedTask;
}

// In order to flush this stream's previous writes, we need to flush the connection. We
// really only need to do any work here if the connection's buffer has any pending writes
// from this stream, but we currently lack a good/efficient/safe way of doing that.
return http2Stream._connection.FlushAsync(cancellationToken);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ public async ValueTask<HttpResponseMessage> SendWithVersionDetectionAndRetryAsyn
// Use HTTP/3 if possible.
if (IsHttp3Supported() && // guard to enable trimming HTTP/3 support
_http3Enabled &&
!request.IsWebSocketH2Request() &&
(request.Version.Major >= 3 || (request.VersionPolicy == HttpVersionPolicy.RequestVersionOrHigher && IsSecure)))
{
Debug.Assert(async);
Expand All @@ -1021,7 +1022,16 @@ public async ValueTask<HttpResponseMessage> SendWithVersionDetectionAndRetryAsyn
Debug.Assert(connection is not null || !_http2Enabled);
if (connection is not null)
{
response = await connection.SendAsync(request, async, cancellationToken).ConfigureAwait(false);
if (!request.IsWebSocketH2Request() || connection.IsConnectEnabled)
{
response = await connection.SendAsync(request, async, cancellationToken).ConfigureAwait(false);
}
else if (request.IsWebSocketH2Request() && !connection.IsConnectEnabled)
{
HttpRequestException exception = new("Extended CONNECT is not supported");
exception.Data["SETTINGS_ENABLE_CONNECT_PROTOCOL"] = false;
throw exception;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// ------------------------------------------------------------------------------
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

namespace System.Net.WebSockets
{
public sealed partial class ClientWebSocket : System.Net.WebSockets.WebSocket
Expand All @@ -18,6 +17,7 @@ public override void Abort() { }
public override System.Threading.Tasks.Task CloseAsync(System.Net.WebSockets.WebSocketCloseStatus closeStatus, string? statusDescription, System.Threading.CancellationToken cancellationToken) { throw null; }
public override System.Threading.Tasks.Task CloseOutputAsync(System.Net.WebSockets.WebSocketCloseStatus closeStatus, string? statusDescription, System.Threading.CancellationToken cancellationToken) { throw null; }
public System.Threading.Tasks.Task ConnectAsync(System.Uri uri, System.Threading.CancellationToken cancellationToken) { throw null; }
public System.Threading.Tasks.Task ConnectAsync(System.Uri uri, System.Net.Http.HttpMessageInvoker? invoker, System.Threading.CancellationToken cancellationToken) { throw null; }
public override void Dispose() { }
public override System.Threading.Tasks.Task<System.Net.WebSockets.WebSocketReceiveResult> ReceiveAsync(System.ArraySegment<byte> buffer, System.Threading.CancellationToken cancellationToken) { throw null; }
public override System.Threading.Tasks.ValueTask<System.Net.WebSockets.ValueWebSocketReceiveResult> ReceiveAsync(System.Memory<byte> buffer, System.Threading.CancellationToken cancellationToken) { throw null; }
Expand All @@ -43,6 +43,12 @@ internal ClientWebSocketOptions() { }
public System.Net.Security.RemoteCertificateValidationCallback? RemoteCertificateValidationCallback { get { throw null; } set { } }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public bool UseDefaultCredentials { get { throw null; } set { } }
public System.Version HttpVersion { get { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
set { } }
public System.Net.Http.HttpVersionPolicy HttpVersionPolicy { get { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
set { } }
public void AddSubProtocol(string subProtocol) { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public void SetBuffer(int receiveBufferSize, int sendBufferSize) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Http\ref\System.Net.Http.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Primitives\ref\System.Net.Primitives.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Security\ref\System.Net.Security.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.WebSockets\ref\System.Net.WebSockets.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Runtime.Versioning;
using System.Security.Cryptography.X509Certificates;

Expand All @@ -12,6 +13,8 @@ public sealed class ClientWebSocketOptions
{
private bool _isReadOnly; // After ConnectAsync is called the options cannot be modified.
private List<string>? _requestedSubProtocols;
private Version _version = Net.HttpVersion.Version11;
private HttpVersionPolicy _versionPolicy = HttpVersionPolicy.RequestVersionOrLower;

internal ClientWebSocketOptions()
{ }
Expand All @@ -32,6 +35,20 @@ public bool UseDefaultCredentials
set => throw new PlatformNotSupportedException();
}

public Version HttpVersion
{
get => _version;
[UnsupportedOSPlatform("browser")]
set => throw new PlatformNotSupportedException();
}

public System.Net.Http.HttpVersionPolicy HttpVersionPolicy
{
get => _versionPolicy;
[UnsupportedOSPlatform("browser")]
set => throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("browser")]
public System.Net.ICredentials Credentials
{
Expand Down
Loading