private static void ParseResponseHeaders(
SafeWinHttpHandle requestHandle,
HttpResponseMessage response,
char[] buffer,
bool stripEncodingHeaders)
{
HttpResponseHeaders responseHeaders = response.Headers;
HttpContentHeaders contentHeaders = response.Content.Headers;
int bufferLength = GetResponseHeader(
requestHandle,
Interop.WinHttp.WINHTTP_QUERY_RAW_HEADERS_CRLF,
buffer);
var reader = new WinHttpResponseHeaderReader(buffer, 0, bufferLength);
// Skip the first line which contains status code, etc. information that we already parsed.
reader.ReadLine();
// Parse the array of headers and split them between Content headers and Response headers.
string headerName;
string headerValue;
while (reader.ReadHeader(out headerName, out headerValue))
{
if (!responseHeaders.TryAddWithoutValidation(headerName, headerValue))
{
if (stripEncodingHeaders)
{
// Remove Content-Length and Content-Encoding headers if we are
// decompressing the response stream in the handler (due to
// WINHTTP not supporting it in a particular downlevel platform).
// This matches the behavior of WINHTTP when it does decompression itself.
if (string.Equals(HttpKnownHeaderNames.ContentLength, headerName, StringComparison.OrdinalIgnoreCase) ||
string.Equals(HttpKnownHeaderNames.ContentEncoding, headerName, StringComparison.OrdinalIgnoreCase))
{
continue;
}
}
// TODO: Issue #2165. Should we log if there is an error here?
contentHeaders.TryAddWithoutValidation(headerName, headerValue);
}
}
}