コード例 #1
0
ファイル: HttpConnection.cs プロジェクト: mikem8361/runtime
        public HttpConnection(Socket sock, HttpEndPointListener epl, bool secure, X509Certificate cert)
        {
            _socket = sock;
            _epl    = epl;
            _secure = secure;
            _cert   = cert;
            if (secure == false)
            {
                _stream = new NetworkStream(sock, false);
            }
            else
            {
#pragma warning disable CA5359
                _sslStream = HttpListener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) =>
                {
                    if (c == null)
                    {
                        return(true);
                    }

                    _clientCert       = c as X509Certificate2 ?? new X509Certificate2(c.GetRawCertData());
                    _clientCertErrors = new int[] { (int)e };
                    return(true);
                });
#pragma warning restore CA5359

                _stream = _sslStream;
            }

            _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            _sslStream?.AuthenticateAsServer(_cert, true, (SslProtocols)ServicePointManager.SecurityProtocol, false);
            Init();
        }
コード例 #2
0
        private static void ProcessAccept(SocketAsyncEventArgs args)
        {
            Socket accepted = null;

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

            HttpEndPointListener epl = (HttpEndPointListener)args.UserToken;

            Accept(epl._socket, args, ref accepted);
            if (accepted == null)
            {
                return;
            }

            if (epl._secure && epl._cert == null)
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl._secure, epl._cert);

            lock (epl._unregisteredConnections)
            {
                epl._unregisteredConnections[conn] = conn;
            }
            conn.BeginReadRequest();
        }
コード例 #3
0
ファイル: HttpEndPointManager.cs プロジェクト: jnm2/corefx
        private static void AddPrefixInternal(string p, HttpListener listener)
        {
            int start = p.IndexOf(':') + 3;
            int colon = p.IndexOf(':', start);

            if (colon != -1)
            {
                // root can't be -1 here, since we've already checked for ending '/' in ListenerPrefix.
                int    root       = p.IndexOf('/', colon, p.Length - colon);
                string portString = p.Substring(colon + 1, root - colon - 1);

                int port;
                if (!int.TryParse(portString, out port) || port <= 0 || port >= 65536)
                {
                    throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_port);
                }
            }

            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path);
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path);
            }

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

            epl.AddPrefix(lp, listener);
        }
コード例 #4
0
ファイル: HttpEndPointManager.cs プロジェクト: blqw/corefx
        private static HttpEndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;

            if (host == "*")
            {
                addr = IPAddress.Any;
            }
            else
            {
                try
                {
                    addr = Dns.GetHostAddresses(host)[0];
                    if (IPAddress.Any.Equals(addr))
                    {
                        // Don't support listening to 0.0.0.0, match windows behavior.
                        throw new HttpListenerException(50, SR.net_listener_not_supported);
                    }
                }
                catch
                {
                    // Throw same error code as windows, request is not supported.
                    throw new HttpListenerException(50, SR.net_listener_not_supported);
                }
            }

            Dictionary <int, HttpEndPointListener> p = null;

            if (s_ipEndPoints.ContainsKey(addr))
            {
                p = s_ipEndPoints[addr];
            }
            else
            {
                p = new Dictionary <int, HttpEndPointListener>();
                s_ipEndPoints[addr] = p;
            }

            HttpEndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = p[port];
            }
            else
            {
                try
                {
                    epl = new HttpEndPointListener(listener, addr, port, secure);
                }
                catch (SocketException ex)
                {
                    throw new HttpListenerException(ex.ErrorCode, ex.Message);
                }
                p[port] = epl;
            }

            return(epl);
        }
コード例 #5
0
ファイル: HttpConnection.cs プロジェクト: pedrobsaila/runtime
 private void Unbind()
 {
     if (_contextBound)
     {
         HttpEndPointListener.UnbindContext(_context);
         _contextBound = false;
     }
 }
コード例 #6
0
        private static HttpEndPointListener 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 = Dns.GetHostEntry(host);
                    if (iphost != null)
                    {
                        addr = iphost.AddressList[0];
                    }
                    else
                    {
                        addr = IPAddress.Any;
                    }
                }
                catch
                {
                    addr = IPAddress.Any;
                }
            }
            Dictionary <int, HttpEndPointListener> p = null;

            if (s_ipEndPoints.ContainsKey(addr))
            {
                p = s_ipEndPoints[addr];
            }
            else
            {
                p = new Dictionary <int, HttpEndPointListener>();
                s_ipEndPoints[addr] = p;
            }

            HttpEndPointListener epl = null;

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

            return(epl);
        }
コード例 #7
0
ファイル: HttpEndPointManager.cs プロジェクト: blqw/corefx
 public static void RemoveEndPoint(HttpEndPointListener epl, IPEndPoint ep)
 {
     lock ((s_ipEndPoints as ICollection).SyncRoot)
     {
         Dictionary <int, HttpEndPointListener> p = null;
         p = s_ipEndPoints[ep.Address];
         p.Remove(ep.Port);
         if (p.Count == 0)
         {
             s_ipEndPoints.Remove(ep.Address);
         }
         epl.Close();
     }
 }
コード例 #8
0
ファイル: HttpEndPointManager.cs プロジェクト: blqw/corefx
        private 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;
            }

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

            epl.RemovePrefix(lp, listener);
        }
コード例 #9
0
        private static void AddPrefixInternal(string p, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path);
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path);
            }

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

            epl.AddPrefix(lp, listener);
        }
コード例 #10
0
        private static void ProcessAccept(SocketAsyncEventArgs args)
        {
            HttpEndPointListener epl = (HttpEndPointListener)args.UserToken !;

            Socket?accepted = args.SocketError == SocketError.Success ? args.AcceptSocket : null;

            epl.Accept(args);

            if (accepted == null)
            {
                return;
            }

            if (epl._secure && epl._cert == null)
            {
                accepted.Close();
                return;
            }

            HttpConnection conn;

            try
            {
                conn = new HttpConnection(accepted, epl, epl._secure, epl._cert !);
            }
            catch
            {
                accepted.Close();
                return;
            }

            lock (epl._unregisteredConnections)
            {
                epl._unregisteredConnections[conn] = conn;
            }
            conn.BeginReadRequest();
        }
コード例 #11
0
ファイル: HttpConnection.cs プロジェクト: robosek/corefx
        public HttpConnection(Socket sock, HttpEndPointListener epl, bool secure, X509Certificate cert)
        {
            _socket = sock;
            _epl    = epl;
            _secure = secure;
            _cert   = cert;
            if (secure == false)
            {
                _stream = new NetworkStream(sock, false);
            }
            else
            {
                _sslStream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) =>
                {
                    if (c == null)
                    {
                        return(true);
                    }

                    var c2 = c as X509Certificate2;
                    if (c2 == null)
                    {
                        c2 = new X509Certificate2(c.GetRawCertData());
                    }

                    _clientCert       = c2;
                    _clientCertErrors = new int[] { (int)e };
                    return(true);
                });

                _stream = _sslStream;
            }

            _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
コード例 #12
0
        private static HttpEndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure, out bool alreadyExists)
        {
            alreadyExists = false;

            IPAddress addr;

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

            if (s_ipEndPoints.ContainsKey(addr))
            {
                p = s_ipEndPoints[addr];
            }
            else
            {
                p = new Dictionary <int, HttpEndPointListener>();
                s_ipEndPoints[addr] = p;
            }

            HttpEndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                alreadyExists = true;
                epl           = p[port];
            }
            else
            {
                try
                {
                    epl = new HttpEndPointListener(listener, addr, port, secure);
                }
                catch (SocketException ex)
                {
                    throw new HttpListenerException(ex.ErrorCode, ex.Message);
                }
                p[port] = epl;
            }

            return(epl);
        }