mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 12:49:35 -10:00
Merge pull request dotnet/corefx#3199 from justinvp/winhttphandler_parseresponseheaders
Reduce allocations when parsing response headers in WinHttpHandler Commit migrated from https://github.com/dotnet/corefx/commit/35f7ef05a8022358ec435152e7099d8263a4422a
This commit is contained in:
commit
52476494de
19 files changed
+1177
-247
No files matched your search
+55
-30
@@ -596,7 +596,7 @@ namespace System.Net.WebSockets
|
||||
return;
|
||||
}
|
||||
|
||||
_subProtocol = GetResponseHeaderStringInfo(HeaderNameWebSocketProtocol);
|
||||
_subProtocol = GetResponseHeader(HeaderNameWebSocketProtocol);
|
||||
}
|
||||
|
||||
private void AddRequestHeaders(Uri uri, ClientWebSocketOptions options)
|
||||
@@ -669,49 +669,74 @@ namespace System.Net.WebSockets
|
||||
return (HttpStatusCode)result;
|
||||
}
|
||||
|
||||
private string GetResponseHeaderStringInfo(string headerName)
|
||||
private unsafe string GetResponseHeader(string headerName, char[] buffer = null)
|
||||
{
|
||||
uint bytesNeeded = 0;
|
||||
bool results = false;
|
||||
const int StackLimit = 128;
|
||||
|
||||
// Call WinHttpQueryHeaders once to obtain the size of the buffer needed. The size is returned in
|
||||
// bytes but the API actually returns Unicode characters.
|
||||
if (!Interop.WinHttp.WinHttpQueryHeaders(
|
||||
_operation.RequestHandle,
|
||||
Interop.WinHttp.WINHTTP_QUERY_CUSTOM,
|
||||
headerName,
|
||||
null,
|
||||
ref bytesNeeded,
|
||||
IntPtr.Zero))
|
||||
Debug.Assert(buffer == null || (buffer != null && buffer.Length > StackLimit));
|
||||
|
||||
int bufferLength;
|
||||
|
||||
if (buffer == null)
|
||||
{
|
||||
int lastError = Marshal.GetLastWin32Error();
|
||||
if (lastError == Interop.WinHttp.ERROR_WINHTTP_HEADER_NOT_FOUND)
|
||||
bufferLength = StackLimit;
|
||||
char* pBuffer = stackalloc char[bufferLength];
|
||||
if (QueryHeaders(headerName, pBuffer, ref bufferLength))
|
||||
{
|
||||
return null;
|
||||
return new string(pBuffer, 0, bufferLength);
|
||||
}
|
||||
|
||||
if (lastError != Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER)
|
||||
}
|
||||
else
|
||||
{
|
||||
bufferLength = buffer.Length;
|
||||
fixed (char* pBuffer = buffer)
|
||||
{
|
||||
throw WinHttpException.CreateExceptionUsingError(lastError);
|
||||
if (QueryHeaders(headerName, pBuffer, ref bufferLength))
|
||||
{
|
||||
return new string(pBuffer, 0, bufferLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int charsNeeded = (int)bytesNeeded / sizeof(char);
|
||||
var buffer = new StringBuilder(charsNeeded, charsNeeded);
|
||||
int lastError = Marshal.GetLastWin32Error();
|
||||
|
||||
results = Interop.WinHttp.WinHttpQueryHeaders(
|
||||
if (lastError == Interop.WinHttp.ERROR_WINHTTP_HEADER_NOT_FOUND)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (lastError == Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER)
|
||||
{
|
||||
buffer = new char[bufferLength];
|
||||
return GetResponseHeader(headerName, buffer);
|
||||
}
|
||||
|
||||
throw WinHttpException.CreateExceptionUsingError(lastError);
|
||||
}
|
||||
|
||||
private unsafe bool QueryHeaders(string headerName, char* buffer, ref int bufferLength)
|
||||
{
|
||||
Debug.Assert(bufferLength >= 0, "bufferLength must not be negative.");
|
||||
|
||||
uint index = 0;
|
||||
|
||||
// Convert the char buffer length to the length in bytes.
|
||||
uint bufferLengthInBytes = (uint)bufferLength * sizeof(char);
|
||||
|
||||
// The WinHttpQueryHeaders buffer length is in bytes,
|
||||
// but the API actually returns Unicode characters.
|
||||
bool result = Interop.WinHttp.WinHttpQueryHeaders(
|
||||
_operation.RequestHandle,
|
||||
Interop.WinHttp.WINHTTP_QUERY_CUSTOM,
|
||||
headerName,
|
||||
buffer,
|
||||
ref bytesNeeded,
|
||||
IntPtr.Zero);
|
||||
if (!results)
|
||||
{
|
||||
WinHttpException.ThrowExceptionUsingLastError();
|
||||
}
|
||||
new IntPtr(buffer),
|
||||
ref bufferLengthInBytes,
|
||||
ref index);
|
||||
|
||||
return buffer.ToString();
|
||||
// Convert the byte buffer length back to the length in chars.
|
||||
bufferLength = (int)bufferLengthInBytes / sizeof(char);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
|
||||
Reference in new issue
Block a user