Merge branch 'release/2.1' into dev

This commit is contained in:
David Fowler 2018-03-27 00:00:13 -07:00
commit 434147fe8e
3 changed files with 48 additions and 33 deletions

View File

@ -56,19 +56,13 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
return new JsonTextWriter(new StreamWriter(output, _utf8NoBom, 1024, leaveOpen: true)); return new JsonTextWriter(new StreamWriter(output, _utf8NoBom, 1024, leaveOpen: true));
} }
private static JsonTextReader CreateJsonTextReader(ReadOnlyMemory<byte> payload)
{
var textReader = new Utf8BufferTextReader();
textReader.SetBuffer(payload);
var reader = new JsonTextReader(textReader);
reader.ArrayPool = JsonArrayPool<char>.Shared;
return reader;
}
public static HandshakeResponseMessage ParseResponseMessage(ReadOnlyMemory<byte> payload) public static HandshakeResponseMessage ParseResponseMessage(ReadOnlyMemory<byte> payload)
{ {
using (var reader = CreateJsonTextReader(payload)) var textReader = Utf8BufferTextReader.Get(payload);
try
{
using (var reader = JsonUtils.CreateJsonTextReader(textReader))
{ {
var token = JToken.ReadFrom(reader); var token = JToken.ReadFrom(reader);
var handshakeJObject = JsonUtils.GetObject(token); var handshakeJObject = JsonUtils.GetObject(token);
@ -85,6 +79,11 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
return new HandshakeResponseMessage(error); return new HandshakeResponseMessage(error);
} }
} }
finally
{
Utf8BufferTextReader.Return(textReader);
}
}
public static bool TryParseRequestMessage(ReadOnlySequence<byte> buffer, out HandshakeRequestMessage requestMessage, out SequencePosition consumed, out SequencePosition examined) public static bool TryParseRequestMessage(ReadOnlySequence<byte> buffer, out HandshakeRequestMessage requestMessage, out SequencePosition consumed, out SequencePosition examined)
{ {
@ -99,7 +98,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
throw new InvalidDataException("Unable to parse payload as a handshake request message."); throw new InvalidDataException("Unable to parse payload as a handshake request message.");
} }
using (var reader = CreateJsonTextReader(payload)) var textReader = Utf8BufferTextReader.Get(payload);
try
{
using (var reader = JsonUtils.CreateJsonTextReader(textReader))
{ {
var token = JToken.ReadFrom(reader); var token = JToken.ReadFrom(reader);
var handshakeJObject = JsonUtils.GetObject(token); var handshakeJObject = JsonUtils.GetObject(token);
@ -107,6 +109,11 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
var protocolVersion = JsonUtils.GetRequiredProperty<int>(handshakeJObject, ProtocolVersionName, JTokenType.Integer); var protocolVersion = JsonUtils.GetRequiredProperty<int>(handshakeJObject, ProtocolVersionName, JTokenType.Integer);
requestMessage = new HandshakeRequestMessage(protocol, protocolVersion); requestMessage = new HandshakeRequestMessage(protocol, protocolVersion);
} }
}
finally
{
Utf8BufferTextReader.Return(textReader);
}
return true; return true;
} }

View File

@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
TextMessageFormatter.WriteRecordSeparator(output); TextMessageFormatter.WriteRecordSeparator(output);
} }
private HubMessage ParseMessage(TextReader textReader, IInvocationBinder binder) private HubMessage ParseMessage(Utf8BufferTextReader textReader, IInvocationBinder binder)
{ {
try try
{ {
@ -108,11 +108,8 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
Dictionary<string, string> headers = null; Dictionary<string, string> headers = null;
var completed = false; var completed = false;
using (var reader = new JsonTextReader(textReader)) using (var reader = JsonUtils.CreateJsonTextReader(textReader))
{ {
reader.ArrayPool = JsonArrayPool<char>.Shared;
reader.CloseInput = false;
JsonUtils.CheckRead(reader); JsonUtils.CheckRead(reader);
// We're always parsing a JSON object // We're always parsing a JSON object

View File

@ -10,6 +10,17 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
{ {
public static class JsonUtils public static class JsonUtils
{ {
internal static JsonTextReader CreateJsonTextReader(Utf8BufferTextReader textReader)
{
var reader = new JsonTextReader(textReader);
reader.ArrayPool = JsonArrayPool<char>.Shared;
// Don't close the output, Utf8BufferTextReader is resettable
reader.CloseInput = false;
return reader;
}
public static JObject GetObject(JToken token) public static JObject GetObject(JToken token)
{ {
if (token == null || token.Type != JTokenType.Object) if (token == null || token.Type != JTokenType.Object)