diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index 50d9d616..f57392a4 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -48,8 +48,7 @@ using WebSocketSharp.Net.WebSockets; namespace WebSocketSharp.Server { /// - /// Provides a simple HTTP server that allows to accept the WebSocket - /// connection requests. + /// Provides a simple HTTP server that allows to accept the WebSocket connection requests. /// /// /// The HttpServer class can provide the multi WebSocket services. @@ -58,16 +57,16 @@ namespace WebSocketSharp.Server { #region Private Fields - private HttpListener _listener; - private Logger _logger; - private int _port; - private Thread _receiveRequestThread; - private string _rootPath; - private bool _secure; - private WebSocketServiceManager _services; - private volatile ServerState _state; - private object _sync; - private bool _windows; + private HttpListener _listener; + private Logger _logger; + private int _port; + private Thread _receiveRequestThread; + private string _rootPath; + private bool _secure; + private WebSocketServiceManager _services; + private volatile ServerState _state; + private object _sync; + private bool _windows; #endregion @@ -77,8 +76,7 @@ namespace WebSocketSharp.Server /// Initializes a new instance of the class. /// /// - /// An instance initialized by this constructor listens for the incoming - /// requests on port 80. + /// An instance initialized by this constructor listens for the incoming requests on port 80. /// public HttpServer () : this (80) @@ -86,17 +84,16 @@ namespace WebSocketSharp.Server } /// - /// Initializes a new instance of the class with - /// the specified . + /// Initializes a new instance of the class with the specified + /// . /// /// /// - /// An instance initialized by this constructor listens for the incoming - /// requests on . + /// An instance initialized by this constructor listens for the incoming requests on + /// . /// /// - /// And if is 443, that instance provides a secure - /// connection. + /// And if is 443, that instance provides a secure connection. /// /// /// @@ -106,24 +103,24 @@ namespace WebSocketSharp.Server /// isn't between 1 and 65535. /// public HttpServer (int port) - : this (port, port == 443 ? true : false) + : this (port, port == 443) { } /// - /// Initializes a new instance of the class with - /// the specified and . + /// Initializes a new instance of the class with the specified + /// and . /// /// - /// An instance initialized by this constructor listens for the incoming - /// requests on . + /// An instance initialized by this constructor listens for the incoming requests on + /// . /// /// /// An that represents the port number on which to listen. /// /// - /// A that indicates providing a secure connection or not. - /// (true indicates providing a secure connection.) + /// A that indicates providing a secure connection or not. (true + /// indicates providing a secure connection.) /// /// /// isn't between 1 and 65535. @@ -134,13 +131,11 @@ namespace WebSocketSharp.Server public HttpServer (int port, bool secure) { if (!port.IsPortNumber ()) - throw new ArgumentOutOfRangeException ( - "port", "Must be between 1 and 65535: " + port); + throw new ArgumentOutOfRangeException ("port", "Must be between 1 and 65535: " + port); if ((port == 80 && secure) || (port == 443 && !secure)) throw new ArgumentException ( - String.Format ( - "Invalid pair of 'port' and 'secure': {0}, {1}", port, secure)); + String.Format ("Invalid pair of 'port' and 'secure': {0}, {1}", port, secure)); _port = port; _secure = secure; @@ -166,9 +161,9 @@ namespace WebSocketSharp.Server /// Gets or sets the scheme used to authenticate the clients. /// /// - /// One of the enum - /// values, indicates the scheme used to authenticate the clients. The default - /// value is . + /// One of the enum values, indicates + /// the scheme used to authenticate the clients. + /// The default value is . /// public AuthenticationSchemes AuthenticationSchemes { get { @@ -184,8 +179,7 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets the certificate used to authenticate the server on the - /// secure connection. + /// Gets or sets the certificate used to authenticate the server on the secure connection. /// /// /// A used to authenticate the server. @@ -200,8 +194,7 @@ namespace WebSocketSharp.Server return; if (EndPointListener.CertificateExists (_port, _listener.CertificateFolderPath)) - _logger.Warn ( - "The server certificate associated with the port number already exists."); + _logger.Warn ("The server certificate associated with the port number already exists."); _listener.DefaultCertificate = value; } @@ -223,8 +216,7 @@ namespace WebSocketSharp.Server /// Gets a value indicating whether the server provides a secure connection. /// /// - /// true if the server provides a secure connection; otherwise, - /// false. + /// true if the server provides a secure connection; otherwise, false. /// public bool IsSecure { get { @@ -233,12 +225,12 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets a value indicating whether the server cleans up the inactive - /// WebSocket sessions periodically. + /// Gets or sets a value indicating whether the server cleans up the inactive sessions in the + /// WebSocket services periodically. /// /// - /// true if the server cleans up the inactive WebSocket 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 { @@ -254,9 +246,9 @@ namespace WebSocketSharp.Server /// Gets the logging functions. /// /// - /// The default logging level is . If you would - /// like to change it, you should set the Log.Level property to any of - /// the enum values. + /// The default logging level is . If you would like to change it, + /// you should set the Log.Level property to any of the enum + /// values. /// /// /// A that provides the logging functions. @@ -280,12 +272,11 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets the name of the realm associated with the - /// . + /// Gets or sets the name of the realm associated with the server. /// /// - /// A that represents the name of the realm. - /// The default value is SECRET AREA. + /// A that represents the name of the realm. The default value is + /// SECRET AREA. /// public string Realm { get { @@ -301,11 +292,11 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets the document root path of server. + /// Gets or sets the document root path of the server. /// /// - /// A that represents the document root path of server. - /// The default value is ./Public. + /// A that represents the document root path of the server. The default + /// value is ./Public. /// public string RootPath { get { @@ -323,13 +314,13 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets the delegate called to find the credentials for an identity - /// used to authenticate a client. + /// Gets or sets the delegate called to find the credentials for an identity used to + /// authenticate a client. /// /// - /// A Func<, > - /// delegate that references the method(s) used to find the credentials. - /// The default value is a function that only returns . + /// A Func<, > delegate that + /// references the method(s) used to find the credentials. The default value is a function + /// that only returns . /// public Func UserCredentialsFinder { get { @@ -345,7 +336,7 @@ namespace WebSocketSharp.Server } /// - /// Gets the access to the WebSocket services provided by the . + /// Gets the access to the WebSocket services provided by the server. /// /// /// A that manages the WebSocket services. @@ -419,10 +410,9 @@ namespace WebSocketSharp.Server } _services.Stop ( - ((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), - true); - _listener.Abort (); + ((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), true); + _listener.Abort (); _state = ServerState.STOP; } @@ -430,7 +420,6 @@ namespace WebSocketSharp.Server { var args = new HttpRequestEventArgs (context); var method = context.Request.HttpMethod; - if (method == "GET") { if (OnGet != null) { OnGet (this, args); @@ -519,8 +508,7 @@ namespace WebSocketSharp.Server var path = context.Path; WebSocketServiceHost host; - if (path == null || - !_services.TryGetServiceHostInternally (path, out host)) { + if (path == null || !_services.TryGetServiceHostInternally (path, out host)) { context.Close (HttpStatusCode.NotImplemented); return; } @@ -528,8 +516,7 @@ namespace WebSocketSharp.Server host.StartSession (context); } - private bool authenticateRequest ( - AuthenticationSchemes scheme, HttpListenerContext context) + private bool authenticateRequest (AuthenticationSchemes scheme, HttpListenerContext context) { if (context.Request.IsAuthenticated) return true; @@ -551,7 +538,7 @@ namespace WebSocketSharp.Server if (_state == ServerState.START || _state == ServerState.SHUTDOWN) { _logger.Error ( String.Format ( - "The '{0}' property cannot set a value because the server has already been started.", + "Set operation of {0} isn't available because the server has already started.", property)); return false; @@ -576,10 +563,7 @@ namespace WebSocketSharp.Server acceptRequestAsync (_listener.GetContext ()); } catch (HttpListenerException ex) { - _logger.Warn ( - String.Format ( - "Receiving has been stopped.\nreason: {0}.", ex.Message)); - + _logger.Warn ("Receiving has been stopped.\nreason: " + ex.Message); break; } catch (Exception ex) { @@ -610,74 +594,64 @@ namespace WebSocketSharp.Server #region Public Methods /// - /// Adds the specified typed WebSocket service with the specified - /// . + /// Adds the specified typed 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 . /// - /// - /// A that represents the absolute path to the WebSocket - /// service. + /// + /// A that represents the absolute path to the WebSocket service to add. /// /// - /// The type of the WebSocket service. The TWithNew must inherit the - /// class and must have a public parameterless - /// constructor. + /// The type of the WebSocket service. + /// The TWithNew must inherit the class and must have a public + /// parameterless constructor. /// - public void AddWebSocketService (string servicePath) + public void AddWebSocketService (string path) where TWithNew : WebSocketService, new () { - AddWebSocketService (servicePath, () => new TWithNew ()); + AddWebSocketService (path, () => new TWithNew ()); } /// - /// Adds the specified typed WebSocket service with the specified - /// and . + /// Adds the specified typed WebSocket service with the specified and + /// . /// /// /// - /// 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 a initialized specified - /// typed WebSocket service instance. + /// returns a initialized specified typed + /// instance. /// /// - /// - /// A that represents the absolute path to the WebSocket - /// service. + /// + /// A that represents the absolute path to the WebSocket service to add. /// - /// - /// A Func<T> delegate that references the method used to initialize - /// a new WebSocket service instance (a new WebSocket session). + /// + /// A Func<T> delegate that references the method used to initialize a new specified + /// typed instance (a new + /// instance). /// /// - /// The type of the WebSocket service. The T must inherit the - /// class. + /// The type of the WebSocket service. The T must inherit the + /// class. /// - public void AddWebSocketService ( - string servicePath, Func serviceConstructor) + public void AddWebSocketService (string path, Func constructor) where T : WebSocketService { - var msg = servicePath.CheckIfValidServicePath () ?? - (serviceConstructor == null - ? "'serviceConstructor' must not be null." - : null); + var msg = path.CheckIfValidServicePath () ?? + (constructor == null ? "'constructor' must not be null." : null); if (msg != null) { - _logger.Error ( - String.Format ("{0}\nservice path: {1}", msg, servicePath ?? "")); - + _logger.Error (String.Format ("{0}\nservice path: {1}", msg, path)); return; } - var host = new WebSocketServiceHost ( - servicePath, serviceConstructor, _logger); - + var host = new WebSocketServiceHost (path, constructor, _logger); if (!KeepClean) host.KeepClean = false; @@ -688,12 +662,11 @@ namespace WebSocketSharp.Server /// Gets the contents of the file with the specified . /// /// - /// An array of that receives the contents of the file if - /// it exists; otherwise, . + /// An array of that receives the contents of the file if it exists; + /// otherwise, . /// /// - /// A that represents the virtual path to the file - /// to get. + /// A that represents the virtual path to the file to find. /// public byte [] GetFile (string path) { @@ -707,31 +680,28 @@ namespace WebSocketSharp.Server } /// - /// Removes the WebSocket service with the specified . + /// 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. + /// + /// A that represents the absolute path to the WebSocket service to find. /// - public bool RemoveWebSocketService (string servicePath) + public bool RemoveWebSocketService (string path) { - var msg = servicePath.CheckIfValidServicePath (); + var msg = path.CheckIfValidServicePath (); if (msg != null) { - _logger.Error ( - String.Format ("{0}\nservice path: {1}", msg, servicePath)); - + _logger.Error (String.Format ("{0}\nservice path: {1}", msg, path)); return false; } - return _services.Remove (servicePath); + return _services.Remove (path); } /// @@ -742,10 +712,7 @@ namespace WebSocketSharp.Server lock (_sync) { var msg = _state.CheckIfStartable () ?? checkIfCertExists (); if (msg != null) { - _logger.Error ( - String.Format ( - "{0}\nstate: {1}\nsecure: {2}", msg, _state, _secure)); - + _logger.Error (String.Format ("{0}\nstate: {1}\nsecure: {2}", msg, _state, _secure)); return; } @@ -779,12 +746,11 @@ namespace WebSocketSharp.Server } /// - /// Stops receiving the HTTP requests with the specified - /// and used to stop the WebSocket services. + /// Stops receiving the HTTP requests with the specified and + /// used to stop the WebSocket services. /// /// - /// A that represents the status code indicating the - /// reason for stop. + /// A that represents the status code indicating the reason for stop. /// /// /// A that represents the reason for stop. @@ -799,8 +765,7 @@ namespace WebSocketSharp.Server if (msg != null) { _logger.Error ( - String.Format ( - "{0}\nstate: {1}\ncode: {2}\nreason: {3}", msg, _state, code, reason)); + String.Format ("{0}\nstate: {1}\ncode: {2}\nreason: {3}", msg, _state, code, reason)); return; } @@ -815,12 +780,12 @@ namespace WebSocketSharp.Server } /// - /// Stops receiving the HTTP requests with the specified - /// and used to stop the WebSocket services. + /// Stops receiving the HTTP requests with the specified and + /// used to stop the WebSocket services. /// /// - /// One of the enum values, represents the - /// status code indicating the reasons for stop. + /// One of the enum values, represents the status code indicating + /// the reasons for stop. /// /// /// A that represents the reason for stop. @@ -833,9 +798,7 @@ namespace WebSocketSharp.Server (data = ((ushort) code).Append (reason)).CheckIfValidControlData ("reason"); if (msg != null) { - _logger.Error ( - String.Format ("{0}\nstate: {1}\nreason: {2}", msg, _state, reason)); - + _logger.Error (String.Format ("{0}\nstate: {1}\nreason: {2}", msg, _state, reason)); return; } diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index a6a12a64..659d3e8d 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -148,11 +148,10 @@ namespace WebSocketSharp.Server var host = _uri.DnsSafeHost; _address = host.ToIPAddress (); if (_address == null || !_address.IsLocal ()) - throw new ArgumentException ( - String.Format ("The host part must be the local host name: {0}", host), "url"); + throw new ArgumentException ("The host part must be the local host name: " + host, "url"); _port = _uri.Port; - _secure = _uri.Scheme == "wss" ? true : false; + _secure = _uri.Scheme == "wss"; init (); } @@ -212,7 +211,7 @@ namespace WebSocketSharp.Server /// isn't a local IP address. /// public WebSocketServer (System.Net.IPAddress address, int port) - : this (address, port, port == 443 ? true : false) + : this (address, port, port == 443) { } @@ -254,8 +253,7 @@ namespace WebSocketSharp.Server public WebSocketServer (System.Net.IPAddress address, int port, bool secure) { if (!address.IsLocal ()) - throw new ArgumentException ( - String.Format ("Must be the local IP address: {0}", address), "address"); + throw new ArgumentException ("Must be the local IP address: " + address, "address"); if (!port.IsPortNumber ()) throw new ArgumentOutOfRangeException ("port", "Must be between 1 and 65535: " + port); @@ -344,8 +342,7 @@ namespace WebSocketSharp.Server /// Gets a value indicating whether the server provides a secure connection. /// /// - /// true if the server provides a secure connection; otherwise, - /// false. + /// true if the server provides a secure connection; otherwise, false. /// public bool IsSecure { get { @@ -469,8 +466,7 @@ namespace WebSocketSharp.Server _listener.Stop (); _services.Stop ( - ((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), - true); + ((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), true); _state = ServerState.STOP; } @@ -511,11 +507,11 @@ namespace WebSocketSharp.Server } private bool authenticateRequest ( - AuthenticationSchemes authScheme, TcpListenerWebSocketContext context) + AuthenticationSchemes scheme, TcpListenerWebSocketContext context) { - var challenge = authScheme == AuthenticationSchemes.Basic + var challenge = scheme == AuthenticationSchemes.Basic ? HttpUtility.CreateBasicAuthChallenge (Realm) - : authScheme == AuthenticationSchemes.Digest + : scheme == AuthenticationSchemes.Digest ? HttpUtility.CreateDigestAuthChallenge (Realm) : null; @@ -525,7 +521,7 @@ namespace WebSocketSharp.Server } var retry = -1; - var expected = authScheme.ToString (); + var expected = scheme.ToString (); var realm = Realm; var credentialsFinder = UserCredentialsFinder; Func auth = null; @@ -542,7 +538,7 @@ namespace WebSocketSharp.Server return auth (); } - context.SetUser (authScheme, realm, credentialsFinder); + context.SetUser (scheme, realm, credentialsFinder); if (context.IsAuthenticated) return true; @@ -591,8 +587,7 @@ namespace WebSocketSharp.Server acceptRequestAsync (_listener.AcceptTcpClient ()); } catch (SocketException ex) { - _logger.Warn (String.Format ("Receiving has been stopped.\nreason: {0}.", ex.Message)); - + _logger.Warn ("Receiving has been stopped.\nreason: " + ex.Message); break; } catch (Exception ex) { @@ -692,7 +687,6 @@ namespace WebSocketSharp.Server if (msg != null) { _logger.Error (String.Format ("{0}\nservice path: {1}", msg, path)); - return; } @@ -722,7 +716,6 @@ namespace WebSocketSharp.Server var msg = path.CheckIfValidServicePath (); if (msg != null) { _logger.Error (String.Format ("{0}\nservice path: {1}", msg, path)); - return false; } @@ -738,7 +731,6 @@ namespace WebSocketSharp.Server var msg = _state.CheckIfStartable () ?? checkIfCertExists (); if (msg != null) { _logger.Error (String.Format ("{0}\nstate: {1}\nsecure: {2}", msg, _state, _secure)); - return; } @@ -825,7 +817,6 @@ namespace WebSocketSharp.Server if (msg != null) { _logger.Error (String.Format ("{0}\nstate: {1}\nreason: {2}", msg, _state, reason)); - return; }