Modified WebSocket.cs
This commit is contained in:
@@ -341,6 +341,77 @@ namespace WebSocketSharp {
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool checkCloseStatusCodeIsValid(ushort code, out string message)
|
||||
{
|
||||
if (code < 1000)
|
||||
{
|
||||
message = "Close status codes in the range 0-999 are not used: " + code;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (code > 4999)
|
||||
{
|
||||
message = "Out of reserved close status code range: " + code;
|
||||
return false;
|
||||
}
|
||||
|
||||
message = String.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool checkFrameIsValid(WsFrame frame)
|
||||
{
|
||||
if (frame.IsNull())
|
||||
{
|
||||
var msg = "The WebSocket frame can not be read from the network stream.";
|
||||
close(CloseStatusCode.ABNORMAL, msg);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// As Server
|
||||
private bool checkRequestIsValid()
|
||||
{
|
||||
return !_context.IsValid
|
||||
? false
|
||||
: !checkRequestHostHeaderIsValid()
|
||||
? false
|
||||
: _context.Headers.Exists("Sec-WebSocket-Version", _version);
|
||||
}
|
||||
|
||||
// As Server
|
||||
private bool checkRequestHostHeaderIsValid()
|
||||
{
|
||||
var authority = _context.Headers["Host"];
|
||||
if (authority.IsNullOrEmpty() || !_uri.IsAbsoluteUri)
|
||||
return true;
|
||||
|
||||
var i = authority.IndexOf(':');
|
||||
var host = i > 0
|
||||
? authority.Substring(0, i)
|
||||
: authority;
|
||||
var type = Uri.CheckHostName(host);
|
||||
|
||||
return type != UriHostNameType.Dns
|
||||
? true
|
||||
: Uri.CheckHostName(_uri.DnsSafeHost) != UriHostNameType.Dns
|
||||
? true
|
||||
: host == _uri.DnsSafeHost;
|
||||
}
|
||||
|
||||
// As Client
|
||||
private bool checkResponseIsValid(ResponseHandshake response)
|
||||
{
|
||||
return !response.IsWebSocketResponse
|
||||
? false
|
||||
: !response.HeaderExists("Sec-WebSocket-Accept", createResponseKey())
|
||||
? false
|
||||
: !response.HeaderExists("Sec-WebSocket-Version") || response.HeaderExists("Sec-WebSocket-Version", _version);
|
||||
}
|
||||
|
||||
private void close(HttpStatusCode code)
|
||||
{
|
||||
if (_readyState != WsState.CONNECTING || _client)
|
||||
@@ -555,77 +626,6 @@ namespace WebSocketSharp {
|
||||
_client = false;
|
||||
}
|
||||
|
||||
private bool isValidCloseStatusCode(ushort code, out string message)
|
||||
{
|
||||
if (code < 1000)
|
||||
{
|
||||
message = "Close status codes in the range 0-999 are not used: " + code;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (code > 4999)
|
||||
{
|
||||
message = "Out of reserved close status code range: " + code;
|
||||
return false;
|
||||
}
|
||||
|
||||
message = String.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool isValidFrame(WsFrame frame)
|
||||
{
|
||||
if (frame.IsNull())
|
||||
{
|
||||
var msg = "The WebSocket frame can not be read from the network stream.";
|
||||
close(CloseStatusCode.ABNORMAL, msg);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// As Server
|
||||
private bool isValidRequest()
|
||||
{
|
||||
return !_context.IsValid
|
||||
? false
|
||||
: !isValidRequestHostHeader()
|
||||
? false
|
||||
: _context.Headers.Exists("Sec-WebSocket-Version", _version);
|
||||
}
|
||||
|
||||
// As Server
|
||||
private bool isValidRequestHostHeader()
|
||||
{
|
||||
var authority = _context.Headers["Host"];
|
||||
if (authority.IsNullOrEmpty() || !_uri.IsAbsoluteUri)
|
||||
return true;
|
||||
|
||||
var i = authority.IndexOf(':');
|
||||
var host = i > 0
|
||||
? authority.Substring(0, i)
|
||||
: authority;
|
||||
var type = Uri.CheckHostName(host);
|
||||
|
||||
return type != UriHostNameType.Dns
|
||||
? true
|
||||
: Uri.CheckHostName(_uri.DnsSafeHost) != UriHostNameType.Dns
|
||||
? true
|
||||
: host == _uri.DnsSafeHost;
|
||||
}
|
||||
|
||||
// As Client
|
||||
private bool isValidResponse(ResponseHandshake response)
|
||||
{
|
||||
return !response.IsWebSocketResponse
|
||||
? false
|
||||
: !response.HeaderExists("Sec-WebSocket-Accept", createResponseKey())
|
||||
? false
|
||||
: !response.HeaderExists("Sec-WebSocket-Version") || response.HeaderExists("Sec-WebSocket-Version", _version);
|
||||
}
|
||||
|
||||
private void onClose(CloseEventArgs eventArgs)
|
||||
{
|
||||
if (!Thread.CurrentThread.IsBackground)
|
||||
@@ -692,7 +692,7 @@ namespace WebSocketSharp {
|
||||
private WsFrame readFrame()
|
||||
{
|
||||
var frame = _wsStream.ReadFrame();
|
||||
return isValidFrame(frame) ? frame : null;
|
||||
return checkFrameIsValid(frame) ? frame : null;
|
||||
}
|
||||
|
||||
private string[] readHandshake()
|
||||
@@ -702,7 +702,7 @@ namespace WebSocketSharp {
|
||||
|
||||
private MessageEventArgs receive(WsFrame frame)
|
||||
{
|
||||
if (!isValidFrame(frame))
|
||||
if (!checkFrameIsValid(frame))
|
||||
return null;
|
||||
|
||||
if ((frame.Fin == Fin.FINAL && frame.Opcode == Opcode.CONT) ||
|
||||
@@ -825,7 +825,7 @@ namespace WebSocketSharp {
|
||||
Console.WriteLine("WS: Info@receiveOpeningHandshake: Opening handshake from client:\n");
|
||||
Console.WriteLine(req.ToString());
|
||||
#endif
|
||||
if (!isValidRequest())
|
||||
if (!checkRequestIsValid())
|
||||
{
|
||||
onError("Invalid WebSocket connection request.");
|
||||
close(HttpStatusCode.BadRequest);
|
||||
@@ -850,7 +850,7 @@ namespace WebSocketSharp {
|
||||
Console.WriteLine("WS: Info@receiveResponseHandshake: Response handshake from server:\n");
|
||||
Console.WriteLine(res.ToString());
|
||||
#endif
|
||||
if (!isValidResponse(res))
|
||||
if (!checkResponseIsValid(res))
|
||||
{
|
||||
var msg = "Invalid response to the WebSocket connection request.";
|
||||
onError(msg);
|
||||
@@ -1144,7 +1144,7 @@ namespace WebSocketSharp {
|
||||
public void Close(ushort code, string reason)
|
||||
{
|
||||
string msg;
|
||||
if (!isValidCloseStatusCode(code, out msg))
|
||||
if (!checkCloseStatusCodeIsValid(code, out msg))
|
||||
{
|
||||
onError(msg);
|
||||
return;
|
||||
|
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,6 +1,6 @@
|
||||
<Overview>
|
||||
<Assemblies>
|
||||
<Assembly Name="websocket-sharp" Version="1.0.2.29585">
|
||||
<Assembly Name="websocket-sharp" Version="1.0.2.30644">
|
||||
<AssemblyPublicKey>[00 24 00 00 04 80 00 00 94 00 00 00 06 02 00 00 00 24 00 00 52 53 41 31 00 04 00 00 11 00 00 00 29 17 fb 89 fe c3 91 f7 2b cb 8b e2 61 d2 3f 05 93 6d 65 a8 9e 63 72 a6 f5 d5 2c f2 9d 20 fa 0b c0 70 6a f6 88 7e 8b 90 3f 39 f5 76 c8 48 e0 bb 7b b2 7b ed d3 10 a7 1a 0f 70 98 0f 7f f4 4b 53 09 d2 a5 ef 36 c3 56 b4 aa f0 91 72 63 25 07 89 e0 93 3e 3f 2e f2 b9 73 0e 12 15 5d 43 56 c3 f4 70 a5 89 fe f7 f6 ac 3e 77 c2 d8 d0 84 91 f4 0c d1 f3 8e dc c3 c3 b8 38 3d 0c bf 17 de 20 78 c1 ]</AssemblyPublicKey>
|
||||
<Attributes>
|
||||
<Attribute>
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user