Ejemplo n.º 1
0
            protected override async Task <Stream> CreateContentReadStreamAsync(CancellationToken cancellationToken)
            {
                if (_contentConsumed)
                {
                    throw new InvalidOperationException(SR.net_http_content_stream_already_read);
                }

                _contentConsumed = true;

                Stream originalStream = _originalContent.TryReadAsStream() ?? await _originalContent.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);

                return(GetDecompressedStream(originalStream));
            }
Ejemplo n.º 2
0
        private async Task <byte[]> GetByteArrayAsyncCore(Task <HttpResponseMessage> getTask)
        {
            // Wait for the response message.
            using (HttpResponseMessage responseMessage = await getTask.ConfigureAwait(false))
            {
                // Make sure it completed successfully.
                responseMessage.EnsureSuccessStatusCode();

                // Get the response content.
                HttpContent c = responseMessage.Content;
                if (c != null)
                {
#if NET46
                    return(await c.ReadAsByteArrayAsync().ConfigureAwait(false));
#else
                    HttpContentHeaders headers = c.Headers;
                    using (Stream responseStream = c.TryReadAsStream() ?? await c.ReadAsStreamAsync().ConfigureAwait(false))
                    {
                        long?  contentLength = headers.ContentLength;
                        Stream buffer; // declared here to share the state machine field across both if/else branches

                        if (contentLength.HasValue)
                        {
                            // If we got a content length, then we assume that it's correct and create a MemoryStream
                            // to which the content will be transferred.  That way, assuming we actually get the exact
                            // amount we were expecting, we can simply return the MemoryStream's underlying buffer.
                            buffer = new HttpContent.LimitMemoryStream(_maxResponseContentBufferSize, (int)contentLength.GetValueOrDefault());
                            await responseStream.CopyToAsync(buffer).ConfigureAwait(false);

                            if (buffer.Length > 0)
                            {
                                return(((HttpContent.LimitMemoryStream)buffer).GetSizedBuffer());
                            }
                        }
                        else
                        {
                            // If we didn't get a content length, then we assume we're going to have to grow
                            // the buffer potentially several times and that it's unlikely the underlying buffer
                            // at the end will be the exact size needed, in which case it's more beneficial to use
                            // ArrayPool buffers and copy out to a new array at the end.
                            buffer = new HttpContent.LimitArrayPoolWriteStream(_maxResponseContentBufferSize);
                            try
                            {
                                await responseStream.CopyToAsync(buffer).ConfigureAwait(false);

                                if (buffer.Length > 0)
                                {
                                    return(((HttpContent.LimitArrayPoolWriteStream)buffer).ToArray());
                                }
                            }
                            finally { buffer.Dispose(); }
                        }
                    }
#endif
                }

                // No content to return.
                return(Array.Empty <byte>());
            }
        }
Ejemplo n.º 3
0
        private async Task <Stream> FinishGetStreamAsync(Task <HttpResponseMessage> getTask)
        {
            HttpResponseMessage response = await getTask.ConfigureAwait(false);

            response.EnsureSuccessStatusCode();
            HttpContent c = response.Content;

            return(c != null ?
                   (c.TryReadAsStream() ?? await c.ReadAsStreamAsync().ConfigureAwait(false)) :
                   Stream.Null);
        }
            private async ValueTask <Stream> CreateContentReadStreamAsyncCore(bool async, CancellationToken cancellationToken)
            {
                if (_contentConsumed)
                {
                    throw new InvalidOperationException(SR.net_http_content_stream_already_read);
                }

                _contentConsumed = true;

                Stream originalStream;

                if (async)
                {
                    originalStream = _originalContent.TryReadAsStream() ?? await _originalContent.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    originalStream = _originalContent.ReadAsStream();
                }
                return(GetDecompressedStream(originalStream));
            }
Ejemplo n.º 5
0
            protected override async Task <Stream> CreateContentReadStreamAsync()
            {
                if (_contentConsumed)
                {
                    throw new InvalidOperationException("content already consumed");
                }

                _contentConsumed = true;

                Stream originalStream = _originalContent.TryReadAsStream() ?? await _originalContent.ReadAsStreamAsync().ConfigureAwait(false);

                return(GetDecompressedStream(originalStream));
            }
Ejemplo n.º 6
0
        private async Task <Stream> FinishGetStreamAsync(Task <HttpResponseMessage> getTask)
        {
            HttpResponseMessage httpResponseMessage = await getTask.ConfigureAwait(false);

            httpResponseMessage.EnsureSuccessStatusCode();
            HttpContent content = httpResponseMessage.Content;
            Stream      stream1;

            if (content != null)
            {
                Stream stream2 = content.TryReadAsStream();
                if (stream2 == null)
                {
                    stream2 = await content.ReadAsStreamAsync().ConfigureAwait(false);
                }
                stream1 = stream2;
            }
            else
            {
                stream1 = Stream.Null;
            }
            return(stream1);
        }
Ejemplo n.º 7
0
        private async Task <string> GetStringAsyncCore(Task <HttpResponseMessage> getTask)
        {
            // Wait for the response message.
            using (HttpResponseMessage responseMessage = await getTask.ConfigureAwait(false))
            {
                // Make sure it completed successfully.
                responseMessage.EnsureSuccessStatusCode();

                // Get the response content.
                HttpContent c = responseMessage.Content;
                if (c != null)
                {
#if NET46
                    return(await c.ReadAsStringAsync().ConfigureAwait(false));
#else
                    HttpContentHeaders headers = c.Headers;

                    // Since the underlying byte[] will never be exposed, we use an ArrayPool-backed
                    // stream to which we copy all of the data from the response.
                    using (Stream responseStream = c.TryReadAsStream() ?? await c.ReadAsStreamAsync().ConfigureAwait(false))
                        using (var buffer = new HttpContent.LimitArrayPoolWriteStream(_maxResponseContentBufferSize, (int)headers.ContentLength.GetValueOrDefault()))
                        {
                            await responseStream.CopyToAsync(buffer).ConfigureAwait(false);

                            if (buffer.Length > 0)
                            {
                                // Decode and return the data from the buffer.
                                return(HttpContent.ReadBufferAsString(buffer.GetBuffer(), headers));
                            }
                        }
#endif
                }

                // No content to return.
                return(string.Empty);
            }
        }
Ejemplo n.º 8
0
 private async Task <string> GetStringAsyncCore(Task <HttpResponseMessage> getTask)
 {
     using (HttpResponseMessage responseMessage = await getTask.ConfigureAwait(false))
     {
         responseMessage.EnsureSuccessStatusCode();
         HttpContent content = responseMessage.Content;
         if (content != null)
         {
             HttpContentHeaders headers = content.Headers;
             Stream             stream  = content.TryReadAsStream();
             if (stream == null)
             {
                 stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
             }
             using (Stream responseStream = stream)
             {
                 using (HttpContent.LimitArrayPoolWriteStream buffer = new HttpContent.LimitArrayPoolWriteStream(this._maxResponseContentBufferSize, (long)(int)headers.ContentLength.GetValueOrDefault()))
                 {
                     try
                     {
                         await responseStream.CopyToAsync((Stream)buffer).ConfigureAwait(false);
                     }
                     catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
                     {
                         throw HttpContent.WrapStreamCopyException(ex);
                     }
                     if (buffer.Length > 0L)
                     {
                         return(HttpContent.ReadBufferAsString(buffer.GetBuffer(), headers));
                     }
                 }
             }
             headers = (HttpContentHeaders)null;
         }
         return(string.Empty);
     }
 }
Ejemplo n.º 9
0
 private async Task <byte[]> GetByteArrayAsyncCore(Task <HttpResponseMessage> getTask)
 {
     using (HttpResponseMessage responseMessage = await getTask.ConfigureAwait(false))
     {
         responseMessage.EnsureSuccessStatusCode();
         HttpContent content = responseMessage.Content;
         if (content != null)
         {
             HttpContentHeaders headers = content.Headers;
             Stream             stream  = content.TryReadAsStream();
             if (stream == null)
             {
                 stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
             }
             using (Stream responseStream = stream)
             {
                 long?  contentLength = headers.ContentLength;
                 Stream buffer;
                 if (contentLength.HasValue)
                 {
                     buffer = (Stream) new HttpContent.LimitMemoryStream(this._maxResponseContentBufferSize, (int)contentLength.GetValueOrDefault());
                     try
                     {
                         await responseStream.CopyToAsync(buffer).ConfigureAwait(false);
                     }
                     catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
                     {
                         throw HttpContent.WrapStreamCopyException(ex);
                     }
                     if (buffer.Length > 0L)
                     {
                         return(((HttpContent.LimitMemoryStream)buffer).GetSizedBuffer());
                     }
                 }
                 else
                 {
                     buffer = (Stream) new HttpContent.LimitArrayPoolWriteStream(this._maxResponseContentBufferSize);
                     try
                     {
                         try
                         {
                             await responseStream.CopyToAsync(buffer).ConfigureAwait(false);
                         }
                         catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
                         {
                             throw HttpContent.WrapStreamCopyException(ex);
                         }
                         if (buffer.Length > 0L)
                         {
                             return(((HttpContent.LimitArrayPoolWriteStream)buffer).ToArray());
                         }
                     }
                     finally
                     {
                         buffer.Dispose();
                     }
                 }
                 buffer = (Stream)null;
             }
             headers = (HttpContentHeaders)null;
         }
         return(Array.Empty <byte>());
     }
 }