From 97fecdbe6f7c5ab836e593b98cdd107fcd3fa996 Mon Sep 17 00:00:00 2001 From: MattMo Date: Fri, 3 Mar 2023 16:12:13 -0800 Subject: [PATCH] Changed all response data extensions to no longer use chunking since we can specify the content length. Bumped package version to 1.3.8 --- Rest.Net/HttpListenerResponseExtensions.cs | 101 +++++++++++++++------ Rest.Net/Rest.Net.csproj | 2 +- 2 files changed, 75 insertions(+), 28 deletions(-) diff --git a/Rest.Net/HttpListenerResponseExtensions.cs b/Rest.Net/HttpListenerResponseExtensions.cs index a11fa94..0916531 100644 --- a/Rest.Net/HttpListenerResponseExtensions.cs +++ b/Rest.Net/HttpListenerResponseExtensions.cs @@ -38,12 +38,14 @@ namespace MontoyaTech.Rest.Net { response.ContentType = "text/plain; charset=utf-8"; - response.ContentLength64 = text.Length; - var bytes = Encoding.UTF8.GetBytes(text); + response.ContentLength64 = bytes.Length; + response.OutputStream.Write(bytes, 0, bytes.Length); + response.OutputStream.Dispose(); + return response; } @@ -56,13 +58,24 @@ namespace MontoyaTech.Rest.Net public static HttpListenerResponse WithCompressedText(this HttpListenerResponse response, string text) { response.ContentType = "text/plain; charset=utf-8"; + response.Headers.Add("Content-Encoding", "gzip"); var bytes = Encoding.UTF8.GetBytes(text); - using (var responseStream = response.OutputStream) - using (var compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true)) - compressedStream.Write(bytes, 0, bytes.Length); + using (var memoryStream = new MemoryStream()) + { + using (var compressedStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) + compressedStream.Write(bytes, 0, bytes.Length); + + response.ContentLength64 = memoryStream.Length; + + memoryStream.Seek(0, SeekOrigin.Begin); + + memoryStream.CopyTo(response.OutputStream); + + response.OutputStream.Dispose(); + } return response; } @@ -83,6 +96,8 @@ namespace MontoyaTech.Rest.Net response.OutputStream.Write(bytes, 0, bytes.Length); + response.OutputStream.Dispose(); + return response; } @@ -99,9 +114,19 @@ namespace MontoyaTech.Rest.Net var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj)); - using (var responseStream = response.OutputStream) - using (var compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true)) - compressedStream.Write(bytes, 0, bytes.Length); + using (var memoryStream = new MemoryStream()) + { + using (var compressedStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) + compressedStream.Write(bytes, 0, bytes.Length); + + response.ContentLength64 = memoryStream.Length; + + memoryStream.Seek(0, SeekOrigin.Begin); + + memoryStream.CopyTo(response.OutputStream); + + response.OutputStream.Dispose(); + } return response; } @@ -123,11 +148,15 @@ namespace MontoyaTech.Rest.Net response.ContentType = mimeType; response.Headers.Add("Content-Deposition", $@"attachment; filename=""{Path.GetFileName(filePath)}"""); - response.SendChunked = true; using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) - using (var responseStream = response.OutputStream) - fileStream.CopyTo(responseStream); + { + response.ContentLength64 = fileStream.Length; + + fileStream.CopyTo(response.OutputStream); + + response.OutputStream.Dispose(); + } return response; } @@ -152,10 +181,10 @@ namespace MontoyaTech.Rest.Net response.ContentType = mimeType; response.Headers.Add("Content-Deposition", $@"attachment; filename=""{Path.GetFileName(filePath)}"""); response.ContentLength64 = content.Length; - response.SendChunked = true; - using (var responseStream = response.OutputStream) - responseStream.Write(content, 0, content.Length); + response.OutputStream.Write(content, 0, content.Length); + + response.OutputStream.Dispose(); return response; } @@ -178,12 +207,21 @@ namespace MontoyaTech.Rest.Net response.ContentType = mimeType; response.Headers.Add("Content-Deposition", $@"attachment; filename=""{Path.GetFileName(filePath)}"""); response.Headers.Add("Content-Encoding", "gzip"); - response.SendChunked = true; - using (var responseStream = response.OutputStream) - using (var compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true)) - using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) - fileStream.CopyTo(compressedStream); + using (var memoryStream = new MemoryStream()) + { + using (var compressedStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) + using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + fileStream.CopyTo(compressedStream); + + response.ContentLength64 = memoryStream.Length; + + memoryStream.Seek(0, SeekOrigin.Begin); + + memoryStream.CopyTo(response.OutputStream); + + response.OutputStream.Dispose(); + } return response; } @@ -208,11 +246,20 @@ namespace MontoyaTech.Rest.Net response.ContentType = mimeType; response.Headers.Add("Content-Deposition", $@"attachment; filename=""{Path.GetFileName(filePath)}"""); response.Headers.Add("Content-Encoding", "gzip"); - response.SendChunked = true; - using (var responseStream = response.OutputStream) - using (var compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true)) - compressedStream.Write(content, 0, content.Length); + using (var memoryStream = new MemoryStream()) + { + using (var compressedStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) + compressedStream.Write(content, 0, content.Length); + + response.ContentLength64 = memoryStream.Length; + + memoryStream.Seek(0, SeekOrigin.Begin); + + memoryStream.CopyTo(response.OutputStream); + + response.OutputStream.Dispose(); + } return response; } @@ -238,10 +285,10 @@ namespace MontoyaTech.Rest.Net response.Headers.Add("Content-Deposition", $@"attachment; filename=""{Path.GetFileName(filePath)}"""); response.Headers.Add("Content-Encoding", "gzip"); response.ContentLength64 = content.Length; - response.SendChunked = true; - using (var responseStream = response.OutputStream) - responseStream.Write(content, 0, content.Length); + response.OutputStream.Write(content, 0, content.Length); + + response.OutputStream.Dispose(); return response; } @@ -258,7 +305,7 @@ namespace MontoyaTech.Rest.Net var bytes = Encoding.UTF8.GetBytes(html); - response.ContentLength64 = html.Length; + response.ContentLength64 = bytes.Length; response.OutputStream.Write(bytes, 0, bytes.Length); diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index c8adbcd..52c90b4 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.3.7 + 1.3.8 Logo_Symbol_Black_Outline.png