Skip to content

Commit

Permalink
Fix content length calculation and disable request buffering (Azure#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Oct 16, 2020
1 parent dd975dd commit 4e52164
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions sdk/core/Azure.Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Fixed
- The race condition in `AzureEventSourceListener` class that sometimes resulted in a `NullReferenceException` in the `EventSource`.
- The overflow exception when content length is larger than `int.MaxValue`.

## 1.5.1 (2020-10-01)

Expand Down
9 changes: 8 additions & 1 deletion sdk/core/Azure.Core/src/Pipeline/HttpWebRequestTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ private HttpWebRequest CreateRequest(Request messageRequest)
var request = WebRequest.CreateHttp(messageRequest.Uri.ToUri());
request.Method = messageRequest.Method.Method;
request.Proxy = _proxy;

foreach (var messageRequestHeader in messageRequest.Headers)
{
if (string.Equals(messageRequestHeader.Name, HttpHeader.Names.ContentLength, StringComparison.OrdinalIgnoreCase))
{
request.ContentLength = int.Parse(messageRequestHeader.Value, CultureInfo.InvariantCulture);
request.ContentLength = long.Parse(messageRequestHeader.Value, CultureInfo.InvariantCulture);
continue;
}

Expand Down Expand Up @@ -180,6 +181,12 @@ private HttpWebRequest CreateRequest(Request messageRequest)
request.Headers.Add(messageRequestHeader.Name, messageRequestHeader.Value);
}

if (request.ContentLength != -1)
{
// disable buffering when the content length is known
// as the content stream is re-playable and we don't want to allocate extra buffers
request.AllowWriteStreamBuffering = false;
}
return request;
}

Expand Down
6 changes: 5 additions & 1 deletion sdk/core/Azure.Core/tests/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public TestServer(RequestDelegate app)
{
_app = app;
_host = new WebHostBuilder()
.UseKestrel(options => options.Listen(new IPEndPoint(IPAddress.Loopback, 0)))
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
options.Listen(new IPEndPoint(IPAddress.Loopback, 0));
})
.ConfigureServices(services =>
{
services.AddSingleton<IStartup>(this);
Expand Down
29 changes: 29 additions & 0 deletions sdk/core/Azure.Core/tests/TransportFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ public async Task SettingHeaderOverridesDefaultContentLength()
Assert.AreEqual(50, contentLength);
}

[Test]
public async Task CanSetContentLenghtOverMaxInt()
{
long contentLength = 0;
using TestServer testServer = new TestServer(
context =>
{
contentLength = context.Request.ContentLength.Value;
});

var requestContentLength = long.MaxValue;
var transport = GetTransport();
Request request = transport.CreateRequest();
request.Method = RequestMethod.Post;
request.Uri.Reset(testServer.Address);
request.Content = RequestContent.Create(new byte[10]);
request.Headers.Add("Content-Length", requestContentLength.ToString());

try
{
await ExecuteRequest(request, transport);
}
catch (Exception)
{
// Sending the request would fail because of length mismatch
}

Assert.AreEqual(requestContentLength, requestContentLength);
}

[Test]
public async Task HostHeaderSetFromUri()
Expand Down

0 comments on commit 4e52164

Please sign in to comment.