Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Move ManagedHandler version/chunking validation earlier #26766

Merged
merged 1 commit into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 4 additions & 30 deletions src/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,38 +220,10 @@ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, Can
Debug.Assert(!_canRetry);
_canRetry = true;

// Send the request.
if (NetEventSource.IsEnabled) Trace($"Sending request: {request}");
try
{
// Our max supported version is 1.1, so if Version > 1.1, degrade to 1.1.
Debug.Assert(request.Version.Major >= 0 && request.Version.Minor >= 0); // guaranteed by Version class
bool isHttp10 = false;
if (request.Version.Major == 0)
{
throw new NotSupportedException(SR.net_http_unsupported_version);
}
else if (request.Version.Major == 1 && request.Version.Minor == 0)
{
isHttp10 = true;
}

// Send the request.
if (NetEventSource.IsEnabled) Trace($"Sending request: {request}");

// Add headers to define content transfer, if not present
if (request.Content != null &&
(!request.HasHeaders || request.Headers.TransferEncodingChunked != true) &&
request.Content.Headers.ContentLength == null)
{
// We have content, but neither Transfer-Encoding or Content-Length is set.
request.Headers.TransferEncodingChunked = true;
}

if (isHttp10 && request.HasHeaders && request.Headers.TransferEncodingChunked == true)
{
// HTTP 1.0 does not support chunking
throw new NotSupportedException(SR.net_http_unsupported_chunking);
}

// Write request line
await WriteStringAsync(request.Method.Method, cancellationToken).ConfigureAwait(false);
await WriteByteAsync((byte)' ', cancellationToken).ConfigureAwait(false);
Expand All @@ -267,6 +239,8 @@ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, Can
await WriteStringAsync(request.RequestUri.PathAndQuery, cancellationToken).ConfigureAwait(false);

// fall-back to 1.1 for all versions other than 1.0
Debug.Assert(request.Version.Major >= 0 && request.Version.Minor >= 0); // guaranteed by Version class
bool isHttp10 = request.Version.Minor == 0 && request.Version.Major == 1;
await WriteBytesAsync(isHttp10 ? s_spaceHttp10NewlineAsciiBytes : s_spaceHttp11NewlineAsciiBytes,
cancellationToken).ConfigureAwait(false);

Expand Down
22 changes: 22 additions & 0 deletions src/System.Net.Http/src/System/Net/Http/Managed/ManagedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ protected internal override Task<HttpResponseMessage> SendAsync(
{
CheckDisposed();
HttpMessageHandler handler = _handler ?? SetupHandlerChain();

if (request.Version.Major == 0)
{
return Task.FromException<HttpResponseMessage>(new NotSupportedException(SR.net_http_unsupported_version));
}

// Add headers to define content transfer, if not present
if (request.Content != null &&
(!request.HasHeaders || request.Headers.TransferEncodingChunked != true) &&
request.Content.Headers.ContentLength == null)
{
// We have content, but neither Transfer-Encoding or Content-Length is set.
request.Headers.TransferEncodingChunked = true;
}

if (request.Version.Minor == 0 && request.Version.Major == 1 &&
request.HasHeaders && request.Headers.TransferEncodingChunked == true)
{
// HTTP 1.0 does not support chunking
return Task.FromException<HttpResponseMessage>(new NotSupportedException(SR.net_http_unsupported_chunking));
}

return handler.SendAsync(request, cancellationToken);
}
}
Expand Down