Example #1
0
        private static void MakeUnsecureHandshake(Uri uri, Stream stream)
        {
            var env     = MakeHandshakeEnvironment(uri, stream);
            var result  = new UpgradeHandshaker(env).Handshake();
            var success = result[HandshakeKeys.Successful] as string == HandshakeKeys.True;

            if (!success)
            {
                throw new Http2HandshakeFailed(HandshakeFailureReason.InternalError);
            }
        }
        public bool Connect(Uri connectUri)
        {
            _path     = connectUri.PathAndQuery;
            _version  = Protocols.Http2;
            _scheme   = connectUri.Scheme;
            _host     = connectUri.Host;
            _port     = connectUri.Port;
            ServerUri = connectUri.Authority;

            if (_sessionAdapter != null)
            {
                return(false);
            }

            try
            {
                int port = connectUri.Port;

                int securePort;

                if (!int.TryParse(ConfigurationManager.AppSettings["securePort"], out securePort))
                {
                    Http2Logger.LogError("Incorrect port in the config file!");
                    return(false);
                }
                _isSecure = port == securePort;

                var tcpClnt = new TcpClient(connectUri.Host, port);

                _clientStream = tcpClnt.GetStream();

                if (_useHandshake)
                {
                    if (_isSecure)
                    {
                        _clientStream = new SslStream(_clientStream, false);
                        _certificate  = LoadPKCS12Certificate(AssemblyName + CertificatePath, String.Empty);

                        _chain = new X509Chain {
                            _certificate
                        };
                        var certList = new X509List {
                            _certificate
                        };

                        (_clientStream as SslStream).AuthenticateAsClient(connectUri.AbsoluteUri, certList, _chain,
                                                                          SslProtocols.Tls, SslStrength.All, false);

                        _selectedProtocol = (_clientStream as SslStream).AlpnSelectedProtocol;
                    }

                    if (!_isSecure || _selectedProtocol == Protocols.Http1)
                    {
                        MakeHandshakeEnvironment();
                        try
                        {
                            var handshakeResult = new UpgradeHandshaker(_environment).Handshake();
                            _environment.Add(HandshakeKeys.Result, handshakeResult);
                            _useHttp20 = handshakeResult[HandshakeKeys.Successful] as string == HandshakeKeys.True;

                            if (!_useHttp20)
                            {
                                Dispose(false);
                                return(true);
                            }
                        }
                        catch (Http2HandshakeFailed ex)
                        {
                            if (ex.Reason == HandshakeFailureReason.InternalError)
                            {
                                _useHttp20 = false;
                            }
                            else
                            {
                                Http2Logger.LogError("Specified server did not respond");
                                Dispose(true);
                                return(false);
                            }
                        }
                    }
                }

                Http2Logger.LogDebug("Handshake finished");

                Protocol = _isSecure ? SslProtocols.Tls : SslProtocols.None;

                if (_useHttp20)
                {
                    _sessionAdapter = new Http2ClientMessageHandler(_clientStream, ConnectionEnd.Client, _isSecure, CancellationToken.None);
                }
            }
            catch (SocketException)
            {
                Http2Logger.LogError("Check if any server listens port " + connectUri.Port);
                Dispose(true);
                return(false);
            }
            catch (Exception ex)
            {
                Http2Logger.LogError("Unknown connection exception was caught: " + ex.Message);
                Dispose(true);
                return(false);
            }

            return(true);
        }