Merge branch 'release/2.2'

This commit is contained in:
John Luo 2018-10-17 12:46:11 -07:00
commit e364177c5f
6 changed files with 73 additions and 38 deletions

View File

@ -126,8 +126,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core
/// <summary>
/// The protocols enabled on this endpoint.
/// </summary>
/// <remarks>Defaults to HTTP/1.x and HTTP/2.</remarks>
public HttpProtocols Protocols { get; set; } = HttpProtocols.Http1AndHttp2;
/// <remarks>Defaults to HTTP/1.x.</remarks>
public HttpProtocols Protocols { get; set; } = HttpProtocols.Http1;
/// <summary>
/// Gets the <see cref="List{IConnectionAdapter}"/> that allows each connection <see cref="System.IO.Stream"/>

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
public void ProtocolsDefault()
{
var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0));
Assert.Equal(HttpProtocols.Http1AndHttp2, listenOptions.Protocols);
Assert.Equal(HttpProtocols.Http1, listenOptions.Protocols);
}
[Fact]

View File

@ -67,10 +67,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
var buffer = new byte[1024];
try
{
await context.Request.Body.ReadUntilLengthAsync(buffer, 6, cts.Token).DefaultTimeout();
int read = await context.Request.Body.ReadAsync(buffer, 0, buffer.Length, cts.Token);
Assert.Equal("Hello ", Encoding.UTF8.GetString(buffer, 0, read));
Assert.Equal("Hello ", Encoding.ASCII.GetString(buffer, 0, 6));
helloTcs.TrySetResult(null);
}
@ -82,7 +81,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
try
{
await context.Request.Body.ReadAsync(buffer, 0, buffer.Length, cts.Token);
await context.Request.Body.ReadAsync(buffer, 0, 1, cts.Token).DefaultTimeout();
context.Response.ContentLength = 12;
await context.Response.WriteAsync("Read success");
@ -133,12 +132,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
{
var stream = await context.Features.Get<IHttpUpgradeFeature>().UpgradeAsync();
var data = new byte[3];
var bytesRead = 0;
while (bytesRead < 3)
{
bytesRead += await stream.ReadAsync(data, bytesRead, data.Length - bytesRead);
}
await stream.ReadUntilLengthAsync(data, 3).DefaultTimeout();
dataRead = Encoding.ASCII.GetString(data, 0, 3) == "abc";
}, new TestServiceContext(LoggerFactory)))
@ -284,9 +279,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
"",
"");
var read = await connection.Reader.ReadAsync(buffer, 0, identifierLength);
Assert.Equal(identifierLength, read);
var id = new string(buffer, 0, read);
var offset = 0;
while (offset < identifierLength)
{
var read = await connection.Reader.ReadAsync(buffer, offset, identifierLength - offset);
offset += read;
Assert.NotEqual(0, read);
}
Assert.Equal(identifierLength, offset);
var id = new string(buffer, 0, offset);
Assert.DoesNotContain(id, usedIds.ToArray());
usedIds.Add(id);
}
@ -670,13 +674,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
var duplexStream = await upgradeFeature.UpgradeAsync();
var buffer = new byte[message.Length];
var read = 0;
while (read < message.Length)
{
read += await duplexStream.ReadAsync(buffer, read, buffer.Length - read).DefaultTimeout();
}
await duplexStream.WriteAsync(buffer, 0, read);
await duplexStream.ReadUntilLengthAsync(buffer, message.Length).DefaultTimeout();
await duplexStream.WriteAsync(buffer, 0, buffer.Length);
}, testContext))
{
using (var connection = server.CreateConnection())
@ -1094,14 +1095,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
Assert.Equal(CoreStrings.SynchronousReadsDisallowed, ioEx2.Message);
var buffer = new byte[5];
var offset = 0;
while (offset < 5)
{
offset += await context.Request.Body.ReadAsync(buffer, offset, 5 - offset);
}
var read = await context.Request.Body.ReadUntilEndAsync(buffer).DefaultTimeout();
Assert.Equal(0, await context.Request.Body.ReadAsync(new byte[1], 0, 1));
Assert.Equal("Hello", Encoding.ASCII.GetString(buffer));
Assert.Equal("Hello", Encoding.ASCII.GetString(buffer, 0, read));
}, testContext))
{
using (var connection = server.CreateConnection())

View File

@ -385,7 +385,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Tests
serverOptions.ConfigureEndpointDefaults(opt =>
{
// Kestrel default.
Assert.Equal(HttpProtocols.Http1AndHttp2, opt.Protocols);
Assert.Equal(HttpProtocols.Http1, opt.Protocols);
ranDefault = true;
});
@ -415,14 +415,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Tests
.LocalhostEndpoint(5002, opt =>
{
// Kestrel default.
Assert.Equal(HttpProtocols.Http1AndHttp2, opt.Protocols);
Assert.Equal(HttpProtocols.Http1, opt.Protocols);
ran2 = true;
})
.Load();
serverOptions.ListenAnyIP(0, opt =>
{
// Kestrel default.
Assert.Equal(HttpProtocols.Http1AndHttp2, opt.Protocols);
Assert.Equal(HttpProtocols.Http1, opt.Protocols);
ran3 = true;
});

View File

@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO
{
public static class StreamFillBufferExtensions
{
public static async Task<int> ReadUntilEndAsync(this Stream stream, byte[] buffer, CancellationToken cancellationToken = default)
{
var offset = 0;
while (offset < buffer.Length)
{
var read = await stream.ReadAsync(buffer, offset, buffer.Length - offset, cancellationToken);
offset += read;
if (read == 0)
{
return offset;
}
}
Assert.Equal(0, await stream.ReadAsync(new byte[1], 0, 1, cancellationToken));
return offset;
}
public static async Task ReadUntilLengthAsync(this Stream stream, byte[] buffer, int length, CancellationToken cancellationToken = default)
{
var offset = 0;
while (offset < length)
{
var read = await stream.ReadAsync(buffer, offset, length - offset, cancellationToken);
offset += read;
Assert.NotEqual(0, read);
}
}
}
}

View File

@ -14,16 +14,10 @@ namespace Microsoft.AspNetCore.Testing
var request = httpContext.Request;
var response = httpContext.Response;
var buffer = new byte[httpContext.Request.ContentLength ?? 0];
var bytesRead = 0;
while (bytesRead < buffer.Length)
{
var count = await request.Body.ReadAsync(buffer, bytesRead, buffer.Length - bytesRead);
bytesRead += count;
}
if (buffer.Length > 0)
{
await request.Body.ReadUntilEndAsync(buffer).DefaultTimeout();
await response.Body.WriteAsync(buffer, 0, buffer.Length);
}
}