diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 07b16c79..cbb954ab 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -848,6 +848,11 @@ namespace WebSocketSharp } } + internal static byte[] UTF8Encode (this string s) + { + return Encoding.UTF8.GetBytes (s); + } + internal static void WriteBytes (this Stream stream, byte[] bytes) { using (var input = new MemoryStream (bytes)) diff --git a/websocket-sharp/Server/WebSocketServiceManager.cs b/websocket-sharp/Server/WebSocketServiceManager.cs index 3f224003..e28f4490 100644 --- a/websocket-sharp/Server/WebSocketServiceManager.cs +++ b/websocket-sharp/Server/WebSocketServiceManager.cs @@ -415,7 +415,7 @@ namespace WebSocketSharp.Server return; } - var bytes = Encoding.UTF8.GetBytes (data); + var bytes = data.UTF8Encode (); if (bytes.LongLength <= WebSocket.FragmentLength) broadcast (Opcode.Text, bytes, null); else @@ -476,7 +476,7 @@ namespace WebSocketSharp.Server return; } - var bytes = Encoding.UTF8.GetBytes (data); + var bytes = data.UTF8Encode (); if (bytes.LongLength <= WebSocket.FragmentLength) broadcastAsync (Opcode.Text, bytes, completed); else diff --git a/websocket-sharp/Server/WebSocketSessionManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs index 96fd6235..1c27e341 100644 --- a/websocket-sharp/Server/WebSocketSessionManager.cs +++ b/websocket-sharp/Server/WebSocketSessionManager.cs @@ -423,7 +423,7 @@ namespace WebSocketSharp.Server return; } - var bytes = Encoding.UTF8.GetBytes (data); + var bytes = data.UTF8Encode (); if (bytes.LongLength <= WebSocket.FragmentLength) broadcast (Opcode.Text, bytes, null); else @@ -484,7 +484,7 @@ namespace WebSocketSharp.Server return; } - var bytes = Encoding.UTF8.GetBytes (data); + var bytes = data.UTF8Encode (); if (bytes.LongLength <= WebSocket.FragmentLength) broadcastAsync (Opcode.Text, bytes, completed); else diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index e6f1b3a0..7a591ffc 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -1601,7 +1601,7 @@ namespace WebSocketSharp ? "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 + : !reason.IsNullOrEmpty () && reason.UTF8Encode ().Length > 123 ? "A reason has greater than the allowable max size." : null; } @@ -1614,14 +1614,14 @@ namespace WebSocketSharp ? "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 + : !reason.IsNullOrEmpty () && reason.UTF8Encode ().Length > 123 ? "A reason has greater than the allowable max size." : null; } internal static string CheckPingParameter (string message, out byte[] bytes) { - bytes = Encoding.UTF8.GetBytes (message); + bytes = message.UTF8Encode (); return bytes.Length > 125 ? "A message has greater than the allowable max size." : null; } @@ -1710,7 +1710,7 @@ namespace WebSocketSharp var buff = new StringBuilder (base64Key, 64); buff.Append (_guid); SHA1 sha1 = new SHA1CryptoServiceProvider (); - var src = sha1.ComputeHash (Encoding.UTF8.GetBytes (buff.ToString ())); + var src = sha1.ComputeHash (buff.ToString ().UTF8Encode ()); return Convert.ToBase64String (src); } @@ -2326,7 +2326,7 @@ namespace WebSocketSharp return; } - send (Opcode.Text, new MemoryStream (Encoding.UTF8.GetBytes (data))); + send (Opcode.Text, new MemoryStream (data.UTF8Encode ())); } /// @@ -2414,7 +2414,7 @@ namespace WebSocketSharp return; } - sendAsync (Opcode.Text, new MemoryStream (Encoding.UTF8.GetBytes (data)), completed); + sendAsync (Opcode.Text, new MemoryStream (data.UTF8Encode ()), completed); } ///