[System]: WebResponseStream now correctly reads the chunk trailer when using gzip/deflate. (#6771)

* [System]: WebResponseStream now correctly reads the chunk trailer when using gzip/deflate.

This should actually fix bug #59779.

* Add missing ConfigureAwait(false)
This commit is contained in:
Martin Baulig authored and Marek Safar committed 2018-02-05 14:45:56 -05:00
1 parent 0bfda265b7
commit da03780370
2 files changed
+60 -2

No files matched your search

+35 -1
View File
@@ -90,6 +90,40 @@ namespace System.Net
return ret;
}
public async Task ReadChunkTrailer (CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested ();
/*
* We are expecting the chunk trailer, but no more data.
*/
if (Decoder.DataAvailable)
ThrowExpectingChunkTrailer ();
/*
* Need to loop here since there might be header fields after
* the chunk trailer.
*/
while (Decoder.WantMore) {
var buffer = new byte[256];
int ret = await InnerStream.ReadAsync (
buffer, 0, buffer.Length, cancellationToken).ConfigureAwait (false);
if (ret <= 0)
ThrowExpectingChunkTrailer ();
Decoder.Write (buffer, 0, ret);
ret = Decoder.Read (buffer, 0, 1);
if (ret != 0)
ThrowExpectingChunkTrailer ();
}
}
static void ThrowExpectingChunkTrailer ()
{
throw new WebException (
"Expecting chuk traier.", null,
WebExceptionStatus.ServerProtocolViolation, null);
}
}
}
@@ -38,6 +38,7 @@ namespace System.Net
class WebResponseStream : WebConnectionStream
{
Stream innerStreamWrapper;
MonoChunkStream innerChunkStream;
long contentLength;
long totalRead;
bool nextReadCalled;
@@ -210,6 +211,29 @@ namespace System.Net
}
async Task<int> InnerReadAsync (byte[] buffer, int offset, int size, CancellationToken cancellationToken)
{
var ret = await InnerReadAsyncInner (buffer, offset, size, cancellationToken).ConfigureAwait (false);
if (ret != 0)
return ret;
if (innerChunkStream == null || innerChunkStream == innerStreamWrapper)
return 0;
/*
* We only get here when using GZip/Deflate decompression with
* chunked encoding. Since the GZipStream/DeflateStream knows
* about the size of the content, it may not have read the
* chunk trailer.
*/
WebConnection.Debug ($"{ME} INNER READ - READ CHUNK TRAILER");
await innerChunkStream.ReadChunkTrailer (cancellationToken).ConfigureAwait (false);
WebConnection.Debug ($"{ME} INNER READ - READ CHUNK TRAILER #DONE");
return 0;
}
async Task<int> InnerReadAsyncInner (byte[] buffer, int offset, int size, CancellationToken cancellationToken)
{
Operation.ThrowIfDisposed (cancellationToken);
@@ -284,7 +308,7 @@ namespace System.Net
ChunkedRead = (tencoding != null && tencoding.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
if (ChunkedRead) {
innerStreamWrapper = new MonoChunkStream (
innerStreamWrapper = innerChunkStream = new MonoChunkStream (
Operation, CreateStreamWrapper (buffer), Headers);
} else if (!IsNtlmAuth () && contentLength > 0 && buffer.Size >= contentLength) {
innerStreamWrapper = new BufferedReadStream (Operation, null, buffer);