Refactored a few for HttpServer.cs
This commit is contained in:
parent
85dc704cc6
commit
cb8d1e75f6
@ -80,18 +80,18 @@ namespace WebSocketSharp.Server
|
||||
/// An instance initialized by this constructor listens for the incoming requests on port 80.
|
||||
/// </remarks>
|
||||
public HttpServer ()
|
||||
: this (80, false)
|
||||
{
|
||||
init ("*", 80, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HttpServer"/> class with the specified
|
||||
/// <paramref name="port"/>.
|
||||
/// Initializes a new instance of the <see cref="HttpServer"/> class with
|
||||
/// the specified <paramref name="port"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// An instance initialized by this constructor listens for the incoming requests
|
||||
/// on <paramref name="port"/>.
|
||||
/// An instance initialized by this constructor listens for the incoming
|
||||
/// requests on <paramref name="port"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If <paramref name="port"/> is 443, that instance provides a secure connection.
|
||||
@ -101,20 +101,24 @@ namespace WebSocketSharp.Server
|
||||
/// An <see cref="int"/> that represents the port number on which to listen.
|
||||
/// </param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// <paramref name="port"/> isn't between 1 and 65535.
|
||||
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
|
||||
/// </exception>
|
||||
public HttpServer (int port)
|
||||
: this (port, port == 443)
|
||||
{
|
||||
if (!port.IsPortNumber ())
|
||||
throw new ArgumentOutOfRangeException (
|
||||
"port", "Not between 1 and 65535 inclusive: " + port);
|
||||
|
||||
init ("*", port, port == 443);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HttpServer"/> class with the specified
|
||||
/// <paramref name="port"/> and <paramref name="secure"/>.
|
||||
/// Initializes a new instance of the <see cref="HttpServer"/> class with
|
||||
/// the specified <paramref name="port"/> and <paramref name="secure"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// An instance initialized by this constructor listens for the incoming requests
|
||||
/// on <paramref name="port"/>.
|
||||
/// An instance initialized by this constructor listens for the incoming
|
||||
/// requests on <paramref name="port"/>.
|
||||
/// </remarks>
|
||||
/// <param name="port">
|
||||
/// An <see cref="int"/> that represents the port number on which to listen.
|
||||
@ -127,30 +131,19 @@ namespace WebSocketSharp.Server
|
||||
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// <paramref name="port"/> isn't between 1 and 65535.
|
||||
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
|
||||
/// </exception>
|
||||
public HttpServer (int port, bool secure)
|
||||
{
|
||||
if (!port.IsPortNumber ())
|
||||
throw new ArgumentOutOfRangeException ("port", "Not between 1 and 65535: " + port);
|
||||
throw new ArgumentOutOfRangeException (
|
||||
"port", "Not between 1 and 65535 inclusive: " + port);
|
||||
|
||||
if ((port == 80 && secure) || (port == 443 && !secure))
|
||||
throw new ArgumentException (
|
||||
String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
|
||||
|
||||
_port = port;
|
||||
_secure = secure;
|
||||
_listener = new HttpListener ();
|
||||
_logger = _listener.Log;
|
||||
_services = new WebSocketServiceManager (_logger);
|
||||
_state = ServerState.Ready;
|
||||
_sync = new object ();
|
||||
|
||||
var os = Environment.OSVersion;
|
||||
_windows = os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX;
|
||||
|
||||
var pref = String.Format ("http{0}://*:{1}/", _secure ? "s" : "", _port);
|
||||
_listener.Prefixes.Add (pref);
|
||||
init ("*", port, secure);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -206,8 +199,8 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the server cleans up the inactive sessions
|
||||
/// in the WebSocket services periodically.
|
||||
/// Gets or sets a value indicating whether the server cleans up
|
||||
/// the inactive sessions in the WebSocket services periodically.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the server cleans up the inactive sessions every 60 seconds;
|
||||
@ -262,8 +255,8 @@ namespace WebSocketSharp.Server
|
||||
/// Gets or sets the name of the realm associated with the server.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// A <see cref="string"/> that represents the name of the realm.
|
||||
/// The default value is <c>"SECRET AREA"</c>.
|
||||
/// A <see cref="string"/> that represents the name of the realm. The default value is
|
||||
/// <c>"SECRET AREA"</c>.
|
||||
/// </value>
|
||||
public string Realm {
|
||||
get {
|
||||
@ -282,12 +275,12 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the server is allowed to be bound to an address
|
||||
/// that is already in use.
|
||||
/// Gets or sets a value indicating whether the server is allowed to be bound to
|
||||
/// an address that is already in use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If you would like to resolve to wait for socket in <c>TIME_WAIT</c> state, you should set
|
||||
/// this property to <c>true</c>.
|
||||
/// If you would like to resolve to wait for socket in <c>TIME_WAIT</c> state,
|
||||
/// you should set this property to <c>true</c>.
|
||||
/// </remarks>
|
||||
/// <value>
|
||||
/// <c>true</c> if the server is allowed to be bound to an address that is already in use;
|
||||
@ -318,9 +311,7 @@ namespace WebSocketSharp.Server
|
||||
/// </value>
|
||||
public string RootPath {
|
||||
get {
|
||||
return _rootPath != null && _rootPath.Length > 0
|
||||
? _rootPath
|
||||
: (_rootPath = "./Public");
|
||||
return _rootPath != null && _rootPath.Length > 0 ? _rootPath : (_rootPath = "./Public");
|
||||
}
|
||||
|
||||
set {
|
||||
@ -339,8 +330,8 @@ namespace WebSocketSharp.Server
|
||||
/// optionally the client for secure connection.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// A <see cref="ServerSslConfiguration"/> that represents the configuration used
|
||||
/// to authenticate the server and optionally the client for secure connection.
|
||||
/// A <see cref="ServerSslConfiguration"/> that represents the configuration used to
|
||||
/// authenticate the server and optionally the client for secure connection.
|
||||
/// </value>
|
||||
public ServerSslConfiguration SslConfiguration {
|
||||
get {
|
||||
@ -363,9 +354,9 @@ namespace WebSocketSharp.Server
|
||||
/// authenticate a client.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// A Func<<see cref="IIdentity"/>, <see cref="NetworkCredential"/>> delegate that
|
||||
/// references the method(s) used to find the credentials. The default value is a function
|
||||
/// that only returns <see langword="null"/>.
|
||||
/// A <c>Func<<see cref="IIdentity"/>, <see cref="NetworkCredential"/>></c> delegate that
|
||||
/// references the method(s) used to find the credentials. The default value is a function that
|
||||
/// only returns <see langword="null"/>.
|
||||
/// </value>
|
||||
public Func<IIdentity, NetworkCredential> UserCredentialsFinder {
|
||||
get {
|
||||
@ -498,9 +489,24 @@ namespace WebSocketSharp.Server
|
||||
return null;
|
||||
}
|
||||
|
||||
return !(usr || port)
|
||||
? "The secure connection requires a server certificate."
|
||||
: null;
|
||||
return !(usr || port) ? "The secure connection requires a server certificate." : null;
|
||||
}
|
||||
|
||||
private void init (string hostname, int port, bool secure)
|
||||
{
|
||||
_port = port;
|
||||
_secure = secure;
|
||||
|
||||
_listener = new HttpListener ();
|
||||
_listener.Prefixes.Add (
|
||||
String.Format ("http{0}://{1}:{2}/", secure ? "s" : "", hostname, port));
|
||||
|
||||
_logger = _listener.Log;
|
||||
_services = new WebSocketServiceManager (_logger);
|
||||
_sync = new object ();
|
||||
|
||||
var os = Environment.OSVersion;
|
||||
_windows = os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX;
|
||||
}
|
||||
|
||||
private void processRequest (HttpListenerContext context)
|
||||
@ -598,6 +604,46 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Adds the WebSocket service with the specified behavior, <paramref name="path"/>,
|
||||
/// and <paramref name="initializer"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <paramref name="initializer"/> returns an initialized specified typed
|
||||
/// <see cref="WebSocketBehavior"/> instance.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="path">
|
||||
/// A <see cref="string"/> that represents the absolute path to the service to add.
|
||||
/// </param>
|
||||
/// <param name="initializer">
|
||||
/// A <c>Func<T></c> delegate that references the method used to initialize
|
||||
/// a new specified typed <see cref="WebSocketBehavior"/> instance (a new
|
||||
/// <see cref="IWebSocketSession"/> instance).
|
||||
/// </param>
|
||||
/// <typeparam name="TBehavior">
|
||||
/// The type of the behavior of the service to add. The TBehavior must inherit
|
||||
/// the <see cref="WebSocketBehavior"/> class.
|
||||
/// </typeparam>
|
||||
public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
|
||||
where TBehavior : WebSocketBehavior
|
||||
{
|
||||
var msg = path.CheckIfValidServicePath () ??
|
||||
(initializer == null ? "'initializer' is null." : null);
|
||||
|
||||
if (msg != null) {
|
||||
_logger.Error (msg);
|
||||
return;
|
||||
}
|
||||
|
||||
_services.Add<TBehavior> (path, initializer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a WebSocket service with the specified behavior and <paramref name="path"/>.
|
||||
/// </summary>
|
||||
@ -619,46 +665,6 @@ namespace WebSocketSharp.Server
|
||||
AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the WebSocket service with the specified behavior, <paramref name="path"/>,
|
||||
/// and <paramref name="initializer"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <paramref name="initializer"/> returns an initialized specified typed
|
||||
/// <see cref="WebSocketBehavior"/> instance.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="path">
|
||||
/// A <see cref="string"/> that represents the absolute path to the service to add.
|
||||
/// </param>
|
||||
/// <param name="initializer">
|
||||
/// A Func<T> delegate that references the method used to initialize a new specified
|
||||
/// typed <see cref="WebSocketBehavior"/> instance (a new <see cref="IWebSocketSession"/>
|
||||
/// instance).
|
||||
/// </param>
|
||||
/// <typeparam name="TBehavior">
|
||||
/// The type of the behavior of the service to add. The TBehavior must inherit
|
||||
/// the <see cref="WebSocketBehavior"/> class.
|
||||
/// </typeparam>
|
||||
public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
|
||||
where TBehavior : WebSocketBehavior
|
||||
{
|
||||
var msg = path.CheckIfValidServicePath () ??
|
||||
(initializer == null ? "'initializer' is null." : null);
|
||||
|
||||
if (msg != null) {
|
||||
_logger.Error (msg);
|
||||
return;
|
||||
}
|
||||
|
||||
_services.Add<TBehavior> (path, initializer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the contents of the file with the specified <paramref name="path"/>.
|
||||
/// </summary>
|
||||
@ -675,9 +681,7 @@ namespace WebSocketSharp.Server
|
||||
if (_windows)
|
||||
path = path.Replace ("/", "\\");
|
||||
|
||||
return File.Exists (path)
|
||||
? File.ReadAllBytes (path)
|
||||
: null;
|
||||
return File.Exists (path) ? File.ReadAllBytes (path) : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -784,8 +788,8 @@ namespace WebSocketSharp.Server
|
||||
/// <see cref="string"/> used to stop the WebSocket services.
|
||||
/// </summary>
|
||||
/// <param name="code">
|
||||
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
|
||||
/// indicating the reason for the stop.
|
||||
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
|
||||
/// the reason for the stop.
|
||||
/// </param>
|
||||
/// <param name="reason">
|
||||
/// A <see cref="string"/> that represents the reason for the stop.
|
||||
|
Loading…
Reference in New Issue
Block a user