Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
16 changes: 15 additions & 1 deletion src/Servers/HttpSys/src/HttpSysListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -52,7 +53,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 +257,19 @@ private void DisposeInternal()
_serverSession.Dispose();
}

internal static PlatformNotSupportedException 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?

{
return new PlatformNotSupportedException();
}

var httpInitializeException = new Win32Exception((int)httpInitializeStatusCode);
return new PlatformNotSupportedException(
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.

Maybe should throw an HttpSysException

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.

Wonder if this should just throw the HttpSysException? Any reason to keep using PlatformNotSupportedException?

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 see above

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 6b01d35: for non-success HttpInitialize status codes, HttpSysListener now throws HttpSysException directly (with status code + HRESULT in the message). The ERROR_SUCCESS branch still falls back to PlatformNotSupportedException since there is no native failure code to surface.

$"HttpInitialize failed with status code 0x{httpInitializeStatusCode:X8} (HRESULT 0x{httpInitializeException.HResult:X8}).",
httpInitializeException);
}

/// <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
32 changes: 32 additions & 0 deletions src/Servers/HttpSys/test/Tests/HttpSysListenerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using Xunit;

namespace Microsoft.AspNetCore.Server.HttpSys;

public class HttpSysListenerTests
{
[Fact]
public void CreateHttpInitializeFailureException_WithErrorCode_IncludesWin32ExceptionDetails()
{
var errorCode = ErrorCodes.ERROR_ACCESS_DENIED;
var exception = HttpSysListener.CreateHttpInitializeFailureException(errorCode);
var expectedWin32Exception = new Win32Exception((int)errorCode);

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

var win32Exception = Assert.IsType<Win32Exception>(exception.InnerException);
Assert.Equal((int)errorCode, win32Exception.NativeErrorCode);
}

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

Assert.Null(exception.InnerException);
}
}
Loading