Changed header message handler to be more resilient to extra spaces or lack of spaces.
This commit is contained in:
Родитель
fd03bfccac
Коммит
4dd84ad5db
|
@ -70,7 +70,6 @@ namespace StreamJsonRpc
|
|||
private enum HeaderParseState
|
||||
{
|
||||
Name,
|
||||
NameValueDelimiter,
|
||||
Value,
|
||||
FieldDelimiter,
|
||||
EndOfHeader,
|
||||
|
@ -120,7 +119,7 @@ namespace StreamJsonRpc
|
|||
if (lastCharRead == ':')
|
||||
{
|
||||
headerName = HeaderEncoding.GetString(this.receivingBuffer, index: 0, count: headerBytesLength - 1);
|
||||
state = HeaderParseState.NameValueDelimiter;
|
||||
state = HeaderParseState.Value;
|
||||
headerBytesLength = 0;
|
||||
}
|
||||
else if (lastCharRead == '\r' && headerBytesLength == 1)
|
||||
|
@ -133,13 +132,13 @@ namespace StreamJsonRpc
|
|||
ThrowUnexpectedToken(lastCharRead);
|
||||
}
|
||||
|
||||
break;
|
||||
case HeaderParseState.NameValueDelimiter:
|
||||
ThrowIfNotExpectedToken(lastCharRead, ' ');
|
||||
state = HeaderParseState.Value;
|
||||
headerBytesLength = 0;
|
||||
break;
|
||||
case HeaderParseState.Value:
|
||||
if (lastCharRead == ' ')
|
||||
{
|
||||
--headerBytesLength;
|
||||
}
|
||||
|
||||
if (lastCharRead == '\r') // spec mandates \r always precedes \n
|
||||
{
|
||||
string value = HeaderEncoding.GetString(this.receivingBuffer, index: 0, count: headerBytesLength - 1);
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
using StreamJsonRpc;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
public class HeaderDelimitedMessageHandlerTests : TestBase
|
||||
{
|
||||
private readonly MemoryStream sendingStream = new MemoryStream();
|
||||
private readonly MemoryStream receivingStream = new MemoryStream();
|
||||
private HeaderDelimitedMessageHandler handler;
|
||||
|
||||
public HeaderDelimitedMessageHandlerTests(ITestOutputHelper logger) : base(logger)
|
||||
{
|
||||
this.handler = new HeaderDelimitedMessageHandler(this.sendingStream, this.receivingStream);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCoreAsync_HandlesSpacingCorrectly()
|
||||
{
|
||||
string content =
|
||||
@"Content-Length: 10
|
||||
Content-Type: application/vscode-jsonrpc;charset=utf-8
|
||||
|
||||
0123456789";
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(content);
|
||||
this.receivingStream.Write(bytes, 0, bytes.Length);
|
||||
this.receivingStream.Flush();
|
||||
this.receivingStream.Position = 0;
|
||||
|
||||
string readContent = this.handler.ReadAsync(default(CancellationToken)).GetAwaiter().GetResult();
|
||||
Assert.Equal<string>("0123456789", readContent);
|
||||
|
||||
this.receivingStream.Position = 0;
|
||||
this.receivingStream.SetLength(0);
|
||||
|
||||
content =
|
||||
@"Content-Length:5
|
||||
|
||||
ABCDE";
|
||||
bytes = Encoding.UTF8.GetBytes(content);
|
||||
this.receivingStream.Write(bytes, 0, bytes.Length);
|
||||
this.receivingStream.Flush();
|
||||
this.receivingStream.Position = 0;
|
||||
|
||||
readContent = this.handler.ReadAsync(default(CancellationToken)).GetAwaiter().GetResult();
|
||||
Assert.Equal<string>("ABCDE", readContent);
|
||||
}
|
||||
}
|
|
@ -47,6 +47,7 @@
|
|||
</Compile>
|
||||
<Compile Include="DelimitedMessageHandlerTests.cs" />
|
||||
<Compile Include="DirectMessageHandler.cs" />
|
||||
<Compile Include="HeaderDelimitedMessageHandlerTests.cs" />
|
||||
<Compile Include="InteropTestBase.cs" />
|
||||
<Compile Include="JsonRpcClientInteropTests.cs" />
|
||||
<Compile Include="JsonRpcRawStreamTests.cs" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче