From d9528ffe752ed2a90a8142594ae7e0c633855140 Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 1 Sep 2014 14:27:04 +0900 Subject: [PATCH] Refactored HttpServer.cs and WebSocketServer.cs --- websocket-sharp/Server/HttpServer.cs | 11 ++-- websocket-sharp/Server/WebSocketServer.cs | 70 ++++++++++++----------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index cc5599de..c7aca59a 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -51,7 +51,7 @@ namespace WebSocketSharp.Server /// Provides a simple HTTP server that allows to accept the WebSocket connection requests. /// /// - /// The HttpServer class can provide the multi WebSocket services. + /// The HttpServer class can provide multiple WebSocket services. /// public class HttpServer { @@ -480,12 +480,12 @@ namespace WebSocketSharp.Server ? OnPatch : null; - if (evt != null) { + if (evt != null) evt (this, new HttpRequestEventArgs (context)); - return; - } + else + context.Response.StatusCode = (int) HttpStatusCode.NotImplemented; - context.Response.StatusCode = (int) HttpStatusCode.NotImplemented; + context.Response.Close (); } private void processRequestAsync (HttpListenerContext context) @@ -504,7 +504,6 @@ namespace WebSocketSharp.Server } processHttpRequest (context); - context.Response.Close (); } catch (Exception ex) { _logger.Fatal (ex.ToString ()); diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index f1b09cf0..d34e2e13 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -52,7 +52,7 @@ namespace WebSocketSharp.Server /// Provides a WebSocket protocol server. /// /// - /// The WebSocketServer class provides the multi WebSocket service. + /// The WebSocketServer class can provide multiple WebSocket services. /// public class WebSocketServer { @@ -86,7 +86,7 @@ namespace WebSocketSharp.Server /// on port 80. /// public WebSocketServer () - : this (80) + : this (System.Net.IPAddress.Any, 80, false) { } @@ -110,7 +110,7 @@ namespace WebSocketSharp.Server /// isn't between 1 and 65535. /// public WebSocketServer (int port) - : this (System.Net.IPAddress.Any, port) + : this (System.Net.IPAddress.Any, port, port == 443) { } @@ -302,8 +302,11 @@ namespace WebSocketSharp.Server } set { - if (!canSet ("AuthenticationSchemes")) + var msg = _state.CheckIfStartable (); + if (msg != null) { + _logger.Error (msg); return; + } _authSchemes = value; } @@ -322,8 +325,11 @@ namespace WebSocketSharp.Server } set { - if (!canSet ("Certificate")) + var msg = _state.CheckIfStartable (); + if (msg != null) { + _logger.Error (msg); return; + } _certificate = value; } @@ -358,8 +364,8 @@ namespace WebSocketSharp.Server /// periodically. /// /// - /// true if the server cleans up the inactive sessions every 60 seconds; otherwise, - /// false. The default value is true. + /// true if the server cleans up the inactive sessions every 60 seconds; + /// otherwise, false. The default value is true. /// public bool KeepClean { get { @@ -413,8 +419,11 @@ namespace WebSocketSharp.Server } set { - if (!canSet ("Realm")) + var msg = _state.CheckIfStartable (); + if (msg != null) { + _logger.Error (msg); return; + } _realm = value; } @@ -438,8 +447,11 @@ namespace WebSocketSharp.Server } set { - if (!canSet ("ReuseAddress")) + var msg = _state.CheckIfStartable (); + if (msg != null) { + _logger.Error (msg); return; + } if (value ^ _reuseAddress) { _listener.Server.SetSocketOption ( @@ -465,8 +477,11 @@ namespace WebSocketSharp.Server } set { - if (!canSet ("UserCredentialsFinder")) + var msg = _state.CheckIfStartable (); + if (msg != null) { + _logger.Error (msg); return; + } _credentialsFinder = value; } @@ -537,30 +552,17 @@ namespace WebSocketSharp.Server } context.SetUser (scheme, realm, credFinder); - if (context.IsAuthenticated) - return true; + if (!context.IsAuthenticated) { + context.SendAuthenticationChallenge (chal); + return auth (); + } - context.SendAuthenticationChallenge (chal); - return auth (); + return true; }; return auth (); } - private bool canSet (string property) - { - if (_state == ServerState.Start || _state == ServerState.ShuttingDown) { - _logger.Error ( - String.Format ( - "Set operation of {0} isn't available because the server has already started.", - property)); - - return false; - } - - return true; - } - private string checkIfCertificateExists () { return _secure && _certificate == null @@ -703,8 +705,8 @@ namespace WebSocketSharp.Server /// /// /// - /// This method converts to URL-decoded string and removes '/' - /// from tail end of . + /// This method converts to URL-decoded string and + /// removes '/' from tail end of . /// /// /// returns an initialized specified typed @@ -745,12 +747,12 @@ namespace WebSocketSharp.Server /// Removes the WebSocket service with the specified . /// /// - /// This method converts to URL-decoded string and removes '/' - /// from tail end of . + /// This method converts to URL-decoded string and + /// removes '/' from tail end of . /// /// - /// true if the WebSocket service is successfully found and removed; otherwise, - /// false. + /// true if the WebSocket service is successfully found and removed; + /// otherwise, false. /// /// /// A that represents the absolute path to the WebSocket service to find.