websocket-sharp/websocket-sharp/Net/EndPointManager.cs

268 lines
6.4 KiB
C#
Raw Normal View History

2014-04-10 13:42:51 +08:00
#region License
/*
* EndPointManager.cs
*
* 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)
2016-05-06 16:06:39 +08:00
* Copyright (c) 2012-2016 sta.blockhead
2014-04-10 13:42:51 +08:00
*
* 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;
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
2016-06-26 15:16:32 +08:00
private static readonly Dictionary<IPEndPoint, EndPointListener> _endpoints;
2014-10-15 12:15:30 +08:00
#endregion
#region Static Constructor
static EndPointManager ()
{
2016-06-26 15:16:32 +08:00
_endpoints = new Dictionary<IPEndPoint, EndPointListener> ();
2014-10-15 12:15:30 +08:00
}
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 listener)
2014-04-10 13:42:51 +08:00
{
var pref = new HttpListenerPrefix (uriPrefix);
2016-05-31 14:31:57 +08:00
var addr = convertToIPAddress (pref.Host);
2020-09-28 20:58:35 +08:00
2020-09-29 21:05:39 +08:00
if (addr == null) {
var msg = "The URI prefix includes an invalid host.";
throw new HttpListenerException (87, msg);
}
2017-12-18 15:28:57 +08:00
2020-09-30 18:38:31 +08:00
if (!addr.IsLocal ()) {
var msg = "The URI prefix includes an invalid host.";
throw new HttpListenerException (87, msg);
}
2016-05-31 14:31:57 +08:00
2016-06-02 15:43:14 +08:00
int port;
2020-09-28 20:58:35 +08:00
2020-09-30 18:41:47 +08:00
if (!Int32.TryParse (pref.Port, out port)) {
var msg = "The URI prefix includes an invalid port.";
throw new HttpListenerException (87, msg);
}
2016-06-02 15:43:14 +08:00
2020-09-30 18:44:57 +08:00
if (!port.IsPortNumber ()) {
var msg = "The URI prefix includes an invalid port.";
throw new HttpListenerException (87, msg);
}
2016-05-31 14:31:57 +08:00
var path = pref.Path;
2020-09-28 20:58:35 +08:00
2020-10-01 18:36:19 +08:00
if (path.IndexOf ('%') != -1) {
var msg = "The URI prefix includes an invalid path.";
throw new HttpListenerException (87, msg);
}
2014-04-10 13:42:51 +08:00
2020-10-01 18:38:19 +08:00
if (path.IndexOf ("//", StringComparison.Ordinal) != -1) {
var msg = "The URI prefix includes an invalid path.";
throw new HttpListenerException (87, msg);
}
2014-04-10 13:42:51 +08:00
2016-06-27 13:58:59 +08:00
var endpoint = new IPEndPoint (addr, port);
2016-06-16 13:56:39 +08:00
EndPointListener lsnr;
2020-09-28 20:58:35 +08:00
2016-06-27 13:58:59 +08:00
if (_endpoints.TryGetValue (endpoint, out lsnr)) {
2016-06-17 15:36:40 +08:00
if (lsnr.IsSecure ^ pref.IsSecure)
throw new HttpListenerException (87, "Includes an invalid scheme.");
2016-06-16 13:56:39 +08:00
}
else {
2020-09-28 20:58:35 +08:00
lsnr = new EndPointListener (
endpoint,
pref.IsSecure,
listener.CertificateFolderPath,
listener.SslConfiguration,
listener.ReuseAddress
);
2016-06-16 13:56:39 +08:00
2016-06-27 13:58:59 +08:00
_endpoints.Add (endpoint, lsnr);
2016-06-16 13:56:39 +08:00
}
lsnr.AddPrefix (pref, listener);
2014-04-10 13:42:51 +08:00
}
private static IPAddress convertToIPAddress (string hostname)
{
2017-12-18 15:45:58 +08:00
if (hostname == "*")
return IPAddress.Any;
if (hostname == "+")
return IPAddress.Any;
return hostname.ToIPAddress ();
}
private static void removePrefix (string uriPrefix, HttpListener listener)
2014-04-10 13:42:51 +08:00
{
2020-09-26 20:14:48 +08:00
var pref = new HttpListenerPrefix (uriPrefix, listener);
2016-06-01 14:10:06 +08:00
var addr = convertToIPAddress (pref.Host);
2020-09-26 20:13:45 +08:00
2017-12-18 15:32:13 +08:00
if (addr == null)
return;
2016-06-01 14:10:06 +08:00
if (!addr.IsLocal ())
return;
2016-06-02 15:43:14 +08:00
int port;
2020-09-26 20:13:45 +08:00
2016-06-02 15:43:14 +08:00
if (!Int32.TryParse (pref.Port, out port))
return;
if (!port.IsPortNumber ())
return;
2016-06-01 14:10:06 +08:00
var path = pref.Path;
2020-09-26 20:13:45 +08:00
if (path.IndexOf ('%') != -1)
2014-04-10 13:42:51 +08:00
return;
if (path.IndexOf ("//", StringComparison.Ordinal) != -1)
2014-04-10 13:42:51 +08:00
return;
2016-06-27 13:58:59 +08:00
var endpoint = new IPEndPoint (addr, port);
2016-06-16 13:56:39 +08:00
EndPointListener lsnr;
2020-09-26 20:13:45 +08:00
2016-06-27 13:58:59 +08:00
if (!_endpoints.TryGetValue (endpoint, out lsnr))
2016-06-16 13:56:39 +08:00
return;
2016-06-17 15:36:40 +08:00
if (lsnr.IsSecure ^ pref.IsSecure)
return;
2020-09-25 20:04:16 +08:00
lsnr.RemovePrefix (pref);
}
#endregion
#region Internal Methods
2016-06-26 15:16:32 +08:00
internal static bool RemoveEndPoint (IPEndPoint endpoint)
{
lock (((ICollection) _endpoints).SyncRoot) {
EndPointListener lsnr;
2020-09-27 18:40:07 +08:00
2016-06-26 15:16:32 +08:00
if (!_endpoints.TryGetValue (endpoint, out lsnr))
return false;
_endpoints.Remove (endpoint);
lsnr.Close ();
return true;
}
}
2014-04-10 13:42:51 +08:00
#endregion
#region Public Methods
public static void AddListener (HttpListener listener)
2014-04-10 13:42:51 +08:00
{
var added = new List<string> ();
2020-09-27 18:38:28 +08:00
2016-06-27 13:58:59 +08:00
lock (((ICollection) _endpoints).SyncRoot) {
2014-04-10 13:42:51 +08:00
try {
foreach (var pref in listener.Prefixes) {
addPrefix (pref, listener);
2014-10-15 12:15:30 +08:00
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, listener);
2014-04-10 13:42:51 +08:00
throw;
}
}
}
public static void AddPrefix (string uriPrefix, HttpListener listener)
2014-04-10 13:42:51 +08:00
{
2016-06-27 13:58:59 +08:00
lock (((ICollection) _endpoints).SyncRoot)
addPrefix (uriPrefix, listener);
2014-04-10 13:42:51 +08:00
}
public static void RemoveListener (HttpListener listener)
2014-04-10 13:42:51 +08:00
{
2016-06-27 13:58:59 +08:00
lock (((ICollection) _endpoints).SyncRoot) {
foreach (var pref in listener.Prefixes)
removePrefix (pref, listener);
2016-05-05 15:39:11 +08:00
}
2014-04-10 13:42:51 +08:00
}
public static void RemovePrefix (string uriPrefix, HttpListener listener)
2014-04-10 13:42:51 +08:00
{
2016-06-27 13:58:59 +08:00
lock (((ICollection) _endpoints).SyncRoot)
removePrefix (uriPrefix, listener);
2014-04-10 13:42:51 +08:00
}
#endregion
}
2012-09-10 00:36:22 +08:00
}