2014-04-10 13:42:51 +08:00
|
|
|
#region License
|
|
|
|
/*
|
|
|
|
* EndPointManager.cs
|
|
|
|
*
|
2015-02-20 15:44:20 +08:00
|
|
|
* This code is derived from EndPointManager.cs (System.Net) of Mono
|
2014-04-10 13:42:51 +08:00
|
|
|
* (http://www.mono-project.com).
|
|
|
|
*
|
|
|
|
* The MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
|
|
|
|
* Copyright (c) 2012-2014 sta.blockhead
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Authors
|
|
|
|
/*
|
|
|
|
* Authors:
|
|
|
|
* - Gonzalo Paniagua Javier <gonzalo@ximian.com>
|
|
|
|
*/
|
|
|
|
#endregion
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2014-10-30 18:09:12 +08:00
|
|
|
#region Contributors
|
|
|
|
/*
|
|
|
|
* Contributors:
|
|
|
|
* - Liryna <liryna.stark@gmail.com>
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
2012-09-10 00:36:22 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net;
|
|
|
|
|
2013-08-27 21:02:50 +08:00
|
|
|
namespace WebSocketSharp.Net
|
|
|
|
{
|
2014-04-10 13:42:51 +08:00
|
|
|
internal sealed class EndPointManager
|
|
|
|
{
|
2014-10-15 12:15:30 +08:00
|
|
|
#region Private Fields
|
2014-04-10 13:42:51 +08:00
|
|
|
|
2014-10-15 12:15:30 +08:00
|
|
|
private static Dictionary<IPAddress, Dictionary<int, EndPointListener>> _ipToEndpoints;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Static Constructor
|
|
|
|
|
|
|
|
static EndPointManager ()
|
|
|
|
{
|
|
|
|
_ipToEndpoints = new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
|
|
|
|
}
|
2014-04-10 13:42:51 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Private Constructors
|
|
|
|
|
|
|
|
private EndPointManager ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
|
|
|
|
private static void addPrefix (string uriPrefix, HttpListener httpListener)
|
|
|
|
{
|
2014-10-23 09:41:06 +08:00
|
|
|
var pref = new HttpListenerPrefix (uriPrefix);
|
2014-10-15 12:15:30 +08:00
|
|
|
if (pref.Path.IndexOf ('%') != -1)
|
2014-05-01 12:27:29 +08:00
|
|
|
throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
|
2014-04-10 13:42:51 +08:00
|
|
|
|
2014-10-15 12:15:30 +08:00
|
|
|
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
2014-04-10 13:42:51 +08:00
|
|
|
throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
|
|
|
|
|
2015-02-18 16:16:42 +08:00
|
|
|
// Listens on all the interfaces if host name cannot be parsed by IPAddress.
|
2015-02-17 16:32:31 +08:00
|
|
|
var epl = getEndPointListener (pref.Host, pref.Port, httpListener, pref.IsSecure);
|
2014-10-15 12:15:30 +08:00
|
|
|
epl.AddPrefix (pref, httpListener);
|
2014-04-10 13:42:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static EndPointListener getEndPointListener (
|
2015-02-16 15:41:58 +08:00
|
|
|
string host, int port, HttpListener httpListener, bool secure)
|
|
|
|
{
|
|
|
|
IPAddress addr;
|
|
|
|
if (host == "*") {
|
|
|
|
addr = IPAddress.Any;
|
|
|
|
}
|
|
|
|
else if (!IPAddress.TryParse (host, out addr)) {
|
|
|
|
try {
|
|
|
|
var iphost = Dns.GetHostEntry (host);
|
2015-02-18 16:16:42 +08:00
|
|
|
addr = iphost != null ? iphost.AddressList[0] : IPAddress.Any;
|
2015-02-16 15:41:58 +08:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
addr = IPAddress.Any;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary<int, EndPointListener> eps = null;
|
|
|
|
if (_ipToEndpoints.ContainsKey (addr)) {
|
|
|
|
eps = _ipToEndpoints[addr];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eps = new Dictionary<int, EndPointListener> ();
|
|
|
|
_ipToEndpoints[addr] = eps;
|
|
|
|
}
|
|
|
|
|
|
|
|
EndPointListener epl = null;
|
|
|
|
if (eps.ContainsKey (port)) {
|
|
|
|
epl = eps[port];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
epl = new EndPointListener (
|
|
|
|
addr,
|
|
|
|
port,
|
|
|
|
secure,
|
|
|
|
httpListener.CertificateFolderPath,
|
|
|
|
httpListener.SslConfiguration,
|
2014-10-20 15:00:57 +08:00
|
|
|
httpListener.ReuseAddress);
|
2014-04-10 13:42:51 +08:00
|
|
|
|
2014-10-15 12:15:30 +08:00
|
|
|
eps[port] = epl;
|
2014-04-10 13:42:51 +08:00
|
|
|
}
|
|
|
|
|
2014-10-15 12:15:30 +08:00
|
|
|
return epl;
|
2014-04-10 13:42:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void removePrefix (string uriPrefix, HttpListener httpListener)
|
|
|
|
{
|
2014-10-23 09:41:06 +08:00
|
|
|
var pref = new HttpListenerPrefix (uriPrefix);
|
2014-10-15 12:15:30 +08:00
|
|
|
if (pref.Path.IndexOf ('%') != -1)
|
2014-04-10 13:42:51 +08:00
|
|
|
return;
|
|
|
|
|
2014-10-15 12:15:30 +08:00
|
|
|
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
2014-04-10 13:42:51 +08:00
|
|
|
return;
|
|
|
|
|
2015-02-17 16:32:31 +08:00
|
|
|
var epl = getEndPointListener (pref.Host, pref.Port, httpListener, pref.IsSecure);
|
2014-10-15 12:15:30 +08:00
|
|
|
epl.RemovePrefix (pref, httpListener);
|
2014-04-10 13:42:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
public static void AddListener (HttpListener httpListener)
|
|
|
|
{
|
|
|
|
var added = new List<string> ();
|
|
|
|
lock (((ICollection) _ipToEndpoints).SyncRoot) {
|
|
|
|
try {
|
2014-10-15 12:15:30 +08:00
|
|
|
foreach (var pref in httpListener.Prefixes) {
|
|
|
|
addPrefix (pref, httpListener);
|
|
|
|
added.Add (pref);
|
2014-04-10 13:42:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
2014-10-15 12:15:30 +08:00
|
|
|
foreach (var pref in added)
|
|
|
|
removePrefix (pref, httpListener);
|
2014-04-10 13:42:51 +08:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void AddPrefix (string uriPrefix, HttpListener httpListener)
|
|
|
|
{
|
2014-05-01 12:27:29 +08:00
|
|
|
lock (((ICollection) _ipToEndpoints).SyncRoot)
|
2014-04-10 13:42:51 +08:00
|
|
|
addPrefix (uriPrefix, httpListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint)
|
|
|
|
{
|
|
|
|
lock (((ICollection) _ipToEndpoints).SyncRoot) {
|
2014-10-15 12:15:30 +08:00
|
|
|
var eps = _ipToEndpoints[endpoint.Address];
|
|
|
|
eps.Remove (endpoint.Port);
|
|
|
|
if (eps.Count == 0)
|
2014-04-10 13:42:51 +08:00
|
|
|
_ipToEndpoints.Remove (endpoint.Address);
|
|
|
|
|
|
|
|
epListener.Close ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RemoveListener (HttpListener httpListener)
|
|
|
|
{
|
2014-05-01 12:27:29 +08:00
|
|
|
lock (((ICollection) _ipToEndpoints).SyncRoot)
|
2014-10-15 12:15:30 +08:00
|
|
|
foreach (var pref in httpListener.Prefixes)
|
|
|
|
removePrefix (pref, httpListener);
|
2014-04-10 13:42:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void RemovePrefix (string uriPrefix, HttpListener httpListener)
|
|
|
|
{
|
2014-05-01 12:27:29 +08:00
|
|
|
lock (((ICollection) _ipToEndpoints).SyncRoot)
|
2014-04-10 13:42:51 +08:00
|
|
|
removePrefix (uriPrefix, httpListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|