Esempio n. 1
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>());
     }
 }
Esempio n. 4
0
 private static Exception GetStreamCopyException(Exception originalException)
 {
     return(!HttpContent.StreamCopyExceptionNeedsWrapping(originalException) ? originalException : HttpContent.WrapStreamCopyException(originalException));
 }