[Modify] Add an Opcode check

This commit is contained in:
sta 2015-12-13 17:54:39 +09:00
parent 842b5b3b3a
commit 1a6e1fb169

View File

@ -377,16 +377,33 @@ namespace WebSocketSharp
return output.ToString (); return output.ToString ();
} }
private static bool isControl (byte opcode)
{
return opcode == (byte) Opcode.Close ||
opcode == (byte) Opcode.Ping ||
opcode == (byte) Opcode.Pong;
}
private static bool isControl (Opcode opcode) private static bool isControl (Opcode opcode)
{ {
return opcode == Opcode.Close || opcode == Opcode.Ping || opcode == Opcode.Pong; return opcode == Opcode.Close || opcode == Opcode.Ping || opcode == Opcode.Pong;
} }
private static bool isData (byte opcode)
{
return opcode == (byte) Opcode.Text || opcode == (byte) Opcode.Binary;
}
private static bool isData (Opcode opcode) private static bool isData (Opcode opcode)
{ {
return opcode == Opcode.Text || opcode == Opcode.Binary; return opcode == Opcode.Text || opcode == Opcode.Binary;
} }
private static bool isSupported (byte opcode)
{
return Enum.IsDefined (typeof (Opcode), opcode);
}
private static string print (WebSocketFrame frame) private static string print (WebSocketFrame frame)
{ {
// Payload Length // Payload Length
@ -452,7 +469,7 @@ Extended Payload Length: {7}
var rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.On : Rsv.Off; var rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.On : Rsv.Off;
// Opcode // Opcode
var opcode = (Opcode) (header[0] & 0x0f); var opcode = (byte) (header[0] & 0x0f);
// MASK // MASK
var mask = (header[1] & 0x80) == 0x80 ? Mask.On : Mask.Off; var mask = (header[1] & 0x80) == 0x80 ? Mask.On : Mask.Off;
@ -461,10 +478,12 @@ Extended Payload Length: {7}
var payloadLen = (byte) (header[1] & 0x7f); var payloadLen = (byte) (header[1] & 0x7f);
// Check if valid header. // Check if valid header.
var err = isControl (opcode) && payloadLen > 125 var err = !isSupported (opcode)
? "A control frame has payload data which is greater than the allowable max length." ? "An unsupported opcode."
: isControl (opcode) && fin == Fin.More : isControl (opcode) && fin == Fin.More
? "A control frame is fragmented." ? "A control frame is fragmented."
: isControl (opcode) && payloadLen > 125
? "A control frame has a long payload length than the allowable max length."
: !isData (opcode) && rsv1 == Rsv.On : !isData (opcode) && rsv1 == Rsv.On
? "A non data frame is compressed." ? "A non data frame is compressed."
: null; : null;
@ -477,7 +496,7 @@ Extended Payload Length: {7}
frame._rsv1 = rsv1; frame._rsv1 = rsv1;
frame._rsv2 = rsv2; frame._rsv2 = rsv2;
frame._rsv3 = rsv3; frame._rsv3 = rsv3;
frame._opcode = opcode; frame._opcode = (Opcode) opcode;
frame._mask = mask; frame._mask = mask;
frame._payloadLength = payloadLen; frame._payloadLength = payloadLen;