Ejemplo n.º 1
0
        public void RemovePrefix(HttpListenerPrefix prefix, HttpListener listener)
        {
            List <HttpListenerPrefix> current, future;

            if (prefix.Host == "*")
            {
                do
                {
                    current = _unhandled;
                    if (current == null)
                    {
                        break;
                    }

                    future = new List <HttpListenerPrefix> (current);
                    if (!removeSpecial(future, prefix))
                    {
                        break; // The prefix wasn't found.
                    }
                }while (Interlocked.CompareExchange(ref _unhandled, future, current) != current);

                leaveIfNoPrefix();
                return;
            }

            if (prefix.Host == "+")
            {
                do
                {
                    current = _all;
                    if (current == null)
                    {
                        break;
                    }

                    future = new List <HttpListenerPrefix> (current);
                    if (!removeSpecial(future, prefix))
                    {
                        break; // The prefix wasn't found.
                    }
                }while (Interlocked.CompareExchange(ref _all, future, current) != current);

                leaveIfNoPrefix();
                return;
            }

            Dictionary <HttpListenerPrefix, HttpListener> prefs, prefs2;

            do
            {
                prefs = _prefixes;
                if (!prefs.ContainsKey(prefix))
                {
                    break;
                }

                prefs2 = new Dictionary <HttpListenerPrefix, HttpListener> (prefs);
                prefs2.Remove(prefix);
            }while (Interlocked.CompareExchange(ref _prefixes, prefs2, prefs) != prefs);

            leaveIfNoPrefix();
        }
Ejemplo n.º 2
0
        internal bool TrySearchHttpListener(Uri uri, out HttpListener listener)
        {
            listener = null;

            if (uri == null)
            {
                return(false);
            }

            var host      = uri.Host;
            var dns       = Uri.CheckHostName(host) == UriHostNameType.Dns;
            var port      = uri.Port.ToString();
            var path      = HttpUtility.UrlDecode(uri.AbsolutePath);
            var pathSlash = path[path.Length - 1] != '/' ? path + "/" : path;

            if (host != null && host.Length > 0)
            {
                var bestLen = -1;
                foreach (var pref in _prefixes.Keys)
                {
                    if (dns)
                    {
                        var prefHost = pref.Host;
                        if (Uri.CheckHostName(prefHost) == UriHostNameType.Dns && prefHost != host)
                        {
                            continue;
                        }
                    }

                    if (pref.Port != port)
                    {
                        continue;
                    }

                    var prefPath = pref.Path;

                    var len = prefPath.Length;
                    if (len < bestLen)
                    {
                        continue;
                    }

                    if (path.StartsWith(prefPath) || pathSlash.StartsWith(prefPath))
                    {
                        bestLen  = len;
                        listener = _prefixes[pref];
                    }
                }

                if (bestLen != -1)
                {
                    return(true);
                }
            }

            var prefs = _unhandled;

            listener = searchHttpListenerFromSpecial(path, prefs);
            if (listener == null && pathSlash != path)
            {
                listener = searchHttpListenerFromSpecial(pathSlash, prefs);
            }

            if (listener != null)
            {
                return(true);
            }

            prefs    = _all;
            listener = searchHttpListenerFromSpecial(path, prefs);
            if (listener == null && pathSlash != path)
            {
                listener = searchHttpListenerFromSpecial(pathSlash, prefs);
            }

            return(listener != null);
        }
Ejemplo n.º 3
0
        private static void addPrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (!addr.IsLocal())
            {
                throw new HttpListenerException(87, "Includes an invalid host.");
            }

            int port;

            if (!Int32.TryParse(pref.Port, out port))
            {
                throw new HttpListenerException(87, "Includes an invalid port.");
            }

            if (!port.IsPortNumber())
            {
                throw new HttpListenerException(87, "Includes an invalid port.");
            }

            var path = pref.Path;

            if (path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;

            if (_endpoints.TryGetValue(endpoint, out lsnr))
            {
                if (lsnr.IsSecure ^ pref.IsSecure)
                {
                    throw new HttpListenerException(87, "Includes an invalid scheme.");
                }
            }
            else
            {
                lsnr =
                    new EndPointListener(
                        endpoint,
                        pref.IsSecure,
                        listener.CertificateFolderPath,
                        listener.SslConfiguration,
                        listener.ReuseAddress
                        );

                _endpoints.Add(endpoint, lsnr);
            }

            lsnr.AddPrefix(pref, listener);
        }
Ejemplo n.º 4
0
 internal HttpListenerPrefixCollection(HttpListener listener)
 {
     _listener = listener;
     _prefixes = new List <string> ();
 }
Ejemplo n.º 5
0
 public static void RemovePrefix(string uriPrefix, HttpListener listener)
 {
     lock (((ICollection)_endpoints).SyncRoot)
         removePrefix(uriPrefix, listener);
 }