static EndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;

            if (IPAddress.TryParse(host, out addr) == false)
            {
                addr = IPAddress.Any;
            }

            Hashtable p = null;              // Dictionary<int, EndPointListener>

            if (ip_to_endpoints.ContainsKey(addr))
            {
                p = (Hashtable)ip_to_endpoints [addr];
            }
            else
            {
                p = new Hashtable();
                ip_to_endpoints [addr] = p;
            }

            EndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p [port];
            }
            else
            {
                epl      = new EndPointListener(addr, port, secure);
                p [port] = epl;
            }

            return(epl);
        }
 public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
 {
     lock (ip_to_endpoints) {
         // Dictionary<int, EndPointListener> p
         Hashtable p = null;
         p = (Hashtable)ip_to_endpoints [ep.Address];
         p.Remove(ep.Port);
         if (p.Count == 0)
         {
             ip_to_endpoints.Remove(ep.Address);
         }
         epl.Close();
     }
 }
        static EndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;

            if (host == "*")
            {
                addr = IPAddress.Any;
            }
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try {
                    IPHostEntry iphost = System.Net.Dns.GetHostByName(host);
                    if (iphost != null)
                    {
                        addr = iphost.AddressList[0];
                    }
                    else
                    {
                        addr = IPAddress.Any;
                    }
                } catch {
                    addr = IPAddress.Any;
                }
            }
            Hashtable p = null;              // Dictionary<int, EndPointListener>

            if (ip_to_endpoints.ContainsKey(addr))
            {
                p = (Hashtable)ip_to_endpoints [addr];
            }
            else
            {
                p = new Hashtable();
                ip_to_endpoints [addr] = p;
            }

            EndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p [port];
            }
            else
            {
                epl      = new EndPointListener(addr, port, secure);
                p [port] = epl;
            }

            return(epl);
        }
        static void RemovePrefixInternal(string prefix, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(prefix);

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

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

            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.RemovePrefix(lp, listener);
        }
Пример #5
0
        static void OnAccept(object sender, EventArgs e)
        {
            SocketAsyncEventArgs args = (SocketAsyncEventArgs)e;
            EndPointListener     epl  = (EndPointListener)args.UserToken;
            Socket accepted           = null;

            if (args.SocketError == SocketError.Success)
            {
                accepted          = args.AcceptSocket;
                args.AcceptSocket = null;
            }

            try {
                if (epl.sock != null)
                {
                    epl.sock.AcceptAsync(args);
                }
            } catch {
                if (accepted != null)
                {
                    try {
                        accepted.Close();
                    } catch {}
                    accepted = null;
                }
            }

            if (accepted == null)
            {
                return;
            }

            if (epl.secure && (epl.cert == null || epl.key == null))
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert, epl.key);

            lock (epl.unregistered) {
                epl.unregistered [conn] = conn;
            }
            conn.BeginReadRequest();
        }
        static void AddPrefixInternal(string p, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)              // TODO: Code?
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            // listens on all the interfaces if host name cannot be parsed by IPAddress.
            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.AddPrefix(lp, listener);
        }
 public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
 {
     this.sock   = sock;
     this.epl    = epl;
     this.secure = secure;
     this.key    = key;
     if (secure == false)
     {
         stream = new NetworkStream(sock, false);
     }
     else
     {
         SslServerStream ssl_stream = new SslServerStream(new NetworkStream(sock, false), cert, false, true, false);
         ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
         ssl_stream.ClientCertValidationDelegate    += OnClientCertificateValidation;
         stream = ssl_stream;
     }
     timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
     Init();
 }