protected internal override ValueTask <int> ReadContentAsync(int version, Memory <byte> buffer, CancellationToken cancellationToken)
 {
     if (IsDisposed(version, out ValueTask <int> task))
     {
         return(task);
     }
     return(_request.ReadContentAsync(buffer, cancellationToken));
 }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            await using ConnectionFactory connectionFactory = new SocketConnectionFactory();
            await using Connection connection = await connectionFactory.ConnectAsync(new DnsEndPoint ("microsoft.com", 80));

            await using HttpConnection httpConnection = new Http1Connection(connection);

            await using (ValueHttpRequest request = (await httpConnection.CreateNewRequestAsync(HttpPrimitiveVersion.Version11, HttpVersionPolicy.RequestVersionExact)).Value)
            {
                request.ConfigureRequest(contentLength: 0, hasTrailingHeaders: false);
                request.WriteRequest(HttpMethod.Get, new Uri("http://microsoft.com"));
                request.WriteHeader("Accept", "text/html");
                await request.CompleteRequestAsync();

                await request.ReadToFinalResponseAsync();

                Console.WriteLine($"Final response code: {request.StatusCode}");

                if (await request.ReadToHeadersAsync())
                {
                    await request.ReadHeadersAsync(new PrintingHeadersSink(), state : null);
                }
                else
                {
                    Console.WriteLine("No headers received.");
                }

                if (await request.ReadToContentAsync())
                {
                    long totalLen = 0;

                    var buffer = new byte[4096];
                    int readLen;

                    do
                    {
                        while ((readLen = await request.ReadContentAsync(buffer)) != 0)
                        {
                            totalLen += readLen;
                        }
                    }while (await request.ReadToNextContentAsync());

                    Console.WriteLine($"Received {totalLen} byte response.");
                }
                else
                {
                    Console.WriteLine("No content received.");
                }

                if (await request.ReadToTrailingHeadersAsync())
                {
                    await request.ReadHeadersAsync(new PrintingHeadersSink(), state : null);
                }
                else
                {
                    Console.WriteLine("No trailing headers received.");
                }
            }
        }
Exemplo n.º 3
0
        public override async ValueTask ProcessAsync(HttpMessage message)
        {
            var pipelineRequest = message.Request;
            var host            = pipelineRequest.Uri.Host;

            HttpMethod method = MapMethod(pipelineRequest.Method);

            Connection connection = await connectionFactory.ConnectAsync(new DnsEndPoint(host, 80));

            HttpConnection httpConnection = new Http1Connection(connection, HttpPrimitiveVersion.Version11);

            await using (ValueHttpRequest request = (await httpConnection.CreateNewRequestAsync(HttpPrimitiveVersion.Version11, HttpVersionPolicy.RequestVersionExact)).Value) {
                RequestContent pipelineContent = pipelineRequest.Content;
                long           contentLength   = 0;
                if (pipelineContent != null)
                {
                    if (!pipelineContent.TryComputeLength(out contentLength))
                    {
                        throw new NotImplementedException();
                    }
                }
                request.ConfigureRequest(contentLength: contentLength, hasTrailingHeaders: false);

                request.WriteRequest(method, pipelineRequest.Uri.ToUri());

                var pipelineHeaders = pipelineRequest.Headers;
                foreach (var header in pipelineHeaders)
                {
                    request.WriteHeader(header.Name, header.Value);
                }

                checked {
                    if (contentLength != 0)
                    {
                        using var ms = new MemoryStream((int)contentLength); // TODO: can the buffer be disposed here?
                        await pipelineContent.WriteToAsync(ms, message.CancellationToken);

                        await request.WriteContentAsync(ms.GetBuffer().AsMemory(0, (int)ms.Length));
                    }
                }

                await request.CompleteRequestAsync();

                var response = new NoAllocResponse();
                message.Response = response;

                await request.ReadToFinalResponseAsync();

                response.SetStatus((int)request.StatusCode);

                if (await request.ReadToHeadersAsync())
                {
                    await request.ReadHeadersAsync(response, state : null);
                }

                if (await request.ReadToContentAsync())
                {
                    var buffer = new byte[4096];
                    int readLen;
                    do
                    {
                        while ((readLen = await request.ReadContentAsync(buffer)) != 0)
                        {
                            if (readLen < 4096)
                            {
                                response.ContentStream = new MemoryStream(buffer, 0, readLen);
                            }
                            else
                            {
                                throw new NotImplementedException();
                            }
                        }
                    }while (await request.ReadToNextContentAsync());
                }

                if (await request.ReadToTrailingHeadersAsync())
                {
                    await request.ReadHeadersAsync(response, state : null);
                }
            }
        }