Skip to content
Open
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
23 changes: 22 additions & 1 deletion src/Servers/HttpSys/src/HttpSysListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ internal sealed partial class HttpSysListener : IDisposable
// with hash collisions will cause the server to consume excess CPU. 1000 headers limits CPU time to under
// 0.5 seconds per request. Respond with a 400 Bad Request.
private const int UnknownHeaderLimit = 1000;
private const uint Win32FacilityHResultBase = 0x80070000u;
private const uint Win32ErrorCodeMask = 0x0000FFFF;

internal MemoryPool<byte> MemoryPool { get; }

Expand All @@ -52,7 +54,7 @@ public HttpSysListener(HttpSysOptions options, IMemoryPoolFactory<byte> memoryPo

if (!HttpApi.Supported)
{
throw new PlatformNotSupportedException();
throw CreateHttpInitializeFailureException(HttpApi.HttpInitializeStatusCode);
}

MemoryPool = memoryPoolFactory.Create(new MemoryPoolOptions { Owner = "httpsys" });
Expand Down Expand Up @@ -256,6 +258,25 @@ private void DisposeInternal()
_serverSession.Dispose();
}

internal static Exception CreateHttpInitializeFailureException(uint httpInitializeStatusCode)
{
if (httpInitializeStatusCode == ErrorCodes.ERROR_SUCCESS)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment here?

{
// Keep the existing PlatformNotSupportedException behavior when no specific native error is available.
return new PlatformNotSupportedException();
}

var hResult = CreateHResultFromWin32Error(httpInitializeStatusCode);
return new HttpSysException(
(int)httpInitializeStatusCode,
$"HttpInitialize failed with status code 0x{httpInitializeStatusCode:X8} (HRESULT 0x{hResult:X8}).");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot I don't think the HResult part is needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 502f04ff: removed the HRESULT text from the HttpInitialize failure message and updated the related HttpSysListenerTests assertions to only validate the status code and exception type/error code.

}

internal static int CreateHResultFromWin32Error(uint statusCode)
{
return unchecked((int)(Win32FacilityHResultBase | (statusCode & Win32ErrorCodeMask)));
}

/// <summary>
/// Accept a request from the incoming request queue.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Servers/HttpSys/src/NativeInterop/HttpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ internal static unsafe uint HttpSetRequestProperty(SafeHandle requestQueueHandle
internal static bool SupportsClientHello { get; }
internal static bool SupportsQueryTlsCipherInfo { get; }
internal static bool Supported { get; }
internal static uint HttpInitializeStatusCode { get; }

static unsafe HttpApi()
{
var statusCode = PInvoke.HttpInitialize(Version, HTTP_INITIALIZE.HTTP_INITIALIZE_SERVER | HTTP_INITIALIZE.HTTP_INITIALIZE_CONFIG);
HttpInitializeStatusCode = statusCode;

if (statusCode == ErrorCodes.ERROR_SUCCESS)
{
Expand Down
30 changes: 30 additions & 0 deletions src/Servers/HttpSys/test/Tests/HttpSysListenerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace Microsoft.AspNetCore.Server.HttpSys;

public class HttpSysListenerTests
{
[Fact]
public void CreateHttpInitializeFailureException_WithErrorCode_ReturnsHttpSysExceptionWithDetails()
{
var errorCode = ErrorCodes.ERROR_ACCESS_DENIED;
var exception = Assert.IsType<HttpSysException>(HttpSysListener.CreateHttpInitializeFailureException(errorCode));
var expectedHResult = HttpSysListener.CreateHResultFromWin32Error(errorCode);

Assert.Contains($"status code 0x{errorCode:X8}", exception.Message);
Comment on lines +11 to +16
Assert.Contains($"HRESULT 0x{expectedHResult:X8}", exception.Message);
Assert.Equal((int)errorCode, exception.NativeErrorCode);
}

[Fact]
public void CreateHttpInitializeFailureException_WithSuccessCode_ReturnsExceptionWithoutInnerException()
{
var exception = HttpSysListener.CreateHttpInitializeFailureException(ErrorCodes.ERROR_SUCCESS);

Assert.IsType<PlatformNotSupportedException>(exception);
Assert.Null(exception.InnerException);
}
}
Loading