Fixed WebSocket.cs: Server don't mask payload data.
This commit is contained in:
parent
b948fa023d
commit
18c225c99d
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
<Properties>
|
<Properties>
|
||||||
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
|
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release_Ubuntu" />
|
||||||
<MonoDevelop.Ide.Workbench />
|
<MonoDevelop.Ide.Workbench />
|
||||||
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
||||||
<BreakpointStore />
|
<BreakpointStore />
|
||||||
|
@ -271,8 +271,8 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
else if (_readyState == WsState.CONNECTING)
|
else if (_readyState == WsState.CONNECTING)
|
||||||
{
|
{
|
||||||
ReadyState = WsState.CLOSED;
|
|
||||||
OnClose.Emit(this, new CloseEventArgs(data));
|
OnClose.Emit(this, new CloseEventArgs(data));
|
||||||
|
ReadyState = WsState.CLOSED;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,7 +280,7 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
OnClose.Emit(this, new CloseEventArgs(data));
|
OnClose.Emit(this, new CloseEventArgs(data));
|
||||||
var frame = new WsFrame(Opcode.CLOSE, data);
|
var frame = createFrame(Fin.FINAL, Opcode.CLOSE, data);
|
||||||
closeHandshake(frame);
|
closeHandshake(frame);
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Console.WriteLine("WS: Info@close: Exit close method.");
|
Console.WriteLine("WS: Info@close: Exit close method.");
|
||||||
@ -289,18 +289,24 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
private void close(CloseStatusCode code, string reason)
|
private void close(CloseStatusCode code, string reason)
|
||||||
{
|
{
|
||||||
byte[] buffer;
|
var data = new List<byte>(((ushort)code).ToBytes(ByteOrder.BIG));
|
||||||
List<byte> data;
|
|
||||||
|
|
||||||
data = new List<byte>(((ushort)code).ToBytes(ByteOrder.BIG));
|
|
||||||
|
|
||||||
if (reason != String.Empty)
|
if (reason != String.Empty)
|
||||||
{
|
{
|
||||||
buffer = Encoding.UTF8.GetBytes(reason);
|
var buffer = Encoding.UTF8.GetBytes(reason);
|
||||||
data.AddRange(buffer);
|
data.AddRange(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(new PayloadData(data.ToArray()));
|
var payloadData = new PayloadData(data.ToArray());
|
||||||
|
|
||||||
|
if (payloadData.Length > 125)
|
||||||
|
{
|
||||||
|
var msg = "Close frame must have a payload length of 125 bytes or less.";
|
||||||
|
error(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(payloadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeConnection()
|
private void closeConnection()
|
||||||
@ -325,17 +331,7 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
private void closeHandshake(WsFrame frame)
|
private void closeHandshake(WsFrame frame)
|
||||||
{
|
{
|
||||||
try
|
if (send(frame) && !Thread.CurrentThread.IsBackground)
|
||||||
{
|
|
||||||
_wsStream.WriteFrame(frame);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_unTransmittedBuffer.Add(frame);
|
|
||||||
error(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Thread.CurrentThread.IsBackground)
|
|
||||||
{
|
{
|
||||||
if (_isClient)
|
if (_isClient)
|
||||||
{
|
{
|
||||||
@ -402,6 +398,18 @@ namespace WebSocketSharp
|
|||||||
return Convert.ToBase64String(keySrc);
|
return Convert.ToBase64String(keySrc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private WsFrame createFrame(Fin fin, Opcode opcode, PayloadData payloadData)
|
||||||
|
{
|
||||||
|
if (_isClient)
|
||||||
|
{
|
||||||
|
return new WsFrame(fin, opcode, payloadData);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new WsFrame(fin, opcode, Mask.UNMASK, payloadData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private string createOpeningHandshake()
|
private string createOpeningHandshake()
|
||||||
{
|
{
|
||||||
byte[] keySrc;
|
byte[] keySrc;
|
||||||
@ -896,7 +904,7 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
private void pong(PayloadData data)
|
private void pong(PayloadData data)
|
||||||
{
|
{
|
||||||
var frame = new WsFrame(Opcode.PONG, data);
|
var frame = createFrame(Fin.FINAL, Opcode.PONG, data);
|
||||||
send(frame);
|
send(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -928,11 +936,11 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
private bool send(WsFrame frame)
|
private bool send(WsFrame frame)
|
||||||
{
|
{
|
||||||
if (_readyState != WsState.OPEN)
|
if (_readyState == WsState.CONNECTING ||
|
||||||
|
_readyState == WsState.CLOSED)
|
||||||
{
|
{
|
||||||
var msg = "Connection isn't established or connection was closed.";
|
var msg = "Connection isn't established or connection was closed.";
|
||||||
error(msg);
|
error(msg);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -945,13 +953,15 @@ namespace WebSocketSharp
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
_unTransmittedBuffer.Add(frame);
|
_unTransmittedBuffer.Add(frame);
|
||||||
|
var msg = "Current data can not be sent because there is untransmitted data.";
|
||||||
|
error(msg);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_unTransmittedBuffer.Add(frame);
|
_unTransmittedBuffer.Add(frame);
|
||||||
error(ex.Message);
|
error(ex.Message);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -969,20 +979,21 @@ namespace WebSocketSharp
|
|||||||
private void send<TStream>(Opcode opcode, TStream stream)
|
private void send<TStream>(Opcode opcode, TStream stream)
|
||||||
where TStream : System.IO.Stream
|
where TStream : System.IO.Stream
|
||||||
{
|
{
|
||||||
if (_unTransmittedBuffer.Count > 0)
|
|
||||||
{
|
|
||||||
var msg = "Current data can not be sent because there is untransmitted data.";
|
|
||||||
error(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
lock(_forSend)
|
lock(_forSend)
|
||||||
{
|
{
|
||||||
|
if (_readyState != WsState.OPEN)
|
||||||
|
{
|
||||||
|
var msg = "Connection isn't established or connection was closed.";
|
||||||
|
error(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var length = stream.Length;
|
var length = stream.Length;
|
||||||
if (length <= _fragmentLen)
|
if (length <= _fragmentLen)
|
||||||
{
|
{
|
||||||
var buffer = new byte[length];
|
var buffer = new byte[length];
|
||||||
stream.Read(buffer, 0, (int)length);
|
stream.Read(buffer, 0, (int)length);
|
||||||
var frame = new WsFrame(opcode, new PayloadData(buffer));
|
var frame = createFrame(Fin.FINAL, opcode, new PayloadData(buffer));
|
||||||
send(frame);
|
send(frame);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -995,19 +1006,11 @@ namespace WebSocketSharp
|
|||||||
private ulong sendFragmented<TStream>(Opcode opcode, TStream stream)
|
private ulong sendFragmented<TStream>(Opcode opcode, TStream stream)
|
||||||
where TStream : System.IO.Stream
|
where TStream : System.IO.Stream
|
||||||
{
|
{
|
||||||
if (_readyState != WsState.OPEN)
|
|
||||||
{
|
|
||||||
var msg = "Connection isn't established or connection was closed.";
|
|
||||||
error(msg);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
WsFrame frame;
|
WsFrame frame;
|
||||||
PayloadData payloadData;
|
PayloadData payloadData;
|
||||||
|
|
||||||
var buffer1 = new byte[_fragmentLen];
|
byte[] buffer1 = new byte[_fragmentLen];
|
||||||
var buffer2 = new byte[_fragmentLen];
|
byte[] buffer2 = new byte[_fragmentLen];
|
||||||
ulong readLen = 0;
|
ulong readLen = 0;
|
||||||
int tmpLen1 = 0;
|
int tmpLen1 = 0;
|
||||||
int tmpLen2 = 0;
|
int tmpLen2 = 0;
|
||||||
@ -1022,22 +1025,22 @@ namespace WebSocketSharp
|
|||||||
{
|
{
|
||||||
if (readLen > 0)
|
if (readLen > 0)
|
||||||
{
|
{
|
||||||
frame = new WsFrame(Fin.MORE, Opcode.CONT, payloadData);
|
frame = createFrame(Fin.MORE, Opcode.CONT, payloadData);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
frame = new WsFrame(Fin.MORE, opcode, payloadData);
|
frame = createFrame(Fin.MORE, opcode, payloadData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (readLen > 0)
|
if (readLen > 0)
|
||||||
{
|
{
|
||||||
frame = new WsFrame(Opcode.CONT, payloadData);
|
frame = createFrame(Fin.FINAL, Opcode.CONT, payloadData);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
frame = new WsFrame(opcode, payloadData);
|
frame = createFrame(Fin.FINAL, opcode, payloadData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1050,11 +1053,11 @@ namespace WebSocketSharp
|
|||||||
tmpLen1 = stream.Read(buffer1, 0, _fragmentLen);
|
tmpLen1 = stream.Read(buffer1, 0, _fragmentLen);
|
||||||
if (tmpLen1 > 0)
|
if (tmpLen1 > 0)
|
||||||
{
|
{
|
||||||
frame = new WsFrame(Fin.MORE, Opcode.CONT, payloadData);
|
frame = createFrame(Fin.MORE, Opcode.CONT, payloadData);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
frame = new WsFrame(Opcode.CONT, payloadData);
|
frame = createFrame(Fin.FINAL, Opcode.CONT, payloadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
readLen += (ulong)tmpLen2;
|
readLen += (ulong)tmpLen2;
|
||||||
@ -1153,7 +1156,15 @@ namespace WebSocketSharp
|
|||||||
public void Ping(string data)
|
public void Ping(string data)
|
||||||
{
|
{
|
||||||
var payloadData = new PayloadData(data);
|
var payloadData = new PayloadData(data);
|
||||||
var frame = new WsFrame(Opcode.PING, payloadData);
|
|
||||||
|
if (payloadData.Length > 125)
|
||||||
|
{
|
||||||
|
var msg = "Ping frame must have a payload length of 125 bytes or less.";
|
||||||
|
error(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var frame = createFrame(Fin.FINAL, Opcode.PING, payloadData);
|
||||||
send(frame);
|
send(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user