예제 #1
0
        internal static async Task <SpdySession> SpdyNamespacedPodPortForwardAsync(
            this k8s.Kubernetes kubernetes,
            string name,
            string @namespace,
            int[] ports,
            CancellationToken cancellationToken = default)
        {
            if (ports.Any() == false)
            {
                throw new ArgumentOutOfRangeException(nameof(ports), "At least one port needs to be specified");
            }
            var uri = new Uri(kubernetes.BaseUri, $"api/v1/namespaces/{@namespace}/pods/{name}/portforward?{string.Join('&', ports.Select(port => $"ports={port}"))}");

            var request = new HttpRequestMessage(HttpMethod.Get, uri);

            request.Headers.TryAddWithoutValidation(
                Microsoft.Net.Http.Headers.HeaderNames.Connection, "Upgrade");
            request.Headers.TryAddWithoutValidation(
                Microsoft.Net.Http.Headers.HeaderNames.Upgrade, "SPDY/3.1");
            if (kubernetes.Credentials != null)
            {
                await kubernetes.Credentials.ProcessHttpRequestAsync(
                    request, cancellationToken)
                .ConfigureAwait(false);
            }

            var response =
                await kubernetes
                .HttpClient.SendAsync(
                    request,
                    // This prevents the http client to buffer the response
                    HttpCompletionOption
                    .ResponseHeadersRead,
                    cancellationToken)
                .ConfigureAwait(false);

            if (response.StatusCode != HttpStatusCode.SwitchingProtocols)
            {
                throw new InvalidOperationException(
                          $"Expected switching protocol, but got {response.StatusCode}");
            }

            var stream = await response.Content.ReadAsStreamAsync()
                         .ConfigureAwait(false);

            return(SpdySession.CreateClient(new StreamingNetworkClient(stream)));
        }
예제 #2
0
            protected override async Task WhenAsync(
                CancellationToken cancellationToken)
            {
                _response = await
                            Server
                            .CreateHttpClient()
                            .SendAsync(
                    new HttpRequestMessage(
                        HttpMethod.Get,
                        new Uri($"{Server.BaseAddress}api/v1/streaming"))
                {
                    Headers =
                    {
                        { Microsoft.Net.Http.Headers.HeaderNames.Connection, SpdyMiddleware.Headers.Connection },
                        { Microsoft.Net.Http.Headers.HeaderNames.Upgrade,    SpdyMiddleware.Headers.Upgrade    }
                    }
                },
                    // This prevents the http client to buffer the response
                    HttpCompletionOption.ResponseHeadersRead,
                    cancellationToken)
                            .ConfigureAwait(false);

                var stream = await _response.Content.ReadAsStreamAsync(cancellationToken)
                             .ConfigureAwait(false);

                await using var client = SpdySession.CreateClient(new StreamingNetworkClient(stream));
                using var spdyStream   = client.CreateStream();

                await spdyStream.SendLastAsync(
                    Encoding.UTF8.GetBytes("This is a request"), cancellationToken : cancellationToken);

                ReadResult readResult;

                do
                {
                    readResult = await spdyStream
                                 .ReceiveAsync(
                        cancellationToken : cancellationToken)
                                 .ConfigureAwait(false);

                    _clientReceivedResponse += Encoding.UTF8.GetString(readResult.Buffer.ToArray());
                } while (!readResult.IsCanceled &&
                         !readResult.IsCompleted);

                await spdyStream.WaitForFullyClosedAsync(cancellationToken)
                .ConfigureAwait(false);
            }
예제 #3
0
        internal async Task <SpdySession> ConnectAsync(
            CancellationToken cancellationToken = default)
        {
            _server =
                _testFramework.NetworkServerFactory.CreateAndStart(
                    IPAddress.Any, 1,
                    ProtocolType.Tcp);
            _client = await _testFramework.ConnectAsync(
                new FrameClientFactory(),
                IPAddress.Any, 1,
                ProtocolType.Tcp,
                cancellationToken)
                      .ConfigureAwait(false);

            return(SpdySession.CreateClient(
                       await _server.WaitForConnectedClientAsync(cancellationToken)
                       .ConfigureAwait(false)));
        }