Refactored HttpServer.cs and WebSocketServer.cs

This commit is contained in:
sta 2014-09-01 14:27:04 +09:00
parent e15cc63036
commit d9528ffe75
2 changed files with 41 additions and 40 deletions

View File

@ -51,7 +51,7 @@ 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.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The HttpServer class can provide the multi WebSocket services. /// The HttpServer class can provide multiple WebSocket services.
/// </remarks> /// </remarks>
public class HttpServer public class HttpServer
{ {
@ -480,12 +480,12 @@ namespace WebSocketSharp.Server
? OnPatch ? OnPatch
: null; : null;
if (evt != null) { if (evt != null)
evt (this, new HttpRequestEventArgs (context)); 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) private void processRequestAsync (HttpListenerContext context)
@ -504,7 +504,6 @@ namespace WebSocketSharp.Server
} }
processHttpRequest (context); processHttpRequest (context);
context.Response.Close ();
} }
catch (Exception ex) { catch (Exception ex) {
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());

View File

@ -52,7 +52,7 @@ namespace WebSocketSharp.Server
/// Provides a WebSocket protocol server. /// Provides a WebSocket protocol server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The WebSocketServer class provides the multi WebSocket service. /// The WebSocketServer class can provide multiple WebSocket services.
/// </remarks> /// </remarks>
public class WebSocketServer public class WebSocketServer
{ {
@ -86,7 +86,7 @@ namespace WebSocketSharp.Server
/// on port 80. /// on port 80.
/// </remarks> /// </remarks>
public WebSocketServer () public WebSocketServer ()
: this (80) : this (System.Net.IPAddress.Any, 80, false)
{ {
} }
@ -110,7 +110,7 @@ namespace WebSocketSharp.Server
/// <paramref name="port"/> isn't between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535.
/// </exception> /// </exception>
public WebSocketServer (int port) 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 { set {
if (!canSet ("AuthenticationSchemes")) var msg = _state.CheckIfStartable ();
if (msg != null) {
_logger.Error (msg);
return; return;
}
_authSchemes = value; _authSchemes = value;
} }
@ -322,8 +325,11 @@ namespace WebSocketSharp.Server
} }
set { set {
if (!canSet ("Certificate")) var msg = _state.CheckIfStartable ();
if (msg != null) {
_logger.Error (msg);
return; return;
}
_certificate = value; _certificate = value;
} }
@ -358,8 +364,8 @@ namespace WebSocketSharp.Server
/// periodically. /// periodically.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the server cleans up the inactive sessions every 60 seconds; otherwise, /// <c>true</c> if the server cleans up the inactive sessions every 60 seconds;
/// <c>false</c>. The default value is <c>true</c>. /// otherwise, <c>false</c>. The default value is <c>true</c>.
/// </value> /// </value>
public bool KeepClean { public bool KeepClean {
get { get {
@ -413,8 +419,11 @@ namespace WebSocketSharp.Server
} }
set { set {
if (!canSet ("Realm")) var msg = _state.CheckIfStartable ();
if (msg != null) {
_logger.Error (msg);
return; return;
}
_realm = value; _realm = value;
} }
@ -438,8 +447,11 @@ namespace WebSocketSharp.Server
} }
set { set {
if (!canSet ("ReuseAddress")) var msg = _state.CheckIfStartable ();
if (msg != null) {
_logger.Error (msg);
return; return;
}
if (value ^ _reuseAddress) { if (value ^ _reuseAddress) {
_listener.Server.SetSocketOption ( _listener.Server.SetSocketOption (
@ -465,8 +477,11 @@ namespace WebSocketSharp.Server
} }
set { set {
if (!canSet ("UserCredentialsFinder")) var msg = _state.CheckIfStartable ();
if (msg != null) {
_logger.Error (msg);
return; return;
}
_credentialsFinder = value; _credentialsFinder = value;
} }
@ -537,30 +552,17 @@ namespace WebSocketSharp.Server
} }
context.SetUser (scheme, realm, credFinder); context.SetUser (scheme, realm, credFinder);
if (context.IsAuthenticated) if (!context.IsAuthenticated) {
return true; context.SendAuthenticationChallenge (chal);
return auth ();
}
context.SendAuthenticationChallenge (chal); return true;
return auth ();
}; };
return auth (); 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 () private string checkIfCertificateExists ()
{ {
return _secure && _certificate == null return _secure && _certificate == null
@ -703,8 +705,8 @@ namespace WebSocketSharp.Server
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// This method converts <paramref name="path"/> to URL-decoded string and removes <c>'/'</c> /// This method converts <paramref name="path"/> to URL-decoded string and
/// from tail end of <paramref name="path"/>. /// removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </para> /// </para>
/// <para> /// <para>
/// <paramref name="initializer"/> returns an initialized specified typed /// <paramref name="initializer"/> returns an initialized specified typed
@ -745,12 +747,12 @@ namespace WebSocketSharp.Server
/// Removes the WebSocket service with the specified <paramref name="path"/>. /// Removes the WebSocket service with the specified <paramref name="path"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method converts <paramref name="path"/> to URL-decoded string and removes <c>'/'</c> /// This method converts <paramref name="path"/> to URL-decoded string and
/// from tail end of <paramref name="path"/>. /// removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// <c>true</c> if the WebSocket service is successfully found and removed; otherwise, /// <c>true</c> if the WebSocket service is successfully found and removed;
/// <c>false</c>. /// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket service to find.