From 69e71bff0b091d1441dd87197e36d8cf29c219f8 Mon Sep 17 00:00:00 2001 From: MattMo Date: Fri, 3 Mar 2023 15:49:39 -0800 Subject: [PATCH] Cleaned up documentation. Response extension now sets the content length for a few extensions. Added WithNoBody response extension. Bumped package version to 1.3.7 --- Rest.Net/HttpListenerResponseExtensions.cs | 23 ++++++++++++++++++++++ Rest.Net/Rest.Net.csproj | 2 +- Rest.Net/Route.cs | 6 ++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Rest.Net/HttpListenerResponseExtensions.cs b/Rest.Net/HttpListenerResponseExtensions.cs index 8e376ef..a11fa94 100644 --- a/Rest.Net/HttpListenerResponseExtensions.cs +++ b/Rest.Net/HttpListenerResponseExtensions.cs @@ -16,6 +16,18 @@ namespace MontoyaTech.Rest.Net /// public static class HttpListenerResponseExtensions { + /// + /// Sets the response to have no body. + /// + /// + /// + public static HttpListenerResponse WithNoBody(this HttpListenerResponse response) + { + response.ContentLength64 = 0; + + return response; + } + /// /// Sets the response content type to text and writes the given text to it. /// @@ -26,7 +38,10 @@ namespace MontoyaTech.Rest.Net { response.ContentType = "text/plain; charset=utf-8"; + response.ContentLength64 = text.Length; + var bytes = Encoding.UTF8.GetBytes(text); + response.OutputStream.Write(bytes, 0, bytes.Length); return response; @@ -63,6 +78,9 @@ namespace MontoyaTech.Rest.Net response.ContentType = "application/json; charset=utf-8"; var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj)); + + response.ContentLength64 = bytes.Length; + response.OutputStream.Write(bytes, 0, bytes.Length); return response; @@ -133,6 +151,7 @@ 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) @@ -218,6 +237,7 @@ namespace MontoyaTech.Rest.Net response.ContentType = mimeType; 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) @@ -237,6 +257,9 @@ namespace MontoyaTech.Rest.Net response.ContentType = "text/html; charset=utf-8"; var bytes = Encoding.UTF8.GetBytes(html); + + response.ContentLength64 = html.Length; + response.OutputStream.Write(bytes, 0, bytes.Length); return response; diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 8a59b13..c8adbcd 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.6 + 1.3.7 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/Route.cs b/Rest.Net/Route.cs index 304442a..7816a5b 100644 --- a/Rest.Net/Route.cs +++ b/Rest.Net/Route.cs @@ -28,7 +28,7 @@ namespace MontoyaTech.Rest.Net private Func Target; /// - /// Whether or not to close the response after the route is invoked. + /// Whether or not to close the response after the route is invoked, default is true. /// public bool CloseResponse = true; @@ -38,9 +38,8 @@ namespace MontoyaTech.Rest.Net internal Route() { } /// - /// Creates a new route with a given method, syntax, target and optional close response flag. + /// Creates a new route with a given method, syntax, target and optional close response flag which defaults to true. /// - /// /// /// /// @@ -63,7 +62,6 @@ namespace MontoyaTech.Rest.Net /// /// Creates a new route with a given method, syntax, target and optional close response flag. /// - /// /// /// ///