Skip to content

Commit 946440a

Browse files
committed
PR review
1 parent 7566aec commit 946440a

File tree

7 files changed

+28
-29
lines changed

7 files changed

+28
-29
lines changed

src/Servers/HttpSys/src/HttpSysListener.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal sealed partial class HttpSysListener : IDisposable
4242
private readonly UrlGroup _urlGroup;
4343
private readonly RequestQueue _requestQueue;
4444
private readonly DisconnectListener _disconnectListener;
45-
private readonly TlsListener _tlsListener;
45+
private readonly TlsListener? _tlsListener;
4646

4747
private readonly object _internalLock;
4848

@@ -73,13 +73,19 @@ public HttpSysListener(HttpSysOptions options, ILoggerFactory loggerFactory)
7373
try
7474
{
7575
_serverSession = new ServerSession();
76-
7776
_requestQueue = new RequestQueue(options.RequestQueueName, options.RequestQueueMode, Logger);
78-
7977
_urlGroup = new UrlGroup(_serverSession, _requestQueue, Logger);
8078

81-
_tlsListener = new TlsListener(options.TlsClientHelloBytesCallback);
82-
_disconnectListener = new DisconnectListener(_requestQueue, Logger, onDisconnect: _tlsListener.ConnectionClosed);
79+
if (options.TlsClientHelloBytesCallback is not null)
80+
{
81+
_tlsListener = new TlsListener(options.TlsClientHelloBytesCallback);
82+
_disconnectListener = new DisconnectListener(_requestQueue, Logger, onDisconnect: _tlsListener.ConnectionClosed);
83+
}
84+
else
85+
{
86+
_tlsListener = null;
87+
_disconnectListener = new DisconnectListener(_requestQueue, Logger);
88+
}
8389
}
8490
catch (Exception exception)
8591
{
@@ -103,7 +109,7 @@ internal enum State
103109

104110
internal UrlGroup UrlGroup => _urlGroup;
105111
internal RequestQueue RequestQueue => _requestQueue;
106-
internal TlsListener TlsListener => _tlsListener;
112+
internal TlsListener? TlsListener => _tlsListener;
107113
internal DisconnectListener DisconnectListener => _disconnectListener;
108114

109115
public HttpSysOptions Options { get; }

src/Servers/HttpSys/src/HttpSysOptions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,20 @@ public Http503VerbosityLevel Http503Verbosity
237237
/// Configures request headers to use <see cref="Encoding.Latin1"/> encoding.
238238
/// </summary>
239239
/// <remarks>
240-
/// Defaults to `false`, in which case <see cref="Encoding.UTF8"/> will be used. />.
240+
/// Defaults to <c>false</c>, in which case <see cref="Encoding.UTF8"/> will be used. />.
241241
/// </remarks>
242242
public bool UseLatin1RequestHeaders { get; set; }
243243

244244
/// <summary>
245245
/// A callback to be invoked to get the TLS client hello bytes.
246246
/// By default is null, and will not be invoked if is null.
247247
/// </summary>
248-
public Action<IFeatureCollection, ReadOnlySpan<byte>> TlsClientHelloBytesCallback { get; set; } = null!;
248+
/// <remarks>
249+
/// Works only if <c>HTTP_SERVICE_CONFIG_SSL_FLAG_ENABLE_CACHE_CLIENT_HELLO</c> flag is set on http.sys service configuration.
250+
/// See <see href="https://learn.microsoft.com/windows/win32/api/http/nf-http-httpsetserviceconfiguration"/>
251+
/// and <see href="https://learn.microsoft.com/windows/win32/api/http/ne-http-http_service_config_id"/>
252+
/// </remarks>
253+
public Action<IFeatureCollection, ReadOnlySpan<byte>>? TlsClientHelloBytesCallback { get; set; }
249254

250255
// Not called when attaching to an existing queue.
251256
internal void Apply(UrlGroup urlGroup, RequestQueue? requestQueue)

src/Servers/HttpSys/src/NativeInterop/DisconnectListener.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ internal sealed partial class DisconnectListener
1414
private readonly RequestQueue _requestQueue;
1515
private readonly ILogger _logger;
1616

17-
internal Action<ulong> OnDisconnect { get; }
17+
internal Action<ulong>? OnDisconnect { get; }
1818

19-
internal DisconnectListener(RequestQueue requestQueue, ILogger logger, Action<ulong> onDisconnect)
19+
internal DisconnectListener(RequestQueue requestQueue, ILogger logger, Action<ulong>? onDisconnect = null)
2020
{
2121
_requestQueue = requestQueue;
2222
_logger = logger;
@@ -124,7 +124,7 @@ private bool TryRemoveConnectionCancellationToken(ulong connectionId, out Connec
124124
bool result;
125125
if (result = _connectionCancellationTokens.TryRemove(connectionId, out connectionCancellation!))
126126
{
127-
OnDisconnect(connectionId);
127+
OnDisconnect?.Invoke(connectionId);
128128
}
129129

130130
return result;

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ internal static partial class HttpApi
3434
[LibraryImport(api_ms_win_core_io_LIB, SetLastError = true)]
3535
internal static partial uint CancelIoEx(SafeHandle handle, SafeNativeOverlapped overlapped);
3636

37-
internal unsafe delegate uint HttpGetRequestPropertyInvoker(SafeHandle requestQueueHandle, ulong requestId, uint propertyId,
37+
internal unsafe delegate uint HttpGetRequestPropertyInvoker(SafeHandle requestQueueHandle, ulong requestId, HTTP_REQUEST_PROPERTY propertyId,
3838
void* qualifier, uint qualifierSize, void* output, uint outputSize, IntPtr bytesReturned, IntPtr overlapped);
3939

40-
internal unsafe delegate uint HttpSetRequestPropertyInvoker(SafeHandle requestQueueHandle, ulong requestId, int propertyId,
40+
internal unsafe delegate uint HttpSetRequestPropertyInvoker(SafeHandle requestQueueHandle, ulong requestId, HTTP_REQUEST_PROPERTY propertyId,
4141
void* input, uint inputSize, IntPtr overlapped);
4242

4343
// HTTP_PROPERTY_FLAGS.Present (1)
@@ -54,24 +54,12 @@ internal unsafe delegate uint HttpSetRequestPropertyInvoker(SafeHandle requestQu
5454

5555
internal static unsafe uint HttpGetRequestProperty(SafeHandle requestQueueHandle, ulong requestId, HTTP_REQUEST_PROPERTY propertyId,
5656
void* qualifier, uint qualifierSize, void* output, uint outputSize, IntPtr bytesReturned, IntPtr overlapped)
57-
{
58-
return HttpGetRequestProperty(requestQueueHandle, requestId, (uint)propertyId, qualifier, qualifierSize, output, outputSize, bytesReturned, overlapped);
59-
}
60-
61-
internal static unsafe uint HttpGetRequestProperty(SafeHandle requestQueueHandle, ulong requestId, uint propertyId,
62-
void* qualifier, uint qualifierSize, void* output, uint outputSize, IntPtr bytesReturned, IntPtr overlapped)
6357
{
6458
return HttpGetRequestInvoker!(requestQueueHandle, requestId, propertyId, qualifier, qualifierSize, output, outputSize, bytesReturned, overlapped);
6559
}
6660

6761
internal static unsafe uint HttpSetRequestProperty(SafeHandle requestQueueHandle, ulong requestId, HTTP_REQUEST_PROPERTY propertyId,
6862
void* input, uint inputSize, IntPtr overlapped)
69-
{
70-
return HttpSetRequestProperty(requestQueueHandle, requestId, (int)propertyId, input, inputSize, overlapped);
71-
}
72-
73-
internal static unsafe uint HttpSetRequestProperty(SafeHandle requestQueueHandle, ulong requestId, int propertyId,
74-
void* input, uint inputSize, IntPtr overlapped)
7563
{
7664
return HttpSetRequestInvoker!(requestQueueHandle, requestId, propertyId, input, inputSize, overlapped);
7765
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#nullable enable
2-
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.TlsClientHelloBytesCallback.get -> System.Action<Microsoft.AspNetCore.Http.Features.IFeatureCollection!, System.ReadOnlySpan<byte>>!
2+
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.TlsClientHelloBytesCallback.get -> System.Action<Microsoft.AspNetCore.Http.Features.IFeatureCollection!, System.ReadOnlySpan<byte>>?
33
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.TlsClientHelloBytesCallback.set -> void

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ internal unsafe bool GetAndInvokeTlsClientHelloMessageBytesCallback(IFeatureColl
247247
{
248248
statusCode = HttpApi.HttpGetRequestProperty(
249249
Server.RequestQueue.Handle, requestId,
250-
11 /* HTTP_REQUEST_PROPERTY.HttpRequestPropertyTlsClientHello */,
250+
(HTTP_REQUEST_PROPERTY)11 /* HTTP_REQUEST_PROPERTY.HttpRequestPropertyTlsClientHello */,
251251
qualifier: null, qualifierSize: 0,
252252
pBuffer, (uint)buffer.Length,
253253
bytesReturned: (IntPtr)bytesReturned, IntPtr.Zero);
@@ -277,7 +277,7 @@ internal unsafe bool GetAndInvokeTlsClientHelloMessageBytesCallback(IFeatureColl
277277
{
278278
statusCode = HttpApi.HttpGetRequestProperty(
279279
Server.RequestQueue.Handle, requestId,
280-
11 /* HTTP_REQUEST_PROPERTY.HttpRequestPropertyTlsClientHello */,
280+
(HTTP_REQUEST_PROPERTY)11 /* HTTP_REQUEST_PROPERTY.HttpRequestPropertyTlsClientHello */,
281281
qualifier: null, qualifierSize: 0,
282282
pBuffer, (uint)buffer.Length,
283283
bytesReturned: (IntPtr)bytesReturned, IntPtr.Zero);

src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override async Task ExecuteAsync()
4848
context = application.CreateContext(Features);
4949
try
5050
{
51-
if (Server.Options.TlsClientHelloBytesCallback is not null)
51+
if (Server.Options.TlsClientHelloBytesCallback is not null && Server.TlsListener is not null)
5252
{
5353
_ = DisconnectToken; // force disconnect to be followed. Required for TlsListener to keep cache about only active connections
5454
Server.TlsListener.InvokeTlsClientHelloCallback(Features, Request);

0 commit comments

Comments
 (0)