From 8ee2fb129ec6a1635336e7821c51a31c07be74b8 Mon Sep 17 00:00:00 2001
From: "Adam D. Ruppe" <destructionator@gmail.com>
Date: Wed, 2 Mar 2022 18:28:22 -0500
Subject: [PATCH] easier to use cgi mode on cmd line

---
 cgi.d | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/cgi.d b/cgi.d
index 9189052..5fc18af 100644
--- a/cgi.d
+++ b/cgi.d
@@ -1031,7 +1031,8 @@ class Cgi {
 			// to be slow if they did that. The spec says it is always there though.
 			// And it has worked reliably for me all year in the live environment,
 			// but some servers might be different.
-			auto contentLength = to!size_t(getenv("CONTENT_LENGTH"));
+			auto cls = getenv("CONTENT_LENGTH");
+			auto contentLength = to!size_t(cls.length ? cls : "0");
 
 			immutable originalContentLength = contentLength;
 			if(contentLength) {
@@ -9795,7 +9796,12 @@ private string nextPieceFromSlash(ref string remainingUrl) {
 	return ident;
 }
 
+/++
+	UDA used to indicate to the [dispatcher] that a trailing slash should always be added to or removed from the url. It will do it as a redirect header as-needed.
++/
 enum AddTrailingSlash;
+/// ditto
+enum RemoveTrailingSlash;
 
 private auto serveApiInternal(T)(string urlPrefix) {
 
@@ -9976,6 +9982,12 @@ private auto serveApiInternal(T)(string urlPrefix) {
 								cgi.setResponseLocation(cgi.pathInfo ~ "/");
 								return true;
 							}
+						} else static if(is(attr == RemoveTrailingSlash)) {
+							if(remainingUrl !is null) {
+								cgi.setResponseLocation(cgi.pathInfo[0 .. lastIndexOf(cgi.pathInfo, "/")]);
+								return true;
+							}
+
 						} else static if(__traits(isSame, AutomaticForm, attr)) {
 							automaticForm = true;
 						}