diff --git a/Example/Example.csproj b/Example/Example.csproj index ea12fe918..7250cd1fd 100644 --- a/Example/Example.csproj +++ b/Example/Example.csproj @@ -1,5 +1,5 @@ - - + + Debug AnyCPU @@ -11,6 +11,25 @@ example v3.5 . + + + 3.5 + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true true @@ -21,6 +40,7 @@ prompt 4 true + AllRules.ruleset none @@ -29,6 +49,7 @@ prompt 4 true + AllRules.ruleset true @@ -39,6 +60,7 @@ prompt 4 true + AllRules.ruleset none @@ -48,6 +70,7 @@ prompt 4 true + AllRules.ruleset @@ -66,4 +89,21 @@ + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + \ No newline at end of file diff --git a/Example1/Example1.csproj b/Example1/Example1.csproj index 95af031f8..eec79c5b5 100644 --- a/Example1/Example1.csproj +++ b/Example1/Example1.csproj @@ -1,5 +1,5 @@ - - + + Debug AnyCPU @@ -10,6 +10,25 @@ Example example1 v3.5 + + + 3.5 + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true true @@ -20,6 +39,7 @@ prompt 4 true + AllRules.ruleset none @@ -28,6 +48,7 @@ prompt 4 true + AllRules.ruleset true @@ -38,6 +59,7 @@ prompt 4 true + AllRules.ruleset none @@ -47,6 +69,7 @@ 4 true NOTIFY + AllRules.ruleset @@ -70,4 +93,21 @@ websocket-sharp + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + \ No newline at end of file diff --git a/Example2/Example2.csproj b/Example2/Example2.csproj index 65bdb2598..3cb0aa9f0 100644 --- a/Example2/Example2.csproj +++ b/Example2/Example2.csproj @@ -1,5 +1,5 @@ - - + + Debug AnyCPU @@ -10,6 +10,25 @@ Example2 example2 v3.5 + + + 3.5 + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true true @@ -20,6 +39,7 @@ prompt 4 true + AllRules.ruleset none @@ -28,6 +48,7 @@ prompt 4 true + AllRules.ruleset true @@ -38,6 +59,7 @@ prompt 4 true + AllRules.ruleset none @@ -46,6 +68,7 @@ prompt 4 true + AllRules.ruleset @@ -66,4 +89,21 @@ + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + \ No newline at end of file diff --git a/websocket-sharp.sln b/websocket-sharp.sln index ef2da1cfd..67b917977 100644 --- a/websocket-sharp.sln +++ b/websocket-sharp.sln @@ -1,6 +1,6 @@  -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "websocket-sharp", "websocket-sharp\websocket-sharp.csproj", "{B357BAC7-529E-4D81-A0D2-71041B19C8DE}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{52805AEC-EFB1-4F42-BB8E-3ED4E692C568}" @@ -11,28 +11,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example2", "Example2\Exampl EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU Debug_Ubuntu|Any CPU = Debug_Ubuntu|Any CPU + Debug|Any CPU = Debug|Any CPU Release_Ubuntu|Any CPU = Release_Ubuntu|Any CPU + Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU - {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU - {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {390E2568-57B7-4D17-91E5-C29336368CCF}.Release_Ubuntu|Any CPU.ActiveCfg = Release_Ubuntu|Any CPU - {390E2568-57B7-4D17-91E5-C29336368CCF}.Release_Ubuntu|Any CPU.Build.0 = Release_Ubuntu|Any CPU - {390E2568-57B7-4D17-91E5-C29336368CCF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {390E2568-57B7-4D17-91E5-C29336368CCF}.Release|Any CPU.Build.0 = Release|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug|Any CPU.Build.0 = Debug|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release_Ubuntu|Any CPU.ActiveCfg = Release_Ubuntu|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release_Ubuntu|Any CPU.Build.0 = Release_Ubuntu|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release|Any CPU.ActiveCfg = Release|Any CPU - {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release|Any CPU.Build.0 = Release|Any CPU {B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU {B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU {B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -41,6 +25,22 @@ Global {B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Release_Ubuntu|Any CPU.Build.0 = Release_Ubuntu|Any CPU {B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Release|Any CPU.ActiveCfg = Release|Any CPU {B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Release|Any CPU.Build.0 = Release|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug|Any CPU.Build.0 = Debug|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release_Ubuntu|Any CPU.ActiveCfg = Release_Ubuntu|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release_Ubuntu|Any CPU.Build.0 = Release_Ubuntu|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release|Any CPU.ActiveCfg = Release|Any CPU + {52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Release|Any CPU.Build.0 = Release|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Release_Ubuntu|Any CPU.ActiveCfg = Release_Ubuntu|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Release_Ubuntu|Any CPU.Build.0 = Release_Ubuntu|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {390E2568-57B7-4D17-91E5-C29336368CCF}.Release|Any CPU.Build.0 = Release|Any CPU {B81A24C8-25BB-42B2-AF99-1E1EACCE74C7}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU {B81A24C8-25BB-42B2-AF99-1E1EACCE74C7}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU {B81A24C8-25BB-42B2-AF99-1E1EACCE74C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -50,6 +50,9 @@ Global {B81A24C8-25BB-42B2-AF99-1E1EACCE74C7}.Release|Any CPU.ActiveCfg = Release|Any CPU {B81A24C8-25BB-42B2-AF99-1E1EACCE74C7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution StartupItem = websocket-sharp\websocket-sharp.csproj Policies = $0 diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 3b82ca7b4..c787ed7a3 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -330,5 +330,26 @@ public static string ToString(this T[] array, string separater) return sb.ToString(); } + + public static int BlockingRead(this System.IO.Stream stream, byte[] buffer, int offset, int count) + { + IAsyncResult ar = stream.BeginRead(buffer, offset, count, null, null); + return stream.EndRead(ar); + } + + public static void ReadExactBytes(this System.IO.Stream stream, byte[] buffer, int offset, int count) + { + int remain = count; + + while (remain > 0) + { + int nread = stream.BlockingRead(buffer, offset, remain); + if (nread == 0) + throw new IOException("Connection has been closed before request bytes are read"); + + remain -= nread; + offset += nread; + } + } } } diff --git a/websocket-sharp/Frame/WsFrame.cs b/websocket-sharp/Frame/WsFrame.cs index f9756b973..53adb8e50 100644 --- a/websocket-sharp/Frame/WsFrame.cs +++ b/websocket-sharp/Frame/WsFrame.cs @@ -192,10 +192,14 @@ public static WsFrame Parse(TStream stream, bool unmask) int readLen = 0; buffer1 = new byte[buffer1Len]; - readLen = stream.Read(buffer1, 0, buffer1Len); - if (readLen < buffer1Len) + + try + { + stream.ReadExactBytes(buffer1, 0, buffer1Len); + } + catch (IOException e) { - return null; + return null; } // FIN @@ -239,11 +243,14 @@ public static WsFrame Parse(TStream stream, bool unmask) if (buffer2Len > 0) { buffer2 = new byte[buffer2Len]; - readLen = stream.Read(buffer2, 0, buffer2Len); - if (readLen < buffer2Len) + try { - return null; + stream.ReadExactBytes(buffer2, 0, buffer2Len); + } + catch (IOException) + { + return null; } extPayloadLen = buffer2; @@ -266,11 +273,14 @@ public static WsFrame Parse(TStream stream, bool unmask) if (masked == Mask.MASK) { maskingKey = new byte[maskingKeyLen]; - readLen = stream.Read(maskingKey, 0, maskingKeyLen); - if (readLen < maskingKeyLen) + try { - return null; + stream.ReadExactBytes(maskingKey, 0, maskingKeyLen); + } + catch (IOException) + { + return null; } } // Payload Data @@ -281,11 +291,14 @@ public static WsFrame Parse(TStream stream, bool unmask) else if (buffer3Len <= (ulong)_readBufferLen) { buffer3 = new byte[buffer3Len]; - readLen = stream.Read(buffer3, 0, (int)buffer3Len); - if (readLen < (int)buffer3Len) + try { - return null; + stream.ReadExactBytes(buffer3, 0, buffer3.Length); + } + catch (IOException) + { + return null; } } else diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index 84ca9111c..ea95c3137 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -294,9 +294,14 @@ public void Stop() } public void Stop(CloseStatusCode code, string reason) + { + Stop((ushort)code, reason); + } + + public void Stop(ushort code, string reason) { if (!IsBound || IsStop) return; - + IsStop = true; _socket.Close(code, reason); } diff --git a/websocket-sharp/Stream/IWsStream.cs b/websocket-sharp/Stream/IWsStream.cs index 27ba1e451..e94ad0394 100644 --- a/websocket-sharp/Stream/IWsStream.cs +++ b/websocket-sharp/Stream/IWsStream.cs @@ -34,7 +34,6 @@ namespace WebSocketSharp.Stream public interface IWsStream : IDisposable { void Close(); - int Read(byte[] buffer, int offset, int size); int ReadByte(); WsFrame ReadFrame(); void Write(byte[] buffer, int offset, int count); diff --git a/websocket-sharp/Stream/WsStream.cs b/websocket-sharp/Stream/WsStream.cs index 936a5cf2b..d5aba92cf 100644 --- a/websocket-sharp/Stream/WsStream.cs +++ b/websocket-sharp/Stream/WsStream.cs @@ -72,26 +72,13 @@ public void Dispose() _innerStream.Dispose(); } - public int Read(byte[] buffer, int offset, int size) - { - lock (_forRead) - { - var readLen = _innerStream.Read(buffer, offset, size); - if (readLen < size) - { - var msg = String.Format("Data can not be read from {0}.", typeof(TStream).Name); - throw new IOException(msg); - } - return readLen; - } - } - public int ReadByte() { - lock (_forRead) - { - return _innerStream.ReadByte(); - } + byte[] buffer = new byte[1]; + + _innerStream.ReadExactBytes(buffer, 0, 1); + + return buffer[0]; } public WsFrame ReadFrame() diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index b64f50ee9..b561939d1 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -297,24 +297,29 @@ private void close(PayloadData data) private void close(CloseStatusCode code, string reason) { - var data = new List(((ushort)code).ToBytes(ByteOrder.BIG)); + closeWithCode((ushort)code, reason); + } - if (reason != String.Empty) - { - var buffer = Encoding.UTF8.GetBytes(reason); - data.AddRange(buffer); - } + private void closeWithCode(ushort code, string reason) + { + var data = new List(code.ToBytes(ByteOrder.BIG)); - var payloadData = new PayloadData(data.ToArray()); + if (reason != String.Empty) + { + var buffer = Encoding.UTF8.GetBytes(reason); + data.AddRange(buffer); + } - if (payloadData.Length > 125) - { - var msg = "Close frame must have a payload length of 125 bytes or less."; - error(msg); - return; - } + var payloadData = new PayloadData(data.ToArray()); - close(payloadData); + 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() @@ -543,6 +548,11 @@ private void error(string message) OnError.Emit(this, new ErrorEventArgs(message)); } + private bool isBindingAnyIP() + { + return _uri.Host.Equals("0.0.0.0"); + } + private bool isValidRequest(string[] request, out string message) { string reqConnection, reqHost, reqUpgrade, secWsVersion; @@ -585,7 +595,7 @@ private bool isValidRequest(string[] request, out string message) return false; } } - else if (request[i].Contains("Host:")) + else if (request[i].Contains("Host:") && !isBindingAnyIP()) { reqHost = request[i].GetHeaderValue(":"); if (expectedHost.NotEqualsDo(reqHost, func("Host"), out message, true)) @@ -1146,6 +1156,11 @@ public void Close(CloseStatusCode code, string reason) close(code, reason); } + public void Close(ushort code, string reason) + { + closeWithCode(code, reason); + } + public void Connect() { if (_readyState == WsState.OPEN) diff --git a/websocket-sharp/websocket-sharp.csproj b/websocket-sharp/websocket-sharp.csproj index 19b710e1b..a34f208d9 100644 --- a/websocket-sharp/websocket-sharp.csproj +++ b/websocket-sharp/websocket-sharp.csproj @@ -1,5 +1,5 @@ - - + + Debug AnyCPU @@ -12,6 +12,25 @@ v3.5 true websocket-sharp.snk + + + 3.5 + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true true @@ -22,14 +41,17 @@ prompt 4 false + AllRules.ruleset - none + pdbonly false bin\Release prompt 4 false + AllRules.ruleset + true true @@ -40,6 +62,7 @@ prompt 4 false + AllRules.ruleset none @@ -48,6 +71,7 @@ prompt 4 false + AllRules.ruleset @@ -79,8 +103,20 @@ - - - + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + \ No newline at end of file