Moved the Ext.CheckIfValidCloseParameters methods to the WebSocket class

This commit is contained in:
sta 2015-05-02 15:35:03 +09:00
parent 28e2992e8d
commit f3e0591a18
4 changed files with 65 additions and 39 deletions

View File

@ -251,33 +251,6 @@ namespace WebSocketSharp
: 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)
{
return data.Length > 125

View File

@ -757,7 +757,9 @@ namespace WebSocketSharp.Server
public void Stop (ushort code, string reason)
{
lock (_sync) {
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
var msg = _state.CheckIfStart () ??
WebSocket.CheckIfValidCloseParameters (code, reason, false);
if (msg != null) {
_logger.Error (msg);
return;
@ -793,7 +795,9 @@ namespace WebSocketSharp.Server
public void Stop (CloseStatusCode code, string reason)
{
lock (_sync) {
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
var msg = _state.CheckIfStart () ??
WebSocket.CheckIfValidCloseParameters (code, reason, false);
if (msg != null) {
_logger.Error (msg);
return;

View File

@ -842,7 +842,9 @@ namespace WebSocketSharp.Server
public void Stop (ushort code, string reason)
{
lock (_sync) {
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
var msg = _state.CheckIfStart () ??
WebSocket.CheckIfValidCloseParameters (code, reason, false);
if (msg != null) {
_logger.Error (msg);
return;
@ -877,7 +879,9 @@ namespace WebSocketSharp.Server
public void Stop (CloseStatusCode code, string reason)
{
lock (_sync) {
var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason);
var msg = _state.CheckIfStart () ??
WebSocket.CheckIfValidCloseParameters (code, reason, false);
if (msg != null) {
_logger.Error (msg);
return;

View File

@ -1560,6 +1560,35 @@ namespace WebSocketSharp
#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
internal void Close (HttpResponse response)
{
@ -1738,7 +1767,9 @@ namespace WebSocketSharp
/// </param>
public void Close (ushort code)
{
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseStatusCode ();
var msg = _readyState.CheckIfClosable () ??
CheckIfValidCloseParameters (code, null, _client);
if (msg != null) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);
@ -1765,7 +1796,9 @@ namespace WebSocketSharp
/// </param>
public void Close (CloseStatusCode code)
{
var msg = _readyState.CheckIfClosable ();
var msg = _readyState.CheckIfClosable () ??
CheckIfValidCloseParameters (code, null, _client);
if (msg != null) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);
@ -1799,7 +1832,9 @@ namespace WebSocketSharp
/// </param>
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) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);
@ -1833,7 +1868,9 @@ namespace WebSocketSharp
/// </param>
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) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);
@ -1887,7 +1924,9 @@ namespace WebSocketSharp
/// </param>
public void CloseAsync (ushort code)
{
var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseStatusCode ();
var msg = _readyState.CheckIfClosable () ??
CheckIfValidCloseParameters (code, null, _client);
if (msg != null) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);
@ -1917,7 +1956,9 @@ namespace WebSocketSharp
/// </param>
public void CloseAsync (CloseStatusCode code)
{
var msg = _readyState.CheckIfClosable ();
var msg = _readyState.CheckIfClosable () ??
CheckIfValidCloseParameters (code, null, _client);
if (msg != null) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);
@ -1956,7 +1997,9 @@ namespace WebSocketSharp
/// </param>
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) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);
@ -1996,7 +2039,9 @@ namespace WebSocketSharp
/// </param>
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) {
_logger.Error (msg);
error ("An error has occurred in closing the connection.", null);