Skip to content

Commit

Permalink
Merge branch 'release/3.0-preview8' => 'release/3.0' (#12710)
Browse files Browse the repository at this point in the history
  • Loading branch information
dougbu authored Aug 3, 2019
2 parents 352df35 + 0dc822a commit 9f2b534
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
<CastleCorePackageVersion>4.2.1</CastleCorePackageVersion>
<FSharpCorePackageVersion>4.2.1</FSharpCorePackageVersion>
<GoogleProtobufPackageVersion>3.8.0</GoogleProtobufPackageVersion>
<GrpcAspNetCorePackageVersion>0.1.22-pre2</GrpcAspNetCorePackageVersion>
<GrpcAspNetCorePackageVersion>0.1.22-pre3</GrpcAspNetCorePackageVersion>
<IdentityServer4AspNetIdentityPackageVersion>3.0.0-preview3.4</IdentityServer4AspNetIdentityPackageVersion>
<IdentityServer4EntityFrameworkPackageVersion>3.0.0-preview3.4</IdentityServer4EntityFrameworkPackageVersion>
<IdentityServer4PackageVersion>3.0.0-preview3.4</IdentityServer4PackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static void Main(string[] args)
CreateHostBuilder(args).Build().Run();
}

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"SPA"
],
"groupIdentity": "Microsoft.DotNet.Web.Spa.ProjectTemplates.Angular",
"precedence": "5000",
"identity": "Microsoft.DotNet.Web.Spa.ProjectTemplates.Angular.CSharp.3.0",
"name": "ASP.NET Core with Angular",
"preferNameDirectory": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"SPA"
],
"groupIdentity": "Microsoft.DotNet.Web.Spa.ProjectTemplates.React",
"precedence": "5000",
"identity": "Microsoft.DotNet.Web.Spa.ProjectTemplates.React.CSharp.3.0",
"name": "ASP.NET Core with React.js",
"preferNameDirectory": true,
Expand Down Expand Up @@ -189,7 +190,6 @@
"binding": "HostIdentifier"
}
},

"tags": {
"language": "C#",
"type": "project"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"SPA"
],
"groupIdentity": "Microsoft.DotNet.Web.Spa.ProjectTemplates.ReactRedux",
"precedence": "5000",
"identity": "Microsoft.DotNet.Web.Spa.ProjectTemplates.ReactRedux.CSharp.3.0",
"name": "ASP.NET Core with React.js and Redux",
"preferNameDirectory": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public partial class HubConnectionContextOptions
public HubConnectionContextOptions() { }
public System.TimeSpan ClientTimeoutInterval { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public System.TimeSpan KeepAliveInterval { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public long? MaximumReceiveMessageSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public int StreamBufferCapacity { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
}
public partial class HubConnectionHandler<THub> : Microsoft.AspNetCore.Connections.ConnectionHandler where THub : Microsoft.AspNetCore.SignalR.Hub
Expand Down
30 changes: 28 additions & 2 deletions src/SignalR/server/Core/src/HubConnectionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class HubConnectionContext
private bool _clientTimeoutActive;
private bool _connectionAborted;
private int _streamBufferCapacity;
private long? _maxMessageSize;

/// <summary>
/// Initializes a new instance of the <see cref="HubConnectionContext"/> class.
Expand All @@ -55,6 +56,7 @@ public HubConnectionContext(ConnectionContext connectionContext, HubConnectionCo
_keepAliveInterval = contextOptions.KeepAliveInterval.Ticks;
_clientTimeoutInterval = contextOptions.ClientTimeoutInterval.Ticks;
_streamBufferCapacity = contextOptions.StreamBufferCapacity;
_maxMessageSize = contextOptions.MaximumReceiveMessageSize;

_connectionContext = connectionContext;
_logger = loggerFactory.CreateLogger<HubConnectionContext>();
Expand Down Expand Up @@ -406,10 +408,20 @@ internal async Task<bool> HandshakeAsync(TimeSpan timeout, IReadOnlyList<string>

if (!buffer.IsEmpty)
{
if (HandshakeProtocol.TryParseRequestMessage(ref buffer, out var handshakeRequestMessage))
var segment = buffer;
var overLength = false;

if (_maxMessageSize != null && buffer.Length > _maxMessageSize.Value)
{
// We give the parser a sliding window of the default message size
segment = segment.Slice(segment.Start, _maxMessageSize.Value);
overLength = true;
}

if (HandshakeProtocol.TryParseRequestMessage(ref segment, out var handshakeRequestMessage))
{
// We parsed the handshake
consumed = buffer.Start;
consumed = segment.Start;
examined = consumed;

Protocol = protocolResolver.GetProtocol(handshakeRequestMessage.Protocol, supportedProtocols);
Expand Down Expand Up @@ -461,6 +473,12 @@ await WriteHandshakeResponseAsync(new HandshakeResponseMessage(
await WriteHandshakeResponseAsync(HandshakeResponseMessage.Empty);
return true;
}
else if (overLength)
{
Log.HandshakeSizeLimitExceeded(_logger, _maxMessageSize.Value);
await WriteHandshakeResponseAsync(new HandshakeResponseMessage("Handshake was canceled."));
return false;
}
}

if (result.IsCompleted)
Expand Down Expand Up @@ -619,6 +637,9 @@ private static class Log
private static readonly Action<ILogger, int, Exception> _clientTimeout =
LoggerMessage.Define<int>(LogLevel.Debug, new EventId(9, "ClientTimeout"), "Client timeout ({ClientTimeout}ms) elapsed without receiving a message from the client. Closing connection.");

private static readonly Action<ILogger, long, Exception> _handshakeSizeLimitExceeded =
LoggerMessage.Define<long>(LogLevel.Debug, new EventId(10, "HandshakeSizeLimitExceeded"), "The maximum message size of {MaxMessageSize}B was exceeded while parsing the Handshake. The message size can be configured in AddHubOptions.");

public static void HandshakeComplete(ILogger logger, string hubProtocol)
{
_handshakeComplete(logger, hubProtocol, null);
Expand Down Expand Up @@ -663,6 +684,11 @@ public static void ClientTimeout(ILogger logger, TimeSpan timeout)
{
_clientTimeout(logger, (int)timeout.TotalMilliseconds, null);
}

public static void HandshakeSizeLimitExceeded(ILogger logger, long maxMessageSize)
{
_handshakeSizeLimitExceeded(logger, maxMessageSize, null);
}
}
}
}
5 changes: 5 additions & 0 deletions src/SignalR/server/Core/src/HubConnectionContextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ public class HubConnectionContextOptions
/// Gets or sets the max buffer size for client upload streams.
/// </summary>
public int StreamBufferCapacity { get; set; }

/// <summary>
/// Gets or sets the maximum message size the client can send.
/// </summary>
public long? MaximumReceiveMessageSize { get; set; }
}
}
1 change: 1 addition & 0 deletions src/SignalR/server/Core/src/HubConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public override async Task OnConnectedAsync(ConnectionContext connection)
KeepAliveInterval = _hubOptions.KeepAliveInterval ?? _globalHubOptions.KeepAliveInterval ?? HubOptionsSetup.DefaultKeepAliveInterval,
ClientTimeoutInterval = _hubOptions.ClientTimeoutInterval ?? _globalHubOptions.ClientTimeoutInterval ?? HubOptionsSetup.DefaultClientTimeoutInterval,
StreamBufferCapacity = _hubOptions.StreamBufferCapacity ?? _globalHubOptions.StreamBufferCapacity ?? HubOptionsSetup.DefaultStreamBufferCapacity,
MaximumReceiveMessageSize = _maximumMessageSize,
};

Log.ConnectedStarting(_logger);
Expand Down
34 changes: 34 additions & 0 deletions src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,40 @@ public async Task HandshakeFailureFromUnsupportedFormatSendsResponseWithError()
}
}

[Fact]
public async Task ConnectionClosedWhenHandshakeLargerThanMaxMessageSize()
{
using (StartVerifiableLog())
{
var connectionHandler = HubConnectionHandlerTestUtils.GetHubConnectionHandler(typeof(HubT), loggerFactory: LoggerFactory,
builder =>
{
builder.AddSignalR(o =>
{
o.MaximumReceiveMessageSize = 1;
});
});

using (var client = new TestClient())
{
client.SupportedFormats = TransferFormat.Text;

var connectionHandlerTask = await client.ConnectAsync(connectionHandler,
sendHandshakeRequestMessage: true,
expectedHandshakeResponseMessage: false);

var message = await client.ReadAsync(isHandshake: true).OrTimeout();

Assert.Equal("Handshake was canceled.", ((HandshakeResponseMessage)message).Error);

// Connection closes
await connectionHandlerTask.OrTimeout();

client.Dispose();
}
}
}

[Fact]
public async Task SendingHandshakeRequestInChunksWorks()
{
Expand Down

0 comments on commit 9f2b534

Please sign in to comment.