Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
 public static void RemovePrefix(string uriPrefix, HttpListener listener)
 {
     lock (((ICollection)_endpoints).SyncRoot)
         removePrefix(uriPrefix, listener);
 }