Moved the Ext.CheckIfValidCloseParameters methods to the WebSocket class
This commit is contained in:
parent
28e2992e8d
commit
f3e0591a18
@ -251,33 +251,6 @@ namespace WebSocketSharp
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string CheckIfValidCloseParameters (this ushort code, string reason)
|
|
||||||
{
|
|
||||||
return !code.IsCloseStatusCode ()
|
|
||||||
? "An invalid close status code."
|
|
||||||
: code.IsNoStatus () && !reason.IsNullOrEmpty ()
|
|
||||||
? "NoStatus cannot have a reason."
|
|
||||||
: !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123
|
|
||||||
? "A reason has greater than the allowable max size."
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static string CheckIfValidCloseParameters (this CloseStatusCode code, string reason)
|
|
||||||
{
|
|
||||||
return code.IsNoStatus () && !reason.IsNullOrEmpty ()
|
|
||||||
? "NoStatus cannot have a reason."
|
|
||||||
: !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123
|
|
||||||
? "A reason has greater than the allowable max size."
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static string CheckIfValidCloseStatusCode (this ushort code)
|
|
||||||
{
|
|
||||||
return !code.IsCloseStatusCode ()
|
|
||||||
? "An invalid close status code."
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static string CheckIfValidControlData (this byte[] data, string paramName)
|
internal static string CheckIfValidControlData (this byte[] data, string paramName)
|
||||||
{
|
{
|
||||||
return data.Length > 125
|
return data.Length > 125
|
||||||
|
@ -757,7 +757,9 @@ namespace WebSocketSharp.Server
|
|||||||
public void Stop (ushort code, string reason)
|
public void Stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
lock (_sync) {
|
lock (_sync) {
|
||||||
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _state.CheckIfStart () ??
|
||||||
|
WebSocket.CheckIfValidCloseParameters (code, reason, false);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
return;
|
return;
|
||||||
@ -793,7 +795,9 @@ namespace WebSocketSharp.Server
|
|||||||
public void Stop (CloseStatusCode code, string reason)
|
public void Stop (CloseStatusCode code, string reason)
|
||||||
{
|
{
|
||||||
lock (_sync) {
|
lock (_sync) {
|
||||||
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _state.CheckIfStart () ??
|
||||||
|
WebSocket.CheckIfValidCloseParameters (code, reason, false);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
return;
|
return;
|
||||||
|
@ -842,7 +842,9 @@ namespace WebSocketSharp.Server
|
|||||||
public void Stop (ushort code, string reason)
|
public void Stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
lock (_sync) {
|
lock (_sync) {
|
||||||
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _state.CheckIfStart () ??
|
||||||
|
WebSocket.CheckIfValidCloseParameters (code, reason, false);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
return;
|
return;
|
||||||
@ -877,7 +879,9 @@ namespace WebSocketSharp.Server
|
|||||||
public void Stop (CloseStatusCode code, string reason)
|
public void Stop (CloseStatusCode code, string reason)
|
||||||
{
|
{
|
||||||
lock (_sync) {
|
lock (_sync) {
|
||||||
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _state.CheckIfStart () ??
|
||||||
|
WebSocket.CheckIfValidCloseParameters (code, reason, false);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
return;
|
return;
|
||||||
|
@ -1560,6 +1560,35 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
#region Internal Methods
|
#region Internal Methods
|
||||||
|
|
||||||
|
internal static string CheckIfValidCloseParameters (ushort code, string reason, bool client)
|
||||||
|
{
|
||||||
|
return !code.IsCloseStatusCode ()
|
||||||
|
? "An invalid close status code."
|
||||||
|
: code == (ushort) CloseStatusCode.NoStatus
|
||||||
|
? (!reason.IsNullOrEmpty () ? "NoStatus cannot have a reason." : null)
|
||||||
|
: code == (ushort) CloseStatusCode.MandatoryExtension && !client
|
||||||
|
? "MandatoryExtension cannot be used by the server."
|
||||||
|
: code == (ushort) CloseStatusCode.ServerError && client
|
||||||
|
? "ServerError cannot be used by the client."
|
||||||
|
: !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123
|
||||||
|
? "A reason has greater than the allowable max size."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidCloseParameters (
|
||||||
|
CloseStatusCode code, string reason, bool client)
|
||||||
|
{
|
||||||
|
return code == CloseStatusCode.NoStatus
|
||||||
|
? (!reason.IsNullOrEmpty () ? "NoStatus cannot have a reason." : null)
|
||||||
|
: code == CloseStatusCode.MandatoryExtension && !client
|
||||||
|
? "MandatoryExtension cannot be used by the server."
|
||||||
|
: code == CloseStatusCode.ServerError && client
|
||||||
|
? "ServerError cannot be used by the client."
|
||||||
|
: !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123
|
||||||
|
? "A reason has greater than the allowable max size."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
// As server
|
// As server
|
||||||
internal void Close (HttpResponse response)
|
internal void Close (HttpResponse response)
|
||||||
{
|
{
|
||||||
@ -1738,7 +1767,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Close (ushort code)
|
public void Close (ushort code)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseStatusCode ();
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, null, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
@ -1765,7 +1796,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Close (CloseStatusCode code)
|
public void Close (CloseStatusCode code)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable ();
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, null, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
@ -1799,7 +1832,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Close (ushort code, string reason)
|
public void Close (ushort code, string reason)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, reason, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
@ -1833,7 +1868,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Close (CloseStatusCode code, string reason)
|
public void Close (CloseStatusCode code, string reason)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, reason, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
@ -1887,7 +1924,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (ushort code)
|
public void CloseAsync (ushort code)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseStatusCode ();
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, null, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
@ -1917,7 +1956,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (CloseStatusCode code)
|
public void CloseAsync (CloseStatusCode code)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable ();
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, null, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
@ -1956,7 +1997,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (ushort code, string reason)
|
public void CloseAsync (ushort code, string reason)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, reason, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
@ -1996,7 +2039,9 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (CloseStatusCode code, string reason)
|
public void CloseAsync (CloseStatusCode code, string reason)
|
||||||
{
|
{
|
||||||
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason);
|
var msg = _readyState.CheckIfClosable () ??
|
||||||
|
CheckIfValidCloseParameters (code, reason, _client);
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
error ("An error has occurred in closing the connection.", null);
|
error ("An error has occurred in closing the connection.", null);
|
||||||
|
Loading…
Reference in New Issue
Block a user