Skip to content
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

Add SslApplicationProtocol.Http3 #56775

Merged
merged 7 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Http3Options()
{
MaxUnidirectionalStreams = 100;
MaxBidirectionalStreams = 100;
Alpn = "h3";
Alpn = SslApplicationProtocol.Http3.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ namespace System.Net.Http
[SupportedOSPlatform("macos")]
internal sealed class Http3Connection : HttpConnectionBase
{
// TODO: once HTTP/3 is standardized, create APIs for this.
public static readonly SslApplicationProtocol Http3ApplicationProtocol = new SslApplicationProtocol("h3");

private readonly HttpConnectionPool _pool;
private readonly HttpAuthority? _origin;
private readonly HttpAuthority _authority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,10 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK
[SupportedOSPlatformGuard("Windows")]
internal static bool IsHttp3Supported() => (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS();

private static readonly List<SslApplicationProtocol> s_http3ApplicationProtocols = CreateHttp3ApplicationProtocols();
private static readonly List<SslApplicationProtocol> s_http3ApplicationProtocols = new List<SslApplicationProtocol>() { SslApplicationProtocol.Http3 };
private static readonly List<SslApplicationProtocol> s_http2ApplicationProtocols = new List<SslApplicationProtocol>() { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11 };
private static readonly List<SslApplicationProtocol> s_http2OnlyApplicationProtocols = new List<SslApplicationProtocol>() { SslApplicationProtocol.Http2 };

private static List<SslApplicationProtocol> CreateHttp3ApplicationProtocols()
{
if (IsHttp3Supported())
{
// TODO: Once the HTTP/3 versions are part of SslApplicationProtocol, see https://github.com/dotnet/runtime/issues/1293, move this back to field initialization.
return new List<SslApplicationProtocol>() { Http3Connection.Http3ApplicationProtocol };
}

return null!;
}

private static SslClientAuthenticationOptions ConstructSslOptions(HttpConnectionPoolManager poolManager, string sslHostName)
{
Debug.Assert(sslHostName != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public async Task Alpn_H3_Success()
return;
}

var options = new Http3Options() { Alpn = "h3" };
var options = new Http3Options() { Alpn = SslApplicationProtocol.Http3.ToString() };
using Http3LoopbackServer server = CreateHttp3LoopbackServer(options);

using var clientDone = new SemaphoreSlim(0);
Expand All @@ -650,7 +650,7 @@ public async Task Alpn_H3_Success()
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();

SslApplicationProtocol negotiatedAlpn = ExtractMsQuicNegotiatedAlpn(connection);
Assert.Equal(new SslApplicationProtocol("h3"), negotiatedAlpn);
Assert.Equal(SslApplicationProtocol.Http3, negotiatedAlpn);

using Http3LoopbackStream stream = await connection.AcceptRequestStreamAsync();
await stream.HandleRequestAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void ConfigureListenOptions(ListenOptions listenOptions)
{
host = host.UseQuic(options =>
{
options.Alpn = "h3";
options.Alpn = SslApplicationProtocol.Http3.ToString();
options.IdleTimeout = TimeSpan.FromMinutes(1);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/System.Net.Security/ref/System.Net.Security.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public readonly struct SslClientHelloInfo
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public static readonly System.Net.Security.SslApplicationProtocol Http11;
public static readonly System.Net.Security.SslApplicationProtocol Http3;
public static readonly System.Net.Security.SslApplicationProtocol Http2;
public static readonly System.Net.Security.SslApplicationProtocol Http11;
i3arnon marked this conversation as resolved.
Show resolved Hide resolved
public SslApplicationProtocol(byte[] protocol) { throw null; }
public SslApplicationProtocol(string protocol) { throw null; }
public System.ReadOnlyMemory<byte> Protocol { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ namespace System.Net.Security
public readonly struct SslApplicationProtocol : IEquatable<SslApplicationProtocol>
{
private static readonly Encoding s_utf8 = Encoding.GetEncoding(Encoding.UTF8.CodePage, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
private static readonly byte[] s_http3Utf8 = new byte[] { 0x68, 0x33 }; // "h3"
private static readonly byte[] s_http2Utf8 = new byte[] { 0x68, 0x32 }; // "h2"
private static readonly byte[] s_http11Utf8 = new byte[] { 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31 }; // "http/1.1"

// Refer to IANA on ApplicationProtocols: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
// h2
/// <summary>Defines a <see cref="SslApplicationProtocol"/> instance for HTTP 3.0.</summary>
public static readonly SslApplicationProtocol Http3 = new SslApplicationProtocol(s_http3Utf8, copy: false);
Copy link
Member

Choose a reason for hiding this comment

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

@karelz, should this require preview? Should

public static readonly Version Version30 = new Version(3, 0);
?

Copy link
Member

Choose a reason for hiding this comment

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

ATM, we don't do that for HttpVersion.Http3 since you can easily construct the version manually and this is very similar.

Either way, if we decide to annotate this, we should be consistent and do the same thing with Http3 version.

/// <summary>Defines a <see cref="SslApplicationProtocol"/> instance for HTTP 2.0.</summary>
public static readonly SslApplicationProtocol Http2 = new SslApplicationProtocol(s_http2Utf8, copy: false);
// http/1.1
/// <summary>Defines a <see cref="SslApplicationProtocol"/> instance for HTTP 1.1.</summary>
public static readonly SslApplicationProtocol Http11 = new SslApplicationProtocol(s_http11Utf8, copy: false);

private readonly byte[] _readOnlyProtocol;
Expand Down Expand Up @@ -77,6 +80,7 @@ public override string ToString()
{
return
arr is null ? string.Empty :
ReferenceEquals(arr, s_http3Utf8) ? "h3" :
ReferenceEquals(arr, s_http2Utf8) ? "h2" :
ReferenceEquals(arr, s_http11Utf8) ? "http/1.1" :
s_utf8.GetString(arr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SslApplicationProtocolTests
[Fact]
public void Constants_Values_AreCorrect()
{
Assert.Equal(new SslApplicationProtocol(new byte[] { 0x68, 0x33 }), SslApplicationProtocol.Http3);
Assert.Equal(new SslApplicationProtocol(new byte[] { 0x68, 0x32 }), SslApplicationProtocol.Http2);
Assert.Equal(new SslApplicationProtocol(new byte[] { 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31 }), SslApplicationProtocol.Http11);
}
Expand Down Expand Up @@ -75,6 +76,7 @@ public void ToString_Rendering_Succeeds()
{
Assert.Equal("http/1.1", SslApplicationProtocol.Http11.ToString());
Assert.Equal("h2", SslApplicationProtocol.Http2.ToString());
Assert.Equal("h3", SslApplicationProtocol.Http3.ToString());
Assert.Equal("hello", new SslApplicationProtocol("hello").ToString());
Assert.Equal("0x0b 0xee", new SslApplicationProtocol(new byte[] { 0x0B, 0xEE }).ToString());
Assert.Equal(string.Empty, default(SslApplicationProtocol).ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ApplicationProtocols_Get_Set_Succeeds()
Assert.Null(_clientOptions.ApplicationProtocols);
Assert.Null(_serverOptions.ApplicationProtocols);

List<SslApplicationProtocol> applnProtos = new List<SslApplicationProtocol> { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11 };
List<SslApplicationProtocol> applnProtos = new List<SslApplicationProtocol> { SslApplicationProtocol.Http3, SslApplicationProtocol.Http2, SslApplicationProtocol.Http11 };
_clientOptions.ApplicationProtocols = applnProtos;
_serverOptions.ApplicationProtocols = applnProtos;

Expand Down