private async Task<RTHttpRequestMessage> ConvertRequestAsync(HttpRequestMessage request)
{
RTHttpRequestMessage rtRequest = new RTHttpRequestMessage(new RTHttpMethod(request.Method.Method), request.RequestUri);
// We can only control the Version on the first request message since the WinRT API
// has this property designed as a filter/handler property. In addition the overall design
// of HTTP/2.0 is such that once the first request is using it, all the other requests
// to the same endpoint will use it as well.
if (Interlocked.Exchange(ref _filterMaxVersionSet, 1) == 0)
{
RTHttpVersion maxVersion;
if (request.Version == HttpVersionInternal.Version20)
{
maxVersion = RTHttpVersion.Http20;
}
else if (request.Version == HttpVersionInternal.Version11)
{
maxVersion = RTHttpVersion.Http11;
}
else if (request.Version == HttpVersionInternal.Version10)
{
maxVersion = RTHttpVersion.Http10;
}
else
{
// TODO (#7878): We need to throw an exception here similar to .NET Desktop
// throw new ArgumentException(SR.GetString(SR.net_wrongversion), "value");
//
// But we need to do that checking in the HttpClientHandler object itself.
// and we have that bug as well for the WinHttpHandler version also.
maxVersion = RTHttpVersion.Http11;
}
// The default for WinRT HttpBaseProtocolFilter.MaxVersion is HttpVersion.Http20.
// So, we only have to change it if we don't want HTTP/2.0.
if (maxVersion != RTHttpVersion.Http20)
{
_next.MaxVersion = maxVersion;
}
}
// Headers
foreach (KeyValuePair<string, IEnumerable<string>> headerPair in request.Headers)
{
foreach (string value in headerPair.Value)
{
bool success = rtRequest.Headers.TryAppendWithoutValidation(headerPair.Key, value);
Debug.Assert(success);
}
}
// Properties
foreach (KeyValuePair<string, object> propertyPair in request.Properties)
{
rtRequest.Properties.Add(propertyPair.Key, propertyPair.Value);
}
// Content
if (request.Content != null)
{
rtRequest.Content = await CreateRequestContentAsync(request, rtRequest.Headers).ConfigureAwait(false);
}
return rtRequest;
}