Beispiel #1
0
        internal Task LoadIntoBufferAsync(long maxBufferSize, CancellationToken cancellationToken)
        {
            this.CheckDisposed();
            if (maxBufferSize > (long)int.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(maxBufferSize), (object)maxBufferSize, SR.Format((IFormatProvider)CultureInfo.InvariantCulture, SR.net_http_content_buffersize_limit, (object)int.MaxValue));
            }
            if (this.IsBuffered)
            {
                return(Task.CompletedTask);
            }
            Exception    error        = (Exception)null;
            MemoryStream memoryStream = this.CreateMemoryStream(maxBufferSize, out error);

            if (memoryStream == null)
            {
                return(Task.FromException(error));
            }
            try
            {
                Task streamAsync = this.SerializeToStreamAsync((Stream)memoryStream, (TransportContext)null, cancellationToken);
                this.CheckTaskNotNull(streamAsync);
                return(this.LoadIntoBufferAsyncCore(streamAsync, memoryStream));
            }
            catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
            {
                return(Task.FromException(HttpContent.GetStreamCopyException(ex)));
            }
        }
Beispiel #2
0
 internal Task CopyToAsync(
     Stream stream,
     TransportContext context,
     CancellationToken cancellationToken)
 {
     this.CheckDisposed();
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     try
     {
         ArraySegment <byte> buffer;
         if (this.TryGetBuffer(out buffer))
         {
             return(HttpContent.CopyToAsyncCore(stream.WriteAsync(new ReadOnlyMemory <byte>(buffer.Array, buffer.Offset, buffer.Count), cancellationToken)));
         }
         Task streamAsync = this.SerializeToStreamAsync(stream, context, cancellationToken);
         this.CheckTaskNotNull(streamAsync);
         return(HttpContent.CopyToAsyncCore(new ValueTask(streamAsync)));
     }
     catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
     {
         return(Task.FromException(HttpContent.GetStreamCopyException(ex)));
     }
 }
Beispiel #3
0
 private static async Task CopyToAsyncCore(ValueTask copyTask)
 {
     try
     {
         await copyTask.ConfigureAwait(false);
     }
     catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
     {
         throw HttpContent.WrapStreamCopyException(ex);
     }
 }
 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);
     }
 }
 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>());
     }
 }
Beispiel #6
0
 private static Exception GetStreamCopyException(Exception originalException)
 {
     return(!HttpContent.StreamCopyExceptionNeedsWrapping(originalException) ? originalException : HttpContent.WrapStreamCopyException(originalException));
 }