Cleanup ReadAsync calls, make IISHttpContext private. (#685)

This commit is contained in:
Justin Kotalik 2018-03-19 11:08:36 -07:00 committed by GitHub
parent d216fb14f5
commit 8c4a83dbbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 71 deletions

View File

@ -15,25 +15,11 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
/// <summary> /// <summary>
/// Reads data from the Input pipe to the user. /// Reads data from the Input pipe to the user.
/// </summary> /// </summary>
/// <param name="buffer"></param> /// <param name="memory"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
public async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) internal async Task<int> ReadAsync(Memory<byte> memory, CancellationToken cancellationToken)
{ {
// Start a task which will continuously call ReadFromIISAsync and WriteToIISAsync
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (count == 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
var memory = new Memory<byte>(buffer, offset, count);
StartProcessingRequestAndResponseBody(); StartProcessingRequestAndResponseBody();
while (true) while (true)
@ -44,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
if (!readableBuffer.IsEmpty) if (!readableBuffer.IsEmpty)
{ {
var actual = Math.Min(readableBuffer.Length, count); var actual = Math.Min(readableBuffer.Length, memory.Length);
readableBuffer = readableBuffer.Slice(0, actual); readableBuffer = readableBuffer.Slice(0, actual);
readableBuffer.CopyTo(memory.Span); readableBuffer.CopyTo(memory.Span);
return (int)actual; return (int)actual;
@ -67,7 +53,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
/// <param name="memory"></param> /// <param name="memory"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
public Task WriteAsync(ReadOnlyMemory<byte> memory, CancellationToken cancellationToken = default(CancellationToken)) internal Task WriteAsync(ReadOnlyMemory<byte> memory, CancellationToken cancellationToken = default(CancellationToken))
{ {
// Want to keep exceptions consistent, // Want to keep exceptions consistent,
@ -88,7 +74,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
/// </summary> /// </summary>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken)) internal Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
{ {
if (!_hasResponseStarted) if (!_hasResponseStarted)
{ {

View File

@ -39,7 +39,9 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
public override unsafe Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) public override unsafe Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{ {
return _httpContext.ReadAsync(buffer, offset, count, cancellationToken); var memory = new Memory<byte>(buffer, offset, count);
return _httpContext.ReadAsync(memory, cancellationToken);
} }
public override long Seek(long offset, SeekOrigin origin) public override long Seek(long offset, SeekOrigin origin)

View File

@ -54,11 +54,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
public override unsafe Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) public override unsafe Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{ {
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
return _httpContext.WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken); return _httpContext.WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken);
} }

View File

@ -26,13 +26,11 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
} }
[ConditionalTheory] [ConditionalTheory]
[InlineData("/NullBuffer")]
[InlineData("/InvalidOffsetSmall")] [InlineData("/InvalidOffsetSmall")]
[InlineData("/InvalidOffsetLarge")] [InlineData("/InvalidOffsetLarge")]
[InlineData("/InvalidCountSmall")] [InlineData("/InvalidCountSmall")]
[InlineData("/InvalidCountLarge")] [InlineData("/InvalidCountLarge")]
[InlineData("/InvalidCountWithOffset")] [InlineData("/InvalidCountWithOffset")]
[InlineData("/InvalidCountZeroRead")]
public async Task TestInvalidReadOperations(string operation) public async Task TestInvalidReadOperations(string operation)
{ {
var result = await _fixture.Client.GetStringAsync($"/TestInvalidReadOperations{operation}"); var result = await _fixture.Client.GetStringAsync($"/TestInvalidReadOperations{operation}");
@ -40,7 +38,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
} }
[ConditionalTheory] [ConditionalTheory]
[InlineData("/NullBuffer")]
[InlineData("/InvalidOffsetSmall")] [InlineData("/InvalidOffsetSmall")]
[InlineData("/InvalidOffsetLarge")] [InlineData("/InvalidOffsetLarge")]
[InlineData("/InvalidCountSmall")] [InlineData("/InvalidCountSmall")]

View File

@ -464,22 +464,7 @@ namespace IISTestSite
app.Run(async context => app.Run(async context =>
{ {
var success = false; var success = false;
if (context.Request.Path.StartsWithSegments("/NullBuffer")) if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
{
try
{
await context.Request.Body.ReadAsync(null, 0, 0);
}
catch (ArgumentNullException)
{
success = true;
}
catch (Exception)
{
success = true;
}
}
else if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
{ {
try try
{ {
@ -534,21 +519,6 @@ namespace IISTestSite
success = true; success = true;
} }
} }
else if (context.Request.Path.StartsWithSegments("/InvalidCountZeroRead"))
{
try
{
await context.Request.Body.ReadAsync(new byte[1], 0, 0);
}
catch (ArgumentOutOfRangeException)
{
success = true;
}
catch (Exception)
{
success = true;
}
}
await context.Response.WriteAsync(success ? "Success" : "Failure"); await context.Response.WriteAsync(success ? "Success" : "Failure");
}); });
@ -558,18 +528,7 @@ namespace IISTestSite
app.Run(async context => app.Run(async context =>
{ {
var success = false; var success = false;
if (context.Request.Path.StartsWithSegments("/NullBuffer")) if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
{
try
{
await context.Response.Body.WriteAsync(null, 0, 0);
}
catch (ArgumentNullException)
{
success = true;
}
}
else if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
{ {
try try
{ {