Fixed: Stream leakage inside CurlHttpDispatcher

This commit is contained in:
Leonardo Galli 2018-12-02 19:24:16 +01:00
parent d02d71c336
commit 899bd086ec
1 changed files with 94 additions and 86 deletions

View File

@ -66,9 +66,9 @@ namespace NzbDrone.Common.Http.Dispatchers
lock (CurlGlobalHandle.Instance) lock (CurlGlobalHandle.Instance)
{ {
Stream responseStream = new MemoryStream(); using (Stream responseStream = new MemoryStream())
Stream headerStream = new MemoryStream(); using (Stream headerStream = new MemoryStream())
{
using (var curlEasy = new CurlEasy()) using (var curlEasy = new CurlEasy())
{ {
curlEasy.AutoReferer = false; curlEasy.AutoReferer = false;
@ -158,6 +158,7 @@ namespace NzbDrone.Common.Http.Dispatchers
} }
} }
} }
}
private void AddProxy(CurlEasy curlEasy, HttpRequest request) private void AddProxy(CurlEasy curlEasy, HttpRequest request)
{ {
@ -259,6 +260,7 @@ namespace NzbDrone.Common.Http.Dispatchers
private byte[] ProcessResponseStream(HttpRequest request, Stream responseStream, WebHeaderCollection webHeaderCollection) private byte[] ProcessResponseStream(HttpRequest request, Stream responseStream, WebHeaderCollection webHeaderCollection)
{ {
byte[] bytes = null;
responseStream.Position = 0; responseStream.Position = 0;
if (responseStream.Length != 0) if (responseStream.Length != 0)
@ -268,21 +270,27 @@ namespace NzbDrone.Common.Http.Dispatchers
{ {
if (encoding.IndexOf("gzip") != -1) if (encoding.IndexOf("gzip") != -1)
{ {
responseStream = new GZipStream(responseStream, CompressionMode.Decompress); using (var zipStream = new GZipStream(responseStream, CompressionMode.Decompress))
{
bytes = zipStream.ToBytes();
}
webHeaderCollection.Remove("Content-Encoding"); webHeaderCollection.Remove("Content-Encoding");
} }
else if (encoding.IndexOf("deflate") != -1) else if (encoding.IndexOf("deflate") != -1)
{ {
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress); using (var deflateStream = new DeflateStream(responseStream, CompressionMode.Decompress))
{
bytes = deflateStream.ToBytes();
}
webHeaderCollection.Remove("Content-Encoding"); webHeaderCollection.Remove("Content-Encoding");
} }
} }
} }
return responseStream.ToBytes(); if (bytes == null) bytes = responseStream.ToBytes();
return bytes;
} }
} }