| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System; |
| | using System.IO; |
| | using System.Net; |
| | using System.Net.Http; |
| | using System.Threading.Tasks; |
| |
|
| | namespace Google.Apis.Http |
| | { |
| | |
| | |
| | |
| | |
| | |
| | |
| | internal sealed class StreamReplacingHttpContent : HttpContent |
| | { |
| | private readonly HttpContent _originalContent; |
| | private readonly Func<Stream, Stream> _replacement; |
| | private bool _streamCreated = false; |
| |
|
| | internal StreamReplacingHttpContent(HttpContent originalContent, Func<Stream, Stream> replacement) |
| | { |
| | _originalContent = originalContent; |
| | _replacement = replacement; |
| |
|
| | foreach (var header in originalContent.Headers) |
| | { |
| | Headers.Add(header.Key, header.Value); |
| | } |
| | } |
| |
|
| | |
| | |
| | protected override bool TryComputeLength(out long length) |
| | { |
| | length = 0; |
| | return false; |
| | } |
| |
|
| | protected override async Task<Stream> CreateContentReadStreamAsync() |
| | { |
| | if (_streamCreated) |
| | { |
| | throw new InvalidOperationException("Cannot read content stream more than once"); |
| | } |
| | _streamCreated = true; |
| | var originalStream = await _originalContent.ReadAsStreamAsync().ConfigureAwait(false); |
| | return _replacement(originalStream); |
| | } |
| |
|
| | protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) |
| | { |
| | using (Stream contentStream = await CreateContentReadStreamAsync().ConfigureAwait(false)) |
| | { |
| | await contentStream.CopyToAsync(stream).ConfigureAwait(false); |
| | } |
| | } |
| | } |
| | } |
| |
|