Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion src/Servers/HttpSys/src/HttpSysListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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 +256,19 @@ 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();
}

return new HttpSysException(
(int)httpInitializeStatusCode,
$"HttpInitialize failed with status code 0x{httpInitializeStatusCode:X8}.");
Comment on lines +267 to +269
}

/// <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
28 changes: 28 additions & 0 deletions src/Servers/HttpSys/test/Tests/HttpSysListenerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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));

Assert.Contains($"status code 0x{errorCode:X8}", exception.Message);
Comment on lines +11 to +16
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