From f882a74c6dfccc0d78197a6480d1bb781c35bfd5 Mon Sep 17 00:00:00 2001 From: MattMo Date: Fri, 24 Feb 2023 12:05:57 -0800 Subject: [PATCH] Added compressed response extensions and added more to the example program. Bumped package version to 1.3.3 --- Rest.Net.Example/Program.cs | 18 ++++++ Rest.Net/HttpListenerResponseExtensions.cs | 70 ++++++++++++++++++++++ Rest.Net/Rest.Net.csproj | 2 +- 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/Rest.Net.Example/Program.cs b/Rest.Net.Example/Program.cs index 20d5087..924b699 100644 --- a/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Program.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; +using System.IO; using MontoyaTech.Rest.Net; using System.Net.Mime; @@ -29,9 +30,13 @@ namespace MontoyaTech.Rest.Net.Example public static void Main(string[] args) { + File.WriteAllText("test.txt", "hello from a file"); + var listener = new RouteListener(8080, new Route(HttpRequestMethod.Get, "/status", Status), new Route(HttpRequestMethod.Post, "/add/{a}/{b}", Add), + new Route(HttpRequestMethod.Get, "/compress", Compress), + new Route(HttpRequestMethod.Get, "/file/compress", CompressFile), new Route(HttpRequestMethod.Get, "/auth/{username}", Exists), new Route(HttpRequestMethod.Post, "/auth/signup", Signup), new Route(HttpRequestMethod.Get, "/auth/", Json) @@ -90,6 +95,19 @@ namespace MontoyaTech.Rest.Net.Example return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString()); } + [RouteGroup("Test")] + [RouteResponse(typeof(string))] + public static HttpListenerResponse Compress(HttpListenerContext context) + { + return context.Response.WithStatus(HttpStatusCode.OK).WithCompressedText("hello world"); + } + + [RouteGroup("Test")] + [RouteResponse(typeof(string))] + public static HttpListenerResponse CompressFile(HttpListenerContext context) + { + return context.Response.WithStatus(HttpStatusCode.OK).WithCompressedFile("test.txt"); + } [RouteGroup("Test")] [RouteRequest(typeof(User))] diff --git a/Rest.Net/HttpListenerResponseExtensions.cs b/Rest.Net/HttpListenerResponseExtensions.cs index ef1380a..0cd202d 100644 --- a/Rest.Net/HttpListenerResponseExtensions.cs +++ b/Rest.Net/HttpListenerResponseExtensions.cs @@ -6,6 +6,8 @@ using System.Text; using System.Threading.Tasks; using System.IO; using Newtonsoft.Json; +using System.IO.Compression; +using System.IO.Pipes; namespace MontoyaTech.Rest.Net { @@ -30,6 +32,26 @@ namespace MontoyaTech.Rest.Net return response; } + /// + /// Sets the response content type to text and writes the given text compressed to it. + /// + /// + /// + /// This response. + 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); + + return response; + } + /// /// Sets the response content type to json and serializes the object as json and writes it. /// @@ -46,6 +68,26 @@ namespace MontoyaTech.Rest.Net return response; } + /// + /// Sets the response content type to json and writes the given json compressed to it. + /// + /// + /// + /// This response. + public static HttpListenerResponse WithCompressedJson(this HttpListenerResponse response, object obj) + { + response.ContentType = "application/json; charset=utf-8"; + response.Headers.Add("Content-Encoding", "gzip"); + + 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); + + return response; + } + /// /// Sets the response content type to a file and writes the given file content to the response. /// @@ -72,6 +114,34 @@ namespace MontoyaTech.Rest.Net return response; } + /// + /// Sets the response content type to a file and compresses the given file content to the response. + /// + /// + /// The path of the file to send. + /// The mime type of the file to send, if null, it will be auto detected if possible. + /// This response. + public static HttpListenerResponse WithCompressedFile(this HttpListenerResponse response, string filePath, string mimeType = null) + { + if (string.IsNullOrWhiteSpace(filePath)) + throw new ArgumentException("filePath must not be null or empty"); + + if (string.IsNullOrWhiteSpace(mimeType)) + mimeType = Path.GetExtension(filePath).GetMimeType(); + + 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); + + return response; + } + /// /// Sets the response content type to html and writes the given html to it. /// diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 60b1412..83a6e69 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.2 + 1.3.3 Logo_Symbol_Black_Outline.png