├── .gitattributes ├── .gitignore ├── README.md └── src ├── HttpServerTest ├── Controller │ └── TestController.cs ├── Form1.Designer.cs ├── Form1.cs ├── Form1.resx ├── HttpServerTest.csproj ├── MException.cs ├── Program.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── Service │ └── TestService.cs └── TestEvent.cs ├── MHttpServer.sln └── MHttpServer ├── Controller.cs ├── IExceptionFilter.cs ├── Listen.cs ├── MHttpServer.csproj ├── MimeMapping.cs ├── Properties └── AssemblyInfo.cs ├── packages.config └── websocket-sharp ├── ByteOrder.cs ├── CloseEventArgs.cs ├── CloseStatusCode.cs ├── CompressionMethod.cs ├── ErrorEventArgs.cs ├── Ext.cs ├── Fin.cs ├── HttpBase.cs ├── HttpRequest.cs ├── HttpResponse.cs ├── LogData.cs ├── LogLevel.cs ├── Logger.cs ├── Mask.cs ├── MessageEventArgs.cs ├── Net ├── AuthenticationBase.cs ├── AuthenticationChallenge.cs ├── AuthenticationResponse.cs ├── AuthenticationSchemes.cs ├── Chunk.cs ├── ChunkStream.cs ├── ChunkedRequestStream.cs ├── ClientSslConfiguration.cs ├── Cookie.cs ├── CookieCollection.cs ├── CookieException.cs ├── EndPointListener.cs ├── EndPointManager.cs ├── HttpBasicIdentity.cs ├── HttpConnection.cs ├── HttpDigestIdentity.cs ├── HttpHeaderInfo.cs ├── HttpHeaderType.cs ├── HttpListener.cs ├── HttpListenerAsyncResult.cs ├── HttpListenerContext.cs ├── HttpListenerException.cs ├── HttpListenerPrefix.cs ├── HttpListenerPrefixCollection.cs ├── HttpListenerRequest.cs ├── HttpListenerResponse.cs ├── HttpRequestHeader.cs ├── HttpResponseHeader.cs ├── HttpStatusCode.cs ├── HttpStreamAsyncResult.cs ├── HttpUtility.cs ├── HttpVersion.cs ├── InputChunkState.cs ├── InputState.cs ├── LineState.cs ├── NetworkCredential.cs ├── QueryStringCollection.cs ├── ReadBufferState.cs ├── RequestStream.cs ├── ResponseStream.cs ├── ServerSslConfiguration.cs ├── WebHeaderCollection.cs └── WebSockets │ ├── HttpListenerWebSocketContext.cs │ ├── TcpListenerWebSocketContext.cs │ └── WebSocketContext.cs ├── Opcode.cs ├── PayloadData.cs ├── Rsv.cs ├── Server ├── HttpRequestEventArgs.cs ├── HttpServer.cs ├── IWebSocketSession.cs ├── ServerState.cs ├── WebSocketBehavior.cs ├── WebSocketServer.cs ├── WebSocketServiceHost.cs ├── WebSocketServiceHost`1.cs ├── WebSocketServiceManager.cs └── WebSocketSessionManager.cs ├── WebSocket.cs ├── WebSocketException.cs ├── WebSocketFrame.cs ├── WebSocketState.cs ├── doc ├── .gitignore └── doc.sh ├── websocket-sharp.csproj └── websocket-sharp.snk /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | # Custom for Visual Studio 3 | *.cs diff=csharp 4 | *.sln merge=union 5 | *.csproj merge=union 6 | *.vbproj merge=union 7 | *.fsproj merge=union 8 | *.dbproj merge=union 9 | 10 | # Standard to msysgit 11 | *.doc diff=astextplain 12 | *.DOC diff=astextplain 13 | *.docx diff=astextplain 14 | *.DOCX diff=astextplain 15 | *.dot diff=astextplain 16 | *.DOT diff=astextplain 17 | *.pdf diff=astextplain 18 | *.PDF diff=astextplain 19 | *.rtf diff=astextplain 20 | *.RTF diff=astextplain 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | #Visual Studio LightSwitch 212 | _Pvt_Extensions/ 213 | GeneratedArtifacts/ 214 | ServiceConfiguration.cscfg 215 | ModelManifest.xml 216 | generated.parameters.xml 217 | ## TODO: Comment the next line if you want version controls the generated client .xap file 218 | *.Client.xap 219 | 220 | # Backup & report files from converting an old project file 221 | # to a newer Visual Studio version. Backup files are not needed, 222 | # because we have git ;-) 223 | _UpgradeReport_Files/ 224 | Backup*/ 225 | UpgradeLog*.XML 226 | UpgradeLog*.htm 227 | 228 | # SQL Server files 229 | *.mdf 230 | *.ldf 231 | 232 | # Business Intelligence projects 233 | *.rdl.data 234 | *.bim.layout 235 | *.bim_*.settings 236 | 237 | # Microsoft Fakes 238 | FakesAssemblies/ 239 | 240 | # GhostDoc plugin setting file 241 | *.GhostDoc.xml 242 | 243 | # Node.js Tools for Visual Studio 244 | .ntvs_analysis.dat 245 | node_modules/ 246 | 247 | # Typescript v1 declaration files 248 | typings/ 249 | 250 | # Visual Studio 6 build log 251 | *.plg 252 | 253 | # Visual Studio 6 workspace options file 254 | *.opt 255 | 256 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 257 | *.vbw 258 | 259 | # Visual Studio LightSwitch build output 260 | **/*.HTMLClient/GeneratedArtifacts 261 | **/*.DesktopClient/GeneratedArtifacts 262 | **/*.DesktopClient/ModelManifest.xml 263 | **/*.Server/GeneratedArtifacts 264 | **/*.Server/ModelManifest.xml 265 | _Pvt_Extensions 266 | 267 | # Paket dependency manager 268 | .paket/paket.exe 269 | paket-files/ 270 | 271 | # FAKE - F# Make 272 | .fake/ 273 | 274 | # JetBrains Rider 275 | .idea/ 276 | *.sln.iml 277 | 278 | # CodeRush 279 | .cr/ 280 | 281 | # Python Tools for Visual Studio (PTVS) 282 | __pycache__/ 283 | *.pyc 284 | 285 | # Mac crap 286 | .DS_Store 287 | 288 | # VisualStudioCode 289 | .vscode 290 | 291 | # Cake - Uncomment if you are using it 292 | # tools/** 293 | # !tools/packages.config 294 | 295 | /ModelCreate 296 | /GWebApi/photoImage 297 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MHttpServer 2 | ## 使用方法: 3 | MHttpServer.Listen listen = new MHttpServer.Listen(8088); 4 | ContainerBuilder builder = new ContainerBuilder(); 5 | builder.RegisterType().PropertiesAutowired(); 6 | listen.InitController(builder); 7 | listen.start(); 8 | -------------------------------------------------------------------------------- /src/HttpServerTest/Controller/TestController.cs: -------------------------------------------------------------------------------- 1 | using HttpServerTest; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace HttpServerTest 8 | { 9 | public class TestController : MHttpServer.Controller 10 | { 11 | TestService testService; 12 | public TestController(TestService _testService) 13 | { 14 | testService = _testService; 15 | } 16 | public void Test(string res) 17 | { 18 | testService.DoAction(res); 19 | } 20 | public string TestDo(string res) 21 | { 22 | TestEvent.OnEvent(res); 23 | return res; 24 | } 25 | [MException] 26 | public void TestDoExceptin(string res) 27 | { 28 | testService.DoException(res); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/HttpServerTest/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace HttpServerTest 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows 窗体设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要修改 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.richTextBox1 = new System.Windows.Forms.RichTextBox(); 32 | this.SuspendLayout(); 33 | // 34 | // richTextBox1 35 | // 36 | this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill; 37 | this.richTextBox1.Location = new System.Drawing.Point(0, 0); 38 | this.richTextBox1.Name = "richTextBox1"; 39 | this.richTextBox1.Size = new System.Drawing.Size(800, 450); 40 | this.richTextBox1.TabIndex = 0; 41 | this.richTextBox1.Text = ""; 42 | // 43 | // Form1 44 | // 45 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 46 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 47 | this.ClientSize = new System.Drawing.Size(800, 450); 48 | this.Controls.Add(this.richTextBox1); 49 | this.Name = "Form1"; 50 | this.Text = "Form1"; 51 | this.Load += new System.EventHandler(this.Form1_Load); 52 | this.ResumeLayout(false); 53 | 54 | } 55 | 56 | #endregion 57 | 58 | private System.Windows.Forms.RichTextBox richTextBox1; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/HttpServerTest/Form1.cs: -------------------------------------------------------------------------------- 1 | using Autofac; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.Data; 6 | using System.Drawing; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Windows.Forms; 10 | 11 | namespace HttpServerTest 12 | { 13 | public partial class Form1 : Form 14 | { 15 | public Form1() 16 | { 17 | InitializeComponent(); 18 | } 19 | 20 | private void Form1_Load(object sender, EventArgs e) 21 | { 22 | TestEvent.informHandle += TestEvent_informHandle; 23 | MHttpServer.Listen listen = new MHttpServer.Listen(8088); 24 | ContainerBuilder builder = new ContainerBuilder(); 25 | builder.RegisterType().PropertiesAutowired(); 26 | listen.InitController(builder); 27 | listen.start(); 28 | } 29 | 30 | private void TestEvent_informHandle(object sender) 31 | { 32 | this.richTextBox1.BeginInvoke(new Action(() => 33 | { 34 | this.richTextBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + ":" + sender.ToString() + Environment.NewLine); 35 | })); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/HttpServerTest/Form1.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /src/HttpServerTest/HttpServerTest.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {DB77AC22-952C-4F04-94F1-3B4BA96379B6} 8 | WinExe 9 | HttpServerTest 10 | HttpServerTest 11 | v4.0 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | ..\CommonM.Http\bin\Debug\Autofac.dll 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | Form 53 | 54 | 55 | Form1.cs 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | Form1.cs 64 | 65 | 66 | ResXFileCodeGenerator 67 | Resources.Designer.cs 68 | Designer 69 | 70 | 71 | True 72 | Resources.resx 73 | 74 | 75 | SettingsSingleFileGenerator 76 | Settings.Designer.cs 77 | 78 | 79 | True 80 | Settings.settings 81 | True 82 | 83 | 84 | 85 | 86 | {90a0f4b8-57b2-46bc-a00f-03e46a7d3855} 87 | MHttpServer 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /src/HttpServerTest/MException.cs: -------------------------------------------------------------------------------- 1 | using MHttpServer; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using WebSocketSharp; 7 | using WebSocketSharp.Server; 8 | 9 | namespace HttpServerTest 10 | { 11 | public class MExceptionAttribute : Attribute, IExceptionFilter 12 | { 13 | public void OnException(Exception ex, HttpRequestEventArgs e) 14 | { 15 | byte[] buffer = Encoding.UTF8.GetBytes("emmmmmmmmm" + ex.Message + ex.StackTrace); 16 | e.Response.StatusCode = 500; 17 | e.Response.ContentType = "application/text; charset =utf-8"; 18 | e.Response.WriteContent(buffer); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/HttpServerTest/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Windows.Forms; 5 | 6 | namespace HttpServerTest 7 | { 8 | static class Program 9 | { 10 | /// 11 | /// 应用程序的主入口点。 12 | /// 13 | [STAThread] 14 | static void Main() 15 | { 16 | Application.EnableVisualStyles(); 17 | Application.SetCompatibleTextRenderingDefault(false); 18 | Application.Run(new Form1()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/HttpServerTest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("HttpServerTest")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("HttpServerTest")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("db77ac22-952c-4f04-94f1-3b4ba96379b6")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/HttpServerTest/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本: 4.0.30319.42000 5 | // 6 | // 对此文件的更改可能导致不正确的行为,如果 7 | // 重新生成代码,则所做更改将丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace HttpServerTest.Properties 12 | { 13 | 14 | 15 | /// 16 | /// 强类型资源类,用于查找本地化字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// 返回此类使用的缓存 ResourceManager 实例。 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("HttpServerTest.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// 覆盖当前线程的 CurrentUICulture 属性 56 | /// 使用此强类型的资源类的资源查找。 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/HttpServerTest/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /src/HttpServerTest/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace HttpServerTest.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/HttpServerTest/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/HttpServerTest/Service/TestService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Windows.Forms; 6 | 7 | namespace HttpServerTest 8 | { 9 | public class TestService 10 | { 11 | public void DoAction(string res) 12 | { 13 | MessageBox.Show(res); 14 | } 15 | public void DoException(string res) 16 | { 17 | throw new Exception("hello exception"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/HttpServerTest/TestEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace HttpServerTest 7 | { 8 | public static class TestEvent 9 | { 10 | public delegate void InformHandle(object sender); 11 | public static event InformHandle informHandle; 12 | public static void OnEvent(object data) 13 | { 14 | informHandle?.Invoke(data); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/MHttpServer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27703.2042 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpServerTest", "HttpServerTest\HttpServerTest.csproj", "{DB77AC22-952C-4F04-94F1-3B4BA96379B6}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MHttpServer", "MHttpServer\MHttpServer.csproj", "{90A0F4B8-57B2-46BC-A00F-03E46A7D3855}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {DB77AC22-952C-4F04-94F1-3B4BA96379B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {DB77AC22-952C-4F04-94F1-3B4BA96379B6}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {DB77AC22-952C-4F04-94F1-3B4BA96379B6}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {DB77AC22-952C-4F04-94F1-3B4BA96379B6}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {90A0F4B8-57B2-46BC-A00F-03E46A7D3855}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {90A0F4B8-57B2-46BC-A00F-03E46A7D3855}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {90A0F4B8-57B2-46BC-A00F-03E46A7D3855}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {90A0F4B8-57B2-46BC-A00F-03E46A7D3855}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {0D92DA2C-FC03-4490-AF2C-3E72E5D56C56} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /src/MHttpServer/Controller.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace MHttpServer 7 | { 8 | public class Controller 9 | { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/MHttpServer/IExceptionFilter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using WebSocketSharp.Server; 6 | 7 | namespace MHttpServer 8 | { 9 | public interface IExceptionFilter 10 | { 11 | void OnException(Exception ex, HttpRequestEventArgs e); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/MHttpServer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("Controller")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Controller")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("90a0f4b8-57b2-46bc-a00f-03e46a7d3855")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | //通过使用 "*",如下所示: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/MHttpServer/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/CloseEventArgs.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * CloseEventArgs.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2016 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 | /// That event occurs when the WebSocket connection has been closed. 39 | /// 40 | /// 41 | /// If you would like to get the reason for the close, you should access 42 | /// the or property. 43 | /// 44 | /// 45 | public class CloseEventArgs : EventArgs 46 | { 47 | #region Private Fields 48 | 49 | private bool _clean; 50 | private PayloadData _payloadData; 51 | 52 | #endregion 53 | 54 | #region Internal Constructors 55 | 56 | internal CloseEventArgs () 57 | { 58 | _payloadData = PayloadData.Empty; 59 | } 60 | 61 | internal CloseEventArgs (ushort code) 62 | : this (code, null) 63 | { 64 | } 65 | 66 | internal CloseEventArgs (CloseStatusCode code) 67 | : this ((ushort) code, null) 68 | { 69 | } 70 | 71 | internal CloseEventArgs (PayloadData payloadData) 72 | { 73 | _payloadData = payloadData; 74 | } 75 | 76 | internal CloseEventArgs (ushort code, string reason) 77 | { 78 | _payloadData = new PayloadData (code, reason); 79 | } 80 | 81 | internal CloseEventArgs (CloseStatusCode code, string reason) 82 | : this ((ushort) code, reason) 83 | { 84 | } 85 | 86 | #endregion 87 | 88 | #region Internal Properties 89 | 90 | internal PayloadData PayloadData { 91 | get { 92 | return _payloadData; 93 | } 94 | } 95 | 96 | #endregion 97 | 98 | #region Public Properties 99 | 100 | /// 101 | /// Gets the status code for the close. 102 | /// 103 | /// 104 | /// A that represents the status code for the close if any. 105 | /// 106 | public ushort Code { 107 | get { 108 | return _payloadData.Code; 109 | } 110 | } 111 | 112 | /// 113 | /// Gets the reason for the close. 114 | /// 115 | /// 116 | /// A that represents the reason for the close if any. 117 | /// 118 | public string Reason { 119 | get { 120 | return _payloadData.Reason ?? String.Empty; 121 | } 122 | } 123 | 124 | /// 125 | /// Gets a value indicating whether the connection has been closed cleanly. 126 | /// 127 | /// 128 | /// true if the connection has been closed cleanly; otherwise, false. 129 | /// 130 | public bool WasClean { 131 | get { 132 | return _clean; 133 | } 134 | 135 | internal set { 136 | _clean = value; 137 | } 138 | } 139 | 140 | #endregion 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/CloseStatusCode.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * CloseStatusCode.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2016 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 | /// 40 | /// Section 7.4 of RFC 6455. 41 | /// 42 | /// 43 | /// "Reserved value" cannot be sent as a status code in 44 | /// closing handshake by an endpoint. 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 55 | /// going away. 56 | /// 57 | Away = 1001, 58 | /// 59 | /// Equivalent to close status 1002. Indicates that an endpoint is 60 | /// terminating the connection due to a protocol error. 61 | /// 62 | ProtocolError = 1002, 63 | /// 64 | /// Equivalent to close status 1003. Indicates that an endpoint is 65 | /// terminating the connection because it has received a type of 66 | /// data that it cannot accept. 67 | /// 68 | UnsupportedData = 1003, 69 | /// 70 | /// Equivalent to close status 1004. Still undefined. A Reserved value. 71 | /// 72 | Undefined = 1004, 73 | /// 74 | /// Equivalent to close status 1005. Indicates that no status code was 75 | /// actually present. A Reserved value. 76 | /// 77 | NoStatus = 1005, 78 | /// 79 | /// Equivalent to close status 1006. Indicates that the connection was 80 | /// closed abnormally. A Reserved value. 81 | /// 82 | Abnormal = 1006, 83 | /// 84 | /// Equivalent to close status 1007. Indicates that an endpoint is 85 | /// terminating the connection because it has received a message that 86 | /// contains data that is not consistent with the type of the message. 87 | /// 88 | InvalidData = 1007, 89 | /// 90 | /// Equivalent to close status 1008. Indicates that an endpoint is 91 | /// terminating the connection because it has received a message that 92 | /// violates its policy. 93 | /// 94 | PolicyViolation = 1008, 95 | /// 96 | /// Equivalent to close status 1009. Indicates that an endpoint is 97 | /// terminating the connection because it has received a message that 98 | /// is too big to process. 99 | /// 100 | TooBig = 1009, 101 | /// 102 | /// Equivalent to close status 1010. Indicates that a client is 103 | /// terminating the connection because it has expected the server to 104 | /// negotiate one or more extension, but the server did not return 105 | /// them in the handshake response. 106 | /// 107 | MandatoryExtension = 1010, 108 | /// 109 | /// Equivalent to close status 1011. Indicates that a server is 110 | /// terminating the connection because it has encountered an unexpected 111 | /// condition that prevented it from fulfilling the request. 112 | /// 113 | ServerError = 1011, 114 | /// 115 | /// Equivalent to close status 1015. Indicates that the connection was 116 | /// closed due to a failure to perform a TLS handshake. A Reserved value. 117 | /// 118 | TlsHandshakeFailure = 1015 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/CompressionMethod.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * CompressionMethod.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2017 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 method for compression. 35 | /// 36 | /// 37 | /// The methods are defined in 38 | /// 39 | /// Compression Extensions for WebSocket. 40 | /// 41 | public enum CompressionMethod : byte 42 | { 43 | /// 44 | /// Specifies no compression. 45 | /// 46 | None, 47 | /// 48 | /// Specifies DEFLATE. 49 | /// 50 | Deflate 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/ErrorEventArgs.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * ErrorEventArgs.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2016 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 | /// That event occurs when the gets an error. 46 | /// 47 | /// 48 | /// If you would like to get the error message, you should access 49 | /// the property. 50 | /// 51 | /// 52 | /// And if the error is due to an exception, you can get it by accessing 53 | /// the property. 54 | /// 55 | /// 56 | public class ErrorEventArgs : EventArgs 57 | { 58 | #region Private Fields 59 | 60 | private Exception _exception; 61 | private string _message; 62 | 63 | #endregion 64 | 65 | #region Internal Constructors 66 | 67 | internal ErrorEventArgs (string message) 68 | : this (message, null) 69 | { 70 | } 71 | 72 | internal ErrorEventArgs (string message, Exception exception) 73 | { 74 | _message = message; 75 | _exception = exception; 76 | } 77 | 78 | #endregion 79 | 80 | #region Public Properties 81 | 82 | /// 83 | /// Gets the exception that caused the error. 84 | /// 85 | /// 86 | /// An instance that represents the cause of 87 | /// the error if it is due to an exception; otherwise, . 88 | /// 89 | public Exception Exception { 90 | get { 91 | return _exception; 92 | } 93 | } 94 | 95 | /// 96 | /// Gets the error message. 97 | /// 98 | /// 99 | /// A that represents the error message. 100 | /// 101 | public string Message { 102 | get { 103 | return _message; 104 | } 105 | } 106 | 107 | #endregion 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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.LongLength == 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.", "length"); 109 | 110 | if (len < 0) 111 | throw new ArgumentOutOfRangeException ("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.Close (); 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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 CookieCollection _cookies; 49 | private string _method; 50 | private string _uri; 51 | 52 | #endregion 53 | 54 | #region Private Constructors 55 | 56 | private HttpRequest (string method, string uri, Version version, NameValueCollection headers) 57 | : base (version, headers) 58 | { 59 | _method = method; 60 | _uri = uri; 61 | } 62 | 63 | #endregion 64 | 65 | #region Internal Constructors 66 | 67 | internal HttpRequest (string method, string uri) 68 | : this (method, uri, HttpVersion.Version11, new NameValueCollection ()) 69 | { 70 | Headers["User-Agent"] = "websocket-sharp/1.0"; 71 | } 72 | 73 | #endregion 74 | 75 | #region Public Properties 76 | 77 | public AuthenticationResponse AuthenticationResponse { 78 | get { 79 | var res = Headers["Authorization"]; 80 | return res != null && res.Length > 0 81 | ? AuthenticationResponse.Parse (res) 82 | : null; 83 | } 84 | } 85 | 86 | public CookieCollection Cookies { 87 | get { 88 | if (_cookies == null) 89 | _cookies = Headers.GetCookies (false); 90 | 91 | return _cookies; 92 | } 93 | } 94 | 95 | public string HttpMethod { 96 | get { 97 | return _method; 98 | } 99 | } 100 | 101 | public bool IsWebSocketRequest { 102 | get { 103 | return _method == "GET" 104 | && ProtocolVersion > HttpVersion.Version10 105 | && Headers.Upgrades ("websocket"); 106 | } 107 | } 108 | 109 | public string RequestUri { 110 | get { 111 | return _uri; 112 | } 113 | } 114 | 115 | #endregion 116 | 117 | #region Internal Methods 118 | 119 | internal static HttpRequest CreateConnectRequest (Uri uri) 120 | { 121 | var host = uri.DnsSafeHost; 122 | var port = uri.Port; 123 | var authority = String.Format ("{0}:{1}", host, port); 124 | var req = new HttpRequest ("CONNECT", authority); 125 | req.Headers["Host"] = port == 80 ? host : authority; 126 | 127 | return req; 128 | } 129 | 130 | internal static HttpRequest CreateWebSocketRequest (Uri uri) 131 | { 132 | var req = new HttpRequest ("GET", uri.PathAndQuery); 133 | var headers = req.Headers; 134 | 135 | // Only includes a port number in the Host header value if it's non-default. 136 | // See: https://tools.ietf.org/html/rfc6455#page-17 137 | var port = uri.Port; 138 | var schm = uri.Scheme; 139 | headers["Host"] = (port == 80 && schm == "ws") || (port == 443 && schm == "wss") 140 | ? uri.DnsSafeHost 141 | : uri.Authority; 142 | 143 | headers["Upgrade"] = "websocket"; 144 | headers["Connection"] = "Upgrade"; 145 | 146 | return req; 147 | } 148 | 149 | internal HttpResponse GetResponse (Stream stream, int millisecondsTimeout) 150 | { 151 | var buff = ToByteArray (); 152 | stream.Write (buff, 0, buff.Length); 153 | 154 | return Read (stream, HttpResponse.Parse, millisecondsTimeout); 155 | } 156 | 157 | internal static HttpRequest Parse (string[] headerParts) 158 | { 159 | var requestLine = headerParts[0].Split (new[] { ' ' }, 3); 160 | if (requestLine.Length != 3) 161 | throw new ArgumentException ("Invalid request line: " + headerParts[0]); 162 | 163 | var headers = new WebHeaderCollection (); 164 | for (int i = 1; i < headerParts.Length; i++) 165 | headers.InternalSet (headerParts[i], false); 166 | 167 | return new HttpRequest ( 168 | requestLine[0], requestLine[1], new Version (requestLine[2].Substring (5)), headers); 169 | } 170 | 171 | internal static HttpRequest 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 buff = new StringBuilder (64); 186 | foreach (var cookie in cookies.Sorted) 187 | if (!cookie.Expired) 188 | buff.AppendFormat ("{0}; ", cookie.ToString ()); 189 | 190 | var len = buff.Length; 191 | if (len > 2) { 192 | buff.Length = len - 2; 193 | Headers["Cookie"] = buff.ToString (); 194 | } 195 | } 196 | 197 | public override string ToString () 198 | { 199 | var output = new StringBuilder (64); 200 | output.AppendFormat ("{0} {1} HTTP/{2}{3}", _method, _uri, ProtocolVersion, CrLf); 201 | 202 | var headers = Headers; 203 | foreach (var key in headers.AllKeys) 204 | output.AppendFormat ("{0}: {1}{2}", key, headers[key], CrLf); 205 | 206 | output.Append (CrLf); 207 | 208 | var entity = EntityBody; 209 | if (entity.Length > 0) 210 | output.Append (entity); 211 | 212 | return output.ToString (); 213 | } 214 | 215 | #endregion 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | var comparison = StringComparison.OrdinalIgnoreCase; 83 | return Headers.Contains ("Connection", "close", comparison); 84 | } 85 | } 86 | 87 | public bool IsProxyAuthenticationRequired { 88 | get { 89 | return _code == "407"; 90 | } 91 | } 92 | 93 | public bool IsRedirect { 94 | get { 95 | return _code == "301" || _code == "302"; 96 | } 97 | } 98 | 99 | public bool IsUnauthorized { 100 | get { 101 | return _code == "401"; 102 | } 103 | } 104 | 105 | public bool IsWebSocketResponse { 106 | get { 107 | return ProtocolVersion > HttpVersion.Version10 108 | && _code == "101" 109 | && Headers.Upgrades ("websocket"); 110 | } 111 | } 112 | 113 | public string Reason { 114 | get { 115 | return _reason; 116 | } 117 | } 118 | 119 | public string StatusCode { 120 | get { 121 | return _code; 122 | } 123 | } 124 | 125 | #endregion 126 | 127 | #region Internal Methods 128 | 129 | internal static HttpResponse CreateCloseResponse (HttpStatusCode code) 130 | { 131 | var res = new HttpResponse (code); 132 | res.Headers["Connection"] = "close"; 133 | 134 | return res; 135 | } 136 | 137 | internal static HttpResponse CreateUnauthorizedResponse (string challenge) 138 | { 139 | var res = new HttpResponse (HttpStatusCode.Unauthorized); 140 | res.Headers["WWW-Authenticate"] = challenge; 141 | 142 | return res; 143 | } 144 | 145 | internal static HttpResponse CreateWebSocketResponse () 146 | { 147 | var res = new HttpResponse (HttpStatusCode.SwitchingProtocols); 148 | 149 | var headers = res.Headers; 150 | headers["Upgrade"] = "websocket"; 151 | headers["Connection"] = "Upgrade"; 152 | 153 | return res; 154 | } 155 | 156 | internal static HttpResponse Parse (string[] headerParts) 157 | { 158 | var statusLine = headerParts[0].Split (new[] { ' ' }, 3); 159 | if (statusLine.Length != 3) 160 | throw new ArgumentException ("Invalid status line: " + headerParts[0]); 161 | 162 | var headers = new WebHeaderCollection (); 163 | for (int i = 1; i < headerParts.Length; i++) 164 | headers.InternalSet (headerParts[i], true); 165 | 166 | return new HttpResponse ( 167 | statusLine[1], statusLine[2], new Version (statusLine[0].Substring (5)), headers); 168 | } 169 | 170 | internal static HttpResponse Read (Stream stream, int millisecondsTimeout) 171 | { 172 | return Read (stream, Parse, millisecondsTimeout); 173 | } 174 | 175 | #endregion 176 | 177 | #region Public Methods 178 | 179 | public void SetCookies (CookieCollection cookies) 180 | { 181 | if (cookies == null || cookies.Count == 0) 182 | return; 183 | 184 | var headers = Headers; 185 | foreach (var cookie in cookies.Sorted) 186 | headers.Add ("Set-Cookie", cookie.ToResponseString ()); 187 | } 188 | 189 | public override string ToString () 190 | { 191 | var output = new StringBuilder (64); 192 | output.AppendFormat ("HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf); 193 | 194 | var headers = Headers; 195 | foreach (var key in headers.AllKeys) 196 | output.AppendFormat ("{0}: {1}{2}", key, headers[key], CrLf); 197 | 198 | output.Append (CrLf); 199 | 200 | var entity = EntityBody; 201 | if (entity.Length > 0) 202 | output.Append (entity); 203 | 204 | return output.ToString (); 205 | } 206 | 207 | #endregion 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/MessageEventArgs.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * MessageEventArgs.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2016 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 | /// That event occurs when the receives 39 | /// a message or a ping if the 40 | /// property is set to true. 41 | /// 42 | /// 43 | /// If you would like to get the message data, you should access 44 | /// the or 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.LongLength > PayloadData.MaxLength) 69 | throw new WebSocketException (CloseStatusCode.TooBig); 70 | 71 | _opcode = opcode; 72 | _rawData = rawData; 73 | } 74 | 75 | #endregion 76 | 77 | #region Internal Properties 78 | 79 | /// 80 | /// Gets the opcode for the message. 81 | /// 82 | /// 83 | /// , , 84 | /// or . 85 | /// 86 | internal Opcode Opcode { 87 | get { 88 | return _opcode; 89 | } 90 | } 91 | 92 | #endregion 93 | 94 | #region Public Properties 95 | 96 | /// 97 | /// Gets the message data as a . 98 | /// 99 | /// 100 | /// A that represents the message data if its type is 101 | /// text or ping and if decoding it to a string has successfully done; 102 | /// otherwise, . 103 | /// 104 | public string Data { 105 | get { 106 | setData (); 107 | return _data; 108 | } 109 | } 110 | 111 | /// 112 | /// Gets a value indicating whether the message type is binary. 113 | /// 114 | /// 115 | /// true if the message type is binary; otherwise, false. 116 | /// 117 | public bool IsBinary { 118 | get { 119 | return _opcode == Opcode.Binary; 120 | } 121 | } 122 | 123 | /// 124 | /// Gets a value indicating whether the message type is ping. 125 | /// 126 | /// 127 | /// true if the message type is ping; otherwise, false. 128 | /// 129 | public bool IsPing { 130 | get { 131 | return _opcode == Opcode.Ping; 132 | } 133 | } 134 | 135 | /// 136 | /// Gets a value indicating whether the message type is text. 137 | /// 138 | /// 139 | /// true if the message type is text; otherwise, false. 140 | /// 141 | public bool IsText { 142 | get { 143 | return _opcode == Opcode.Text; 144 | } 145 | } 146 | 147 | /// 148 | /// Gets the message data as an array of . 149 | /// 150 | /// 151 | /// An array of that represents the message data. 152 | /// 153 | public byte[] RawData { 154 | get { 155 | setData (); 156 | return _rawData; 157 | } 158 | } 159 | 160 | #endregion 161 | 162 | #region Private Methods 163 | 164 | private void setData () 165 | { 166 | if (_dataSet) 167 | return; 168 | 169 | if (_opcode == Opcode.Binary) { 170 | _dataSet = true; 171 | return; 172 | } 173 | 174 | _data = _rawData.UTF8Decode (); 175 | _dataSet = true; 176 | } 177 | 178 | #endregion 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 ("buffer"); 125 | 126 | if (offset < 0) 127 | throw new ArgumentOutOfRangeException ("offset", "A negative value."); 128 | 129 | if (count < 0) 130 | throw new ArgumentOutOfRangeException ("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 ("asyncResult"); 189 | 190 | var ares = asyncResult as HttpStreamAsyncResult; 191 | if (ares == null) 192 | throw new ArgumentException ("A wrong IAsyncResult.", "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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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, ISerializable 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 | /// Initializes a new instance of the class from 69 | /// the specified and . 70 | /// 71 | /// 72 | /// A that contains the serialized object data. 73 | /// 74 | /// 75 | /// A that specifies the source for the deserialization. 76 | /// 77 | protected CookieException ( 78 | SerializationInfo serializationInfo, StreamingContext streamingContext) 79 | : base (serializationInfo, streamingContext) 80 | { 81 | } 82 | 83 | #endregion 84 | 85 | #region Public Constructors 86 | 87 | /// 88 | /// Initializes a new instance of the class. 89 | /// 90 | public CookieException () 91 | : base () 92 | { 93 | } 94 | 95 | #endregion 96 | 97 | #region Public Methods 98 | 99 | /// 100 | /// Populates the specified with the data needed to serialize 101 | /// the current . 102 | /// 103 | /// 104 | /// A that holds the serialized object data. 105 | /// 106 | /// 107 | /// A that specifies the destination for the serialization. 108 | /// 109 | [SecurityPermission ( 110 | SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] 111 | public override void GetObjectData ( 112 | SerializationInfo serializationInfo, StreamingContext streamingContext) 113 | { 114 | base.GetObjectData (serializationInfo, streamingContext); 115 | } 116 | 117 | #endregion 118 | 119 | #region Explicit Interface Implementation 120 | 121 | /// 122 | /// Populates the specified with the data needed to serialize 123 | /// the current . 124 | /// 125 | /// 126 | /// A that holds the serialized object data. 127 | /// 128 | /// 129 | /// A that specifies the destination for the serialization. 130 | /// 131 | [SecurityPermission ( 132 | SecurityAction.LinkDemand, 133 | Flags = SecurityPermissionFlag.SerializationFormatter, 134 | SerializationFormatter = true)] 135 | void ISerializable.GetObjectData ( 136 | SerializationInfo serializationInfo, StreamingContext streamingContext) 137 | { 138 | base.GetObjectData (serializationInfo, streamingContext); 139 | } 140 | 141 | #endregion 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 == null) 87 | throw new HttpListenerException (87, "Includes an invalid host."); 88 | 89 | if (!addr.IsLocal ()) 90 | throw new HttpListenerException (87, "Includes an invalid host."); 91 | 92 | int port; 93 | if (!Int32.TryParse (pref.Port, out port)) 94 | throw new HttpListenerException (87, "Includes an invalid port."); 95 | 96 | if (!port.IsPortNumber ()) 97 | throw new HttpListenerException (87, "Includes an invalid port."); 98 | 99 | var path = pref.Path; 100 | if (path.IndexOf ('%') != -1) 101 | throw new HttpListenerException (87, "Includes an invalid path."); 102 | 103 | if (path.IndexOf ("//", StringComparison.Ordinal) != -1) 104 | throw new HttpListenerException (87, "Includes an invalid path."); 105 | 106 | var endpoint = new IPEndPoint (addr, port); 107 | 108 | EndPointListener lsnr; 109 | if (_endpoints.TryGetValue (endpoint, out lsnr)) { 110 | if (lsnr.IsSecure ^ pref.IsSecure) 111 | throw new HttpListenerException (87, "Includes an invalid scheme."); 112 | } 113 | else { 114 | lsnr = 115 | new EndPointListener ( 116 | endpoint, 117 | pref.IsSecure, 118 | listener.CertificateFolderPath, 119 | listener.SslConfiguration, 120 | listener.ReuseAddress 121 | ); 122 | 123 | _endpoints.Add (endpoint, lsnr); 124 | } 125 | 126 | lsnr.AddPrefix (pref, listener); 127 | } 128 | 129 | private static IPAddress convertToIPAddress (string hostname) 130 | { 131 | if (hostname == "*") 132 | return IPAddress.Any; 133 | 134 | if (hostname == "+") 135 | return IPAddress.Any; 136 | 137 | return hostname.ToIPAddress (); 138 | } 139 | 140 | private static void removePrefix (string uriPrefix, HttpListener listener) 141 | { 142 | var pref = new HttpListenerPrefix (uriPrefix); 143 | 144 | var addr = convertToIPAddress (pref.Host); 145 | if (addr == null) 146 | return; 147 | 148 | if (!addr.IsLocal ()) 149 | return; 150 | 151 | int port; 152 | if (!Int32.TryParse (pref.Port, out port)) 153 | return; 154 | 155 | if (!port.IsPortNumber ()) 156 | return; 157 | 158 | var path = pref.Path; 159 | if (path.IndexOf ('%') != -1) 160 | return; 161 | 162 | if (path.IndexOf ("//", StringComparison.Ordinal) != -1) 163 | return; 164 | 165 | var endpoint = new IPEndPoint (addr, port); 166 | 167 | EndPointListener lsnr; 168 | if (!_endpoints.TryGetValue (endpoint, out lsnr)) 169 | return; 170 | 171 | if (lsnr.IsSecure ^ pref.IsSecure) 172 | return; 173 | 174 | lsnr.RemovePrefix (pref, listener); 175 | } 176 | 177 | #endregion 178 | 179 | #region Internal Methods 180 | 181 | internal static bool RemoveEndPoint (IPEndPoint endpoint) 182 | { 183 | lock (((ICollection) _endpoints).SyncRoot) { 184 | EndPointListener lsnr; 185 | if (!_endpoints.TryGetValue (endpoint, out lsnr)) 186 | return false; 187 | 188 | _endpoints.Remove (endpoint); 189 | lsnr.Close (); 190 | 191 | return true; 192 | } 193 | } 194 | 195 | #endregion 196 | 197 | #region Public Methods 198 | 199 | public static void AddListener (HttpListener listener) 200 | { 201 | var added = new List (); 202 | lock (((ICollection) _endpoints).SyncRoot) { 203 | try { 204 | foreach (var pref in listener.Prefixes) { 205 | addPrefix (pref, listener); 206 | added.Add (pref); 207 | } 208 | } 209 | catch { 210 | foreach (var pref in added) 211 | removePrefix (pref, listener); 212 | 213 | throw; 214 | } 215 | } 216 | } 217 | 218 | public static void AddPrefix (string uriPrefix, HttpListener listener) 219 | { 220 | lock (((ICollection) _endpoints).SyncRoot) 221 | addPrefix (uriPrefix, listener); 222 | } 223 | 224 | public static void RemoveListener (HttpListener listener) 225 | { 226 | lock (((ICollection) _endpoints).SyncRoot) { 227 | foreach (var pref in listener.Prefixes) 228 | removePrefix (pref, listener); 229 | } 230 | } 231 | 232 | public static void RemovePrefix (string uriPrefix, HttpListener listener) 233 | { 234 | lock (((ICollection) _endpoints).SyncRoot) 235 | removePrefix (uriPrefix, listener); 236 | } 237 | 238 | #endregion 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Net/HttpBasicIdentity.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpBasicIdentity.cs 4 | * 5 | * This code is derived from HttpListenerBasicIdentity.cs (System.Net) of 6 | * Mono (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-2017 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 username and password from an HTTP Basic authentication attempt. 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 a basic authentication attempt. 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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Net/HttpDigestIdentity.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * HttpDigestIdentity.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2014-2017 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 username and other parameters from 37 | /// an HTTP Digest authentication attempt. 38 | /// 39 | public class HttpDigestIdentity : GenericIdentity 40 | { 41 | #region Private Fields 42 | 43 | private NameValueCollection _parameters; 44 | 45 | #endregion 46 | 47 | #region Internal Constructors 48 | 49 | internal HttpDigestIdentity (NameValueCollection parameters) 50 | : base (parameters["username"], "Digest") 51 | { 52 | _parameters = parameters; 53 | } 54 | 55 | #endregion 56 | 57 | #region Public Properties 58 | 59 | /// 60 | /// Gets the algorithm parameter from a digest authentication attempt. 61 | /// 62 | /// 63 | /// A that represents the algorithm parameter. 64 | /// 65 | public string Algorithm { 66 | get { 67 | return _parameters["algorithm"]; 68 | } 69 | } 70 | 71 | /// 72 | /// Gets the cnonce parameter from a digest authentication attempt. 73 | /// 74 | /// 75 | /// A that represents the cnonce parameter. 76 | /// 77 | public string Cnonce { 78 | get { 79 | return _parameters["cnonce"]; 80 | } 81 | } 82 | 83 | /// 84 | /// Gets the nc parameter from a digest authentication attempt. 85 | /// 86 | /// 87 | /// A that represents the nc parameter. 88 | /// 89 | public string Nc { 90 | get { 91 | return _parameters["nc"]; 92 | } 93 | } 94 | 95 | /// 96 | /// Gets the nonce parameter from a digest authentication attempt. 97 | /// 98 | /// 99 | /// A that represents the nonce parameter. 100 | /// 101 | public string Nonce { 102 | get { 103 | return _parameters["nonce"]; 104 | } 105 | } 106 | 107 | /// 108 | /// Gets the opaque parameter from a digest authentication attempt. 109 | /// 110 | /// 111 | /// A that represents the opaque parameter. 112 | /// 113 | public string Opaque { 114 | get { 115 | return _parameters["opaque"]; 116 | } 117 | } 118 | 119 | /// 120 | /// Gets the qop parameter from a digest authentication attempt. 121 | /// 122 | /// 123 | /// A that represents the qop parameter. 124 | /// 125 | public string Qop { 126 | get { 127 | return _parameters["qop"]; 128 | } 129 | } 130 | 131 | /// 132 | /// Gets the realm parameter from a digest authentication attempt. 133 | /// 134 | /// 135 | /// A that represents the realm parameter. 136 | /// 137 | public string Realm { 138 | get { 139 | return _parameters["realm"]; 140 | } 141 | } 142 | 143 | /// 144 | /// Gets the response parameter from a digest authentication attempt. 145 | /// 146 | /// 147 | /// A that represents the response parameter. 148 | /// 149 | public string Response { 150 | get { 151 | return _parameters["response"]; 152 | } 153 | } 154 | 155 | /// 156 | /// Gets the uri parameter from a digest authentication attempt. 157 | /// 158 | /// 159 | /// A that represents the uri parameter. 160 | /// 161 | public string Uri { 162 | get { 163 | return _parameters["uri"]; 164 | } 165 | } 166 | 167 | #endregion 168 | 169 | #region Internal Methods 170 | 171 | internal bool IsValid ( 172 | string password, string realm, string method, string entity 173 | ) 174 | { 175 | var copied = new NameValueCollection (_parameters); 176 | copied["password"] = password; 177 | copied["realm"] = realm; 178 | copied["method"] = method; 179 | copied["entity"] = entity; 180 | 181 | var expected = AuthenticationResponse.CreateRequestDigest (copied); 182 | return _parameters["response"] == expected; 183 | } 184 | 185 | #endregion 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | protected HttpListenerException ( 66 | SerializationInfo serializationInfo, StreamingContext streamingContext) 67 | : base (serializationInfo, streamingContext) 68 | { 69 | } 70 | 71 | #endregion 72 | 73 | #region Public Constructors 74 | 75 | /// 76 | /// Initializes a new instance of the class. 77 | /// 78 | public HttpListenerException () 79 | { 80 | } 81 | 82 | /// 83 | /// Initializes a new instance of the class 84 | /// with the specified . 85 | /// 86 | /// 87 | /// An that identifies the error. 88 | /// 89 | public HttpListenerException (int errorCode) 90 | : base (errorCode) 91 | { 92 | } 93 | 94 | /// 95 | /// Initializes a new instance of the class 96 | /// with the specified and . 97 | /// 98 | /// 99 | /// An that identifies the error. 100 | /// 101 | /// 102 | /// A that describes the error. 103 | /// 104 | public HttpListenerException (int errorCode, string message) 105 | : base (errorCode, message) 106 | { 107 | } 108 | 109 | #endregion 110 | 111 | #region Public Properties 112 | 113 | /// 114 | /// Gets the error code that identifies the error that occurred. 115 | /// 116 | /// 117 | /// An that identifies the error. 118 | /// 119 | public override int ErrorCode { 120 | get { 121 | return NativeErrorCode; 122 | } 123 | } 124 | 125 | #endregion 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Net/NetworkCredential.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * NetworkCredential.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2014-2017 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 password-based authentication. 35 | /// 36 | public class NetworkCredential 37 | { 38 | #region Private Fields 39 | 40 | private string _domain; 41 | private static readonly string[] _noRoles; 42 | private string _password; 43 | private string[] _roles; 44 | private string _username; 45 | 46 | #endregion 47 | 48 | #region Static Constructor 49 | 50 | static NetworkCredential () 51 | { 52 | _noRoles = new string[0]; 53 | } 54 | 55 | #endregion 56 | 57 | #region Public Constructors 58 | 59 | /// 60 | /// Initializes a new instance of the class with 61 | /// the specified and . 62 | /// 63 | /// 64 | /// A that represents the username associated with 65 | /// the credentials. 66 | /// 67 | /// 68 | /// A that represents the password for the username 69 | /// associated with the credentials. 70 | /// 71 | /// 72 | /// is . 73 | /// 74 | /// 75 | /// is empty. 76 | /// 77 | public NetworkCredential (string username, string password) 78 | : this (username, password, null, null) 79 | { 80 | } 81 | 82 | /// 83 | /// Initializes a new instance of the class with 84 | /// the specified , , 85 | /// and . 86 | /// 87 | /// 88 | /// A that represents the username associated with 89 | /// the credentials. 90 | /// 91 | /// 92 | /// A that represents the password for the username 93 | /// associated with the credentials. 94 | /// 95 | /// 96 | /// A that represents the domain associated with 97 | /// the credentials. 98 | /// 99 | /// 100 | /// An array of that represents the roles 101 | /// associated with the credentials if any. 102 | /// 103 | /// 104 | /// is . 105 | /// 106 | /// 107 | /// is empty. 108 | /// 109 | public NetworkCredential ( 110 | string username, string password, string domain, params string[] roles 111 | ) 112 | { 113 | if (username == null) 114 | throw new ArgumentNullException ("username"); 115 | 116 | if (username.Length == 0) 117 | throw new ArgumentException ("An empty string.", "username"); 118 | 119 | _username = username; 120 | _password = password; 121 | _domain = domain; 122 | _roles = roles; 123 | } 124 | 125 | #endregion 126 | 127 | #region Public Properties 128 | 129 | /// 130 | /// Gets the domain associated with the credentials. 131 | /// 132 | /// 133 | /// This property returns an empty string if the domain was 134 | /// initialized with . 135 | /// 136 | /// 137 | /// A that represents the domain name 138 | /// to which the username belongs. 139 | /// 140 | public string Domain { 141 | get { 142 | return _domain ?? String.Empty; 143 | } 144 | 145 | internal set { 146 | _domain = value; 147 | } 148 | } 149 | 150 | /// 151 | /// Gets the password for the username associated with the credentials. 152 | /// 153 | /// 154 | /// This property returns an empty string if the password was 155 | /// initialized with . 156 | /// 157 | /// 158 | /// A that represents the password. 159 | /// 160 | public string Password { 161 | get { 162 | return _password ?? String.Empty; 163 | } 164 | 165 | internal set { 166 | _password = value; 167 | } 168 | } 169 | 170 | /// 171 | /// Gets the roles associated with the credentials. 172 | /// 173 | /// 174 | /// This property returns an empty array if the roles were 175 | /// initialized with . 176 | /// 177 | /// 178 | /// An array of that represents the role names 179 | /// to which the username belongs. 180 | /// 181 | public string[] Roles { 182 | get { 183 | return _roles ?? _noRoles; 184 | } 185 | 186 | internal set { 187 | _roles = value; 188 | } 189 | } 190 | 191 | /// 192 | /// Gets the username associated with the credentials. 193 | /// 194 | /// 195 | /// A that represents the username. 196 | /// 197 | public string Username { 198 | get { 199 | return _username; 200 | } 201 | 202 | internal set { 203 | _username = value; 204 | } 205 | } 206 | 207 | #endregion 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Net/QueryStringCollection.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * QueryStringCollection.cs 4 | * 5 | * This code is derived from HttpUtility.cs (System.Net) 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) 2018 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 | #region Public Constructors 52 | 53 | public QueryStringCollection () 54 | { 55 | } 56 | 57 | public QueryStringCollection (int capacity) 58 | : base (capacity) 59 | { 60 | } 61 | 62 | #endregion 63 | 64 | #region Public Methods 65 | 66 | public static QueryStringCollection Parse (string query) 67 | { 68 | return Parse (query, Encoding.UTF8); 69 | } 70 | 71 | public static QueryStringCollection Parse (string query, Encoding encoding) 72 | { 73 | if (query == null) 74 | return new QueryStringCollection (1); 75 | 76 | var len = query.Length; 77 | if (len == 0) 78 | return new QueryStringCollection (1); 79 | 80 | if (query == "?") 81 | return new QueryStringCollection (1); 82 | 83 | if (query[0] == '?') 84 | query = query.Substring (1); 85 | 86 | if (encoding == null) 87 | encoding = Encoding.UTF8; 88 | 89 | var ret = new QueryStringCollection (); 90 | 91 | var components = query.Split ('&'); 92 | foreach (var component in components) { 93 | len = component.Length; 94 | if (len == 0) 95 | continue; 96 | 97 | if (component == "=") 98 | continue; 99 | 100 | var i = component.IndexOf ('='); 101 | if (i < 0) { 102 | ret.Add (null, HttpUtility.UrlDecode (component, encoding)); 103 | continue; 104 | } 105 | 106 | if (i == 0) { 107 | ret.Add ( 108 | null, HttpUtility.UrlDecode (component.Substring (1), encoding) 109 | ); 110 | 111 | continue; 112 | } 113 | 114 | var name = HttpUtility.UrlDecode (component.Substring (0, i), encoding); 115 | 116 | var start = i + 1; 117 | var val = start < len 118 | ? HttpUtility.UrlDecode ( 119 | component.Substring (start), encoding 120 | ) 121 | : String.Empty; 122 | 123 | ret.Add (name, val); 124 | } 125 | 126 | return ret; 127 | } 128 | 129 | public override string ToString () 130 | { 131 | if (Count == 0) 132 | return String.Empty; 133 | 134 | var buff = new StringBuilder (); 135 | 136 | foreach (var key in AllKeys) 137 | buff.AppendFormat ("{0}={1}&", key, this[key]); 138 | 139 | if (buff.Length > 0) 140 | buff.Length--; 141 | 142 | return buff.ToString (); 143 | } 144 | 145 | #endregion 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Opcode.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * Opcode.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2016 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 | /// 39 | /// Section 5.2 of RFC 6455. 40 | /// 41 | internal enum Opcode : byte 42 | { 43 | /// 44 | /// Equivalent to numeric value 0. Indicates continuation frame. 45 | /// 46 | Cont = 0x0, 47 | /// 48 | /// Equivalent to numeric value 1. Indicates text frame. 49 | /// 50 | Text = 0x1, 51 | /// 52 | /// Equivalent to numeric value 2. Indicates binary frame. 53 | /// 54 | Binary = 0x2, 55 | /// 56 | /// Equivalent to numeric value 8. Indicates connection close frame. 57 | /// 58 | Close = 0x8, 59 | /// 60 | /// Equivalent to numeric value 9. Indicates ping frame. 61 | /// 62 | Ping = 0x9, 63 | /// 64 | /// Equivalent to numeric value 10. Indicates pong frame. 65 | /// 66 | Pong = 0xa 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/PayloadData.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * PayloadData.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2016 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 ushort _code; 40 | private bool _codeSet; 41 | private byte[] _data; 42 | private long _extDataLength; 43 | private long _length; 44 | private string _reason; 45 | private bool _reasonSet; 46 | 47 | #endregion 48 | 49 | #region Public Fields 50 | 51 | /// 52 | /// Represents the empty payload data. 53 | /// 54 | public static readonly PayloadData Empty; 55 | 56 | /// 57 | /// Represents the allowable max length. 58 | /// 59 | /// 60 | /// 61 | /// A will occur if the payload data length is 62 | /// greater than the value of this field. 63 | /// 64 | /// 65 | /// If you would like to change the value, you must set it to a value between 66 | /// WebSocket.FragmentLength and Int64.MaxValue inclusive. 67 | /// 68 | /// 69 | public static readonly ulong MaxLength; 70 | 71 | #endregion 72 | 73 | #region Static Constructor 74 | 75 | static PayloadData () 76 | { 77 | Empty = new PayloadData (); 78 | MaxLength = Int64.MaxValue; 79 | } 80 | 81 | #endregion 82 | 83 | #region Internal Constructors 84 | 85 | internal PayloadData () 86 | { 87 | _code = 1005; 88 | _reason = String.Empty; 89 | 90 | _data = WebSocket.EmptyBytes; 91 | 92 | _codeSet = true; 93 | _reasonSet = true; 94 | } 95 | 96 | internal PayloadData (byte[] data) 97 | : this (data, data.LongLength) 98 | { 99 | } 100 | 101 | internal PayloadData (byte[] data, long length) 102 | { 103 | _data = data; 104 | _length = length; 105 | } 106 | 107 | internal PayloadData (ushort code, string reason) 108 | { 109 | _code = code; 110 | _reason = reason ?? String.Empty; 111 | 112 | _data = code.Append (reason); 113 | _length = _data.LongLength; 114 | 115 | _codeSet = true; 116 | _reasonSet = true; 117 | } 118 | 119 | #endregion 120 | 121 | #region Internal Properties 122 | 123 | internal ushort Code { 124 | get { 125 | if (!_codeSet) { 126 | _code = _length > 1 127 | ? _data.SubArray (0, 2).ToUInt16 (ByteOrder.Big) 128 | : (ushort) 1005; 129 | 130 | _codeSet = true; 131 | } 132 | 133 | return _code; 134 | } 135 | } 136 | 137 | internal long ExtensionDataLength { 138 | get { 139 | return _extDataLength; 140 | } 141 | 142 | set { 143 | _extDataLength = value; 144 | } 145 | } 146 | 147 | internal bool HasReservedCode { 148 | get { 149 | return _length > 1 && Code.IsReserved (); 150 | } 151 | } 152 | 153 | internal string Reason { 154 | get { 155 | if (!_reasonSet) { 156 | _reason = _length > 2 157 | ? _data.SubArray (2, _length - 2).UTF8Decode () 158 | : String.Empty; 159 | 160 | _reasonSet = true; 161 | } 162 | 163 | return _reason; 164 | } 165 | } 166 | 167 | #endregion 168 | 169 | #region Public Properties 170 | 171 | public byte[] ApplicationData { 172 | get { 173 | return _extDataLength > 0 174 | ? _data.SubArray (_extDataLength, _length - _extDataLength) 175 | : _data; 176 | } 177 | } 178 | 179 | public byte[] ExtensionData { 180 | get { 181 | return _extDataLength > 0 182 | ? _data.SubArray (0, _extDataLength) 183 | : WebSocket.EmptyBytes; 184 | } 185 | } 186 | 187 | public ulong Length { 188 | get { 189 | return (ulong) _length; 190 | } 191 | } 192 | 193 | #endregion 194 | 195 | #region Internal Methods 196 | 197 | internal void Mask (byte[] key) 198 | { 199 | for (long i = 0; i < _length; i++) 200 | _data[i] = (byte) (_data[i] ^ key[i % 4]); 201 | } 202 | 203 | #endregion 204 | 205 | #region Public Methods 206 | 207 | public IEnumerator GetEnumerator () 208 | { 209 | foreach (var b in _data) 210 | yield return b; 211 | } 212 | 213 | public byte[] ToArray () 214 | { 215 | return _data; 216 | } 217 | 218 | public override string ToString () 219 | { 220 | return BitConverter.ToString (_data); 221 | } 222 | 223 | #endregion 224 | 225 | #region Explicit Interface Implementations 226 | 227 | IEnumerator IEnumerable.GetEnumerator () 228 | { 229 | return GetEnumerator (); 230 | } 231 | 232 | #endregion 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Server/IWebSocketSession.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * IWebSocketSession.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2013-2018 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 access to the information in a WebSocket session. 36 | /// 37 | public interface IWebSocketSession 38 | { 39 | #region Properties 40 | 41 | /// 42 | /// Gets the current state of the WebSocket connection for the session. 43 | /// 44 | /// 45 | /// 46 | /// One of the enum values. 47 | /// 48 | /// 49 | /// It indicates the current state of the connection. 50 | /// 51 | /// 52 | WebSocketState ConnectionState { get; } 53 | 54 | /// 55 | /// Gets the information in the WebSocket handshake request. 56 | /// 57 | /// 58 | /// A instance that provides the access to 59 | /// the information in the handshake request. 60 | /// 61 | WebSocketContext Context { get; } 62 | 63 | /// 64 | /// Gets the unique ID of the session. 65 | /// 66 | /// 67 | /// A that represents the unique ID of the session. 68 | /// 69 | string ID { get; } 70 | 71 | /// 72 | /// Gets the name of the WebSocket subprotocol for the session. 73 | /// 74 | /// 75 | /// A that represents the name of the subprotocol 76 | /// if present. 77 | /// 78 | string Protocol { get; } 79 | 80 | /// 81 | /// Gets the time that the session has started. 82 | /// 83 | /// 84 | /// A that represents the time that the session 85 | /// has started. 86 | /// 87 | DateTime StartTime { get; } 88 | 89 | #endregion 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/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 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Server/WebSocketServiceHost.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketServiceHost.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2017 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 43 | /// a WebSocket service provided by the or 44 | /// . 45 | /// 46 | /// 47 | /// This class is an abstract class. 48 | /// 49 | public abstract class WebSocketServiceHost 50 | { 51 | #region Private Fields 52 | 53 | private Logger _log; 54 | private string _path; 55 | private WebSocketSessionManager _sessions; 56 | 57 | #endregion 58 | 59 | #region Protected Constructors 60 | 61 | /// 62 | /// Initializes a new instance of the class 63 | /// with the specified and . 64 | /// 65 | /// 66 | /// A that represents the absolute path to the service. 67 | /// 68 | /// 69 | /// A that represents the logging function for the service. 70 | /// 71 | protected WebSocketServiceHost (string path, Logger log) 72 | { 73 | _path = path; 74 | _log = log; 75 | 76 | _sessions = new WebSocketSessionManager (log); 77 | } 78 | 79 | #endregion 80 | 81 | #region Internal Properties 82 | 83 | internal ServerState State { 84 | get { 85 | return _sessions.State; 86 | } 87 | } 88 | 89 | #endregion 90 | 91 | #region Protected Properties 92 | 93 | /// 94 | /// Gets the logging function for the service. 95 | /// 96 | /// 97 | /// A that provides the logging function. 98 | /// 99 | protected Logger Log { 100 | get { 101 | return _log; 102 | } 103 | } 104 | 105 | #endregion 106 | 107 | #region Public Properties 108 | 109 | /// 110 | /// Gets or sets a value indicating whether the service cleans up 111 | /// the inactive sessions periodically. 112 | /// 113 | /// 114 | /// The set operation does nothing if the service has already started or 115 | /// it is shutting down. 116 | /// 117 | /// 118 | /// true if the service cleans up the inactive sessions every 119 | /// 60 seconds; otherwise, false. 120 | /// 121 | public bool KeepClean { 122 | get { 123 | return _sessions.KeepClean; 124 | } 125 | 126 | set { 127 | _sessions.KeepClean = value; 128 | } 129 | } 130 | 131 | /// 132 | /// Gets the path to the service. 133 | /// 134 | /// 135 | /// A that represents the absolute path to 136 | /// the service. 137 | /// 138 | public string Path { 139 | get { 140 | return _path; 141 | } 142 | } 143 | 144 | /// 145 | /// Gets the management function for the sessions in the service. 146 | /// 147 | /// 148 | /// A that manages the sessions in 149 | /// the service. 150 | /// 151 | public WebSocketSessionManager Sessions { 152 | get { 153 | return _sessions; 154 | } 155 | } 156 | 157 | /// 158 | /// Gets the of the behavior of the service. 159 | /// 160 | /// 161 | /// A that represents the type of the behavior of 162 | /// the service. 163 | /// 164 | public abstract Type BehaviorType { get; } 165 | 166 | /// 167 | /// Gets or sets the time to wait for the response to the WebSocket Ping or 168 | /// Close. 169 | /// 170 | /// 171 | /// The set operation does nothing if the service has already started or 172 | /// it is shutting down. 173 | /// 174 | /// 175 | /// A to wait for the response. 176 | /// 177 | /// 178 | /// The value specified for a set operation is zero or less. 179 | /// 180 | public TimeSpan WaitTime { 181 | get { 182 | return _sessions.WaitTime; 183 | } 184 | 185 | set { 186 | _sessions.WaitTime = value; 187 | } 188 | } 189 | 190 | #endregion 191 | 192 | #region Internal Methods 193 | 194 | internal void Start () 195 | { 196 | _sessions.Start (); 197 | } 198 | 199 | internal void StartSession (WebSocketContext context) 200 | { 201 | CreateSession ().Start (context, _sessions); 202 | } 203 | 204 | internal void Stop (ushort code, string reason) 205 | { 206 | _sessions.Stop (code, reason); 207 | } 208 | 209 | #endregion 210 | 211 | #region Protected Methods 212 | 213 | /// 214 | /// Creates a new session for the service. 215 | /// 216 | /// 217 | /// A instance that represents 218 | /// the new session. 219 | /// 220 | protected abstract WebSocketBehavior CreateSession (); 221 | 222 | #endregion 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/Server/WebSocketServiceHost`1.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketServiceHost`1.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2015-2017 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 _creator; 39 | 40 | #endregion 41 | 42 | #region Internal Constructors 43 | 44 | internal WebSocketServiceHost ( 45 | string path, Func creator, Logger log 46 | ) 47 | : this (path, creator, null, log) 48 | { 49 | } 50 | 51 | internal WebSocketServiceHost ( 52 | string path, 53 | Func creator, 54 | Action initializer, 55 | Logger log 56 | ) 57 | : base (path, log) 58 | { 59 | _creator = createCreator (creator, initializer); 60 | } 61 | 62 | #endregion 63 | 64 | #region Public Properties 65 | 66 | public override Type BehaviorType { 67 | get { 68 | return typeof (TBehavior); 69 | } 70 | } 71 | 72 | #endregion 73 | 74 | #region Private Methods 75 | 76 | private Func createCreator ( 77 | Func creator, Action initializer 78 | ) 79 | { 80 | if (initializer == null) 81 | return creator; 82 | 83 | return () => { 84 | var ret = creator (); 85 | initializer (ret); 86 | 87 | return ret; 88 | }; 89 | } 90 | 91 | #endregion 92 | 93 | #region Protected Methods 94 | 95 | protected override WebSocketBehavior CreateSession () 96 | { 97 | return _creator (); 98 | } 99 | 100 | #endregion 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/WebSocketException.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketException.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2012-2016 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 fatal error occurs in 35 | /// the WebSocket communication. 36 | /// 37 | public class WebSocketException : Exception 38 | { 39 | #region Private Fields 40 | 41 | private CloseStatusCode _code; 42 | 43 | #endregion 44 | 45 | #region Internal Constructors 46 | 47 | internal WebSocketException () 48 | : this (CloseStatusCode.Abnormal, null, null) 49 | { 50 | } 51 | 52 | internal WebSocketException (Exception innerException) 53 | : this (CloseStatusCode.Abnormal, null, innerException) 54 | { 55 | } 56 | 57 | internal WebSocketException (string message) 58 | : this (CloseStatusCode.Abnormal, message, null) 59 | { 60 | } 61 | 62 | internal WebSocketException (CloseStatusCode code) 63 | : this (code, null, null) 64 | { 65 | } 66 | 67 | internal WebSocketException (string message, Exception innerException) 68 | : this (CloseStatusCode.Abnormal, message, innerException) 69 | { 70 | } 71 | 72 | internal WebSocketException (CloseStatusCode code, Exception innerException) 73 | : this (code, null, innerException) 74 | { 75 | } 76 | 77 | internal WebSocketException (CloseStatusCode code, string message) 78 | : this (code, message, null) 79 | { 80 | } 81 | 82 | internal WebSocketException ( 83 | CloseStatusCode code, string message, Exception innerException 84 | ) 85 | : base (message ?? code.GetMessage (), innerException) 86 | { 87 | _code = code; 88 | } 89 | 90 | #endregion 91 | 92 | #region Public Properties 93 | 94 | /// 95 | /// Gets the status code indicating the cause of the exception. 96 | /// 97 | /// 98 | /// One of the enum values that represents 99 | /// the status code indicating the cause of the exception. 100 | /// 101 | public CloseStatusCode Code { 102 | get { 103 | return _code; 104 | } 105 | } 106 | 107 | #endregion 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/WebSocketState.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | /* 3 | * WebSocketState.cs 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2010-2016 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 | /// 39 | /// The WebSocket API. 40 | /// 41 | public enum WebSocketState : ushort 42 | { 43 | /// 44 | /// Equivalent to numeric value 0. Indicates that the connection has not 45 | /// yet been established. 46 | /// 47 | Connecting = 0, 48 | /// 49 | /// Equivalent to numeric value 1. Indicates that the connection has 50 | /// been established, and the communication is possible. 51 | /// 52 | Open = 1, 53 | /// 54 | /// Equivalent to numeric value 2. Indicates that the connection is 55 | /// going through the closing handshake, or the close method has 56 | /// been invoked. 57 | /// 58 | Closing = 2, 59 | /// 60 | /// Equivalent to numeric value 3. Indicates that the connection has 61 | /// been closed or could not be established. 62 | /// 63 | Closed = 3 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/doc/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore MonoDevelop build results. 2 | 3 | html 4 | mdoc 5 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/doc/doc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # @(#) doc.sh ver.0.0.2 2013.01.24 4 | # 5 | # Usage: 6 | # doc.sh 7 | # 8 | # Description: 9 | # Creating documentation for websocket-sharp. 10 | # 11 | ########################################################################### 12 | 13 | SRC_DIR="../bin/Release_Ubuntu" 14 | XML="${SRC_DIR}/websocket-sharp.xml" 15 | DLL="${SRC_DIR}/websocket-sharp.dll" 16 | 17 | DOC_DIR="." 18 | MDOC_DIR="${DOC_DIR}/mdoc" 19 | HTML_DIR="${DOC_DIR}/html" 20 | 21 | createDir() { 22 | if [ ! -d $1 ]; then 23 | mkdir -p $1 24 | fi 25 | } 26 | 27 | set -e 28 | createDir ${MDOC_DIR} 29 | createDir ${HTML_DIR} 30 | mdoc update --delete -fno-assembly-versions -i ${XML} -o ${MDOC_DIR}/ ${DLL} 31 | mdoc export-html -o ${HTML_DIR}/ ${MDOC_DIR}/ 32 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/websocket-sharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {B357BAC7-529E-4D81-A0D2-71041B19C8DE} 9 | Library 10 | WebSocketSharp 11 | websocket-sharp 12 | v3.5 13 | true 14 | websocket-sharp.snk 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug 21 | DEBUG 22 | prompt 23 | 4 24 | false 25 | 26 | 27 | none 28 | false 29 | bin\Release 30 | prompt 31 | 4 32 | false 33 | 34 | 35 | true 36 | full 37 | false 38 | bin\Debug_Ubuntu 39 | DEBUG 40 | prompt 41 | 4 42 | false 43 | 44 | 45 | none 46 | false 47 | bin\Release_Ubuntu 48 | prompt 49 | 4 50 | false 51 | true 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /src/MHttpServer/websocket-sharp/websocket-sharp.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hello-Mango/MHttpServer/3ea980daf28b2af327d1d47c3695ed55c76be617/src/MHttpServer/websocket-sharp/websocket-sharp.snk --------------------------------------------------------------------------------