Beispiel #1
0
        public async Task RejectsTooSmallBuffer()
        {
            // Arrange
            var chunks = new ReadsToChunksStream(new MemoryStream());

            // Act, Assert
            Func <Task> actionAsync = () => chunks.ReadAsync(new byte[5], 0, 5);

            actionAsync.ShouldThrow <ArgumentOutOfRangeException>().Which.Message.Should().StartWith("The number of bytes to read must be greater than or equal to 6.");
        }
Beispiel #2
0
        public async Task RoundTrip()
        {
            // Arrange
            var expected       = "This is the content the will go through both chunk streams.";
            var inner          = new MemoryStream(Encoding.ASCII.GetBytes(expected));
            var readToChunks   = new ReadsToChunksStream(inner);
            var readFromChunks = new ReadsFromChunksStream(readToChunks);
            var outputStream   = new MemoryStream();

            // Act
            await readFromChunks.CopyToAsync(outputStream, 10);

            // Assert
            Encoding.ASCII.GetString(outputStream.ToArray()).Should().Be(expected);
        }
Beispiel #3
0
        private static async Task <IEnumerable <string> > GetReadBuffersAsync(string input, int bufferSize)
        {
            // Arrange
            var stream  = new ReadsToChunksStream(new MemoryStream(Encoding.ASCII.GetBytes(input)));
            var buffers = new List <string>();

            int read = -1;

            while (read != 0)
            {
                var buffer = new byte[bufferSize];

                // Act
                read = await stream.ReadAsync(buffer, 0, bufferSize);

                if (read > 0)
                {
                    buffers.Add(Encoding.ASCII.GetString(buffer, 0, read));
                }
            }

            return(buffers);
        }
Beispiel #4
0
        private async Task <Stream> SendRequestHeadAsync(StreamWriter writer, HttpRequestMessage request)
        {
            var location = request.Method != ConnectMethod ? request.RequestUri.PathAndQuery : $"{request.RequestUri.DnsSafeHost}:{request.RequestUri.Port}";
            await writer.WriteAsync($"{request.Method.Method} {location} HTTP/{request.Version}" + LineSeparator).ConfigureAwait(false);

            if (!MethodsWithoutHostHeader.Contains(request.Method))
            {
                string host = request.Headers.Contains(HostHeader) ? request.Headers.Host : request.RequestUri.Host;
                await WriteHeaderAsync(writer, HostHeader, host).ConfigureAwait(false);
            }

            Stream contentStream = null;

            if (request.Content != null && !MethodsWithoutRequestBody.Contains(request.Method))
            {
                contentStream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false);

                // determine whether to use chunked transfer encoding
                long?contentLength = null;
                if (!request.Headers.TransferEncodingChunked.GetValueOrDefault(false))
                {
                    try
                    {
                        contentLength = contentStream.Length;
                    }
                    catch (Exception)
                    {
                        // we cannot get the request content length, so fall back to chunking
                    }
                }

                // set the appropriate content transfer headers
                if (contentLength.HasValue)
                {
                    // TODO: we are preferring the content length provided by the caller... is this right?
                    contentLength = request.Content.Headers.ContentLength ?? contentLength;
                    await WriteHeaderAsync(writer, ContentLengthHeader, contentLength.ToString()).ConfigureAwait(false);
                }
                else
                {
                    contentStream = new ReadsToChunksStream(contentStream);
                    await WriteHeaderAsync(writer, TransferEncodingHeader, "chunked").ConfigureAwait(false);
                }

                // write all content headers
                foreach (var header in request.Content.Headers.Where(p => !SpecialHeaders.Contains(p.Key)))
                {
                    await WriteHeaderAsync(writer, header).ConfigureAwait(false);
                }
            }

            // writer the rest of the request headers
            foreach (var header in request.Headers.Where(p => !SpecialHeaders.Contains(p.Key)))
            {
                await WriteHeaderAsync(writer, header).ConfigureAwait(false);
            }

            await writer.WriteAsync(LineSeparator).ConfigureAwait(false);

            await writer.FlushAsync().ConfigureAwait(false);

            return(contentStream);
        }