From 76212ef908be1ae5bc4ac32846246cb538e0c1e3 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Tue, 25 Dec 2018 00:22:35 +0100 Subject: [PATCH] Do our own decompression to workaround mono bug. --- .../Http/Dispatchers/ManagedHttpDispatcher.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs index 70ee66390..d9046eb55 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.IO.Compression; using System.Net; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; @@ -28,7 +29,7 @@ namespace NzbDrone.Common.Http.Dispatchers // Deflate is not a standard and could break depending on implementation. // we should just stick with the more compatible Gzip //http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net - webRequest.AutomaticDecompression = DecompressionMethods.GZip; + webRequest.AutomaticDecompression = DecompressionMethods.None; webRequest.Method = request.Method.ToString(); webRequest.UserAgent = _userAgentBuilder.GetUserAgent(request.UseSimplifiedUserAgent); @@ -36,6 +37,8 @@ namespace NzbDrone.Common.Http.Dispatchers webRequest.AllowAutoRedirect = false; webRequest.CookieContainer = cookies; + webRequest.Headers.Add("Accept-Encoding", "gzip"); + if (request.RequestTimeout != TimeSpan.Zero) { webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds); @@ -107,6 +110,20 @@ namespace NzbDrone.Common.Http.Dispatchers try { data = responseStream.ToBytes(); + + // Do our own decompression. + if (httpWebResponse.ContentEncoding == "gzip") + { + using (var compressedStream = new MemoryStream(data)) + using (var gzip = new GZipStream(compressedStream, CompressionMode.Decompress)) + using (var decompressedStream = new MemoryStream()) + { + gzip.CopyTo(decompressedStream); + data = decompressedStream.ToArray(); + } + + httpWebResponse.Headers.Remove("Content-Encoding"); + } } catch (Exception ex) {