public void AddPrefix(HttpListenerPrefix prefix, HttpListener listener)
        {
            List <HttpListenerPrefix> current, future;

            if (prefix.Host == "*")
            {
                do
                {
                    current = _unhandled;
                    future  = current != null
                   ? new List <HttpListenerPrefix> (current)
                   : new List <HttpListenerPrefix> ();

                    prefix.Listener = listener;
                    addSpecial(future, prefix);
                }while (Interlocked.CompareExchange(ref _unhandled, future, current) != current);

                return;
            }

            if (prefix.Host == "+")
            {
                do
                {
                    current = _all;
                    future  = current != null
                   ? new List <HttpListenerPrefix> (current)
                   : new List <HttpListenerPrefix> ();

                    prefix.Listener = listener;
                    addSpecial(future, prefix);
                }while (Interlocked.CompareExchange(ref _all, future, current) != current);

                return;
            }

            Dictionary <HttpListenerPrefix, HttpListener> prefs, prefs2;

            do
            {
                prefs = _prefixes;
                if (prefs.ContainsKey(prefix))
                {
                    if (prefs[prefix] != listener)
                    {
                        throw new HttpListenerException(
                                  87, String.Format("There's another listener for {0}.", prefix)
                                  );
                    }

                    return;
                }

                prefs2         = new Dictionary <HttpListenerPrefix, HttpListener> (prefs);
                prefs2[prefix] = listener;
            }while (Interlocked.CompareExchange(ref _prefixes, prefs2, prefs) != prefs);
        }
Esempio n. 2
0
        private static void removePrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (addr == null)
            {
                return;
            }

            if (!addr.IsLocal())
            {
                return;
            }

            int port;

            if (!Int32.TryParse(pref.Port, out port))
            {
                return;
            }

            if (!port.IsPortNumber())
            {
                return;
            }

            var path = pref.Path;

            if (path.IndexOf('%') != -1)
            {
                return;
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;

            if (!_endpoints.TryGetValue(endpoint, out lsnr))
            {
                return;
            }

            if (lsnr.IsSecure ^ pref.IsSecure)
            {
                return;
            }

            lsnr.RemovePrefix(pref, listener);
        }
        private static void addSpecial(List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            var path = prefix.Path;

            foreach (var pref in prefixes)
            {
                if (pref.Path == path)
                {
                    throw new HttpListenerException(87, "The prefix is already in use.");
                }
            }

            prefixes.Add(prefix);
        }
        /// <summary>
        /// Adds the specified <paramref name="uriPrefix"/> to the collection.
        /// </summary>
        /// <param name="uriPrefix">
        /// A <see cref="string"/> that represents the URI prefix to add. The prefix must be
        /// a well-formed URI prefix with http or https scheme, and must end with a <c>'/'</c>.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="uriPrefix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="uriPrefix"/> is invalid.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The <see cref="HttpListener"/> associated with this collection is closed.
        /// </exception>
        public void Add(string uriPrefix)
        {
            _listener.CheckDisposed();
            HttpListenerPrefix.CheckPrefix(uriPrefix);
            if (_prefixes.Contains(uriPrefix))
            {
                return;
            }

            _prefixes.Add(uriPrefix);
            if (_listener.IsListening)
            {
                EndPointManager.AddPrefix(uriPrefix, _listener);
            }
        }
        private static bool removeSpecial(List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            var path = prefix.Path;
            var cnt  = prefixes.Count;

            for (var i = 0; i < cnt; i++)
            {
                if (prefixes[i].Path == path)
                {
                    prefixes.RemoveAt(i);
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 6
0
        private static void addPrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (addr == null)
            {
                throw new HttpListenerException(87, "Includes an invalid 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);
        }
        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();
        }