Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for failed autobahn test suites #144

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions src/Fleck/Handlers/Hybi13Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IHandler Create(WebSocketHttpRequest request, Action<string> onMes
ReceiveData = d => Hybi13Handler.ReceiveData(d, readState, (op, data) => Hybi13Handler.ProcessFrame(op, data, onMessage, onClose, onBinary, onPing, onPong))
};
}

public static byte[] FrameData(byte[] payload, FrameType frameType)
{
var memoryStream = new MemoryStream();
Expand Down Expand Up @@ -58,7 +58,7 @@ public static void ReceiveData(List<byte> data, ReadState readState, Action<Fram
var frameType = (FrameType)(data[0] & 15);
var isMasked = (data[1] & 128) != 0;
var length = (data[1] & 127);

var isControlFrame = (data[0]&15) >= 0x08 && (data[0]&15) <= 0x0F;

if (!isMasked
|| !Enum.IsDefined(typeof(FrameType), frameType)
Expand All @@ -68,6 +68,18 @@ public static void ReceiveData(List<byte> data, ReadState readState, Action<Fram

var index = 2;
int payloadLength;

// control frame types may only have a maximum payload length of 125 (autobahn test case 2.5)
if (isControlFrame && length > 125)
throw new WebSocketException(WebSocketStatusCodes.ProtocolError);

// control frames must not be fragmented (autobahn test case 5.1)
if (isControlFrame && !isFinal)
throw new WebSocketException(WebSocketStatusCodes.ProtocolError);

// a close frame MAY contain data, but if it does, it must be at least 2 bytes (autobahn test case 7.3.2)
if (frameType == FrameType.Close && length == 1)
throw new WebSocketException(WebSocketStatusCodes.ProtocolError);

if (length == 127)
{
Expand Down Expand Up @@ -104,21 +116,36 @@ public static void ReceiveData(List<byte> data, ReadState readState, Action<Fram
.Take(payloadLength)
.Select((x, i) => (byte)(x ^ maskBytes[i % 4]));


readState.Data.AddRange(payload);
data.RemoveRange(0, index + payloadLength);

if (frameType != FrameType.Continuation)
readState.FrameType = frameType;

if (isFinal && readState.FrameType.HasValue)
// control frames are processed immediatly (since they cannot be fragmented) and can be receive in between
// and can be received and processed between a fragmented frame (autobahn test case 5.6)
if (isControlFrame)
{
var stateData = readState.Data.ToArray();
var stateFrameType = readState.FrameType;
readState.Clear();

processFrame(stateFrameType.Value, stateData);
processFrame(frameType, payload.ToArray());
data.RemoveRange(0, index + payloadLength);
}
else
{
readState.Data.AddRange(payload);
data.RemoveRange(0, index + payloadLength);

// frame types for multiple fragments are sent on the first frame, and the rest of the fragments must
// have a frame type of continuation (autobahn test case 5.18)
if (readState.FragmentNumber == 1)
readState.FrameType = frameType;
else if(frameType != FrameType.Continuation)
throw new WebSocketException(WebSocketStatusCodes.ProtocolError);

readState.FragmentNumber++;

if (isFinal && readState.FrameType.HasValue)
{
var stateData = readState.Data.ToArray();
var stateFrameType = readState.FrameType;
readState.Clear();

processFrame(stateFrameType.Value, stateData);
}
}
}
}

Expand All @@ -127,9 +154,6 @@ public static void ProcessFrame(FrameType frameType, byte[] data, Action<string>
switch (frameType)
{
case FrameType.Close:
if (data.Length == 1 || data.Length>125)
throw new WebSocketException(WebSocketStatusCodes.ProtocolError);

if (data.Length >= 2)
{
var closeCode = (ushort)data.Take(2).ToArray().ToLittleEndianInt();
Expand Down
3 changes: 3 additions & 0 deletions src/Fleck/ReadState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ public class ReadState
public ReadState()
{
Data = new List<byte>();
FragmentNumber = 1;
}
public List<byte> Data { get; private set; }
public FrameType? FrameType { get; set; }
public int FragmentNumber { get; set; }
public void Clear()
{
Data.Clear();
FrameType = null;
FragmentNumber = 1;
}
}
}
Expand Down