Refactored a few for EndPointManager.cs

This commit is contained in:
sta 2015-08-29 16:37:52 +09:00
parent efb922a67a
commit 653064223b

View File

@ -55,7 +55,8 @@ namespace WebSocketSharp.Net
{ {
#region Private Fields #region Private Fields
private static Dictionary<IPAddress, Dictionary<int, EndPointListener>> _addressToEndpoints; private static readonly Dictionary<IPAddress, Dictionary<int, EndPointListener>>
_addressToEndpoints;
#endregion #endregion
@ -81,18 +82,19 @@ namespace WebSocketSharp.Net
private static void addPrefix (string uriPrefix, HttpListener listener) private static void addPrefix (string uriPrefix, HttpListener listener)
{ {
var pref = new HttpListenerPrefix (uriPrefix); var pref = new HttpListenerPrefix (uriPrefix);
if (pref.Path.IndexOf ('%') != -1)
var path = pref.Path;
if (path.IndexOf ('%') != -1)
throw new HttpListenerException (400, "Invalid path."); // TODO: Code? throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1) if (path.IndexOf ("//", StringComparison.Ordinal) != -1)
throw new HttpListenerException (400, "Invalid path."); // TODO: Code? throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
// Listens on all the interfaces if host name cannot be parsed by IPAddress. // Listens on all the interfaces if host name cannot be parsed by IPAddress.
var lsnr = getEndPointListener (pref.Host, pref.Port, listener, pref.IsSecure); getEndPointListener (pref, listener).AddPrefix (pref, listener);
lsnr.AddPrefix (pref, listener);
} }
private static IPAddress convertToAddress (string hostname) private static IPAddress convertToIPAddress (string hostname)
{ {
if (hostname == "*" || hostname == "+") if (hostname == "*" || hostname == "+")
return IPAddress.Any; return IPAddress.Any;
@ -111,9 +113,9 @@ namespace WebSocketSharp.Net
} }
private static EndPointListener getEndPointListener ( private static EndPointListener getEndPointListener (
string host, int port, HttpListener listener, bool secure) HttpListenerPrefix prefix, HttpListener listener)
{ {
var addr = convertToAddress (host); var addr = convertToIPAddress (prefix.Host);
Dictionary<int, EndPointListener> eps = null; Dictionary<int, EndPointListener> eps = null;
if (_addressToEndpoints.ContainsKey (addr)) { if (_addressToEndpoints.ContainsKey (addr)) {
@ -124,6 +126,8 @@ namespace WebSocketSharp.Net
_addressToEndpoints[addr] = eps; _addressToEndpoints[addr] = eps;
} }
var port = prefix.Port;
EndPointListener lsnr = null; EndPointListener lsnr = null;
if (eps.ContainsKey (port)) { if (eps.ContainsKey (port)) {
lsnr = eps[port]; lsnr = eps[port];
@ -133,7 +137,7 @@ namespace WebSocketSharp.Net
addr, addr,
port, port,
listener.ReuseAddress, listener.ReuseAddress,
secure, prefix.IsSecure,
listener.CertificateFolderPath, listener.CertificateFolderPath,
listener.SslConfiguration); listener.SslConfiguration);
@ -146,14 +150,15 @@ namespace WebSocketSharp.Net
private static void removePrefix (string uriPrefix, HttpListener listener) private static void removePrefix (string uriPrefix, HttpListener listener)
{ {
var pref = new HttpListenerPrefix (uriPrefix); var pref = new HttpListenerPrefix (uriPrefix);
if (pref.Path.IndexOf ('%') != -1)
var path = pref.Path;
if (path.IndexOf ('%') != -1)
return; return;
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1) if (path.IndexOf ("//", StringComparison.Ordinal) != -1)
return; return;
var lsnr = getEndPointListener (pref.Host, pref.Port, listener, pref.IsSecure); getEndPointListener (pref, listener).RemovePrefix (pref, listener);
lsnr.RemovePrefix (pref, listener);
} }
#endregion #endregion