Example #1
0
        public void StartAndCloseSession()
        {
            var requestStr = ConfigurationManager.AppSettings["smallTestFile"];
            Uri uri;

            Uri.TryCreate(TestHelpers.GetAddress() + requestStr, UriKind.Absolute, out uri);

            bool gotException = false;
            var  stream       = TestHelpers.GetHandshakedStream(uri);

            try
            {
                using (
                    var adapter = new Http2ClientMessageHandler(stream, ConnectionEnd.Client, TestHelpers.UseSecurePort,
                                                                new CancellationToken()))
                {
                    adapter.StartSessionAsync();
                }
            }
            catch (Exception)
            {
                gotException = true;
            }

            Assert.Equal(gotException, false);
        }
Example #2
0
        public static void SendRequest(Http2ClientMessageHandler adapter, Uri uri)
        {
            var pairs = GetHeadersList(uri);

            adapter.SendRequest(pairs, Constants.DefaultStreamPriority, true);
        }
        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);
        }