Ejemplo n.º 1
0
 public ChunkedStream(Stream innerStream)
 {
     _byteStreamReader = new ByteStreamReader(innerStream, 4096, false);
     _disposed = false;
     _chunkSize = -1;
     _remaining = -1;
 }
Ejemplo n.º 2
0
        public async Task<HttpResponseMessage> ReceiveResponseAsync(Stream stream, HttpRequestMessage request)
        {
            ByteStreamReader reader = new ByteStreamReader(stream, BufferSize, false);

            var response = await ReadResponseHeadAsync(reader, request);

            if (!MethodsWithoutResponseBody.Contains(request.Method))
            {
                ReadResponseBody(reader, response);
            }

            return response;
        }
Ejemplo n.º 3
0
        private async Task<HttpResponseMessage> ReadResponseHeadAsync(ByteStreamReader reader, HttpRequestMessage request)
        {
            // initialize the response
            var response = new HttpResponseMessage { RequestMessage = request };

            // read the first line of the response
            string line = await reader.ReadLineAsync();
            string[] pieces = line.Split(new[] { ' ' }, 3);
            if (pieces[0] != "HTTP/1.1")
            {
                throw new HttpRequestException("The HTTP version the response is not supported.");
            }

            response.StatusCode = (HttpStatusCode)int.Parse(pieces[1]);
            response.ReasonPhrase = pieces[2];

            // read the headers
            response.Content = new ByteArrayContent(new byte[0]);
            while ((line = await reader.ReadLineAsync()) != null && line != string.Empty)
            {
                pieces = line.Split(new[] { ":" }, 2, StringSplitOptions.None);
                if (pieces[1].StartsWith(" "))
                {
                    pieces[1] = pieces[1].Substring(1);
                }

                var headers = HttpHeaderCategories.IsContentHeader(pieces[0]) ? (HttpHeaders)response.Content.Headers : response.Headers;
                headers.TryAddWithoutValidation(pieces[0], pieces[1]);
            }

            return response;
        }
Ejemplo n.º 4
0
        private void ReadResponseBody(ByteStreamReader reader, HttpResponseMessage response)
        {
            HttpContent content = null;
            if (response.Headers.TransferEncodingChunked.GetValueOrDefault(false))
            {
                // read the body with chunked transfer encoding
                var remainingStream = reader.GetRemainingStream();
                var chunkedStream = new ChunkedStream(remainingStream);
                content = new StreamContent(chunkedStream);
            }
            else if (response.Content.Headers.ContentLength.HasValue)
            {
                // read the body with a content-length
                var remainingStream = reader.GetRemainingStream();
                var limitedStream = new LimitedStream(remainingStream, response.Content.Headers.ContentLength.Value);
                content = new StreamContent(limitedStream);
            }

            if (content != null)
            {
                // copy over the content headers
                foreach (var header in response.Content.Headers)
                {
                    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }

                response.Content = content;
            }
        }
Ejemplo n.º 5
0
 public void Setup(string[] lines)
 {
     Stream = GetStream(lines);
     Reader = new ByteStreamReader(Stream, BufferSize, PreserveLineEndings); 
 }