[Modify] Polish it

It's same as the HttpListener class.
This commit is contained in:
sta 2016-04-25 15:44:55 +09:00
parent bb9dcb9bbc
commit b6e0a74f25

View File

@ -63,6 +63,7 @@ namespace WebSocketSharp.Server
private System.Net.IPAddress _address; private System.Net.IPAddress _address;
private AuthenticationSchemes _authSchemes; private AuthenticationSchemes _authSchemes;
private Func<IIdentity, NetworkCredential> _credFinder; private Func<IIdentity, NetworkCredential> _credFinder;
private static readonly string _defaultRealm;
private bool _dnsStyle; private bool _dnsStyle;
private string _hostname; private string _hostname;
private TcpListener _listener; private TcpListener _listener;
@ -79,6 +80,15 @@ namespace WebSocketSharp.Server
#endregion #endregion
#region Static Constructor
static WebSocketServer ()
{
_defaultRealm = "SECRET AREA";
}
#endregion
#region Public Constructors #region Public Constructors
/// <summary> /// <summary>
@ -393,13 +403,17 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// 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>
/// <remarks>
/// If this property is <see langword="null"/> or empty, <c>"SECRET AREA"</c> will be used as
/// the name of the realm.
/// </remarks>
/// <value> /// <value>
/// A <see cref="string"/> that represents the name of the realm. The default value is /// A <see cref="string"/> that represents the name of the realm. The default value is
/// <c>"SECRET AREA"</c>. /// <see langword="null"/>.
/// </value> /// </value>
public string Realm { public string Realm {
get { get {
return _realm ?? (_realm = "SECRET AREA"); return _realm;
} }
set { set {
@ -592,6 +606,12 @@ namespace WebSocketSharp.Server
: null; : null;
} }
private string getRealm ()
{
var realm = _realm;
return realm != null && realm.Length > 0 ? realm : _defaultRealm;
}
private void init (string hostname, System.Net.IPAddress address, int port, bool secure) private void init (string hostname, System.Net.IPAddress address, int port, bool secure)
{ {
_hostname = hostname ?? address.ToString (); _hostname = hostname ?? address.ToString ();
@ -642,7 +662,7 @@ namespace WebSocketSharp.Server
try { try {
var ctx = cl.GetWebSocketContext (null, _secure, _sslConfig, _logger); var ctx = cl.GetWebSocketContext (null, _secure, _sslConfig, _logger);
if (_authSchemes != AuthenticationSchemes.Anonymous && if (_authSchemes != AuthenticationSchemes.Anonymous &&
!authenticate (ctx, _authSchemes, Realm, UserCredentialsFinder)) !authenticate (ctx, _authSchemes, getRealm (), UserCredentialsFinder))
return; return;
processRequest (ctx); processRequest (ctx);