|
|
|
@ -80,18 +80,18 @@ namespace WebSocketSharp.Server
|
|
|
|
/// 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.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
public HttpServer ()
|
|
|
|
public HttpServer ()
|
|
|
|
: this (80, false)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
init ("*", 80, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="HttpServer"/> class with the specified
|
|
|
|
/// Initializes a new instance of the <see cref="HttpServer"/> class with
|
|
|
|
/// <paramref name="port"/>.
|
|
|
|
/// the specified <paramref name="port"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>
|
|
|
|
/// <para>
|
|
|
|
/// An instance initialized by this constructor listens for the incoming requests
|
|
|
|
/// An instance initialized by this constructor listens for the incoming
|
|
|
|
/// on <paramref name="port"/>.
|
|
|
|
/// requests on <paramref name="port"/>.
|
|
|
|
/// </para>
|
|
|
|
/// </para>
|
|
|
|
/// <para>
|
|
|
|
/// <para>
|
|
|
|
/// If <paramref name="port"/> is 443, that instance provides a secure connection.
|
|
|
|
/// 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.
|
|
|
|
/// An <see cref="int"/> that represents the port number on which to listen.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
/// <paramref name="port"/> isn't between 1 and 65535.
|
|
|
|
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
|
|
|
|
/// </exception>
|
|
|
|
/// </exception>
|
|
|
|
public HttpServer (int port)
|
|
|
|
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>
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="HttpServer"/> class with the specified
|
|
|
|
/// Initializes a new instance of the <see cref="HttpServer"/> class with
|
|
|
|
/// <paramref name="port"/> and <paramref name="secure"/>.
|
|
|
|
/// the specified <paramref name="port"/> and <paramref name="secure"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <remarks>
|
|
|
|
/// An instance initialized by this constructor listens for the incoming requests
|
|
|
|
/// An instance initialized by this constructor listens for the incoming
|
|
|
|
/// on <paramref name="port"/>.
|
|
|
|
/// requests on <paramref name="port"/>.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="port">
|
|
|
|
/// <param name="port">
|
|
|
|
/// An <see cref="int"/> that represents the port number on which to listen.
|
|
|
|
/// 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.
|
|
|
|
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
|
|
|
|
/// </exception>
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
/// <paramref name="port"/> isn't between 1 and 65535.
|
|
|
|
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
|
|
|
|
/// </exception>
|
|
|
|
/// </exception>
|
|
|
|
public HttpServer (int port, bool secure)
|
|
|
|
public HttpServer (int port, bool secure)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!port.IsPortNumber ())
|
|
|
|
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))
|
|
|
|
if ((port == 80 && secure) || (port == 443 && !secure))
|
|
|
|
throw new ArgumentException (
|
|
|
|
throw new ArgumentException (
|
|
|
|
String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
|
|
|
|
String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
|
|
|
|
|
|
|
|
|
|
|
|
_port = port;
|
|
|
|
init ("*", port, secure);
|
|
|
|
_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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
@ -206,8 +199,8 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether the server cleans up the inactive sessions
|
|
|
|
/// Gets or sets a value indicating whether the server cleans up
|
|
|
|
/// in the WebSocket services periodically.
|
|
|
|
/// the inactive sessions in the WebSocket services periodically.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// <c>true</c> if the server cleans up the inactive sessions every 60 seconds;
|
|
|
|
/// <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.
|
|
|
|
/// Gets or sets the name of the realm associated with the server.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the name of the realm.
|
|
|
|
/// A <see cref="string"/> that represents the name of the realm. The default value is
|
|
|
|
/// The default value is <c>"SECRET AREA"</c>.
|
|
|
|
/// <c>"SECRET AREA"</c>.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public string Realm {
|
|
|
|
public string Realm {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
@ -282,12 +275,12 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether the server is allowed to be bound to an address
|
|
|
|
/// Gets or sets a value indicating whether the server is allowed to be bound to
|
|
|
|
/// that is already in use.
|
|
|
|
/// an address that is already in use.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <remarks>
|
|
|
|
/// If you would like to resolve to wait for socket in <c>TIME_WAIT</c> state, you should set
|
|
|
|
/// If you would like to resolve to wait for socket in <c>TIME_WAIT</c> state,
|
|
|
|
/// this property to <c>true</c>.
|
|
|
|
/// you should set this property to <c>true</c>.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// <c>true</c> if the server is allowed to be bound to an address that is already in use;
|
|
|
|
/// <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>
|
|
|
|
/// </value>
|
|
|
|
public string RootPath {
|
|
|
|
public string RootPath {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
return _rootPath != null && _rootPath.Length > 0
|
|
|
|
return _rootPath != null && _rootPath.Length > 0 ? _rootPath : (_rootPath = "./Public");
|
|
|
|
? _rootPath
|
|
|
|
|
|
|
|
: (_rootPath = "./Public");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set {
|
|
|
|
set {
|
|
|
|
@ -339,8 +330,8 @@ namespace WebSocketSharp.Server
|
|
|
|
/// optionally the client for secure connection.
|
|
|
|
/// optionally the client for secure connection.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="ServerSslConfiguration"/> that represents the configuration used
|
|
|
|
/// A <see cref="ServerSslConfiguration"/> that represents the configuration used to
|
|
|
|
/// to authenticate the server and optionally the client for secure connection.
|
|
|
|
/// authenticate the server and optionally the client for secure connection.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public ServerSslConfiguration SslConfiguration {
|
|
|
|
public ServerSslConfiguration SslConfiguration {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
@ -363,9 +354,9 @@ namespace WebSocketSharp.Server
|
|
|
|
/// authenticate a client.
|
|
|
|
/// authenticate a client.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// A Func<<see cref="IIdentity"/>, <see cref="NetworkCredential"/>> delegate that
|
|
|
|
/// 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
|
|
|
|
/// references the method(s) used to find the credentials. The default value is a function that
|
|
|
|
/// that only returns <see langword="null"/>.
|
|
|
|
/// only returns <see langword="null"/>.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public Func<IIdentity, NetworkCredential> UserCredentialsFinder {
|
|
|
|
public Func<IIdentity, NetworkCredential> UserCredentialsFinder {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
@ -498,9 +489,24 @@ namespace WebSocketSharp.Server
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return !(usr || port)
|
|
|
|
return !(usr || port) ? "The secure connection requires a server certificate." : null;
|
|
|
|
? "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)
|
|
|
|
private void processRequest (HttpListenerContext context)
|
|
|
|
@ -567,7 +573,7 @@ namespace WebSocketSharp.Server
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (HttpListenerException ex) {
|
|
|
|
catch (HttpListenerException ex) {
|
|
|
|
_logger.Warn ("Receiving has been stopped.\nreason: " + ex.Message);
|
|
|
|
_logger.Warn ("Receiving has been stopped.\n reason: " + ex.Message);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
catch (Exception ex) {
|
|
|
|
@ -598,27 +604,6 @@ namespace WebSocketSharp.Server
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Adds a WebSocket service with the specified behavior and <paramref name="path"/>.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
|
|
/// This method converts <paramref name="path"/> to URL-decoded string,
|
|
|
|
|
|
|
|
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
|
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
|
|
/// <param name="path">
|
|
|
|
|
|
|
|
/// A <see cref="string"/> that represents the absolute path to the service to add.
|
|
|
|
|
|
|
|
/// </param>
|
|
|
|
|
|
|
|
/// <typeparam name="TBehaviorWithNew">
|
|
|
|
|
|
|
|
/// The type of the behavior of the service to add. The TBehaviorWithNew must inherit
|
|
|
|
|
|
|
|
/// the <see cref="WebSocketBehavior"/> class, and must have a public parameterless
|
|
|
|
|
|
|
|
/// constructor.
|
|
|
|
|
|
|
|
/// </typeparam>
|
|
|
|
|
|
|
|
public void AddWebSocketService<TBehaviorWithNew> (string path)
|
|
|
|
|
|
|
|
where TBehaviorWithNew : WebSocketBehavior, new ()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Adds the WebSocket service with the specified behavior, <paramref name="path"/>,
|
|
|
|
/// Adds the WebSocket service with the specified behavior, <paramref name="path"/>,
|
|
|
|
/// and <paramref name="initializer"/>.
|
|
|
|
/// and <paramref name="initializer"/>.
|
|
|
|
@ -637,9 +622,9 @@ namespace WebSocketSharp.Server
|
|
|
|
/// A <see cref="string"/> that represents the absolute path to the service to add.
|
|
|
|
/// A <see cref="string"/> that represents the absolute path to the service to add.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="initializer">
|
|
|
|
/// <param name="initializer">
|
|
|
|
/// A Func<T> delegate that references the method used to initialize a new specified
|
|
|
|
/// A <c>Func<T></c> delegate that references the method used to initialize
|
|
|
|
/// typed <see cref="WebSocketBehavior"/> instance (a new <see cref="IWebSocketSession"/>
|
|
|
|
/// a new specified typed <see cref="WebSocketBehavior"/> instance (a new
|
|
|
|
/// instance).
|
|
|
|
/// <see cref="IWebSocketSession"/> instance).
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <typeparam name="TBehavior">
|
|
|
|
/// <typeparam name="TBehavior">
|
|
|
|
/// The type of the behavior of the service to add. The TBehavior must inherit
|
|
|
|
/// The type of the behavior of the service to add. The TBehavior must inherit
|
|
|
|
@ -659,6 +644,27 @@ namespace WebSocketSharp.Server
|
|
|
|
_services.Add<TBehavior> (path, initializer);
|
|
|
|
_services.Add<TBehavior> (path, initializer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Adds a WebSocket service with the specified behavior and <paramref name="path"/>.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
|
|
/// This method converts <paramref name="path"/> to URL-decoded string,
|
|
|
|
|
|
|
|
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
|
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
|
|
/// <param name="path">
|
|
|
|
|
|
|
|
/// A <see cref="string"/> that represents the absolute path to the service to add.
|
|
|
|
|
|
|
|
/// </param>
|
|
|
|
|
|
|
|
/// <typeparam name="TBehaviorWithNew">
|
|
|
|
|
|
|
|
/// The type of the behavior of the service to add. The TBehaviorWithNew must inherit
|
|
|
|
|
|
|
|
/// the <see cref="WebSocketBehavior"/> class, and must have a public parameterless
|
|
|
|
|
|
|
|
/// constructor.
|
|
|
|
|
|
|
|
/// </typeparam>
|
|
|
|
|
|
|
|
public void AddWebSocketService<TBehaviorWithNew> (string path)
|
|
|
|
|
|
|
|
where TBehaviorWithNew : WebSocketBehavior, new ()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the contents of the file with the specified <paramref name="path"/>.
|
|
|
|
/// Gets the contents of the file with the specified <paramref name="path"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
@ -675,9 +681,7 @@ namespace WebSocketSharp.Server
|
|
|
|
if (_windows)
|
|
|
|
if (_windows)
|
|
|
|
path = path.Replace ("/", "\\");
|
|
|
|
path = path.Replace ("/", "\\");
|
|
|
|
|
|
|
|
|
|
|
|
return File.Exists (path)
|
|
|
|
return File.Exists (path) ? File.ReadAllBytes (path) : null;
|
|
|
|
? File.ReadAllBytes (path)
|
|
|
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
@ -784,8 +788,8 @@ namespace WebSocketSharp.Server
|
|
|
|
/// <see cref="string"/> used to stop the WebSocket services.
|
|
|
|
/// <see cref="string"/> used to stop the WebSocket services.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="code">
|
|
|
|
/// <param name="code">
|
|
|
|
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
|
|
|
|
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
|
|
|
|
/// indicating the reason for the stop.
|
|
|
|
/// the reason for the stop.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="reason">
|
|
|
|
/// <param name="reason">
|
|
|
|
/// A <see cref="string"/> that represents the reason for the stop.
|
|
|
|
/// A <see cref="string"/> that represents the reason for the stop.
|
|
|
|
|