├── .gitignore ├── README.md ├── websocket-sharp-core ├── websocket-sharp-core.csproj ├── Properties │ └── PublishProfiles │ │ └── FolderProfile.pubxml ├── Server │ ├── ServerState.cs │ ├── IWebSocketSession.cs │ ├── HttpRequestEventArgs.cs │ ├── WebSocketServiceHost`1.cs │ └── WebSocketServiceHost.cs ├── Net │ ├── HttpHeaderType.cs │ ├── LineState.cs │ ├── InputState.cs │ ├── InputChunkState.cs │ ├── AuthenticationSchemes.cs │ ├── HttpVersion.cs │ ├── QueryStringCollection.cs │ ├── Chunk.cs │ ├── HttpBasicIdentity.cs │ ├── CookieException.cs │ ├── ReadBufferState.cs │ ├── HttpHeaderInfo.cs │ ├── AuthenticationBase.cs │ ├── HttpListenerException.cs │ ├── HttpStreamAsyncResult.cs │ ├── AuthenticationChallenge.cs │ ├── HttpListenerAsyncResult.cs │ ├── HttpDigestIdentity.cs │ ├── HttpResponseHeader.cs │ ├── ServerSslConfiguration.cs │ ├── SslConfiguration.cs │ ├── NetworkCredential.cs │ ├── ClientSslConfiguration.cs │ ├── ChunkedRequestStream.cs │ ├── EndPointManager.cs │ ├── HttpRequestHeader.cs │ ├── HttpListenerPrefix.cs │ └── HttpListenerContext.cs ├── ByteOrder.cs ├── Mask.cs ├── Rsv.cs ├── CompressionMethod.cs ├── Fin.cs ├── LogLevel.cs ├── Opcode.cs ├── WebSocketState.cs ├── WebSocketException.cs ├── ErrorEventArgs.cs ├── CloseEventArgs.cs ├── PayloadData.cs ├── LogData.cs ├── CloseStatusCode.cs ├── MessageEventArgs.cs ├── Helpers.cs ├── HttpResponse.cs ├── HttpBase.cs └── HttpRequest.cs ├── LICENSE └── websocket-sharp-core.sln /.gitignore: -------------------------------------------------------------------------------- 1 | obj/ 2 | bin/ 3 | *.user 4 | .vs/ 5 | /.idea/* 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # websocket-sharp-core 2 | A websocket-sharp library port to .net standart 2.0 3 | 4 | Original library: 5 | https://github.com/sta/websocket-sharp 6 | 7 | Only client functionality was tested 8 | -------------------------------------------------------------------------------- /websocket-sharp-core/websocket-sharp-core.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | websocket-sharp port to net standart 6 | 7 | 8 | 9 | true 10 | websocketsharp.core 11 | 1.0.1 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /websocket-sharp-core/Properties/PublishProfiles/FolderProfile.pubxml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | FileSystem 9 | Release 10 | netstandard2.0 11 | bin\Release\PublishOutput 12 | 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ImoutoChan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /websocket-sharp-core.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26730.12 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "websocket-sharp-core", "websocket-sharp-core\websocket-sharp-core.csproj", "{F4FD2BA8-46A8-420A-8700-0076DC8E563A}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F4FD2BA8-46A8-420A-8700-0076DC8E563A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F4FD2BA8-46A8-420A-8700-0076DC8E563A}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F4FD2BA8-46A8-420A-8700-0076DC8E563A}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F4FD2BA8-46A8-420A-8700-0076DC8E563A}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {1986D51E-1FD2-4417-A21F-B182A7AC91B1} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /websocket-sharp-core/Server/ServerState.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ServerState.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp.Server 32 | { 33 | internal enum ServerState 34 | { 35 | Ready, 36 | Start, 37 | ShuttingDown, 38 | Stop 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpHeaderType.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpHeaderType.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp.Net 32 | { 33 | [Flags] 34 | internal enum HttpHeaderType 35 | { 36 | Unspecified = 0, 37 | Request = 1, 38 | Response = 1 << 1, 39 | Restricted = 1 << 2, 40 | MultiValue = 1 << 3, 41 | MultiValueInRequest = 1 << 4, 42 | MultiValueInResponse = 1 << 5 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /websocket-sharp-core/ByteOrder.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ByteOrder.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Specifies the byte order. 35 | /// 36 | public enum ByteOrder 37 | { 38 | /// 39 | /// Specifies Little-endian. 40 | /// 41 | Little, 42 | /// 43 | /// Specifies Big-endian. 44 | /// 45 | Big 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/LineState.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * LineState.cs 4 | * 5 | * This code is derived from HttpConnection.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2014-2015 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | 42 | namespace WebSocketSharp.Net 43 | { 44 | internal enum LineState 45 | { 46 | None, 47 | Cr, 48 | Lf 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/InputState.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * InputState.cs 4 | * 5 | * This code is derived from HttpConnection.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2014-2015 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | 42 | namespace WebSocketSharp.Net 43 | { 44 | internal enum InputState 45 | { 46 | RequestLine, 47 | Headers 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/InputChunkState.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * InputChunkState.cs 4 | * 5 | * This code is derived from ChunkStream.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2003 Ximian, Inc (http://www.ximian.com) 11 | * Copyright (c) 2014-2015 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | 42 | namespace WebSocketSharp.Net 43 | { 44 | internal enum InputChunkState 45 | { 46 | None, 47 | Data, 48 | DataEnded, 49 | Trailer, 50 | End 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /websocket-sharp-core/Mask.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * Mask.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Indicates whether the payload data of a WebSocket frame is masked. 35 | /// 36 | /// 37 | /// The values of this enumeration are defined in 38 | /// Section 5.2 of RFC 6455. 39 | /// 40 | internal enum Mask : byte 41 | { 42 | /// 43 | /// Equivalent to numeric value 0. Indicates not masked. 44 | /// 45 | Off = 0x0, 46 | /// 47 | /// Equivalent to numeric value 1. Indicates masked. 48 | /// 49 | On = 0x1 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /websocket-sharp-core/Rsv.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * Rsv.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Indicates whether each RSV (RSV1, RSV2, and RSV3) of a WebSocket frame is non-zero. 35 | /// 36 | /// 37 | /// The values of this enumeration are defined in 38 | /// Section 5.2 of RFC 6455. 39 | /// 40 | internal enum Rsv : byte 41 | { 42 | /// 43 | /// Equivalent to numeric value 0. Indicates zero. 44 | /// 45 | Off = 0x0, 46 | /// 47 | /// Equivalent to numeric value 1. Indicates non-zero. 48 | /// 49 | On = 0x1 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /websocket-sharp-core/CompressionMethod.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * CompressionMethod.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Specifies the compression method used to compress a message on the WebSocket connection. 35 | /// 36 | /// 37 | /// The compression methods are defined in 38 | /// 39 | /// Compression Extensions for WebSocket. 40 | /// 41 | public enum CompressionMethod : byte 42 | { 43 | /// 44 | /// Specifies non compression. 45 | /// 46 | None, 47 | /// 48 | /// Specifies DEFLATE. 49 | /// 50 | Deflate 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /websocket-sharp-core/Fin.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * Fin.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Indicates whether a WebSocket frame is the final frame of a message. 35 | /// 36 | /// 37 | /// The values of this enumeration are defined in 38 | /// Section 5.2 of RFC 6455. 39 | /// 40 | internal enum Fin : byte 41 | { 42 | /// 43 | /// Equivalent to numeric value 0. Indicates more frames of a message follow. 44 | /// 45 | More = 0x0, 46 | /// 47 | /// Equivalent to numeric value 1. Indicates the final frame of a message. 48 | /// 49 | Final = 0x1 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /websocket-sharp-core/LogLevel.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * LogLevel.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Specifies the logging level. 35 | /// 36 | public enum LogLevel 37 | { 38 | /// 39 | /// Specifies the bottom logging level. 40 | /// 41 | Trace, 42 | /// 43 | /// Specifies the 2nd logging level from the bottom. 44 | /// 45 | Debug, 46 | /// 47 | /// Specifies the 3rd logging level from the bottom. 48 | /// 49 | Info, 50 | /// 51 | /// Specifies the 3rd logging level from the top. 52 | /// 53 | Warn, 54 | /// 55 | /// Specifies the 2nd logging level from the top. 56 | /// 57 | Error, 58 | /// 59 | /// Specifies the top logging level. 60 | /// 61 | Fatal 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/AuthenticationSchemes.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * AuthenticationSchemes.cs 4 | * 5 | * This code is derived from AuthenticationSchemes.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2012-2016 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Atsushi Enomoto 37 | */ 38 | #endregion 39 | 40 | using System; 41 | 42 | namespace WebSocketSharp.Net 43 | { 44 | /// 45 | /// Specifies the scheme for authentication. 46 | /// 47 | public enum AuthenticationSchemes 48 | { 49 | /// 50 | /// No authentication is allowed. 51 | /// 52 | None, 53 | /// 54 | /// Specifies digest authentication. 55 | /// 56 | Digest = 1, 57 | /// 58 | /// Specifies basic authentication. 59 | /// 60 | Basic = 8, 61 | /// 62 | /// Specifies anonymous authentication. 63 | /// 64 | Anonymous = 0x8000 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpVersion.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpVersion.cs 4 | * 5 | * This code is derived from System.Net.HttpVersion.cs of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2012-2014 sta.blockhead 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy 13 | * of this software and associated documentation files (the "Software"), to deal 14 | * in the Software without restriction, including without limitation the rights 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the Software is 17 | * furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included in 20 | * all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | * THE SOFTWARE. 29 | */ 30 | #endregion 31 | 32 | #region Authors 33 | /* 34 | * Authors: 35 | * - Lawrence Pit 36 | */ 37 | #endregion 38 | 39 | using System; 40 | 41 | namespace WebSocketSharp.Net 42 | { 43 | /// 44 | /// Provides the HTTP version numbers. 45 | /// 46 | public class HttpVersion 47 | { 48 | #region Public Fields 49 | 50 | /// 51 | /// Provides a instance for the HTTP/1.0. 52 | /// 53 | public static readonly Version Version10 = new Version (1, 0); 54 | 55 | /// 56 | /// Provides a instance for the HTTP/1.1. 57 | /// 58 | public static readonly Version Version11 = new Version (1, 1); 59 | 60 | #endregion 61 | 62 | #region Public Constructors 63 | 64 | /// 65 | /// Initializes a new instance of the class. 66 | /// 67 | public HttpVersion () 68 | { 69 | } 70 | 71 | #endregion 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/QueryStringCollection.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * QueryStringCollection.cs 4 | * 5 | * This code is derived from System.Net.HttpUtility.cs of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005-2009 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2014 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Patrik Torstensson 37 | * - Wictor Wilén (decode/encode functions) 38 | * - Tim Coleman 39 | * - Gonzalo Paniagua Javier 40 | */ 41 | #endregion 42 | 43 | using System; 44 | using System.Collections.Specialized; 45 | using System.Text; 46 | 47 | namespace WebSocketSharp.Net 48 | { 49 | internal sealed class QueryStringCollection : NameValueCollection 50 | { 51 | public override string ToString () 52 | { 53 | var cnt = Count; 54 | if (cnt == 0) 55 | return String.Empty; 56 | 57 | var output = new StringBuilder (); 58 | var keys = AllKeys; 59 | foreach (var key in keys) 60 | output.AppendFormat ("{0}={1}&", key, this [key]); 61 | 62 | if (output.Length > 0) 63 | output.Length--; 64 | 65 | return output.ToString (); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /websocket-sharp-core/Opcode.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * Opcode.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Indicates the WebSocket frame type. 35 | /// 36 | /// 37 | /// The values of this enumeration are defined in 38 | /// Section 5.2 of RFC 6455. 39 | /// 40 | public enum Opcode : byte 41 | { 42 | /// 43 | /// Equivalent to numeric value 0. Indicates continuation frame. 44 | /// 45 | Cont = 0x0, 46 | /// 47 | /// Equivalent to numeric value 1. Indicates text frame. 48 | /// 49 | Text = 0x1, 50 | /// 51 | /// Equivalent to numeric value 2. Indicates binary frame. 52 | /// 53 | Binary = 0x2, 54 | /// 55 | /// Equivalent to numeric value 8. Indicates connection close frame. 56 | /// 57 | Close = 0x8, 58 | /// 59 | /// Equivalent to numeric value 9. Indicates ping frame. 60 | /// 61 | Ping = 0x9, 62 | /// 63 | /// Equivalent to numeric value 10. Indicates pong frame. 64 | /// 65 | Pong = 0xa 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /websocket-sharp-core/WebSocketState.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketState.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2010-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Indicates the state of a WebSocket connection. 35 | /// 36 | /// 37 | /// The values of this enumeration are defined in 38 | /// The WebSocket API. 39 | /// 40 | public enum WebSocketState : ushort 41 | { 42 | /// 43 | /// Equivalent to numeric value 0. Indicates that the connection hasn't yet been established. 44 | /// 45 | Connecting = 0, 46 | /// 47 | /// Equivalent to numeric value 1. Indicates that the connection has been established, 48 | /// and the communication is possible. 49 | /// 50 | Open = 1, 51 | /// 52 | /// Equivalent to numeric value 2. Indicates that the connection is going through 53 | /// the closing handshake, or the WebSocket.Close method has been invoked. 54 | /// 55 | Closing = 2, 56 | /// 57 | /// Equivalent to numeric value 3. Indicates that the connection has been closed or 58 | /// couldn't be established. 59 | /// 60 | Closed = 3 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/Chunk.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * Chunk.cs 4 | * 5 | * This code is derived from ChunkStream.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2003 Ximian, Inc (http://www.ximian.com) 11 | * Copyright (c) 2014-2015 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | 42 | namespace WebSocketSharp.Net 43 | { 44 | internal class Chunk 45 | { 46 | #region Private Fields 47 | 48 | private byte[] _data; 49 | private int _offset; 50 | 51 | #endregion 52 | 53 | #region Public Constructors 54 | 55 | public Chunk (byte[] data) 56 | { 57 | _data = data; 58 | } 59 | 60 | #endregion 61 | 62 | #region Public Properties 63 | 64 | public int ReadLeft { 65 | get { 66 | return _data.Length - _offset; 67 | } 68 | } 69 | 70 | #endregion 71 | 72 | #region Public Methods 73 | 74 | public int Read (byte[] buffer, int offset, int count) 75 | { 76 | var left = _data.Length - _offset; 77 | if (left == 0) 78 | return left; 79 | 80 | if (count > left) 81 | count = left; 82 | 83 | Buffer.BlockCopy (_data, _offset, buffer, offset, count); 84 | _offset += count; 85 | 86 | return count; 87 | } 88 | 89 | #endregion 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpBasicIdentity.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpBasicIdentity.cs 4 | * 5 | * This code is derived from System.Net.HttpListenerBasicIdentity.cs of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2014 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | using System.Security.Principal; 42 | 43 | namespace WebSocketSharp.Net 44 | { 45 | /// 46 | /// Holds the user name and password from the HTTP Basic authentication credentials. 47 | /// 48 | public class HttpBasicIdentity : GenericIdentity 49 | { 50 | #region Private Fields 51 | 52 | private string _password; 53 | 54 | #endregion 55 | 56 | #region internal Constructors 57 | 58 | internal HttpBasicIdentity (string username, string password) 59 | : base (username, "Basic") 60 | { 61 | _password = password; 62 | } 63 | 64 | #endregion 65 | 66 | #region Public Properties 67 | 68 | /// 69 | /// Gets the password from the HTTP Basic authentication credentials. 70 | /// 71 | /// 72 | /// A that represents the password. 73 | /// 74 | public virtual string Password { 75 | get { 76 | return _password; 77 | } 78 | } 79 | 80 | #endregion 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/CookieException.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * CookieException.cs 4 | * 5 | * This code is derived from System.Net.CookieException.cs of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2012-2014 sta.blockhead 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy 13 | * of this software and associated documentation files (the "Software"), to deal 14 | * in the Software without restriction, including without limitation the rights 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the Software is 17 | * furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included in 20 | * all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | * THE SOFTWARE. 29 | */ 30 | #endregion 31 | 32 | #region Authors 33 | /* 34 | * Authors: 35 | * - Lawrence Pit 36 | */ 37 | #endregion 38 | 39 | using System; 40 | using System.Runtime.Serialization; 41 | //using System.Security.Permissions; 42 | 43 | namespace WebSocketSharp.Net 44 | { 45 | /// 46 | /// The exception that is thrown when a gets an error. 47 | /// 48 | [Serializable] 49 | public class CookieException : FormatException 50 | { 51 | #region Internal Constructors 52 | 53 | internal CookieException (string message) 54 | : base (message) 55 | { 56 | } 57 | 58 | internal CookieException (string message, Exception innerException) 59 | : base (message, innerException) 60 | { 61 | } 62 | 63 | #endregion 64 | 65 | #region Protected Constructors 66 | 67 | 68 | 69 | #endregion 70 | 71 | #region Public Constructors 72 | 73 | /// 74 | /// Initializes a new instance of the class. 75 | /// 76 | public CookieException () 77 | : base () 78 | { 79 | } 80 | 81 | #endregion 82 | 83 | #region Public Methods 84 | 85 | 86 | 87 | #endregion 88 | 89 | #region Explicit Interface Implementation 90 | 91 | 92 | 93 | #endregion 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /websocket-sharp-core/Server/IWebSocketSession.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * IWebSocketSession.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using WebSocketSharp.Net.WebSockets; 31 | 32 | namespace WebSocketSharp.Server 33 | { 34 | /// 35 | /// Exposes the properties used to access the information in a session in a WebSocket service. 36 | /// 37 | public interface IWebSocketSession 38 | { 39 | #region Properties 40 | 41 | /// 42 | /// Gets the information in the connection request to the WebSocket service. 43 | /// 44 | /// 45 | /// A that provides the access to the connection request. 46 | /// 47 | WebSocketContext Context { get; } 48 | 49 | /// 50 | /// Gets the unique ID of the session. 51 | /// 52 | /// 53 | /// A that represents the unique ID of the session. 54 | /// 55 | string ID { get; } 56 | 57 | /// 58 | /// Gets the WebSocket subprotocol used in the session. 59 | /// 60 | /// 61 | /// A that represents the subprotocol if any. 62 | /// 63 | string Protocol { get; } 64 | 65 | /// 66 | /// Gets the time that the session has started. 67 | /// 68 | /// 69 | /// A that represents the time that the session has started. 70 | /// 71 | DateTime StartTime { get; } 72 | 73 | /// 74 | /// Gets the state of the used in the session. 75 | /// 76 | /// 77 | /// One of the enum values, indicates the state of 78 | /// the used in the session. 79 | /// 80 | WebSocketState State { get; } 81 | 82 | #endregion 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /websocket-sharp-core/Server/HttpRequestEventArgs.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpRequestEventArgs.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using WebSocketSharp.Net; 31 | 32 | namespace WebSocketSharp.Server 33 | { 34 | /// 35 | /// Represents the event data for the HTTP request event that the emits. 36 | /// 37 | /// 38 | /// 39 | /// An HTTP request event occurs when the receives an HTTP request. 40 | /// 41 | /// 42 | /// If you would like to get the request data sent from a client, 43 | /// you should access the property. 44 | /// 45 | /// 46 | /// And if you would like to get the response data used to return a response, 47 | /// you should access the property. 48 | /// 49 | /// 50 | public class HttpRequestEventArgs : EventArgs 51 | { 52 | #region Private Fields 53 | 54 | private HttpListenerRequest _request; 55 | private HttpListenerResponse _response; 56 | 57 | #endregion 58 | 59 | #region Internal Constructors 60 | 61 | internal HttpRequestEventArgs (HttpListenerContext context) 62 | { 63 | _request = context.Request; 64 | _response = context.Response; 65 | } 66 | 67 | #endregion 68 | 69 | #region Public Properties 70 | 71 | /// 72 | /// Gets the HTTP request data sent from a client. 73 | /// 74 | /// 75 | /// A that represents the request data. 76 | /// 77 | public HttpListenerRequest Request { 78 | get { 79 | return _request; 80 | } 81 | } 82 | 83 | /// 84 | /// Gets the HTTP response data used to return a response to the client. 85 | /// 86 | /// 87 | /// A that represents the response data. 88 | /// 89 | public HttpListenerResponse Response { 90 | get { 91 | return _response; 92 | } 93 | } 94 | 95 | #endregion 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/ReadBufferState.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ReadBufferState.cs 4 | * 5 | * This code is derived from ChunkedInputStream.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2014-2015 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | 42 | namespace WebSocketSharp.Net 43 | { 44 | internal class ReadBufferState 45 | { 46 | #region Private Fields 47 | 48 | private HttpStreamAsyncResult _asyncResult; 49 | private byte[] _buffer; 50 | private int _count; 51 | private int _initialCount; 52 | private int _offset; 53 | 54 | #endregion 55 | 56 | #region Public Constructors 57 | 58 | public ReadBufferState ( 59 | byte[] buffer, int offset, int count, HttpStreamAsyncResult asyncResult) 60 | { 61 | _buffer = buffer; 62 | _offset = offset; 63 | _count = count; 64 | _initialCount = count; 65 | _asyncResult = asyncResult; 66 | } 67 | 68 | #endregion 69 | 70 | #region Public Properties 71 | 72 | public HttpStreamAsyncResult AsyncResult { 73 | get { 74 | return _asyncResult; 75 | } 76 | 77 | set { 78 | _asyncResult = value; 79 | } 80 | } 81 | 82 | public byte[] Buffer { 83 | get { 84 | return _buffer; 85 | } 86 | 87 | set { 88 | _buffer = value; 89 | } 90 | } 91 | 92 | public int Count { 93 | get { 94 | return _count; 95 | } 96 | 97 | set { 98 | _count = value; 99 | } 100 | } 101 | 102 | public int InitialCount { 103 | get { 104 | return _initialCount; 105 | } 106 | 107 | set { 108 | _initialCount = value; 109 | } 110 | } 111 | 112 | public int Offset { 113 | get { 114 | return _offset; 115 | } 116 | 117 | set { 118 | _offset = value; 119 | } 120 | } 121 | 122 | #endregion 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpHeaderInfo.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpHeaderInfo.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp.Net 32 | { 33 | internal class HttpHeaderInfo 34 | { 35 | #region Private Fields 36 | 37 | private string _name; 38 | private HttpHeaderType _type; 39 | 40 | #endregion 41 | 42 | #region Internal Constructors 43 | 44 | internal HttpHeaderInfo (string name, HttpHeaderType type) 45 | { 46 | _name = name; 47 | _type = type; 48 | } 49 | 50 | #endregion 51 | 52 | #region Internal Properties 53 | 54 | internal bool IsMultiValueInRequest { 55 | get { 56 | return (_type & HttpHeaderType.MultiValueInRequest) == HttpHeaderType.MultiValueInRequest; 57 | } 58 | } 59 | 60 | internal bool IsMultiValueInResponse { 61 | get { 62 | return (_type & HttpHeaderType.MultiValueInResponse) == HttpHeaderType.MultiValueInResponse; 63 | } 64 | } 65 | 66 | #endregion 67 | 68 | #region Public Properties 69 | 70 | public bool IsRequest { 71 | get { 72 | return (_type & HttpHeaderType.Request) == HttpHeaderType.Request; 73 | } 74 | } 75 | 76 | public bool IsResponse { 77 | get { 78 | return (_type & HttpHeaderType.Response) == HttpHeaderType.Response; 79 | } 80 | } 81 | 82 | public string Name { 83 | get { 84 | return _name; 85 | } 86 | } 87 | 88 | public HttpHeaderType Type { 89 | get { 90 | return _type; 91 | } 92 | } 93 | 94 | #endregion 95 | 96 | #region Public Methods 97 | 98 | public bool IsMultiValue (bool response) 99 | { 100 | return (_type & HttpHeaderType.MultiValue) == HttpHeaderType.MultiValue 101 | ? (response ? IsResponse : IsRequest) 102 | : (response ? IsMultiValueInResponse : IsMultiValueInRequest); 103 | } 104 | 105 | public bool IsRestricted (bool response) 106 | { 107 | return (_type & HttpHeaderType.Restricted) == HttpHeaderType.Restricted 108 | ? (response ? IsResponse : IsRequest) 109 | : false; 110 | } 111 | 112 | #endregion 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /websocket-sharp-core/WebSocketException.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketException.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// The exception that is thrown when a gets a fatal error. 35 | /// 36 | public class WebSocketException : Exception 37 | { 38 | #region Private Fields 39 | 40 | private CloseStatusCode _code; 41 | 42 | #endregion 43 | 44 | #region Internal Constructors 45 | 46 | internal WebSocketException () 47 | : this (CloseStatusCode.Abnormal, null, null) 48 | { 49 | } 50 | 51 | internal WebSocketException (Exception innerException) 52 | : this (CloseStatusCode.Abnormal, null, innerException) 53 | { 54 | } 55 | 56 | internal WebSocketException (string message) 57 | : this (CloseStatusCode.Abnormal, message, null) 58 | { 59 | } 60 | 61 | internal WebSocketException (CloseStatusCode code) 62 | : this (code, null, null) 63 | { 64 | } 65 | 66 | internal WebSocketException (string message, Exception innerException) 67 | : this (CloseStatusCode.Abnormal, message, innerException) 68 | { 69 | } 70 | 71 | internal WebSocketException (CloseStatusCode code, Exception innerException) 72 | : this (code, null, innerException) 73 | { 74 | } 75 | 76 | internal WebSocketException (CloseStatusCode code, string message) 77 | : this (code, message, null) 78 | { 79 | } 80 | 81 | internal WebSocketException (CloseStatusCode code, string message, Exception innerException) 82 | : base (message ?? code.GetMessage (), innerException) 83 | { 84 | _code = code; 85 | } 86 | 87 | #endregion 88 | 89 | #region Public Properties 90 | 91 | /// 92 | /// Gets the status code indicating the cause of the exception. 93 | /// 94 | /// 95 | /// One of the enum values, represents the status code 96 | /// indicating the cause of the exception. 97 | /// 98 | public CloseStatusCode Code { 99 | get { 100 | return _code; 101 | } 102 | } 103 | 104 | #endregion 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /websocket-sharp-core/ErrorEventArgs.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ErrorEventArgs.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | #region Contributors 30 | /* 31 | * Contributors: 32 | * - Frank Razenberg 33 | */ 34 | #endregion 35 | 36 | using System; 37 | 38 | namespace WebSocketSharp 39 | { 40 | /// 41 | /// Represents the event data for the event. 42 | /// 43 | /// 44 | /// 45 | /// A event occurs when the gets 46 | /// an error. 47 | /// 48 | /// 49 | /// If you would like to get the error message, you should access 50 | /// the property. 51 | /// 52 | /// 53 | /// And if the error is due to an exception, you can get the exception by accessing 54 | /// the property. 55 | /// 56 | /// 57 | public class ErrorEventArgs : EventArgs 58 | { 59 | #region Private Fields 60 | 61 | private Exception _exception; 62 | private string _message; 63 | 64 | #endregion 65 | 66 | #region Internal Constructors 67 | 68 | internal ErrorEventArgs (string message) 69 | : this (message, null) 70 | { 71 | } 72 | 73 | internal ErrorEventArgs (string message, Exception exception) 74 | { 75 | _message = message; 76 | _exception = exception; 77 | } 78 | 79 | #endregion 80 | 81 | #region Public Properties 82 | 83 | /// 84 | /// Gets the exception that caused the error. 85 | /// 86 | /// 87 | /// An instance that represents the cause of the error, 88 | /// or if the error isn't due to an exception. 89 | /// 90 | public Exception Exception { 91 | get { 92 | return _exception; 93 | } 94 | } 95 | 96 | /// 97 | /// Gets the error message. 98 | /// 99 | /// 100 | /// A that represents the error message. 101 | /// 102 | public string Message { 103 | get { 104 | return _message; 105 | } 106 | } 107 | 108 | #endregion 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /websocket-sharp-core/Server/WebSocketServiceHost`1.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketServiceHost`1.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp.Server 32 | { 33 | internal class WebSocketServiceHost : WebSocketServiceHost 34 | where TBehavior : WebSocketBehavior 35 | { 36 | #region Private Fields 37 | 38 | private Func _initializer; 39 | private Logger _logger; 40 | private string _path; 41 | private WebSocketSessionManager _sessions; 42 | 43 | #endregion 44 | 45 | #region Internal Constructors 46 | 47 | internal WebSocketServiceHost (string path, Func initializer, Logger logger) 48 | { 49 | _path = path; 50 | _initializer = initializer; 51 | _logger = logger; 52 | _sessions = new WebSocketSessionManager (logger); 53 | } 54 | 55 | #endregion 56 | 57 | #region Public Properties 58 | 59 | public override bool KeepClean { 60 | get { 61 | return _sessions.KeepClean; 62 | } 63 | 64 | set { 65 | var msg = _sessions.State.CheckIfAvailable (true, false, false); 66 | if (msg != null) { 67 | _logger.Error (msg); 68 | return; 69 | } 70 | 71 | _sessions.KeepClean = value; 72 | } 73 | } 74 | 75 | public override string Path { 76 | get { 77 | return _path; 78 | } 79 | } 80 | 81 | public override WebSocketSessionManager Sessions { 82 | get { 83 | return _sessions; 84 | } 85 | } 86 | 87 | public override Type Type { 88 | get { 89 | return typeof (TBehavior); 90 | } 91 | } 92 | 93 | public override TimeSpan WaitTime { 94 | get { 95 | return _sessions.WaitTime; 96 | } 97 | 98 | set { 99 | var msg = _sessions.State.CheckIfAvailable (true, false, false) ?? 100 | value.CheckIfValidWaitTime (); 101 | 102 | if (msg != null) { 103 | _logger.Error (msg); 104 | return; 105 | } 106 | 107 | _sessions.WaitTime = value; 108 | } 109 | } 110 | 111 | #endregion 112 | 113 | #region Protected Methods 114 | 115 | protected override WebSocketBehavior CreateSession () 116 | { 117 | return _initializer (); 118 | } 119 | 120 | #endregion 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/AuthenticationBase.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * AuthenticationBase.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using System.Collections.Specialized; 31 | using System.Text; 32 | 33 | namespace WebSocketSharp.Net 34 | { 35 | internal abstract class AuthenticationBase 36 | { 37 | #region Private Fields 38 | 39 | private AuthenticationSchemes _scheme; 40 | 41 | #endregion 42 | 43 | #region Internal Fields 44 | 45 | internal NameValueCollection Parameters; 46 | 47 | #endregion 48 | 49 | #region Protected Constructors 50 | 51 | protected AuthenticationBase (AuthenticationSchemes scheme, NameValueCollection parameters) 52 | { 53 | _scheme = scheme; 54 | Parameters = parameters; 55 | } 56 | 57 | #endregion 58 | 59 | #region Public Properties 60 | 61 | public string Algorithm { 62 | get { 63 | return Parameters["algorithm"]; 64 | } 65 | } 66 | 67 | public string Nonce { 68 | get { 69 | return Parameters["nonce"]; 70 | } 71 | } 72 | 73 | public string Opaque { 74 | get { 75 | return Parameters["opaque"]; 76 | } 77 | } 78 | 79 | public string Qop { 80 | get { 81 | return Parameters["qop"]; 82 | } 83 | } 84 | 85 | public string Realm { 86 | get { 87 | return Parameters["realm"]; 88 | } 89 | } 90 | 91 | public AuthenticationSchemes Scheme { 92 | get { 93 | return _scheme; 94 | } 95 | } 96 | 97 | #endregion 98 | 99 | #region Internal Methods 100 | 101 | internal static string CreateNonceValue () 102 | { 103 | var src = new byte[16]; 104 | var rand = new Random (); 105 | rand.NextBytes (src); 106 | 107 | var res = new StringBuilder (32); 108 | foreach (var b in src) 109 | res.Append (b.ToString ("x2")); 110 | 111 | return res.ToString (); 112 | } 113 | 114 | internal static NameValueCollection ParseParameters (string value) 115 | { 116 | var res = new NameValueCollection (); 117 | foreach (var param in value.SplitHeaderValue (',')) { 118 | var i = param.IndexOf ('='); 119 | var name = i > 0 ? param.Substring (0, i).Trim () : null; 120 | var val = i < 0 121 | ? param.Trim ().Trim ('"') 122 | : i < param.Length - 1 123 | ? param.Substring (i + 1).Trim ().Trim ('"') 124 | : String.Empty; 125 | 126 | res.Add (name, val); 127 | } 128 | 129 | return res; 130 | } 131 | 132 | internal abstract string ToBasicString (); 133 | 134 | internal abstract string ToDigestString (); 135 | 136 | #endregion 137 | 138 | #region Public Methods 139 | 140 | public override string ToString () 141 | { 142 | return _scheme == AuthenticationSchemes.Basic 143 | ? ToBasicString () 144 | : _scheme == AuthenticationSchemes.Digest 145 | ? ToDigestString () 146 | : String.Empty; 147 | } 148 | 149 | #endregion 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpListenerException.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpListenerException.cs 4 | * 5 | * This code is derived from System.Net.HttpListenerException.cs of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2012-2014 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | using System.ComponentModel; 42 | using System.Runtime.Serialization; 43 | 44 | namespace WebSocketSharp.Net 45 | { 46 | /// 47 | /// The exception that is thrown when a gets an error 48 | /// processing an HTTP request. 49 | /// 50 | [Serializable] 51 | public class HttpListenerException : Win32Exception 52 | { 53 | #region Protected Constructors 54 | 55 | /// 56 | /// Initializes a new instance of the class from 57 | /// the specified and . 58 | /// 59 | /// 60 | /// A that contains the serialized object data. 61 | /// 62 | /// 63 | /// A that specifies the source for the deserialization. 64 | /// 65 | 66 | 67 | #endregion 68 | 69 | #region Public Constructors 70 | 71 | /// 72 | /// Initializes a new instance of the class. 73 | /// 74 | public HttpListenerException () 75 | { 76 | } 77 | 78 | /// 79 | /// Initializes a new instance of the class 80 | /// with the specified . 81 | /// 82 | /// 83 | /// An that identifies the error. 84 | /// 85 | public HttpListenerException (int errorCode) 86 | : base (errorCode) 87 | { 88 | } 89 | 90 | /// 91 | /// Initializes a new instance of the class 92 | /// with the specified and . 93 | /// 94 | /// 95 | /// An that identifies the error. 96 | /// 97 | /// 98 | /// A that describes the error. 99 | /// 100 | public HttpListenerException (int errorCode, string message) 101 | : base (errorCode, message) 102 | { 103 | } 104 | 105 | #endregion 106 | 107 | #region Public Properties 108 | 109 | /// 110 | /// Gets the error code that identifies the error that occurred. 111 | /// 112 | /// 113 | /// An that identifies the error. 114 | /// 115 | public int ErrorCode { 116 | get { 117 | return NativeErrorCode; 118 | } 119 | } 120 | 121 | #endregion 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /websocket-sharp-core/CloseEventArgs.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * CloseEventArgs.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Represents the event data for the event. 35 | /// 36 | /// 37 | /// 38 | /// A event occurs when the WebSocket connection 39 | /// has been closed. 40 | /// 41 | /// 42 | /// If you would like to get the reason for the close, you should access 43 | /// the or property. 44 | /// 45 | /// 46 | public class CloseEventArgs : EventArgs 47 | { 48 | #region Private Fields 49 | 50 | private bool _clean; 51 | private ushort _code; 52 | private PayloadData _payloadData; 53 | private string _reason; 54 | 55 | #endregion 56 | 57 | #region Internal Constructors 58 | 59 | internal CloseEventArgs () 60 | { 61 | _code = (ushort) CloseStatusCode.NoStatus; 62 | _payloadData = PayloadData.Empty; 63 | } 64 | 65 | internal CloseEventArgs (ushort code) 66 | { 67 | _code = code; 68 | } 69 | 70 | internal CloseEventArgs (CloseStatusCode code) 71 | : this ((ushort) code) 72 | { 73 | } 74 | 75 | internal CloseEventArgs (PayloadData payloadData) 76 | { 77 | _payloadData = payloadData; 78 | 79 | var data = payloadData.ApplicationData; 80 | var len = data.Length; 81 | _code = len > 1 82 | ? data.SubArray (0, 2).ToUInt16 (ByteOrder.Big) 83 | : (ushort) CloseStatusCode.NoStatus; 84 | 85 | _reason = len > 2 86 | ? data.SubArray (2, len - 2).UTF8Decode () 87 | : String.Empty; 88 | } 89 | 90 | internal CloseEventArgs (ushort code, string reason) 91 | { 92 | _code = code; 93 | _reason = reason; 94 | } 95 | 96 | internal CloseEventArgs (CloseStatusCode code, string reason) 97 | : this ((ushort) code, reason) 98 | { 99 | } 100 | 101 | #endregion 102 | 103 | #region Internal Properties 104 | 105 | internal PayloadData PayloadData { 106 | get { 107 | return _payloadData ?? (_payloadData = new PayloadData (_code.Append (_reason))); 108 | } 109 | } 110 | 111 | #endregion 112 | 113 | #region Public Properties 114 | 115 | /// 116 | /// Gets the status code for the close. 117 | /// 118 | /// 119 | /// A that represents the status code for the close if any. 120 | /// 121 | public ushort Code { 122 | get { 123 | return _code; 124 | } 125 | } 126 | 127 | /// 128 | /// Gets the reason for the close. 129 | /// 130 | /// 131 | /// A that represents the reason for the close if any. 132 | /// 133 | public string Reason { 134 | get { 135 | return _reason ?? String.Empty; 136 | } 137 | } 138 | 139 | /// 140 | /// Gets a value indicating whether the connection has been closed cleanly. 141 | /// 142 | /// 143 | /// true if the connection has been closed cleanly; otherwise, false. 144 | /// 145 | public bool WasClean { 146 | get { 147 | return _clean; 148 | } 149 | 150 | internal set { 151 | _clean = value; 152 | } 153 | } 154 | 155 | #endregion 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpStreamAsyncResult.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpStreamAsyncResult.cs 4 | * 5 | * This code is derived from HttpStreamAsyncResult.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2012-2015 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | using System.Threading; 42 | 43 | namespace WebSocketSharp.Net 44 | { 45 | internal class HttpStreamAsyncResult : IAsyncResult 46 | { 47 | #region Private Fields 48 | 49 | private byte[] _buffer; 50 | private AsyncCallback _callback; 51 | private bool _completed; 52 | private int _count; 53 | private Exception _exception; 54 | private int _offset; 55 | private object _state; 56 | private object _sync; 57 | private int _syncRead; 58 | private ManualResetEvent _waitHandle; 59 | 60 | #endregion 61 | 62 | #region Internal Constructors 63 | 64 | internal HttpStreamAsyncResult (AsyncCallback callback, object state) 65 | { 66 | _callback = callback; 67 | _state = state; 68 | _sync = new object (); 69 | } 70 | 71 | #endregion 72 | 73 | #region Internal Properties 74 | 75 | internal byte[] Buffer { 76 | get { 77 | return _buffer; 78 | } 79 | 80 | set { 81 | _buffer = value; 82 | } 83 | } 84 | 85 | internal int Count { 86 | get { 87 | return _count; 88 | } 89 | 90 | set { 91 | _count = value; 92 | } 93 | } 94 | 95 | internal Exception Exception { 96 | get { 97 | return _exception; 98 | } 99 | } 100 | 101 | internal bool HasException { 102 | get { 103 | return _exception != null; 104 | } 105 | } 106 | 107 | internal int Offset { 108 | get { 109 | return _offset; 110 | } 111 | 112 | set { 113 | _offset = value; 114 | } 115 | } 116 | 117 | internal int SyncRead { 118 | get { 119 | return _syncRead; 120 | } 121 | 122 | set { 123 | _syncRead = value; 124 | } 125 | } 126 | 127 | #endregion 128 | 129 | #region Public Properties 130 | 131 | public object AsyncState { 132 | get { 133 | return _state; 134 | } 135 | } 136 | 137 | public WaitHandle AsyncWaitHandle { 138 | get { 139 | lock (_sync) 140 | return _waitHandle ?? (_waitHandle = new ManualResetEvent (_completed)); 141 | } 142 | } 143 | 144 | public bool CompletedSynchronously { 145 | get { 146 | return _syncRead == _count; 147 | } 148 | } 149 | 150 | public bool IsCompleted { 151 | get { 152 | lock (_sync) 153 | return _completed; 154 | } 155 | } 156 | 157 | #endregion 158 | 159 | #region Internal Methods 160 | 161 | internal void Complete () 162 | { 163 | lock (_sync) { 164 | if (_completed) 165 | return; 166 | 167 | _completed = true; 168 | if (_waitHandle != null) 169 | _waitHandle.Set (); 170 | 171 | if (_callback != null) 172 | _callback.BeginInvoke (this, ar => _callback.EndInvoke (ar), null); 173 | } 174 | } 175 | 176 | internal void Complete (Exception exception) 177 | { 178 | _exception = exception; 179 | Complete (); 180 | } 181 | 182 | #endregion 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /websocket-sharp-core/PayloadData.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * PayloadData.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using System.Collections; 31 | using System.Collections.Generic; 32 | 33 | namespace WebSocketSharp 34 | { 35 | internal class PayloadData : IEnumerable 36 | { 37 | #region Private Fields 38 | 39 | private byte[] _data; 40 | private long _extDataLength; 41 | private long _length; 42 | 43 | #endregion 44 | 45 | #region Public Fields 46 | 47 | /// 48 | /// Represents the empty payload data. 49 | /// 50 | public static readonly PayloadData Empty; 51 | 52 | /// 53 | /// Represents the allowable max length. 54 | /// 55 | /// 56 | /// 57 | /// A will occur if the payload data length is 58 | /// greater than the value of this field. 59 | /// 60 | /// 61 | /// If you would like to change the value, you must set it to a value between 62 | /// WebSocket.FragmentLength and Int64.MaxValue inclusive. 63 | /// 64 | /// 65 | public static readonly ulong MaxLength; 66 | 67 | #endregion 68 | 69 | #region Static Constructor 70 | 71 | static PayloadData () 72 | { 73 | Empty = new PayloadData (); 74 | MaxLength = Int64.MaxValue; 75 | } 76 | 77 | #endregion 78 | 79 | #region Internal Constructors 80 | 81 | internal PayloadData () 82 | { 83 | _data = WebSocket.EmptyBytes; 84 | } 85 | 86 | internal PayloadData (byte[] data) 87 | : this (data, data.Length) 88 | { 89 | } 90 | 91 | internal PayloadData (byte[] data, long length) 92 | { 93 | _data = data; 94 | _length = length; 95 | } 96 | 97 | #endregion 98 | 99 | #region Internal Properties 100 | 101 | internal long ExtensionDataLength { 102 | get { 103 | return _extDataLength; 104 | } 105 | 106 | set { 107 | _extDataLength = value; 108 | } 109 | } 110 | 111 | internal bool IncludesReservedCloseStatusCode { 112 | get { 113 | return _length > 1 && _data.SubArray (0, 2).ToUInt16 (ByteOrder.Big).IsReserved (); 114 | } 115 | } 116 | 117 | #endregion 118 | 119 | #region Public Properties 120 | 121 | public byte[] ApplicationData => _extDataLength > 0 122 | ? _data.SubArray ((int)_extDataLength, (int)_length - (int)_extDataLength) 123 | : _data; 124 | 125 | public byte[] ExtensionData => _extDataLength > 0 126 | ? _data.SubArray (0, (int)_extDataLength) 127 | : WebSocket.EmptyBytes; 128 | 129 | public ulong Length => (ulong) _length; 130 | 131 | #endregion 132 | 133 | #region Internal Methods 134 | 135 | internal void Mask (byte[] key) 136 | { 137 | for (long i = 0; i < _length; i++) 138 | _data[i] = (byte) (_data[i] ^ key[i % 4]); 139 | } 140 | 141 | #endregion 142 | 143 | #region Public Methods 144 | 145 | public IEnumerator GetEnumerator () 146 | { 147 | foreach (var b in _data) 148 | yield return b; 149 | } 150 | 151 | public byte[] ToArray () 152 | { 153 | return _data; 154 | } 155 | 156 | public override string ToString () 157 | { 158 | return BitConverter.ToString (_data); 159 | } 160 | 161 | #endregion 162 | 163 | #region Explicit Interface Implementations 164 | 165 | IEnumerator IEnumerable.GetEnumerator () 166 | { 167 | return GetEnumerator (); 168 | } 169 | 170 | #endregion 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/AuthenticationChallenge.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * AuthenticationChallenge.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using System.Collections.Specialized; 31 | using System.Text; 32 | 33 | namespace WebSocketSharp.Net 34 | { 35 | internal class AuthenticationChallenge : AuthenticationBase 36 | { 37 | #region Private Constructors 38 | 39 | private AuthenticationChallenge (AuthenticationSchemes scheme, NameValueCollection parameters) 40 | : base (scheme, parameters) 41 | { 42 | } 43 | 44 | #endregion 45 | 46 | #region Internal Constructors 47 | 48 | internal AuthenticationChallenge (AuthenticationSchemes scheme, string realm) 49 | : base (scheme, new NameValueCollection ()) 50 | { 51 | Parameters["realm"] = realm; 52 | if (scheme == AuthenticationSchemes.Digest) { 53 | Parameters["nonce"] = CreateNonceValue (); 54 | Parameters["algorithm"] = "MD5"; 55 | Parameters["qop"] = "auth"; 56 | } 57 | } 58 | 59 | #endregion 60 | 61 | #region Public Properties 62 | 63 | public string Domain { 64 | get { 65 | return Parameters["domain"]; 66 | } 67 | } 68 | 69 | public string Stale { 70 | get { 71 | return Parameters["stale"]; 72 | } 73 | } 74 | 75 | #endregion 76 | 77 | #region Internal Methods 78 | 79 | internal static AuthenticationChallenge CreateBasicChallenge (string realm) 80 | { 81 | return new AuthenticationChallenge (AuthenticationSchemes.Basic, realm); 82 | } 83 | 84 | internal static AuthenticationChallenge CreateDigestChallenge (string realm) 85 | { 86 | return new AuthenticationChallenge (AuthenticationSchemes.Digest, realm); 87 | } 88 | 89 | internal static AuthenticationChallenge Parse (string value) 90 | { 91 | var chal = value.Split (new[] { ' ' }, 2); 92 | if (chal.Length != 2) 93 | return null; 94 | 95 | var schm = chal[0].ToLower (); 96 | return schm == "basic" 97 | ? new AuthenticationChallenge ( 98 | AuthenticationSchemes.Basic, ParseParameters (chal[1])) 99 | : schm == "digest" 100 | ? new AuthenticationChallenge ( 101 | AuthenticationSchemes.Digest, ParseParameters (chal[1])) 102 | : null; 103 | } 104 | 105 | internal override string ToBasicString () 106 | { 107 | return String.Format ("Basic realm=\"{0}\"", Parameters["realm"]); 108 | } 109 | 110 | internal override string ToDigestString () 111 | { 112 | var output = new StringBuilder (128); 113 | 114 | var domain = Parameters["domain"]; 115 | if (domain != null) 116 | output.AppendFormat ( 117 | "Digest realm=\"{0}\", domain=\"{1}\", nonce=\"{2}\"", 118 | Parameters["realm"], 119 | domain, 120 | Parameters["nonce"]); 121 | else 122 | output.AppendFormat ( 123 | "Digest realm=\"{0}\", nonce=\"{1}\"", Parameters["realm"], Parameters["nonce"]); 124 | 125 | var opaque = Parameters["opaque"]; 126 | if (opaque != null) 127 | output.AppendFormat (", opaque=\"{0}\"", opaque); 128 | 129 | var stale = Parameters["stale"]; 130 | if (stale != null) 131 | output.AppendFormat (", stale={0}", stale); 132 | 133 | var algo = Parameters["algorithm"]; 134 | if (algo != null) 135 | output.AppendFormat (", algorithm={0}", algo); 136 | 137 | var qop = Parameters["qop"]; 138 | if (qop != null) 139 | output.AppendFormat (", qop=\"{0}\"", qop); 140 | 141 | return output.ToString (); 142 | } 143 | 144 | #endregion 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /websocket-sharp-core/LogData.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * LogData.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using System.Diagnostics; 31 | using System.Text; 32 | 33 | namespace WebSocketSharp 34 | { 35 | /// 36 | /// Represents a log data used by the class. 37 | /// 38 | public class LogData 39 | { 40 | #region Private Fields 41 | 42 | private StackFrame _caller; 43 | private DateTime _date; 44 | private LogLevel _level; 45 | private string _message; 46 | 47 | #endregion 48 | 49 | #region Internal Constructors 50 | 51 | internal LogData (LogLevel level, StackFrame caller, string message) 52 | { 53 | _level = level; 54 | _caller = caller; 55 | _message = message ?? String.Empty; 56 | _date = DateTime.Now; 57 | } 58 | 59 | #endregion 60 | 61 | #region Public Properties 62 | 63 | /// 64 | /// Gets the information of the logging method caller. 65 | /// 66 | /// 67 | /// A that provides the information of the logging method caller. 68 | /// 69 | public StackFrame Caller { 70 | get { 71 | return _caller; 72 | } 73 | } 74 | 75 | /// 76 | /// Gets the date and time when the log data was created. 77 | /// 78 | /// 79 | /// A that represents the date and time when the log data was created. 80 | /// 81 | public DateTime Date { 82 | get { 83 | return _date; 84 | } 85 | } 86 | 87 | /// 88 | /// Gets the logging level of the log data. 89 | /// 90 | /// 91 | /// One of the enum values, indicates the logging level of the log data. 92 | /// 93 | public LogLevel Level { 94 | get { 95 | return _level; 96 | } 97 | } 98 | 99 | /// 100 | /// Gets the message of the log data. 101 | /// 102 | /// 103 | /// A that represents the message of the log data. 104 | /// 105 | public string Message { 106 | get { 107 | return _message; 108 | } 109 | } 110 | 111 | #endregion 112 | 113 | #region Public Methods 114 | 115 | /// 116 | /// Returns a that represents the current . 117 | /// 118 | /// 119 | /// A that represents the current . 120 | /// 121 | public override string ToString () 122 | { 123 | var header = String.Format ("{0}|{1,-5}|", _date, _level); 124 | var method = _caller.GetMethod (); 125 | var type = method.DeclaringType; 126 | #if DEBUG 127 | var lineNum = _caller.GetFileLineNumber (); 128 | var headerAndCaller = 129 | String.Format ("{0}{1}.{2}:{3}|", header, type.Name, method.Name, lineNum); 130 | #else 131 | var headerAndCaller = String.Format ("{0}{1}.{2}|", header, type.Name, method.Name); 132 | #endif 133 | var msgs = _message.Replace ("\r\n", "\n").TrimEnd ('\n').Split ('\n'); 134 | if (msgs.Length <= 1) 135 | return String.Format ("{0}{1}", headerAndCaller, _message); 136 | 137 | var buff = new StringBuilder (String.Format ("{0}{1}\n", headerAndCaller, msgs[0]), 64); 138 | 139 | var fmt = String.Format ("{{0,{0}}}{{1}}\n", header.Length); 140 | for (var i = 1; i < msgs.Length; i++) 141 | buff.AppendFormat (fmt, "", msgs[i]); 142 | 143 | buff.Length--; 144 | return buff.ToString (); 145 | } 146 | 147 | #endregion 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /websocket-sharp-core/CloseStatusCode.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * CloseStatusCode.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Indicates the status code for the WebSocket connection close. 35 | /// 36 | /// 37 | /// 38 | /// The values of this enumeration are defined in 39 | /// Section 7.4 of RFC 6455. 40 | /// 41 | /// 42 | /// "Reserved value" must not be set as a status code in a connection close frame by 43 | /// an endpoint. It's designated for use in applications expecting a status code to 44 | /// indicate that the connection was closed due to the system grounds. 45 | /// 46 | /// 47 | public enum CloseStatusCode : ushort 48 | { 49 | /// 50 | /// Equivalent to close status 1000. Indicates normal close. 51 | /// 52 | Normal = 1000, 53 | /// 54 | /// Equivalent to close status 1001. Indicates that an endpoint is going away. 55 | /// 56 | Away = 1001, 57 | /// 58 | /// Equivalent to close status 1002. Indicates that an endpoint is terminating 59 | /// the connection due to a protocol error. 60 | /// 61 | ProtocolError = 1002, 62 | /// 63 | /// Equivalent to close status 1003. Indicates that an endpoint is terminating 64 | /// the connection because it has received a type of data that it cannot accept. 65 | /// 66 | UnsupportedData = 1003, 67 | /// 68 | /// Equivalent to close status 1004. Still undefined. A Reserved value. 69 | /// 70 | Undefined = 1004, 71 | /// 72 | /// Equivalent to close status 1005. Indicates that no status code was actually present. 73 | /// A Reserved value. 74 | /// 75 | NoStatus = 1005, 76 | /// 77 | /// Equivalent to close status 1006. Indicates that the connection was closed abnormally. 78 | /// A Reserved value. 79 | /// 80 | Abnormal = 1006, 81 | /// 82 | /// Equivalent to close status 1007. Indicates that an endpoint is terminating 83 | /// the connection because it has received a message that contains data that 84 | /// isn't consistent with the type of the message. 85 | /// 86 | InvalidData = 1007, 87 | /// 88 | /// Equivalent to close status 1008. Indicates that an endpoint is terminating 89 | /// the connection because it has received a message that violates its policy. 90 | /// 91 | PolicyViolation = 1008, 92 | /// 93 | /// Equivalent to close status 1009. Indicates that an endpoint is terminating 94 | /// the connection because it has received a message that is too big to process. 95 | /// 96 | TooBig = 1009, 97 | /// 98 | /// Equivalent to close status 1010. Indicates that a client is terminating 99 | /// the connection because it has expected the server to negotiate one or more extension, 100 | /// but the server didn't return them in the handshake response. 101 | /// 102 | MandatoryExtension = 1010, 103 | /// 104 | /// Equivalent to close status 1011. Indicates that a server is terminating 105 | /// the connection because it has encountered an unexpected condition that 106 | /// prevented it from fulfilling the request. 107 | /// 108 | ServerError = 1011, 109 | /// 110 | /// Equivalent to close status 1015. Indicates that the connection was closed 111 | /// due to a failure to perform a TLS handshake. A Reserved value. 112 | /// 113 | TlsHandshakeFailure = 1015 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /websocket-sharp-core/Server/WebSocketServiceHost.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketServiceHost.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | #region Contributors 30 | /* 31 | * Contributors: 32 | * - Juan Manuel Lallana 33 | */ 34 | #endregion 35 | 36 | using System; 37 | using WebSocketSharp.Net.WebSockets; 38 | 39 | namespace WebSocketSharp.Server 40 | { 41 | /// 42 | /// Exposes the methods and properties used to access the information in a WebSocket service 43 | /// provided by the or . 44 | /// 45 | /// 46 | /// The WebSocketServiceHost class is an abstract class. 47 | /// 48 | public abstract class WebSocketServiceHost 49 | { 50 | #region Protected Constructors 51 | 52 | /// 53 | /// Initializes a new instance of the class. 54 | /// 55 | protected WebSocketServiceHost () 56 | { 57 | } 58 | 59 | #endregion 60 | 61 | #region Internal Properties 62 | 63 | internal ServerState State { 64 | get { 65 | return Sessions.State; 66 | } 67 | } 68 | 69 | #endregion 70 | 71 | #region Public Properties 72 | 73 | /// 74 | /// Gets or sets a value indicating whether the WebSocket service cleans up 75 | /// the inactive sessions periodically. 76 | /// 77 | /// 78 | /// true if the service cleans up the inactive sessions periodically; 79 | /// otherwise, false. 80 | /// 81 | public abstract bool KeepClean { get; set; } 82 | 83 | /// 84 | /// Gets the path to the WebSocket service. 85 | /// 86 | /// 87 | /// A that represents the absolute path to the service. 88 | /// 89 | public abstract string Path { get; } 90 | 91 | /// 92 | /// Gets the access to the sessions in the WebSocket service. 93 | /// 94 | /// 95 | /// A that manages the sessions in the service. 96 | /// 97 | public abstract WebSocketSessionManager Sessions { get; } 98 | 99 | /// 100 | /// Gets the of the behavior of the WebSocket service. 101 | /// 102 | /// 103 | /// A that represents the type of the behavior of the service. 104 | /// 105 | public abstract Type Type { get; } 106 | 107 | /// 108 | /// Gets or sets the wait time for the response to the WebSocket Ping or Close. 109 | /// 110 | /// 111 | /// A that represents the wait time. The default value is 112 | /// the same as 1 second. 113 | /// 114 | public abstract TimeSpan WaitTime { get; set; } 115 | 116 | #endregion 117 | 118 | #region Internal Methods 119 | 120 | internal void Start () 121 | { 122 | Sessions.Start (); 123 | } 124 | 125 | internal void StartSession (WebSocketContext context) 126 | { 127 | CreateSession ().Start (context, Sessions); 128 | } 129 | 130 | internal void Stop (ushort code, string reason) 131 | { 132 | var e = new CloseEventArgs (code, reason); 133 | var send = !code.IsReserved (); 134 | var bytes = send ? WebSocketFrame.CreateCloseFrame (e.PayloadData, false).ToArray () : null; 135 | Sessions.Stop (e, bytes, send); 136 | } 137 | 138 | #endregion 139 | 140 | #region Protected Methods 141 | 142 | /// 143 | /// Creates a new session in the WebSocket service. 144 | /// 145 | /// 146 | /// A instance that represents a new session. 147 | /// 148 | protected abstract WebSocketBehavior CreateSession (); 149 | 150 | #endregion 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /websocket-sharp-core/MessageEventArgs.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * MessageEventArgs.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp 32 | { 33 | /// 34 | /// Represents the event data for the event. 35 | /// 36 | /// 37 | /// 38 | /// A event occurs when the receives 39 | /// a text or binary message, or a ping if the property is 40 | /// set to true. 41 | /// 42 | /// 43 | /// If you would like to get the message data, you should access the or 44 | /// property. 45 | /// 46 | /// 47 | public class MessageEventArgs : EventArgs 48 | { 49 | #region Private Fields 50 | 51 | private string _data; 52 | private bool _dataSet; 53 | private Opcode _opcode; 54 | private byte[] _rawData; 55 | 56 | #endregion 57 | 58 | #region Internal Constructors 59 | 60 | internal MessageEventArgs (WebSocketFrame frame) 61 | { 62 | _opcode = frame.Opcode; 63 | _rawData = frame.PayloadData.ApplicationData; 64 | } 65 | 66 | internal MessageEventArgs (Opcode opcode, byte[] rawData) 67 | { 68 | if ((ulong) rawData.Length > PayloadData.MaxLength) 69 | throw new WebSocketException (CloseStatusCode.TooBig); 70 | 71 | _opcode = opcode; 72 | _rawData = rawData; 73 | } 74 | 75 | #endregion 76 | 77 | #region Public Properties 78 | 79 | /// 80 | /// Gets the message data as a . 81 | /// 82 | /// 83 | /// A that represents the message data, 84 | /// or if the message data cannot be decoded to a string. 85 | /// 86 | public string Data { 87 | get { 88 | if (!_dataSet) { 89 | _data = _opcode != Opcode.Binary 90 | ? _rawData.UTF8Decode () 91 | : BitConverter.ToString (_rawData); 92 | 93 | _dataSet = true; 94 | } 95 | 96 | return _data; 97 | } 98 | } 99 | 100 | /// 101 | /// Gets a value indicating whether the message type is binary. 102 | /// 103 | /// 104 | /// true if the message type is binary; otherwise, false. 105 | /// 106 | public bool IsBinary { 107 | get { 108 | return _opcode == Opcode.Binary; 109 | } 110 | } 111 | 112 | /// 113 | /// Gets a value indicating whether the message type is ping. 114 | /// 115 | /// 116 | /// true if the message type is ping; otherwise, false. 117 | /// 118 | public bool IsPing { 119 | get { 120 | return _opcode == Opcode.Ping; 121 | } 122 | } 123 | 124 | /// 125 | /// Gets a value indicating whether the message type is text. 126 | /// 127 | /// 128 | /// true if the message type is text; otherwise, false. 129 | /// 130 | public bool IsText { 131 | get { 132 | return _opcode == Opcode.Text; 133 | } 134 | } 135 | 136 | /// 137 | /// Gets the message data as an array of . 138 | /// 139 | /// 140 | /// An array of that represents the message data. 141 | /// 142 | public byte[] RawData { 143 | get { 144 | return _rawData; 145 | } 146 | } 147 | 148 | /// 149 | /// Gets the message type. 150 | /// 151 | /// 152 | /// , , or . 153 | /// 154 | [Obsolete ("This property will be removed. Use any of the Is properties instead.")] 155 | public Opcode Type { 156 | get { 157 | return _opcode; 158 | } 159 | } 160 | 161 | #endregion 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpListenerAsyncResult.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpListenerAsyncResult.cs 4 | * 5 | * This code is derived from ListenerAsyncResult.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Ximian, Inc. (http://www.ximian.com) 11 | * Copyright (c) 2012-2016 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | #region Contributors 41 | /* 42 | * Contributors: 43 | * - Nicholas Devenish 44 | */ 45 | #endregion 46 | 47 | using System; 48 | using System.Threading; 49 | 50 | namespace WebSocketSharp.Net 51 | { 52 | internal class HttpListenerAsyncResult : IAsyncResult 53 | { 54 | #region Private Fields 55 | 56 | private AsyncCallback _callback; 57 | private bool _completed; 58 | private HttpListenerContext _context; 59 | private bool _endCalled; 60 | private Exception _exception; 61 | private bool _inGet; 62 | private object _state; 63 | private object _sync; 64 | private bool _syncCompleted; 65 | private ManualResetEvent _waitHandle; 66 | 67 | #endregion 68 | 69 | #region Internal Constructors 70 | 71 | internal HttpListenerAsyncResult (AsyncCallback callback, object state) 72 | { 73 | _callback = callback; 74 | _state = state; 75 | _sync = new object (); 76 | } 77 | 78 | #endregion 79 | 80 | #region Internal Properties 81 | 82 | internal bool EndCalled { 83 | get { 84 | return _endCalled; 85 | } 86 | 87 | set { 88 | _endCalled = value; 89 | } 90 | } 91 | 92 | internal bool InGet { 93 | get { 94 | return _inGet; 95 | } 96 | 97 | set { 98 | _inGet = value; 99 | } 100 | } 101 | 102 | #endregion 103 | 104 | #region Public Properties 105 | 106 | public object AsyncState { 107 | get { 108 | return _state; 109 | } 110 | } 111 | 112 | public WaitHandle AsyncWaitHandle { 113 | get { 114 | lock (_sync) 115 | return _waitHandle ?? (_waitHandle = new ManualResetEvent (_completed)); 116 | } 117 | } 118 | 119 | public bool CompletedSynchronously { 120 | get { 121 | return _syncCompleted; 122 | } 123 | } 124 | 125 | public bool IsCompleted { 126 | get { 127 | lock (_sync) 128 | return _completed; 129 | } 130 | } 131 | 132 | #endregion 133 | 134 | #region Private Methods 135 | 136 | private static void complete (HttpListenerAsyncResult asyncResult) 137 | { 138 | lock (asyncResult._sync) { 139 | asyncResult._completed = true; 140 | 141 | var waitHandle = asyncResult._waitHandle; 142 | if (waitHandle != null) 143 | waitHandle.Set (); 144 | } 145 | 146 | var callback = asyncResult._callback; 147 | if (callback == null) 148 | return; 149 | 150 | ThreadPool.QueueUserWorkItem ( 151 | state => { 152 | try { 153 | callback (asyncResult); 154 | } 155 | catch { 156 | } 157 | }, 158 | null 159 | ); 160 | } 161 | 162 | #endregion 163 | 164 | #region Internal Methods 165 | 166 | internal void Complete (Exception exception) 167 | { 168 | _exception = _inGet && (exception is ObjectDisposedException) 169 | ? new HttpListenerException (995, "The listener is closed.") 170 | : exception; 171 | 172 | complete (this); 173 | } 174 | 175 | internal void Complete (HttpListenerContext context) 176 | { 177 | Complete (context, false); 178 | } 179 | 180 | internal void Complete (HttpListenerContext context, bool syncCompleted) 181 | { 182 | _context = context; 183 | _syncCompleted = syncCompleted; 184 | 185 | complete (this); 186 | } 187 | 188 | internal HttpListenerContext GetContext () 189 | { 190 | if (_exception != null) 191 | throw _exception; 192 | 193 | return _context; 194 | } 195 | 196 | #endregion 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /websocket-sharp-core/Helpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | 7 | namespace Helpers 8 | { 9 | public static class Helpers 10 | { 11 | public static T WaitForResult(this Task task) 12 | { 13 | try 14 | { 15 | return AsyncHelpers.RunSync(() => task); 16 | } 17 | catch (Exception x) 18 | { 19 | throw task.Exception; 20 | } 21 | } 22 | public static void WaitForResult(this Task task) 23 | { 24 | try 25 | { 26 | AsyncHelpers.RunSync(() => task); 27 | } 28 | catch (Exception x) 29 | { 30 | throw task.Exception; 31 | } 32 | } 33 | } 34 | 35 | public static class AsyncHelpers 36 | { 37 | /// 38 | /// Execute's an async Task method which has a void return value synchronously 39 | /// 40 | /// Task method to execute 41 | public static void RunSync(Func task) 42 | { 43 | var oldContext = SynchronizationContext.Current; 44 | var synch = new ExclusiveSynchronizationContext(); 45 | SynchronizationContext.SetSynchronizationContext(synch); 46 | synch.Post(async _ => 47 | { 48 | try 49 | { 50 | await task(); 51 | } 52 | catch (Exception e) 53 | { 54 | synch.InnerException = e; 55 | throw; 56 | } 57 | finally 58 | { 59 | synch.EndMessageLoop(); 60 | } 61 | }, null); 62 | synch.BeginMessageLoop(); 63 | 64 | SynchronizationContext.SetSynchronizationContext(oldContext); 65 | } 66 | 67 | /// 68 | /// Execute's an async Task method which has a T return type synchronously 69 | /// 70 | /// Return Type 71 | /// Task method to execute 72 | /// 73 | public static T RunSync(Func> task) 74 | { 75 | var oldContext = SynchronizationContext.Current; 76 | var synch = new ExclusiveSynchronizationContext(); 77 | SynchronizationContext.SetSynchronizationContext(synch); 78 | T ret = default(T); 79 | synch.Post(async _ => 80 | { 81 | try 82 | { 83 | ret = await task(); 84 | } 85 | catch (Exception e) 86 | { 87 | synch.InnerException = e; 88 | throw; 89 | } 90 | finally 91 | { 92 | synch.EndMessageLoop(); 93 | } 94 | }, null); 95 | synch.BeginMessageLoop(); 96 | SynchronizationContext.SetSynchronizationContext(oldContext); 97 | return ret; 98 | } 99 | 100 | private class ExclusiveSynchronizationContext : SynchronizationContext 101 | { 102 | private bool done; 103 | public Exception InnerException { get; set; } 104 | readonly AutoResetEvent workItemsWaiting = new AutoResetEvent(false); 105 | readonly Queue> items = 106 | new Queue>(); 107 | 108 | public override void Send(SendOrPostCallback d, object state) 109 | { 110 | throw new NotSupportedException("We cannot send to our same thread"); 111 | } 112 | 113 | public override void Post(SendOrPostCallback d, object state) 114 | { 115 | lock (items) 116 | { 117 | items.Enqueue(Tuple.Create(d, state)); 118 | } 119 | workItemsWaiting.Set(); 120 | } 121 | 122 | public void EndMessageLoop() 123 | { 124 | Post(_ => done = true, null); 125 | } 126 | 127 | public void BeginMessageLoop() 128 | { 129 | while (!done) 130 | { 131 | Tuple task = null; 132 | lock (items) 133 | { 134 | if (items.Count > 0) 135 | { 136 | task = items.Dequeue(); 137 | } 138 | } 139 | if (task != null) 140 | { 141 | task.Item1(task.Item2); 142 | if (InnerException != null) // the method threw an exeption 143 | { 144 | throw new AggregateException("AsyncHelpers.Run method threw an exception.", InnerException); 145 | } 146 | } 147 | else 148 | { 149 | workItemsWaiting.WaitOne(); 150 | } 151 | } 152 | } 153 | 154 | public override SynchronizationContext CreateCopy() 155 | { 156 | return this; 157 | } 158 | } 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpDigestIdentity.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpDigestIdentity.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using System.Collections.Specialized; 31 | using System.Security.Principal; 32 | 33 | namespace WebSocketSharp.Net 34 | { 35 | /// 36 | /// Holds the user name and other parameters from the HTTP Digest authentication credentials. 37 | /// 38 | public class HttpDigestIdentity : GenericIdentity 39 | { 40 | #region Private Fields 41 | 42 | private NameValueCollection _parameters; 43 | 44 | #endregion 45 | 46 | #region Internal Constructors 47 | 48 | internal HttpDigestIdentity (NameValueCollection parameters) 49 | : base (parameters ["username"], "Digest") 50 | { 51 | _parameters = parameters; 52 | } 53 | 54 | #endregion 55 | 56 | #region Public Properties 57 | 58 | /// 59 | /// Gets the algorithm parameter from the HTTP Digest authentication credentials. 60 | /// 61 | /// 62 | /// A that represents the algorithm parameter. 63 | /// 64 | public string Algorithm { 65 | get { 66 | return _parameters ["algorithm"]; 67 | } 68 | } 69 | 70 | /// 71 | /// Gets the cnonce parameter from the HTTP Digest authentication credentials. 72 | /// 73 | /// 74 | /// A that represents the cnonce parameter. 75 | /// 76 | public string Cnonce { 77 | get { 78 | return _parameters ["cnonce"]; 79 | } 80 | } 81 | 82 | /// 83 | /// Gets the nc parameter from the HTTP Digest authentication credentials. 84 | /// 85 | /// 86 | /// A that represents the nc parameter. 87 | /// 88 | public string Nc { 89 | get { 90 | return _parameters ["nc"]; 91 | } 92 | } 93 | 94 | /// 95 | /// Gets the nonce parameter from the HTTP Digest authentication credentials. 96 | /// 97 | /// 98 | /// A that represents the nonce parameter. 99 | /// 100 | public string Nonce { 101 | get { 102 | return _parameters ["nonce"]; 103 | } 104 | } 105 | 106 | /// 107 | /// Gets the opaque parameter from the HTTP Digest authentication credentials. 108 | /// 109 | /// 110 | /// A that represents the opaque parameter. 111 | /// 112 | public string Opaque { 113 | get { 114 | return _parameters ["opaque"]; 115 | } 116 | } 117 | 118 | /// 119 | /// Gets the qop parameter from the HTTP Digest authentication credentials. 120 | /// 121 | /// 122 | /// A that represents the qop parameter. 123 | /// 124 | public string Qop { 125 | get { 126 | return _parameters ["qop"]; 127 | } 128 | } 129 | 130 | /// 131 | /// Gets the realm parameter from the HTTP Digest authentication credentials. 132 | /// 133 | /// 134 | /// A that represents the realm parameter. 135 | /// 136 | public string Realm { 137 | get { 138 | return _parameters ["realm"]; 139 | } 140 | } 141 | 142 | /// 143 | /// Gets the response parameter from the HTTP Digest authentication credentials. 144 | /// 145 | /// 146 | /// A that represents the response parameter. 147 | /// 148 | public string Response { 149 | get { 150 | return _parameters ["response"]; 151 | } 152 | } 153 | 154 | /// 155 | /// Gets the uri parameter from the HTTP Digest authentication credentials. 156 | /// 157 | /// 158 | /// A that represents the uri parameter. 159 | /// 160 | public string Uri { 161 | get { 162 | return _parameters ["uri"]; 163 | } 164 | } 165 | 166 | #endregion 167 | 168 | #region Internal Methods 169 | 170 | internal bool IsValid (string password, string realm, string method, string entity) 171 | { 172 | var parameters = new NameValueCollection (_parameters); 173 | parameters ["password"] = password; 174 | parameters ["realm"] = realm; 175 | parameters ["method"] = method; 176 | parameters ["entity"] = entity; 177 | 178 | return _parameters ["response"] == AuthenticationResponse.CreateRequestDigest (parameters); 179 | } 180 | 181 | #endregion 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpResponseHeader.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpResponseHeader.cs 4 | * 5 | * This code is derived from System.Net.HttpResponseHeader.cs of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2014 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | namespace WebSocketSharp.Net 41 | { 42 | /// 43 | /// Contains the HTTP headers that can be specified in a server response. 44 | /// 45 | /// 46 | /// The HttpResponseHeader enumeration contains the HTTP response headers defined in 47 | /// RFC 2616 for the HTTP/1.1 and 48 | /// RFC 6455 for the WebSocket. 49 | /// 50 | public enum HttpResponseHeader 51 | { 52 | /// 53 | /// Indicates the Cache-Control header. 54 | /// 55 | CacheControl, 56 | /// 57 | /// Indicates the Connection header. 58 | /// 59 | Connection, 60 | /// 61 | /// Indicates the Date header. 62 | /// 63 | Date, 64 | /// 65 | /// Indicates the Keep-Alive header. 66 | /// 67 | KeepAlive, 68 | /// 69 | /// Indicates the Pragma header. 70 | /// 71 | Pragma, 72 | /// 73 | /// Indicates the Trailer header. 74 | /// 75 | Trailer, 76 | /// 77 | /// Indicates the Transfer-Encoding header. 78 | /// 79 | TransferEncoding, 80 | /// 81 | /// Indicates the Upgrade header. 82 | /// 83 | Upgrade, 84 | /// 85 | /// Indicates the Via header. 86 | /// 87 | Via, 88 | /// 89 | /// Indicates the Warning header. 90 | /// 91 | Warning, 92 | /// 93 | /// Indicates the Allow header. 94 | /// 95 | Allow, 96 | /// 97 | /// Indicates the Content-Length header. 98 | /// 99 | ContentLength, 100 | /// 101 | /// Indicates the Content-Type header. 102 | /// 103 | ContentType, 104 | /// 105 | /// Indicates the Content-Encoding header. 106 | /// 107 | ContentEncoding, 108 | /// 109 | /// Indicates the Content-Language header. 110 | /// 111 | ContentLanguage, 112 | /// 113 | /// Indicates the Content-Location header. 114 | /// 115 | ContentLocation, 116 | /// 117 | /// Indicates the Content-MD5 header. 118 | /// 119 | ContentMd5, 120 | /// 121 | /// Indicates the Content-Range header. 122 | /// 123 | ContentRange, 124 | /// 125 | /// Indicates the Expires header. 126 | /// 127 | Expires, 128 | /// 129 | /// Indicates the Last-Modified header. 130 | /// 131 | LastModified, 132 | /// 133 | /// Indicates the Accept-Ranges header. 134 | /// 135 | AcceptRanges, 136 | /// 137 | /// Indicates the Age header. 138 | /// 139 | Age, 140 | /// 141 | /// Indicates the ETag header. 142 | /// 143 | ETag, 144 | /// 145 | /// Indicates the Location header. 146 | /// 147 | Location, 148 | /// 149 | /// Indicates the Proxy-Authenticate header. 150 | /// 151 | ProxyAuthenticate, 152 | /// 153 | /// Indicates the Retry-After header. 154 | /// 155 | RetryAfter, 156 | /// 157 | /// Indicates the Server header. 158 | /// 159 | Server, 160 | /// 161 | /// Indicates the Set-Cookie header. 162 | /// 163 | SetCookie, 164 | /// 165 | /// Indicates the Vary header. 166 | /// 167 | Vary, 168 | /// 169 | /// Indicates the WWW-Authenticate header. 170 | /// 171 | WwwAuthenticate, 172 | /// 173 | /// Indicates the Sec-WebSocket-Extensions header. 174 | /// 175 | SecWebSocketExtensions, 176 | /// 177 | /// Indicates the Sec-WebSocket-Accept header. 178 | /// 179 | SecWebSocketAccept, 180 | /// 181 | /// Indicates the Sec-WebSocket-Protocol header. 182 | /// 183 | SecWebSocketProtocol, 184 | /// 185 | /// Indicates the Sec-WebSocket-Version header. 186 | /// 187 | SecWebSocketVersion 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/ServerSslConfiguration.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ServerSslConfiguration.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2014 liryna 8 | * Copyright (c) 2014 sta.blockhead 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy 11 | * of this software and associated documentation files (the "Software"), to deal 12 | * in the Software without restriction, including without limitation the rights 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | * copies of the Software, and to permit persons to whom the Software is 15 | * furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | * THE SOFTWARE. 27 | */ 28 | #endregion 29 | 30 | #region Authors 31 | /* 32 | * Authors: 33 | * - Liryna 34 | */ 35 | #endregion 36 | 37 | using System.Net.Security; 38 | using System.Security.Authentication; 39 | using System.Security.Cryptography.X509Certificates; 40 | 41 | namespace WebSocketSharp.Net 42 | { 43 | /// 44 | /// Stores the parameters used to configure a instance as a server. 45 | /// 46 | public class ServerSslConfiguration : SslConfiguration 47 | { 48 | #region Private Fields 49 | 50 | private X509Certificate2 _cert; 51 | private bool _clientCertRequired; 52 | 53 | #endregion 54 | 55 | #region Public Constructors 56 | 57 | /// 58 | /// Initializes a new instance of the class with 59 | /// the specified . 60 | /// 61 | /// 62 | /// A that represents the certificate used to authenticate 63 | /// the server. 64 | /// 65 | public ServerSslConfiguration (X509Certificate2 serverCertificate) 66 | : this (serverCertificate, false, SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls, false) 67 | { 68 | } 69 | 70 | /// 71 | /// Initializes a new instance of the class with 72 | /// the specified , 73 | /// , , 74 | /// and . 75 | /// 76 | /// 77 | /// A that represents the certificate used to authenticate 78 | /// the server. 79 | /// 80 | /// 81 | /// true if the client must supply a certificate for authentication; 82 | /// otherwise, false. 83 | /// 84 | /// 85 | /// The enum value that represents the protocols used for 86 | /// authentication. 87 | /// 88 | /// 89 | /// true if the certificate revocation list is checked during authentication; 90 | /// otherwise, false. 91 | /// 92 | public ServerSslConfiguration ( 93 | X509Certificate2 serverCertificate, 94 | bool clientCertificateRequired, 95 | SslProtocols enabledSslProtocols, 96 | bool checkCertificateRevocation) 97 | : base (enabledSslProtocols, checkCertificateRevocation) 98 | { 99 | _cert = serverCertificate; 100 | _clientCertRequired = clientCertificateRequired; 101 | } 102 | 103 | #endregion 104 | 105 | #region Public Properties 106 | 107 | /// 108 | /// Gets or sets a value indicating whether the client must supply a certificate for 109 | /// authentication. 110 | /// 111 | /// 112 | /// true if the client must supply a certificate; otherwise, false. 113 | /// 114 | public bool ClientCertificateRequired { 115 | get { 116 | return _clientCertRequired; 117 | } 118 | 119 | set { 120 | _clientCertRequired = value; 121 | } 122 | } 123 | 124 | /// 125 | /// Gets or sets the callback used to validate the certificate supplied by the client. 126 | /// 127 | /// 128 | /// If this callback returns true, the client certificate will be valid. 129 | /// 130 | /// 131 | /// A delegate that references the method 132 | /// used to validate the client certificate. The default value is a function that only returns 133 | /// true. 134 | /// 135 | public RemoteCertificateValidationCallback ClientCertificateValidationCallback { 136 | get { 137 | return CertificateValidationCallback; 138 | } 139 | 140 | set { 141 | CertificateValidationCallback = value; 142 | } 143 | } 144 | 145 | /// 146 | /// Gets or sets the certificate used to authenticate the server for secure connection. 147 | /// 148 | /// 149 | /// A that represents the certificate used to authenticate 150 | /// the server. 151 | /// 152 | public X509Certificate2 ServerCertificate { 153 | get { 154 | return _cert; 155 | } 156 | 157 | set { 158 | _cert = value; 159 | } 160 | } 161 | 162 | #endregion 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/SslConfiguration.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * SslConfiguration.cs 4 | * 5 | * This code is derived from ClientSslConfiguration.cs. 6 | * 7 | * The MIT License 8 | * 9 | * Copyright (c) 2014 liryna 10 | * Copyright (c) 2014 sta.blockhead 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy 13 | * of this software and associated documentation files (the "Software"), to deal 14 | * in the Software without restriction, including without limitation the rights 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the Software is 17 | * furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included in 20 | * all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | * THE SOFTWARE. 29 | */ 30 | #endregion 31 | 32 | #region Authors 33 | /* 34 | * Authors: 35 | * - Liryna 36 | */ 37 | #endregion 38 | 39 | using System.Net.Security; 40 | using System.Security.Authentication; 41 | 42 | namespace WebSocketSharp.Net 43 | { 44 | /// 45 | /// Stores the parameters used to configure a instance. 46 | /// 47 | /// 48 | /// The SslConfiguration class is an abstract class. 49 | /// 50 | public abstract class SslConfiguration 51 | { 52 | #region Private Fields 53 | 54 | private LocalCertificateSelectionCallback _certSelectionCallback; 55 | private RemoteCertificateValidationCallback _certValidationCallback; 56 | private bool _checkCertRevocation; 57 | private SslProtocols _enabledProtocols; 58 | 59 | #endregion 60 | 61 | #region Protected Constructors 62 | 63 | /// 64 | /// Initializes a new instance of the class with 65 | /// the specified and 66 | /// . 67 | /// 68 | /// 69 | /// The enum value that represents the protocols used for 70 | /// authentication. 71 | /// 72 | /// 73 | /// true if the certificate revocation list is checked during authentication; 74 | /// otherwise, false. 75 | /// 76 | protected SslConfiguration (SslProtocols enabledSslProtocols, bool checkCertificateRevocation) 77 | { 78 | _enabledProtocols = enabledSslProtocols; 79 | _checkCertRevocation = checkCertificateRevocation; 80 | } 81 | 82 | #endregion 83 | 84 | #region Protected Properties 85 | 86 | /// 87 | /// Gets or sets the callback used to select a certificate to supply to the remote party. 88 | /// 89 | /// 90 | /// If this callback returns , no certificate will be supplied. 91 | /// 92 | /// 93 | /// A delegate that references the method 94 | /// used to select a certificate. The default value is a function that only returns 95 | /// . 96 | /// 97 | protected LocalCertificateSelectionCallback CertificateSelectionCallback { 98 | get { 99 | return _certSelectionCallback ?? 100 | (_certSelectionCallback = 101 | (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => 102 | null); 103 | } 104 | 105 | set { 106 | _certSelectionCallback = value; 107 | } 108 | } 109 | 110 | /// 111 | /// Gets or sets the callback used to validate the certificate supplied by the remote party. 112 | /// 113 | /// 114 | /// If this callback returns true, the certificate will be valid. 115 | /// 116 | /// 117 | /// A delegate that references the method 118 | /// used to validate the certificate. The default value is a function that only returns 119 | /// true. 120 | /// 121 | protected RemoteCertificateValidationCallback CertificateValidationCallback { 122 | get { 123 | return _certValidationCallback ?? 124 | (_certValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true); 125 | } 126 | 127 | set { 128 | _certValidationCallback = value; 129 | } 130 | } 131 | 132 | #endregion 133 | 134 | #region Public Properties 135 | 136 | /// 137 | /// Gets or sets a value indicating whether the certificate revocation list is checked 138 | /// during authentication. 139 | /// 140 | /// 141 | /// true if the certificate revocation list is checked; otherwise, false. 142 | /// 143 | public bool CheckCertificateRevocation { 144 | get { 145 | return _checkCertRevocation; 146 | } 147 | 148 | set { 149 | _checkCertRevocation = value; 150 | } 151 | } 152 | 153 | /// 154 | /// Gets or sets the SSL protocols used for authentication. 155 | /// 156 | /// 157 | /// The enum value that represents the protocols used for 158 | /// authentication. 159 | /// 160 | public SslProtocols EnabledSslProtocols { 161 | get { 162 | return _enabledProtocols; 163 | } 164 | 165 | set { 166 | _enabledProtocols = value; 167 | } 168 | } 169 | 170 | #endregion 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /websocket-sharp-core/HttpResponse.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpResponse.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using System.Collections.Specialized; 31 | using System.IO; 32 | using System.Text; 33 | using WebSocketSharp.Net; 34 | 35 | namespace WebSocketSharp 36 | { 37 | internal class HttpResponse : HttpBase 38 | { 39 | #region Private Fields 40 | 41 | private string _code; 42 | private string _reason; 43 | 44 | #endregion 45 | 46 | #region Private Constructors 47 | 48 | private HttpResponse (string code, string reason, Version version, NameValueCollection headers) 49 | : base (version, headers) 50 | { 51 | _code = code; 52 | _reason = reason; 53 | } 54 | 55 | #endregion 56 | 57 | #region Internal Constructors 58 | 59 | internal HttpResponse (HttpStatusCode code) 60 | : this (code, code.GetDescription ()) 61 | { 62 | } 63 | 64 | internal HttpResponse (HttpStatusCode code, string reason) 65 | : this (((int) code).ToString (), reason, HttpVersion.Version11, new NameValueCollection ()) 66 | { 67 | Headers["Server"] = "websocket-sharp/1.0"; 68 | } 69 | 70 | #endregion 71 | 72 | #region Public Properties 73 | 74 | public CookieCollection Cookies { 75 | get { 76 | return Headers.GetCookies (true); 77 | } 78 | } 79 | 80 | public bool HasConnectionClose { 81 | get { 82 | return Headers.Contains ("Connection", "close"); 83 | } 84 | } 85 | 86 | public bool IsProxyAuthenticationRequired { 87 | get { 88 | return _code == "407"; 89 | } 90 | } 91 | 92 | public bool IsRedirect { 93 | get { 94 | return _code == "301" || _code == "302"; 95 | } 96 | } 97 | 98 | public bool IsUnauthorized { 99 | get { 100 | return _code == "401"; 101 | } 102 | } 103 | 104 | public bool IsWebSocketResponse { 105 | get { 106 | var headers = Headers; 107 | return ProtocolVersion > HttpVersion.Version10 && 108 | _code == "101" && 109 | headers.Contains ("Upgrade", "websocket") && 110 | headers.Contains ("Connection", "Upgrade"); 111 | } 112 | } 113 | 114 | public string Reason { 115 | get { 116 | return _reason; 117 | } 118 | } 119 | 120 | public string StatusCode { 121 | get { 122 | return _code; 123 | } 124 | } 125 | 126 | #endregion 127 | 128 | #region Internal Methods 129 | 130 | internal static HttpResponse CreateCloseResponse (HttpStatusCode code) 131 | { 132 | var res = new HttpResponse (code); 133 | res.Headers["Connection"] = "close"; 134 | 135 | return res; 136 | } 137 | 138 | internal static HttpResponse CreateUnauthorizedResponse (string challenge) 139 | { 140 | var res = new HttpResponse (HttpStatusCode.Unauthorized); 141 | res.Headers["WWW-Authenticate"] = challenge; 142 | 143 | return res; 144 | } 145 | 146 | internal static HttpResponse CreateWebSocketResponse () 147 | { 148 | var res = new HttpResponse (HttpStatusCode.SwitchingProtocols); 149 | 150 | var headers = res.Headers; 151 | headers["Upgrade"] = "websocket"; 152 | headers["Connection"] = "Upgrade"; 153 | 154 | return res; 155 | } 156 | 157 | internal static HttpResponse Parse (string[] headerParts) 158 | { 159 | var statusLine = headerParts[0].Split (new[] { ' ' }, 3); 160 | if (statusLine.Length != 3) 161 | throw new ArgumentException ("Invalid status line: " + headerParts[0]); 162 | 163 | var headers = new WebHeaderCollection (); 164 | for (int i = 1; i < headerParts.Length; i++) 165 | headers.InternalSet (headerParts[i], true); 166 | 167 | return new HttpResponse ( 168 | statusLine[1], statusLine[2], new Version (statusLine[0].Substring (5)), headers); 169 | } 170 | 171 | internal static HttpResponse Read (Stream stream, int millisecondsTimeout) 172 | { 173 | return Read (stream, Parse, millisecondsTimeout); 174 | } 175 | 176 | #endregion 177 | 178 | #region Public Methods 179 | 180 | public void SetCookies (CookieCollection cookies) 181 | { 182 | if (cookies == null || cookies.Count == 0) 183 | return; 184 | 185 | var headers = Headers; 186 | foreach (var cookie in cookies.Sorted) 187 | headers.Add ("Set-Cookie", cookie.ToResponseString ()); 188 | } 189 | 190 | public override string ToString () 191 | { 192 | var output = new StringBuilder (64); 193 | output.AppendFormat ("HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf); 194 | 195 | var headers = Headers; 196 | foreach (var key in headers.AllKeys) 197 | output.AppendFormat ("{0}: {1}{2}", key, headers[key], CrLf); 198 | 199 | output.Append (CrLf); 200 | 201 | var entity = EntityBody; 202 | if (entity.Length > 0) 203 | output.Append (entity); 204 | 205 | return output.ToString (); 206 | } 207 | 208 | #endregion 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /websocket-sharp-core/HttpBase.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpBase.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2014 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | using System.Collections.Generic; 31 | using System.Collections.Specialized; 32 | using System.IO; 33 | using System.Text; 34 | using System.Threading; 35 | using WebSocketSharp.Net; 36 | 37 | namespace WebSocketSharp 38 | { 39 | internal abstract class HttpBase 40 | { 41 | #region Private Fields 42 | 43 | private NameValueCollection _headers; 44 | private const int _headersMaxLength = 8192; 45 | private Version _version; 46 | 47 | #endregion 48 | 49 | #region Internal Fields 50 | 51 | internal byte[] EntityBodyData; 52 | 53 | #endregion 54 | 55 | #region Protected Fields 56 | 57 | protected const string CrLf = "\r\n"; 58 | 59 | #endregion 60 | 61 | #region Protected Constructors 62 | 63 | protected HttpBase (Version version, NameValueCollection headers) 64 | { 65 | _version = version; 66 | _headers = headers; 67 | } 68 | 69 | #endregion 70 | 71 | #region Public Properties 72 | 73 | public string EntityBody { 74 | get { 75 | if (EntityBodyData == null || EntityBodyData.Length == 0) 76 | return String.Empty; 77 | 78 | Encoding enc = null; 79 | 80 | var contentType = _headers["Content-Type"]; 81 | if (contentType != null && contentType.Length > 0) 82 | enc = HttpUtility.GetEncoding (contentType); 83 | 84 | return (enc ?? Encoding.UTF8).GetString (EntityBodyData); 85 | } 86 | } 87 | 88 | public NameValueCollection Headers { 89 | get { 90 | return _headers; 91 | } 92 | } 93 | 94 | public Version ProtocolVersion { 95 | get { 96 | return _version; 97 | } 98 | } 99 | 100 | #endregion 101 | 102 | #region Private Methods 103 | 104 | private static byte[] readEntityBody (Stream stream, string length) 105 | { 106 | long len; 107 | if (!Int64.TryParse (length, out len)) 108 | throw new ArgumentException ("Cannot be parsed.", nameof(length)); 109 | 110 | if (len < 0) 111 | throw new ArgumentOutOfRangeException (nameof(length), "Less than zero."); 112 | 113 | return len > 1024 114 | ? stream.ReadBytes (len, 1024) 115 | : len > 0 116 | ? stream.ReadBytes ((int) len) 117 | : null; 118 | } 119 | 120 | private static string[] readHeaders (Stream stream, int maxLength) 121 | { 122 | var buff = new List (); 123 | var cnt = 0; 124 | Action add = i => { 125 | if (i == -1) 126 | throw new EndOfStreamException ("The header cannot be read from the data source."); 127 | 128 | buff.Add ((byte) i); 129 | cnt++; 130 | }; 131 | 132 | var read = false; 133 | while (cnt < maxLength) { 134 | if (stream.ReadByte ().EqualsWith ('\r', add) && 135 | stream.ReadByte ().EqualsWith ('\n', add) && 136 | stream.ReadByte ().EqualsWith ('\r', add) && 137 | stream.ReadByte ().EqualsWith ('\n', add)) { 138 | read = true; 139 | break; 140 | } 141 | } 142 | 143 | if (!read) 144 | throw new WebSocketException ("The length of header part is greater than the max length."); 145 | 146 | return Encoding.UTF8.GetString (buff.ToArray ()) 147 | .Replace (CrLf + " ", " ") 148 | .Replace (CrLf + "\t", " ") 149 | .Split (new[] { CrLf }, StringSplitOptions.RemoveEmptyEntries); 150 | } 151 | 152 | #endregion 153 | 154 | #region Protected Methods 155 | 156 | protected static T Read (Stream stream, Func parser, int millisecondsTimeout) 157 | where T : HttpBase 158 | { 159 | var timeout = false; 160 | var timer = new Timer ( 161 | state => { 162 | timeout = true; 163 | stream.Dispose(); 164 | }, 165 | null, 166 | millisecondsTimeout, 167 | -1); 168 | 169 | T http = null; 170 | Exception exception = null; 171 | try { 172 | http = parser (readHeaders (stream, _headersMaxLength)); 173 | var contentLen = http.Headers["Content-Length"]; 174 | if (contentLen != null && contentLen.Length > 0) 175 | http.EntityBodyData = readEntityBody (stream, contentLen); 176 | } 177 | catch (Exception ex) { 178 | exception = ex; 179 | } 180 | finally { 181 | timer.Change (-1, -1); 182 | timer.Dispose (); 183 | } 184 | 185 | var msg = timeout 186 | ? "A timeout has occurred while reading an HTTP request/response." 187 | : exception != null 188 | ? "An exception has occurred while reading an HTTP request/response." 189 | : null; 190 | 191 | if (msg != null) 192 | throw new WebSocketException (msg, exception); 193 | 194 | return http; 195 | } 196 | 197 | #endregion 198 | 199 | #region Public Methods 200 | 201 | public byte[] ToByteArray () 202 | { 203 | return Encoding.UTF8.GetBytes (ToString ()); 204 | } 205 | 206 | #endregion 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/NetworkCredential.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * NetworkCredential.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2014-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | using System; 30 | 31 | namespace WebSocketSharp.Net 32 | { 33 | /// 34 | /// Provides the credentials for the HTTP authentication (Basic/Digest). 35 | /// 36 | public class NetworkCredential 37 | { 38 | #region Private Fields 39 | 40 | private string _domain; 41 | private string _password; 42 | private string[] _roles; 43 | private string _userName; 44 | 45 | #endregion 46 | 47 | #region Public Constructors 48 | 49 | /// 50 | /// Initializes a new instance of the class with 51 | /// the specified user name and password. 52 | /// 53 | /// 54 | /// A that represents the user name associated with the credentials. 55 | /// 56 | /// 57 | /// A that represents the password for the user name associated with 58 | /// the credentials. 59 | /// 60 | /// 61 | /// is . 62 | /// 63 | /// 64 | /// is empty. 65 | /// 66 | public NetworkCredential (string userName, string password) 67 | : this (userName, password, null, null) 68 | { 69 | } 70 | 71 | /// 72 | /// Initializes a new instance of the class with 73 | /// the specified user name, password, domain, and roles. 74 | /// 75 | /// 76 | /// A that represents the user name associated with the credentials. 77 | /// 78 | /// 79 | /// A that represents the password for the user name associated with 80 | /// the credentials. 81 | /// 82 | /// 83 | /// A that represents the name of the user domain associated with 84 | /// the credentials. 85 | /// 86 | /// 87 | /// An array of that contains the role names to which 88 | /// the user associated with the credentials belongs if any. 89 | /// 90 | /// 91 | /// is . 92 | /// 93 | /// 94 | /// is empty. 95 | /// 96 | public NetworkCredential ( 97 | string userName, string password, string domain, params string[] roles) 98 | { 99 | if (userName == null) 100 | throw new ArgumentNullException (nameof(userName)); 101 | 102 | if (userName.Length == 0) 103 | throw new ArgumentException ("An empty string.", nameof(userName)); 104 | 105 | _userName = userName; 106 | _password = password; 107 | _domain = domain; 108 | _roles = roles; 109 | } 110 | 111 | #endregion 112 | 113 | #region Public Properties 114 | 115 | /// 116 | /// Gets the name of the user domain associated with the credentials. 117 | /// 118 | /// 119 | /// A that represents the name of the user domain associated with 120 | /// the credentials. 121 | /// 122 | public string Domain { 123 | get { 124 | return _domain ?? String.Empty; 125 | } 126 | 127 | internal set { 128 | _domain = value; 129 | } 130 | } 131 | 132 | /// 133 | /// Gets the password for the user name associated with the credentials. 134 | /// 135 | /// 136 | /// A that represents the password for the user name associated with 137 | /// the credentials. 138 | /// 139 | public string Password { 140 | get { 141 | return _password ?? String.Empty; 142 | } 143 | 144 | internal set { 145 | _password = value; 146 | } 147 | } 148 | 149 | /// 150 | /// Gets the role names to which the user associated with the credentials belongs. 151 | /// 152 | /// 153 | /// An array of that contains the role names to which 154 | /// the user associated with the credentials belongs if any. 155 | /// 156 | public string[] Roles { 157 | get { 158 | return _roles ?? (_roles = new string[0]); 159 | } 160 | 161 | internal set { 162 | _roles = value; 163 | } 164 | } 165 | 166 | /// 167 | /// Gets the user name associated with the credentials. 168 | /// 169 | /// 170 | /// A that represents the user name associated with the credentials. 171 | /// 172 | public string UserName { 173 | get { 174 | return _userName; 175 | } 176 | 177 | internal set { 178 | _userName = value; 179 | } 180 | } 181 | 182 | #endregion 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/ClientSslConfiguration.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ClientSslConfiguration.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2014 liryna 8 | * Copyright (c) 2014 sta.blockhead 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy 11 | * of this software and associated documentation files (the "Software"), to deal 12 | * in the Software without restriction, including without limitation the rights 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | * copies of the Software, and to permit persons to whom the Software is 15 | * furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | * THE SOFTWARE. 27 | */ 28 | #endregion 29 | 30 | #region Authors 31 | /* 32 | * Authors: 33 | * - Liryna 34 | */ 35 | #endregion 36 | 37 | using System.Net.Security; 38 | using System.Security.Authentication; 39 | using System.Security.Cryptography.X509Certificates; 40 | 41 | namespace WebSocketSharp.Net 42 | { 43 | /// 44 | /// Stores the parameters used to configure a instance as a client. 45 | /// 46 | public class ClientSslConfiguration : SslConfiguration 47 | { 48 | #region Private Fields 49 | 50 | private X509CertificateCollection _certs; 51 | private string _host; 52 | 53 | #endregion 54 | 55 | #region Public Constructors 56 | 57 | /// 58 | /// Initializes a new instance of the class with 59 | /// the specified . 60 | /// 61 | /// 62 | /// A that represents the name of the server that shares 63 | /// a secure connection. 64 | /// 65 | public ClientSslConfiguration (string targetHost) 66 | : this (targetHost, null, SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls, false) 67 | { 68 | } 69 | 70 | /// 71 | /// Initializes a new instance of the class with 72 | /// the specified , , 73 | /// , and . 74 | /// 75 | /// 76 | /// A that represents the name of the server that shares 77 | /// a secure connection. 78 | /// 79 | /// 80 | /// A that contains client certificates. 81 | /// 82 | /// 83 | /// The enum value that represents the protocols used for 84 | /// authentication. 85 | /// 86 | /// 87 | /// true if the certificate revocation list is checked during authentication; 88 | /// otherwise, false. 89 | /// 90 | public ClientSslConfiguration ( 91 | string targetHost, 92 | X509CertificateCollection clientCertificates, 93 | SslProtocols enabledSslProtocols, 94 | bool checkCertificateRevocation) 95 | : base (enabledSslProtocols, checkCertificateRevocation) 96 | { 97 | _host = targetHost; 98 | _certs = clientCertificates; 99 | } 100 | 101 | #endregion 102 | 103 | #region Public Properties 104 | 105 | /// 106 | /// Gets or sets the collection that contains client certificates. 107 | /// 108 | /// 109 | /// A that contains client certificates. 110 | /// 111 | public X509CertificateCollection ClientCertificates { 112 | get { 113 | return _certs; 114 | } 115 | 116 | set { 117 | _certs = value; 118 | } 119 | } 120 | 121 | /// 122 | /// Gets or sets the callback used to select a client certificate to supply to the server. 123 | /// 124 | /// 125 | /// If this callback returns , no client certificate will be supplied. 126 | /// 127 | /// 128 | /// A delegate that references the method 129 | /// used to select the client certificate. The default value is a function that only returns 130 | /// . 131 | /// 132 | public LocalCertificateSelectionCallback ClientCertificateSelectionCallback { 133 | get { 134 | return CertificateSelectionCallback; 135 | } 136 | 137 | set { 138 | CertificateSelectionCallback = value; 139 | } 140 | } 141 | 142 | /// 143 | /// Gets or sets the callback used to validate the certificate supplied by the server. 144 | /// 145 | /// 146 | /// If this callback returns true, the server certificate will be valid. 147 | /// 148 | /// 149 | /// A delegate that references the method 150 | /// used to validate the server certificate. The default value is a function that only returns 151 | /// true. 152 | /// 153 | public RemoteCertificateValidationCallback ServerCertificateValidationCallback { 154 | get { 155 | return CertificateValidationCallback; 156 | } 157 | 158 | set { 159 | CertificateValidationCallback = value; 160 | } 161 | } 162 | 163 | /// 164 | /// Gets or sets the name of the server that shares a secure connection. 165 | /// 166 | /// 167 | /// A that represents the name of the server that shares 168 | /// a secure connection. 169 | /// 170 | public string TargetHost { 171 | get { 172 | return _host; 173 | } 174 | 175 | set { 176 | _host = value; 177 | } 178 | } 179 | 180 | #endregion 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/ChunkedRequestStream.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ChunkedRequestStream.cs 4 | * 5 | * This code is derived from ChunkedInputStream.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2012-2015 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | using System.IO; 42 | 43 | namespace WebSocketSharp.Net 44 | { 45 | internal class ChunkedRequestStream : RequestStream 46 | { 47 | #region Private Fields 48 | 49 | private const int _bufferLength = 8192; 50 | private HttpListenerContext _context; 51 | private ChunkStream _decoder; 52 | private bool _disposed; 53 | private bool _noMoreData; 54 | 55 | #endregion 56 | 57 | #region Internal Constructors 58 | 59 | internal ChunkedRequestStream ( 60 | Stream stream, byte[] buffer, int offset, int count, HttpListenerContext context) 61 | : base (stream, buffer, offset, count) 62 | { 63 | _context = context; 64 | _decoder = new ChunkStream ((WebHeaderCollection) context.Request.Headers); 65 | } 66 | 67 | #endregion 68 | 69 | #region Internal Properties 70 | 71 | internal ChunkStream Decoder { 72 | get { 73 | return _decoder; 74 | } 75 | 76 | set { 77 | _decoder = value; 78 | } 79 | } 80 | 81 | #endregion 82 | 83 | #region Private Methods 84 | 85 | private void onRead (IAsyncResult asyncResult) 86 | { 87 | var rstate = (ReadBufferState) asyncResult.AsyncState; 88 | var ares = rstate.AsyncResult; 89 | try { 90 | var nread = base.EndRead (asyncResult); 91 | _decoder.Write (ares.Buffer, ares.Offset, nread); 92 | nread = _decoder.Read (rstate.Buffer, rstate.Offset, rstate.Count); 93 | rstate.Offset += nread; 94 | rstate.Count -= nread; 95 | if (rstate.Count == 0 || !_decoder.WantMore || nread == 0) { 96 | _noMoreData = !_decoder.WantMore && nread == 0; 97 | ares.Count = rstate.InitialCount - rstate.Count; 98 | ares.Complete (); 99 | 100 | return; 101 | } 102 | 103 | ares.Offset = 0; 104 | ares.Count = Math.Min (_bufferLength, _decoder.ChunkLeft + 6); 105 | base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, rstate); 106 | } 107 | catch (Exception ex) { 108 | _context.Connection.SendError (ex.Message, 400); 109 | ares.Complete (ex); 110 | } 111 | } 112 | 113 | #endregion 114 | 115 | #region Public Methods 116 | 117 | public override IAsyncResult BeginRead ( 118 | byte[] buffer, int offset, int count, AsyncCallback callback, object state) 119 | { 120 | if (_disposed) 121 | throw new ObjectDisposedException (GetType ().ToString ()); 122 | 123 | if (buffer == null) 124 | throw new ArgumentNullException (nameof(buffer)); 125 | 126 | if (offset < 0) 127 | throw new ArgumentOutOfRangeException (nameof(offset), "A negative value."); 128 | 129 | if (count < 0) 130 | throw new ArgumentOutOfRangeException (nameof(count), "A negative value."); 131 | 132 | var len = buffer.Length; 133 | if (offset + count > len) 134 | throw new ArgumentException ( 135 | "The sum of 'offset' and 'count' is greater than 'buffer' length."); 136 | 137 | var ares = new HttpStreamAsyncResult (callback, state); 138 | if (_noMoreData) { 139 | ares.Complete (); 140 | return ares; 141 | } 142 | 143 | var nread = _decoder.Read (buffer, offset, count); 144 | offset += nread; 145 | count -= nread; 146 | if (count == 0) { 147 | // Got all we wanted, no need to bother the decoder yet. 148 | ares.Count = nread; 149 | ares.Complete (); 150 | 151 | return ares; 152 | } 153 | 154 | if (!_decoder.WantMore) { 155 | _noMoreData = nread == 0; 156 | ares.Count = nread; 157 | ares.Complete (); 158 | 159 | return ares; 160 | } 161 | 162 | ares.Buffer = new byte[_bufferLength]; 163 | ares.Offset = 0; 164 | ares.Count = _bufferLength; 165 | 166 | var rstate = new ReadBufferState (buffer, offset, count, ares); 167 | rstate.InitialCount += nread; 168 | base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, rstate); 169 | 170 | return ares; 171 | } 172 | 173 | public override void Close () 174 | { 175 | if (_disposed) 176 | return; 177 | 178 | _disposed = true; 179 | base.Close (); 180 | } 181 | 182 | public override int EndRead (IAsyncResult asyncResult) 183 | { 184 | if (_disposed) 185 | throw new ObjectDisposedException (GetType ().ToString ()); 186 | 187 | if (asyncResult == null) 188 | throw new ArgumentNullException (nameof(asyncResult)); 189 | 190 | var ares = asyncResult as HttpStreamAsyncResult; 191 | if (ares == null) 192 | throw new ArgumentException ("A wrong IAsyncResult.", nameof(asyncResult)); 193 | 194 | if (!ares.IsCompleted) 195 | ares.AsyncWaitHandle.WaitOne (); 196 | 197 | if (ares.HasException) 198 | throw new HttpListenerException (400, "I/O operation aborted."); 199 | 200 | return ares.Count; 201 | } 202 | 203 | public override int Read (byte[] buffer, int offset, int count) 204 | { 205 | var ares = BeginRead (buffer, offset, count, null, null); 206 | return EndRead (ares); 207 | } 208 | 209 | #endregion 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/EndPointManager.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * EndPointManager.cs 4 | * 5 | * This code is derived from EndPointManager.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2012-2016 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | #region Contributors 41 | /* 42 | * Contributors: 43 | * - Liryna 44 | */ 45 | #endregion 46 | 47 | using System; 48 | using System.Collections; 49 | using System.Collections.Generic; 50 | using System.Net; 51 | 52 | namespace WebSocketSharp.Net 53 | { 54 | internal sealed class EndPointManager 55 | { 56 | #region Private Fields 57 | 58 | private static readonly Dictionary _endpoints; 59 | 60 | #endregion 61 | 62 | #region Static Constructor 63 | 64 | static EndPointManager () 65 | { 66 | _endpoints = new Dictionary (); 67 | } 68 | 69 | #endregion 70 | 71 | #region Private Constructors 72 | 73 | private EndPointManager () 74 | { 75 | } 76 | 77 | #endregion 78 | 79 | #region Private Methods 80 | 81 | private static void addPrefix (string uriPrefix, HttpListener listener) 82 | { 83 | var pref = new HttpListenerPrefix (uriPrefix); 84 | 85 | var addr = convertToIPAddress (pref.Host); 86 | if (!addr.IsLocal ()) 87 | throw new HttpListenerException (87, "Includes an invalid host."); 88 | 89 | int port; 90 | if (!Int32.TryParse (pref.Port, out port)) 91 | throw new HttpListenerException (87, "Includes an invalid port."); 92 | 93 | if (!port.IsPortNumber ()) 94 | throw new HttpListenerException (87, "Includes an invalid port."); 95 | 96 | var path = pref.Path; 97 | if (path.IndexOf ('%') != -1) 98 | throw new HttpListenerException (87, "Includes an invalid path."); 99 | 100 | if (path.IndexOf ("//", StringComparison.Ordinal) != -1) 101 | throw new HttpListenerException (87, "Includes an invalid path."); 102 | 103 | var endpoint = new IPEndPoint (addr, port); 104 | 105 | EndPointListener lsnr; 106 | if (_endpoints.TryGetValue (endpoint, out lsnr)) { 107 | if (lsnr.IsSecure ^ pref.IsSecure) 108 | throw new HttpListenerException (87, "Includes an invalid scheme."); 109 | } 110 | else { 111 | lsnr = 112 | new EndPointListener ( 113 | endpoint, 114 | pref.IsSecure, 115 | listener.CertificateFolderPath, 116 | listener.SslConfiguration, 117 | listener.ReuseAddress 118 | ); 119 | 120 | _endpoints.Add (endpoint, lsnr); 121 | } 122 | 123 | lsnr.AddPrefix (pref, listener); 124 | } 125 | 126 | private static IPAddress convertToIPAddress (string hostname) 127 | { 128 | return hostname == "*" || hostname == "+" ? IPAddress.Any : hostname.ToIPAddress (); 129 | } 130 | 131 | private static void removePrefix (string uriPrefix, HttpListener listener) 132 | { 133 | var pref = new HttpListenerPrefix (uriPrefix); 134 | 135 | var addr = convertToIPAddress (pref.Host); 136 | if (!addr.IsLocal ()) 137 | return; 138 | 139 | int port; 140 | if (!Int32.TryParse (pref.Port, out port)) 141 | return; 142 | 143 | if (!port.IsPortNumber ()) 144 | return; 145 | 146 | var path = pref.Path; 147 | if (path.IndexOf ('%') != -1) 148 | return; 149 | 150 | if (path.IndexOf ("//", StringComparison.Ordinal) != -1) 151 | return; 152 | 153 | var endpoint = new IPEndPoint (addr, port); 154 | 155 | EndPointListener lsnr; 156 | if (!_endpoints.TryGetValue (endpoint, out lsnr)) 157 | return; 158 | 159 | if (lsnr.IsSecure ^ pref.IsSecure) 160 | return; 161 | 162 | lsnr.RemovePrefix (pref, listener); 163 | } 164 | 165 | #endregion 166 | 167 | #region Internal Methods 168 | 169 | internal static bool RemoveEndPoint (IPEndPoint endpoint) 170 | { 171 | lock (((ICollection) _endpoints).SyncRoot) { 172 | EndPointListener lsnr; 173 | if (!_endpoints.TryGetValue (endpoint, out lsnr)) 174 | return false; 175 | 176 | _endpoints.Remove (endpoint); 177 | lsnr.Close (); 178 | 179 | return true; 180 | } 181 | } 182 | 183 | #endregion 184 | 185 | #region Public Methods 186 | 187 | public static void AddListener (HttpListener listener) 188 | { 189 | var added = new List (); 190 | lock (((ICollection) _endpoints).SyncRoot) { 191 | try { 192 | foreach (var pref in listener.Prefixes) { 193 | addPrefix (pref, listener); 194 | added.Add (pref); 195 | } 196 | } 197 | catch { 198 | foreach (var pref in added) 199 | removePrefix (pref, listener); 200 | 201 | throw; 202 | } 203 | } 204 | } 205 | 206 | public static void AddPrefix (string uriPrefix, HttpListener listener) 207 | { 208 | lock (((ICollection) _endpoints).SyncRoot) 209 | addPrefix (uriPrefix, listener); 210 | } 211 | 212 | public static void RemoveListener (HttpListener listener) 213 | { 214 | lock (((ICollection) _endpoints).SyncRoot) { 215 | foreach (var pref in listener.Prefixes) 216 | removePrefix (pref, listener); 217 | } 218 | } 219 | 220 | public static void RemovePrefix (string uriPrefix, HttpListener listener) 221 | { 222 | lock (((ICollection) _endpoints).SyncRoot) 223 | removePrefix (uriPrefix, listener); 224 | } 225 | 226 | #endregion 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpRequestHeader.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpRequestHeader.cs 4 | * 5 | * This code is derived from System.Net.HttpRequestHeader.cs of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2014 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | namespace WebSocketSharp.Net 41 | { 42 | /// 43 | /// Contains the HTTP headers that may be specified in a client request. 44 | /// 45 | /// 46 | /// The HttpRequestHeader enumeration contains the HTTP request headers defined in 47 | /// RFC 2616 for the HTTP/1.1 and 48 | /// RFC 6455 for the WebSocket. 49 | /// 50 | public enum HttpRequestHeader 51 | { 52 | /// 53 | /// Indicates the Cache-Control header. 54 | /// 55 | CacheControl, 56 | /// 57 | /// Indicates the Connection header. 58 | /// 59 | Connection, 60 | /// 61 | /// Indicates the Date header. 62 | /// 63 | Date, 64 | /// 65 | /// Indicates the Keep-Alive header. 66 | /// 67 | KeepAlive, 68 | /// 69 | /// Indicates the Pragma header. 70 | /// 71 | Pragma, 72 | /// 73 | /// Indicates the Trailer header. 74 | /// 75 | Trailer, 76 | /// 77 | /// Indicates the Transfer-Encoding header. 78 | /// 79 | TransferEncoding, 80 | /// 81 | /// Indicates the Upgrade header. 82 | /// 83 | Upgrade, 84 | /// 85 | /// Indicates the Via header. 86 | /// 87 | Via, 88 | /// 89 | /// Indicates the Warning header. 90 | /// 91 | Warning, 92 | /// 93 | /// Indicates the Allow header. 94 | /// 95 | Allow, 96 | /// 97 | /// Indicates the Content-Length header. 98 | /// 99 | ContentLength, 100 | /// 101 | /// Indicates the Content-Type header. 102 | /// 103 | ContentType, 104 | /// 105 | /// Indicates the Content-Encoding header. 106 | /// 107 | ContentEncoding, 108 | /// 109 | /// Indicates the Content-Language header. 110 | /// 111 | ContentLanguage, 112 | /// 113 | /// Indicates the Content-Location header. 114 | /// 115 | ContentLocation, 116 | /// 117 | /// Indicates the Content-MD5 header. 118 | /// 119 | ContentMd5, 120 | /// 121 | /// Indicates the Content-Range header. 122 | /// 123 | ContentRange, 124 | /// 125 | /// Indicates the Expires header. 126 | /// 127 | Expires, 128 | /// 129 | /// Indicates the Last-Modified header. 130 | /// 131 | LastModified, 132 | /// 133 | /// Indicates the Accept header. 134 | /// 135 | Accept, 136 | /// 137 | /// Indicates the Accept-Charset header. 138 | /// 139 | AcceptCharset, 140 | /// 141 | /// Indicates the Accept-Encoding header. 142 | /// 143 | AcceptEncoding, 144 | /// 145 | /// Indicates the Accept-Language header. 146 | /// 147 | AcceptLanguage, 148 | /// 149 | /// Indicates the Authorization header. 150 | /// 151 | Authorization, 152 | /// 153 | /// Indicates the Cookie header. 154 | /// 155 | Cookie, 156 | /// 157 | /// Indicates the Expect header. 158 | /// 159 | Expect, 160 | /// 161 | /// Indicates the From header. 162 | /// 163 | From, 164 | /// 165 | /// Indicates the Host header. 166 | /// 167 | Host, 168 | /// 169 | /// Indicates the If-Match header. 170 | /// 171 | IfMatch, 172 | /// 173 | /// Indicates the If-Modified-Since header. 174 | /// 175 | IfModifiedSince, 176 | /// 177 | /// Indicates the If-None-Match header. 178 | /// 179 | IfNoneMatch, 180 | /// 181 | /// Indicates the If-Range header. 182 | /// 183 | IfRange, 184 | /// 185 | /// Indicates the If-Unmodified-Since header. 186 | /// 187 | IfUnmodifiedSince, 188 | /// 189 | /// Indicates the Max-Forwards header. 190 | /// 191 | MaxForwards, 192 | /// 193 | /// Indicates the Proxy-Authorization header. 194 | /// 195 | ProxyAuthorization, 196 | /// 197 | /// Indicates the Referer header. 198 | /// 199 | Referer, 200 | /// 201 | /// Indicates the Range header. 202 | /// 203 | Range, 204 | /// 205 | /// Indicates the TE header. 206 | /// 207 | Te, 208 | /// 209 | /// Indicates the Translate header. 210 | /// 211 | Translate, 212 | /// 213 | /// Indicates the User-Agent header. 214 | /// 215 | UserAgent, 216 | /// 217 | /// Indicates the Sec-WebSocket-Key header. 218 | /// 219 | SecWebSocketKey, 220 | /// 221 | /// Indicates the Sec-WebSocket-Extensions header. 222 | /// 223 | SecWebSocketExtensions, 224 | /// 225 | /// Indicates the Sec-WebSocket-Protocol header. 226 | /// 227 | SecWebSocketProtocol, 228 | /// 229 | /// Indicates the Sec-WebSocket-Version header. 230 | /// 231 | SecWebSocketVersion 232 | } 233 | } 234 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpListenerPrefix.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpListenerPrefix.cs 4 | * 5 | * This code is derived from ListenerPrefix.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2012-2016 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | * - Oleg Mihailik 38 | */ 39 | #endregion 40 | 41 | using System; 42 | using System.Net; 43 | 44 | namespace WebSocketSharp.Net 45 | { 46 | internal sealed class HttpListenerPrefix 47 | { 48 | #region Private Fields 49 | 50 | private string _host; 51 | private HttpListener _listener; 52 | private string _original; 53 | private string _path; 54 | private string _port; 55 | private string _prefix; 56 | private bool _secure; 57 | 58 | #endregion 59 | 60 | #region Internal Constructors 61 | 62 | /// 63 | /// Initializes a new instance of the class with 64 | /// the specified . 65 | /// 66 | /// 67 | /// This constructor must be called after calling the CheckPrefix method. 68 | /// 69 | /// 70 | /// A that represents the URI prefix. 71 | /// 72 | internal HttpListenerPrefix (string uriPrefix) 73 | { 74 | _original = uriPrefix; 75 | parse (uriPrefix); 76 | } 77 | 78 | #endregion 79 | 80 | #region Public Properties 81 | 82 | public string Host { 83 | get { 84 | return _host; 85 | } 86 | } 87 | 88 | public bool IsSecure { 89 | get { 90 | return _secure; 91 | } 92 | } 93 | 94 | public HttpListener Listener { 95 | get { 96 | return _listener; 97 | } 98 | 99 | set { 100 | _listener = value; 101 | } 102 | } 103 | 104 | public string Original { 105 | get { 106 | return _original; 107 | } 108 | } 109 | 110 | public string Path { 111 | get { 112 | return _path; 113 | } 114 | } 115 | 116 | public string Port { 117 | get { 118 | return _port; 119 | } 120 | } 121 | 122 | #endregion 123 | 124 | #region Private Methods 125 | 126 | private void parse (string uriPrefix) 127 | { 128 | if (uriPrefix.StartsWith ("https")) 129 | _secure = true; 130 | 131 | var len = uriPrefix.Length; 132 | var startHost = uriPrefix.IndexOf (':') + 3; 133 | var root = uriPrefix.IndexOf ('/', startHost + 1, len - startHost - 1); 134 | 135 | var colon = uriPrefix.LastIndexOf (':', root - 1, root - startHost - 1); 136 | if (uriPrefix[root - 1] != ']' && colon > startHost) { 137 | _host = uriPrefix.Substring (startHost, colon - startHost); 138 | _port = uriPrefix.Substring (colon + 1, root - colon - 1); 139 | } 140 | else { 141 | _host = uriPrefix.Substring (startHost, root - startHost); 142 | _port = _secure ? "443" : "80"; 143 | } 144 | 145 | _path = uriPrefix.Substring (root); 146 | 147 | _prefix = 148 | String.Format ("http{0}://{1}:{2}{3}", _secure ? "s" : "", _host, _port, _path); 149 | } 150 | 151 | #endregion 152 | 153 | #region Public Methods 154 | 155 | public static void CheckPrefix (string uriPrefix) 156 | { 157 | if (uriPrefix == null) 158 | throw new ArgumentNullException (nameof(uriPrefix)); 159 | 160 | var len = uriPrefix.Length; 161 | if (len == 0) 162 | throw new ArgumentException ("An empty string.", nameof(uriPrefix)); 163 | 164 | if (!(uriPrefix.StartsWith ("http://") || uriPrefix.StartsWith ("https://"))) 165 | throw new ArgumentException ("The scheme isn't 'http' or 'https'.", nameof(uriPrefix)); 166 | 167 | var startHost = uriPrefix.IndexOf (':') + 3; 168 | if (startHost >= len) 169 | throw new ArgumentException ("No host is specified.", nameof(uriPrefix)); 170 | 171 | if (uriPrefix[startHost] == ':') 172 | throw new ArgumentException ("No host is specified.", nameof(uriPrefix)); 173 | 174 | var root = uriPrefix.IndexOf ('/', startHost, len - startHost); 175 | if (root == startHost) 176 | throw new ArgumentException ("No host is specified.", nameof(uriPrefix)); 177 | 178 | if (root == -1 || uriPrefix[len - 1] != '/') 179 | throw new ArgumentException ("Ends without '/'.", nameof(uriPrefix)); 180 | 181 | if (uriPrefix[root - 1] == ':') 182 | throw new ArgumentException ("No port is specified.", nameof(uriPrefix)); 183 | 184 | if (root == len - 2) 185 | throw new ArgumentException ("No path is specified.", nameof(uriPrefix)); 186 | } 187 | 188 | /// 189 | /// Determines whether this instance and the specified have the same value. 190 | /// 191 | /// 192 | /// This method will be required to detect duplicates in any collection. 193 | /// 194 | /// 195 | /// An to compare to this instance. 196 | /// 197 | /// 198 | /// true if is a and 199 | /// its value is the same as this instance; otherwise, false. 200 | /// 201 | public override bool Equals (Object obj) 202 | { 203 | var pref = obj as HttpListenerPrefix; 204 | return pref != null && pref._prefix == _prefix; 205 | } 206 | 207 | /// 208 | /// Gets the hash code for this instance. 209 | /// 210 | /// 211 | /// This method will be required to detect duplicates in any collection. 212 | /// 213 | /// 214 | /// An that represents the hash code. 215 | /// 216 | public override int GetHashCode () 217 | { 218 | return _prefix.GetHashCode (); 219 | } 220 | 221 | public override string ToString () 222 | { 223 | return _prefix; 224 | } 225 | 226 | #endregion 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /websocket-sharp-core/HttpRequest.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpRequest.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2015 sta.blockhead 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #endregion 28 | 29 | #region Contributors 30 | /* 31 | * Contributors: 32 | * - David Burhans 33 | */ 34 | #endregion 35 | 36 | using System; 37 | using System.Collections.Specialized; 38 | using System.IO; 39 | using System.Text; 40 | using WebSocketSharp.Net; 41 | 42 | namespace WebSocketSharp 43 | { 44 | internal class HttpRequest : HttpBase 45 | { 46 | #region Private Fields 47 | 48 | private string _method; 49 | private string _uri; 50 | private bool _websocketRequest; 51 | private bool _websocketRequestSet; 52 | 53 | #endregion 54 | 55 | #region Private Constructors 56 | 57 | private HttpRequest(string method, string uri, Version version, NameValueCollection headers) 58 | : base(version, headers) 59 | { 60 | _method = method; 61 | _uri = uri; 62 | } 63 | 64 | #endregion 65 | 66 | #region Internal Constructors 67 | 68 | internal HttpRequest(string method, string uri) 69 | : this(method, uri, HttpVersion.Version11, new NameValueCollection()) 70 | { 71 | Headers["User-Agent"] = "websocket-sharp/1.0"; 72 | } 73 | 74 | #endregion 75 | 76 | #region Public Properties 77 | 78 | public AuthenticationResponse AuthenticationResponse 79 | { 80 | get 81 | { 82 | var res = Headers["Authorization"]; 83 | return res != null && res.Length > 0 84 | ? AuthenticationResponse.Parse(res) 85 | : null; 86 | } 87 | } 88 | 89 | public CookieCollection Cookies 90 | { 91 | get { return Headers.GetCookies(false); } 92 | } 93 | 94 | public string HttpMethod 95 | { 96 | get { return _method; } 97 | } 98 | 99 | public bool IsWebSocketRequest 100 | { 101 | get 102 | { 103 | if (!_websocketRequestSet) 104 | { 105 | var headers = Headers; 106 | _websocketRequest = _method == "GET" && 107 | ProtocolVersion > HttpVersion.Version10 && 108 | headers.Contains("Upgrade", "websocket") && 109 | headers.Contains("Connection", "Upgrade"); 110 | 111 | _websocketRequestSet = true; 112 | } 113 | 114 | return _websocketRequest; 115 | } 116 | } 117 | 118 | public string RequestUri 119 | { 120 | get { return _uri; } 121 | } 122 | 123 | #endregion 124 | 125 | #region Internal Methods 126 | 127 | internal static HttpRequest CreateConnectRequest(Uri uri) 128 | { 129 | var host = uri.DnsSafeHost; 130 | var port = uri.Port; 131 | var authority = String.Format("{0}:{1}", host, port); 132 | var req = new HttpRequest("CONNECT", authority); 133 | req.Headers["Host"] = port == 80 ? host : authority; 134 | 135 | return req; 136 | } 137 | 138 | internal static HttpRequest CreateWebSocketRequest(Uri uri) 139 | { 140 | var req = new HttpRequest("GET", uri.PathAndQuery); 141 | var headers = req.Headers; 142 | 143 | // Only includes a port number in the Host header value if it's non-default. 144 | // See: https://tools.ietf.org/html/rfc6455#page-17 145 | var port = uri.Port; 146 | var schm = uri.Scheme; 147 | headers["Host"] = (port == 80 && schm == "ws") || (port == 443 && schm == "wss") 148 | ? uri.DnsSafeHost 149 | : uri.Authority; 150 | 151 | headers["Upgrade"] = "websocket"; 152 | headers["Connection"] = "Upgrade"; 153 | 154 | return req; 155 | } 156 | 157 | internal HttpResponse GetResponse(Stream stream, int millisecondsTimeout) 158 | { 159 | var buff = ToByteArray(); 160 | stream.Write(buff, 0, buff.Length); 161 | 162 | return Read(stream, HttpResponse.Parse, millisecondsTimeout); 163 | } 164 | 165 | internal static HttpRequest Parse(string[] headerParts) 166 | { 167 | var requestLine = headerParts[0].Split(new[] {' '}, 3); 168 | if (requestLine.Length != 3) 169 | throw new ArgumentException("Invalid request line: " + headerParts[0]); 170 | 171 | var headers = new WebHeaderCollection(); 172 | for (int i = 1; i < headerParts.Length; i++) 173 | headers.InternalSet(headerParts[i], false); 174 | 175 | return new HttpRequest( 176 | requestLine[0], requestLine[1], new Version(requestLine[2].Substring(5)), headers); 177 | } 178 | 179 | internal static HttpRequest Read(Stream stream, int millisecondsTimeout) 180 | { 181 | return Read(stream, Parse, millisecondsTimeout); 182 | } 183 | 184 | #endregion 185 | 186 | #region Public Methods 187 | 188 | public void SetCookies(CookieCollection cookies) 189 | { 190 | if (cookies == null || cookies.Count == 0) 191 | return; 192 | 193 | var buff = new StringBuilder(64); 194 | foreach (var cookie in cookies.Sorted) 195 | if (!cookie.Expired) 196 | buff.AppendFormat("{0}; ", cookie.ToString()); 197 | 198 | var len = buff.Length; 199 | if (len > 2) 200 | { 201 | buff.Length = len - 2; 202 | Headers["Cookie"] = buff.ToString(); 203 | } 204 | } 205 | 206 | public override string ToString() 207 | { 208 | var output = new StringBuilder(64); 209 | output.AppendFormat("{0} {1} HTTP/{2}{3}", _method, _uri, ProtocolVersion, CrLf); 210 | 211 | var headers = Headers; 212 | foreach (var key in headers.AllKeys) 213 | output.AppendFormat("{0}: {1}{2}", key, headers[key], CrLf); 214 | 215 | output.Append(CrLf); 216 | 217 | var entity = EntityBody; 218 | if (entity.Length > 0) 219 | output.Append(entity); 220 | 221 | return output.ToString(); 222 | } 223 | 224 | #endregion 225 | } 226 | } 227 | -------------------------------------------------------------------------------- /websocket-sharp-core/Net/HttpListenerContext.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpListenerContext.cs 4 | * 5 | * This code is derived from HttpListenerContext.cs (System.Net) of Mono 6 | * (http://www.mono-project.com). 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) 11 | * Copyright (c) 2012-2016 sta.blockhead 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | */ 31 | #endregion 32 | 33 | #region Authors 34 | /* 35 | * Authors: 36 | * - Gonzalo Paniagua Javier 37 | */ 38 | #endregion 39 | 40 | using System; 41 | using System.Security.Principal; 42 | using WebSocketSharp.Net.WebSockets; 43 | 44 | namespace WebSocketSharp.Net 45 | { 46 | /// 47 | /// Provides the access to the HTTP request and response objects used by 48 | /// the . 49 | /// 50 | /// 51 | /// This class cannot be inherited. 52 | /// 53 | public sealed class HttpListenerContext 54 | { 55 | #region Private Fields 56 | 57 | private HttpConnection _connection; 58 | private string _error; 59 | private int _errorStatus; 60 | private HttpListener _listener; 61 | private HttpListenerRequest _request; 62 | private HttpListenerResponse _response; 63 | private IPrincipal _user; 64 | private HttpListenerWebSocketContext _websocketContext; 65 | 66 | #endregion 67 | 68 | #region Internal Constructors 69 | 70 | internal HttpListenerContext (HttpConnection connection) 71 | { 72 | _connection = connection; 73 | _errorStatus = 400; 74 | _request = new HttpListenerRequest (this); 75 | _response = new HttpListenerResponse (this); 76 | } 77 | 78 | #endregion 79 | 80 | #region Internal Properties 81 | 82 | internal HttpConnection Connection { 83 | get { 84 | return _connection; 85 | } 86 | } 87 | 88 | internal string ErrorMessage { 89 | get { 90 | return _error; 91 | } 92 | 93 | set { 94 | _error = value; 95 | } 96 | } 97 | 98 | internal int ErrorStatus { 99 | get { 100 | return _errorStatus; 101 | } 102 | 103 | set { 104 | _errorStatus = value; 105 | } 106 | } 107 | 108 | internal bool HasError { 109 | get { 110 | return _error != null; 111 | } 112 | } 113 | 114 | internal HttpListener Listener { 115 | get { 116 | return _listener; 117 | } 118 | 119 | set { 120 | _listener = value; 121 | } 122 | } 123 | 124 | #endregion 125 | 126 | #region Public Properties 127 | 128 | /// 129 | /// Gets the HTTP request object that represents a client request. 130 | /// 131 | /// 132 | /// A that represents the client request. 133 | /// 134 | public HttpListenerRequest Request { 135 | get { 136 | return _request; 137 | } 138 | } 139 | 140 | /// 141 | /// Gets the HTTP response object used to send a response to the client. 142 | /// 143 | /// 144 | /// A that represents a response to the client request. 145 | /// 146 | public HttpListenerResponse Response { 147 | get { 148 | return _response; 149 | } 150 | } 151 | 152 | /// 153 | /// Gets the client information (identity, authentication, and security roles). 154 | /// 155 | /// 156 | /// A instance that represents the client information. 157 | /// 158 | public IPrincipal User { 159 | get { 160 | return _user; 161 | } 162 | } 163 | 164 | #endregion 165 | 166 | #region Internal Methods 167 | 168 | internal bool Authenticate () 169 | { 170 | var schm = _listener.SelectAuthenticationScheme (_request); 171 | if (schm == AuthenticationSchemes.Anonymous) 172 | return true; 173 | 174 | if (schm == AuthenticationSchemes.None) { 175 | _response.Close (HttpStatusCode.Forbidden); 176 | return false; 177 | } 178 | 179 | var realm = _listener.GetRealm (); 180 | var user = 181 | HttpUtility.CreateUser ( 182 | _request.Headers["Authorization"], 183 | schm, 184 | realm, 185 | _request.HttpMethod, 186 | _listener.GetUserCredentialsFinder () 187 | ); 188 | 189 | if (user == null || !user.Identity.IsAuthenticated) { 190 | _response.CloseWithAuthChallenge (new AuthenticationChallenge (schm, realm).ToString ()); 191 | return false; 192 | } 193 | 194 | _user = user; 195 | return true; 196 | } 197 | 198 | internal bool Register () 199 | { 200 | return _listener.RegisterContext (this); 201 | } 202 | 203 | internal void Unregister () 204 | { 205 | _listener.UnregisterContext (this); 206 | } 207 | 208 | #endregion 209 | 210 | #region Public Methods 211 | 212 | /// 213 | /// Accepts a WebSocket handshake request. 214 | /// 215 | /// 216 | /// A that represents 217 | /// the WebSocket handshake request. 218 | /// 219 | /// 220 | /// A that represents the subprotocol supported on 221 | /// this WebSocket connection. 222 | /// 223 | /// 224 | /// 225 | /// is empty. 226 | /// 227 | /// 228 | /// -or- 229 | /// 230 | /// 231 | /// contains an invalid character. 232 | /// 233 | /// 234 | /// 235 | /// This method has already been called. 236 | /// 237 | public HttpListenerWebSocketContext AcceptWebSocket (string protocol) 238 | { 239 | if (_websocketContext != null) 240 | throw new InvalidOperationException ("The accepting is already in progress."); 241 | 242 | if (protocol != null) { 243 | if (protocol.Length == 0) 244 | throw new ArgumentException ("An empty string.", nameof(protocol)); 245 | 246 | if (!protocol.IsToken ()) 247 | throw new ArgumentException ("Contains an invalid character.", nameof(protocol)); 248 | } 249 | 250 | _websocketContext = new HttpListenerWebSocketContext (this, protocol); 251 | return _websocketContext; 252 | } 253 | 254 | #endregion 255 | } 256 | } 257 | --------------------------------------------------------------------------------