Example #1
0
        //
        // GenericLoopbackServer implementation
        //

        public override async Task <HttpRequestData> HandleRequestAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList <HttpHeaderData> headers = null, string content = "")
        {
            using (Http2LoopbackConnection connection = await EstablishConnectionAsync().ConfigureAwait(false))
            {
                return(await connection.HandleRequestAsync(statusCode, headers, content).ConfigureAwait(false));
            }
        }
        public static async Task <Http2LoopbackConnection> CreateAsync(SocketWrapper socket, Stream stream, Http2Options httpOptions, TimeSpan timeout)
        {
            if (httpOptions.UseSsl)
            {
                var sslStream = new SslStream(stream, false, delegate { return(true); });

                using (X509Certificate2 cert = Configuration.Certificates.GetServerCertificate())
                {
#if !NETFRAMEWORK
                    SslServerAuthenticationOptions options = new SslServerAuthenticationOptions();

                    options.EnabledSslProtocols = httpOptions.SslProtocols;

                    var protocols = new List <SslApplicationProtocol>();
                    protocols.Add(SslApplicationProtocol.Http2);
                    options.ApplicationProtocols = protocols;

                    options.ServerCertificate = cert;

                    options.ClientCertificateRequired = httpOptions.ClientCertificateRequired;

                    await sslStream.AuthenticateAsServerAsync(options, CancellationToken.None).ConfigureAwait(false);
#else
                    await sslStream.AuthenticateAsServerAsync(cert, httpOptions.ClientCertificateRequired, httpOptions.SslProtocols, checkCertificateRevocation : false).ConfigureAwait(false);
#endif
                }

                stream = sslStream;
            }

            var con = new Http2LoopbackConnection(socket, stream, timeout, httpOptions.EnableTransparentPingResponse);
            await con.ReadPrefixAsync().ConfigureAwait(false);

            return(con);
        }
Example #3
0
 public override async Task AcceptConnectionAsync(Func <GenericLoopbackConnection, Task> funcAsync)
 {
     using (Http2LoopbackConnection connection = await EstablishConnectionAsync().ConfigureAwait(false))
     {
         await funcAsync(connection).ConfigureAwait(false);
     }
 }
Example #4
0
        //
        // GenericLoopbackServer implementation
        //

        public override async Task <HttpRequestData> HandleRequestAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList <HttpHeaderData> headers = null, string content = null)
        {
            Http2LoopbackConnection connection = await EstablishConnectionAsync().ConfigureAwait(false);

            (int streamId, HttpRequestData requestData) = await connection.ReadAndParseRequestHeaderAsync().ConfigureAwait(false);

            // We are about to close the connection, after we send the response.
            // So, send a GOAWAY frame now so the client won't inadvertantly try to reuse the connection.
            await connection.SendGoAway(streamId).ConfigureAwait(false);

            if (content == null)
            {
                await connection.SendResponseHeadersAsync(streamId, endStream : true, statusCode, isTrailingHeader : false, headers : headers).ConfigureAwait(false);
            }
            else
            {
                await connection.SendResponseHeadersAsync(streamId, endStream : false, statusCode, isTrailingHeader : false, headers : headers).ConfigureAwait(false);

                await connection.SendResponseBodyAsync(streamId, Encoding.ASCII.GetBytes(content)).ConfigureAwait(false);
            }

            await connection.WaitForConnectionShutdownAsync().ConfigureAwait(false);

            return(requestData);
        }
Example #5
0
        public async Task <(Http2LoopbackConnection, SettingsFrame)> EstablishConnectionGetSettingsAsync(params SettingsEntry[] settingsEntries)
        {
            Http2LoopbackConnection connection = await AcceptConnectionAsync().ConfigureAwait(false);

            // Receive the initial client settings frame.
            Frame receivedFrame = await connection.ReadFrameAsync(Timeout).ConfigureAwait(false);

            Assert.Equal(FrameType.Settings, receivedFrame.Type);
            Assert.Equal(FrameFlags.None, receivedFrame.Flags);
            Assert.Equal(0, receivedFrame.StreamId);

            var clientSettingsFrame = (SettingsFrame)receivedFrame;

            // Receive the initial client window update frame.
            receivedFrame = await connection.ReadFrameAsync(Timeout).ConfigureAwait(false);

            Assert.Equal(FrameType.WindowUpdate, receivedFrame.Type);
            Assert.Equal(FrameFlags.None, receivedFrame.Flags);
            Assert.Equal(0, receivedFrame.StreamId);

            // Send the initial server settings frame.
            SettingsFrame settingsFrame = new SettingsFrame(settingsEntries);
            await connection.WriteFrameAsync(settingsFrame).ConfigureAwait(false);

            // Send the client settings frame ACK.
            Frame settingsAck = new Frame(0, FrameType.Settings, FrameFlags.Ack, 0);
            await connection.WriteFrameAsync(settingsAck).ConfigureAwait(false);

            // The client will send us a SETTINGS ACK eventually, but not necessarily right away.
            connection.ExpectSettingsAck();

            return(connection, clientSettingsFrame);
        }
Example #6
0
        public async Task <(Http2LoopbackConnection, SettingsFrame)> EstablishConnectionGetSettingsAsync(TimeSpan?timeout, TimeSpan?ackTimeout, params SettingsEntry[] settingsEntries)
        {
            Http2LoopbackConnection connection = await AcceptConnectionAsync(timeout).ConfigureAwait(false);

            SettingsFrame clientSettingsFrame = await connection.ReadAndSendSettingsAsync(ackTimeout, settingsEntries).ConfigureAwait(false);

            return(connection, clientSettingsFrame);
        }
Example #7
0
        public async Task <Http2LoopbackConnection> AcceptConnectionAsync()
        {
            RemoveInvalidConnections();

            if (!AllowMultipleConnections && _connections.Count != 0)
            {
                throw new InvalidOperationException("Connection already established. Set `AllowMultipleConnections = true` to bypass.");
            }

            Socket connectionSocket = await _listenSocket.AcceptAsync().ConfigureAwait(false);

            Http2LoopbackConnection connection = new Http2LoopbackConnection(connectionSocket, _options);

            _connections.Add(connection);

            return(connection);
        }
Example #8
0
        public async Task <Http2LoopbackConnection> AcceptConnectionAsync(TimeSpan?timeout)
        {
            RemoveInvalidConnections();

            if (!AllowMultipleConnections && _connections.Count != 0)
            {
                throw new InvalidOperationException("Connection already established. Set `AllowMultipleConnections = true` to bypass.");
            }

            Socket connectionSocket = await _listenSocket.AcceptAsync().ConfigureAwait(false);

            var stream = new NetworkStream(connectionSocket, ownsSocket: true);
            Http2LoopbackConnection connection =
                timeout != null ? await Http2LoopbackConnection.CreateAsync(connectionSocket, stream, _options, timeout.Value).ConfigureAwait(false) :
                await Http2LoopbackConnection.CreateAsync(connectionSocket, stream, _options).ConfigureAwait(false);

            _connections.Add(connection);

            return(connection);
        }
Example #9
0
 public override async Task <GenericLoopbackConnection> CreateConnectionAsync(SocketWrapper socket, Stream stream, GenericLoopbackOptions options = null)
 {
     return(await Http2LoopbackConnection.CreateAsync(socket, stream, CreateOptions(options)).ConfigureAwait(false));
 }