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>
/// Reads data from the Input pipe to the user.
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <param name="memory"></param>
/// <param name="cancellationToken"></param>
/// <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();
while (true)
@ -44,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{
if (!readableBuffer.IsEmpty)
{
var actual = Math.Min(readableBuffer.Length, count);
var actual = Math.Min(readableBuffer.Length, memory.Length);
readableBuffer = readableBuffer.Slice(0, actual);
readableBuffer.CopyTo(memory.Span);
return (int)actual;
@ -67,7 +53,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
/// <param name="memory"></param>
/// <param name="cancellationToken"></param>
/// <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,
@ -88,7 +74,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
internal Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
{
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)
{
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)

View File

@ -54,11 +54,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
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);
}

View File

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

View File

@ -464,22 +464,7 @@ namespace IISTestSite
app.Run(async context =>
{
var success = false;
if (context.Request.Path.StartsWithSegments("/NullBuffer"))
{
try
{
await context.Request.Body.ReadAsync(null, 0, 0);
}
catch (ArgumentNullException)
{
success = true;
}
catch (Exception)
{
success = true;
}
}
else if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
{
try
{
@ -534,21 +519,6 @@ namespace IISTestSite
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");
});
@ -558,18 +528,7 @@ namespace IISTestSite
app.Run(async context =>
{
var success = false;
if (context.Request.Path.StartsWithSegments("/NullBuffer"))
{
try
{
await context.Response.Body.WriteAsync(null, 0, 0);
}
catch (ArgumentNullException)
{
success = true;
}
}
else if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
if (context.Request.Path.StartsWithSegments("/InvalidOffsetSmall"))
{
try
{