Skip to content

Commit d734541

Browse files
authored
Update ITlsHandshakeFeature.HostName API + misc. improvements in HttpSys calls (#48788)
1 parent eccb892 commit d734541

File tree

10 files changed

+20
-18
lines changed

10 files changed

+20
-18
lines changed

src/Servers/Connections.Abstractions/src/Features/ITlsHandshakeFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface ITlsHandshakeFeature
2929
/// Gets the host name from the "server_name" (SNI) extension of the client hello if present.
3030
/// See <see href="https://www.rfc-editor.org/rfc/rfc6066#section-3">RFC 6066</see>.
3131
/// </summary>
32-
string? HostName => null;
32+
string HostName => string.Empty;
3333
#endif
3434

3535
/// <summary>

src/Servers/Connections.Abstractions/src/PublicAPI/net8.0/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Microsoft.AspNetCore.Connections.Features.IConnectionMetricsTagsFeature
66
Microsoft.AspNetCore.Connections.Features.IConnectionMetricsTagsFeature.Tags.get -> System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string!, object?>>!
77
Microsoft.AspNetCore.Connections.Features.IConnectionNamedPipeFeature
88
Microsoft.AspNetCore.Connections.Features.IConnectionNamedPipeFeature.NamedPipe.get -> System.IO.Pipes.NamedPipeServerStream!
9-
Microsoft.AspNetCore.Connections.Features.ITlsHandshakeFeature.HostName.get -> string?
9+
Microsoft.AspNetCore.Connections.Features.ITlsHandshakeFeature.HostName.get -> string!
1010
Microsoft.AspNetCore.Connections.Features.ITlsHandshakeFeature.NegotiatedCipherSuite.get -> System.Net.Security.TlsCipherSuite?
1111
Microsoft.AspNetCore.Connections.IConnectionListenerFactorySelector
1212
Microsoft.AspNetCore.Connections.IConnectionListenerFactorySelector.CanBind(System.Net.EndPoint! endpoint) -> bool

src/Servers/HttpSys/src/NativeInterop/HttpApi.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ internal static unsafe partial uint HttpCreateRequestQueue(HTTPAPI_VERSION versi
7979

8080
[LibraryImport(HTTPAPI, SetLastError = true)]
8181
internal static unsafe partial uint HttpDelegateRequestEx(SafeHandle pReqQueueHandle, SafeHandle pDelegateQueueHandle, ulong requestId,
82-
ulong delegateUrlGroupId, ulong propertyInfoSetSize, HTTP_DELEGATE_REQUEST_PROPERTY_INFO* pRequestPropertyBuffer);
82+
ulong delegateUrlGroupId, uint propertyInfoSetSize, HTTP_DELEGATE_REQUEST_PROPERTY_INFO* pRequestPropertyBuffer);
8383

84-
[LibraryImport(HTTPAPI, SetLastError = true)]
85-
internal static unsafe partial uint HttpQueryRequestProperty(SafeHandle requestQueueHandle, ulong requestId, HTTP_REQUEST_PROPERTY propertyId,
86-
void* qualifier, ulong qualifierSize, void* output, ulong outputSize, ulong* bytesReturned, IntPtr overlapped);
84+
internal delegate uint HttpGetRequestPropertyInvoker(SafeHandle requestQueueHandle, ulong requestId, HTTP_REQUEST_PROPERTY propertyId,
85+
void* qualifier, uint qualifierSize, void* output, uint outputSize, uint* bytesReturned, IntPtr overlapped);
8786

8887
internal delegate uint HttpSetRequestPropertyInvoker(SafeHandle requestQueueHandle, ulong requestId, HTTP_REQUEST_PROPERTY propertyId, void* input, uint inputSize, IntPtr overlapped);
8988

@@ -123,6 +122,7 @@ internal static HTTP_API_VERSION ApiVersion
123122
}
124123

125124
internal static SafeLibraryHandle? HttpApiModule { get; private set; }
125+
internal static HttpGetRequestPropertyInvoker? HttpGetRequestProperty { get; private set; }
126126
internal static HttpSetRequestPropertyInvoker? HttpSetRequestProperty { get; private set; }
127127
[MemberNotNullWhen(true, nameof(HttpSetRequestProperty))]
128128
internal static bool SupportsTrailers { get; private set; }
@@ -147,6 +147,7 @@ private static void InitHttpApi(ushort majorVersion, ushort minorVersion)
147147
if (supported)
148148
{
149149
HttpApiModule = SafeLibraryHandle.Open(HTTPAPI);
150+
HttpGetRequestProperty = HttpApiModule.GetProcAddress<HttpGetRequestPropertyInvoker>("HttpQueryRequestProperty", throwIfNotFound: false);
150151
HttpSetRequestProperty = HttpApiModule.GetProcAddress<HttpSetRequestPropertyInvoker>("HttpSetRequestProperty", throwIfNotFound: false);
151152
SupportsReset = HttpSetRequestProperty != null;
152153
SupportsTrailers = IsFeatureSupported(HTTP_FEATURE_ID.HttpFeatureResponseTrailers);

src/Servers/HttpSys/src/RequestProcessing/Request.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ internal Request(RequestContext requestContext)
165165

166166
User = RequestContext.GetUser();
167167

168+
SniHostName = string.Empty;
168169
if (IsHttps)
169170
{
170171
GetTlsHandshakeResults();
@@ -323,7 +324,7 @@ private AspNetCore.HttpSys.Internal.SocketAddress LocalEndPoint
323324

324325
internal WindowsPrincipal User { get; }
325326

326-
public string? SniHostName { get; private set; }
327+
public string SniHostName { get; private set; }
327328

328329
public SslProtocols Protocol { get; private set; }
329330

src/Servers/HttpSys/src/RequestProcessing/RequestContext.FeatureCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ bool IHttpBodyControlFeature.AllowSynchronousIO
587587

588588
int ITlsHandshakeFeature.KeyExchangeStrength => Request.KeyExchangeStrength;
589589

590-
string? ITlsHandshakeFeature.HostName => Request.SniHostName;
590+
string ITlsHandshakeFeature.HostName => Request.SniHostName;
591591

592592
IHeaderDictionary IHttpResponseTrailersFeature.Trailers
593593
{

src/Servers/HttpSys/src/RequestProcessing/RequestContext.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.AspNetCore.Http;
99
using Microsoft.AspNetCore.HttpSys.Internal;
1010
using Microsoft.Extensions.Logging;
11-
using static Microsoft.AspNetCore.HttpSys.Internal.UnsafeNclNativeMethods;
1211

1312
namespace Microsoft.AspNetCore.Server.HttpSys;
1413

@@ -228,25 +227,28 @@ internal void ForceCancelRequest()
228227

229228
internal unsafe HttpApiTypes.HTTP_REQUEST_PROPERTY_SNI GetClientSni()
230229
{
230+
if (HttpApi.HttpGetRequestProperty != null)
231+
{
231232
var buffer = new byte[HttpApiTypes.SniPropertySizeInBytes];
232233
fixed (byte* pBuffer = buffer)
233234
{
234-
var statusCode = HttpApi.HttpQueryRequestProperty(
235+
var statusCode = HttpApi.HttpGetRequestProperty(
235236
Server.RequestQueue.Handle,
236237
RequestId,
237238
HttpApiTypes.HTTP_REQUEST_PROPERTY.HttpRequestPropertySni,
238239
qualifier: null,
239240
qualifierSize: 0,
240241
(void*)pBuffer,
241-
(ulong)buffer.Length,
242+
(uint)buffer.Length,
242243
bytesReturned: null,
243244
IntPtr.Zero);
244245

245-
if (statusCode == ErrorCodes.ERROR_SUCCESS)
246+
if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS)
246247
{
247248
return Marshal.PtrToStructure<HttpApiTypes.HTTP_REQUEST_PROPERTY_SNI>((IntPtr)pBuffer);
248249
}
249250
}
251+
}
250252

251253
return default;
252254
}

src/Servers/HttpSys/test/FunctionalTests/HttpsTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Data.Common;
65
using System.Diagnostics;
76
using System.IO;
87
using System.Net.Http;

src/Servers/Kestrel/Core/src/Internal/TlsConnectionFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public X509Certificate2? ClientCertificate
4242
}
4343
}
4444

45-
public string? HostName { get; set; }
45+
public string HostName { get; set; } = string.Empty;
4646

4747
public ReadOnlyMemory<byte> ApplicationProtocol => _sslStream.NegotiatedApplicationProtocol.Protocol;
4848

src/Servers/Kestrel/Core/src/Middleware/HttpsConnectionMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private Task DoOptionsBasedHandshakeAsync(ConnectionContext context, SslStream s
318318
{
319319
selector = (sender, name) =>
320320
{
321-
feature.HostName = name;
321+
feature.HostName = name ?? string.Empty;
322322
var cert = _serverCertificateSelector(context, name);
323323
if (cert != null)
324324
{

src/Shared/HttpSys/NativeInterop/HttpApiTypes.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using System.Runtime.CompilerServices;
87
using System.Runtime.InteropServices;
98
using System.Security.Authentication;
109
using Microsoft.AspNetCore.Http;
@@ -109,8 +108,8 @@ internal struct HTTP_REQUEST_PROPERTY_STREAM_ERROR
109108
internal uint ErrorCode;
110109
}
111110

112-
internal const int HTTP_REQUEST_PROPERTY_SNI_HOST_MAX_LENGTH = 255;
113-
internal const int SniPropertySizeInBytes = (sizeof(ushort) * (HTTP_REQUEST_PROPERTY_SNI_HOST_MAX_LENGTH + 1)) + sizeof(ulong);
111+
private const int HTTP_REQUEST_PROPERTY_SNI_HOST_MAX_LENGTH = 255;
112+
internal const int SniPropertySizeInBytes = (sizeof(ushort) * (HTTP_REQUEST_PROPERTY_SNI_HOST_MAX_LENGTH + 1)) + sizeof(uint);
114113

115114
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Size = SniPropertySizeInBytes)]
116115
internal struct HTTP_REQUEST_PROPERTY_SNI

0 commit comments

Comments
 (0)