Changed all response data extensions to no longer use chunking since we can specify the content length. Bumped package version to 1.3.8

This commit is contained in:
MattMo 2023-03-03 16:12:13 -08:00
parent 69e71bff0b
commit 97fecdbe6f
2 changed files with 75 additions and 28 deletions

View File

@ -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);

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.3.7</Version>
<Version>1.3.8</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>