-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Throw HttpSysException with HttpInitialize status details #66859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
72786bb
359c2cc
64a280f
edcf167
fa3a04e
081a2a6
9426098
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; } | ||
|
|
||
|
|
@@ -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" }); | ||
|
|
@@ -256,6 +258,25 @@ private void DisposeInternal() | |
| _serverSession.Dispose(); | ||
| } | ||
|
|
||
| internal static Exception CreateHttpInitializeFailureException(uint httpInitializeStatusCode) | ||
| { | ||
| if (httpInitializeStatusCode == ErrorCodes.ERROR_SUCCESS) | ||
| { | ||
| // 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})."); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot I don't think the HResult part is needed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in 502f04ff: removed the HRESULT text from the |
||
| } | ||
|
|
||
| internal static int CreateHResultFromWin32Error(uint statusCode) | ||
| { | ||
| return unchecked((int)(Win32FacilityHResultBase | (statusCode & Win32ErrorCodeMask))); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Accept a request from the incoming request queue. | ||
| /// </summary> | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment here?