From 1d60b9b08543a66e20de64832bb3ea02c71770aa Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Wed, 10 Aug 2016 17:53:29 +0100
Subject: [PATCH] Don't escape forward slashes - that breaks pathing.

---
 GalleryShare/Embed/DirectoryListing.xslt      |  4 ++--
 .../RequestRouter/RouteDirectoryListing.cs    | 19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/GalleryShare/Embed/DirectoryListing.xslt b/GalleryShare/Embed/DirectoryListing.xslt
index ff1e21f..2a4f4bf 100644
--- a/GalleryShare/Embed/DirectoryListing.xslt
+++ b/GalleryShare/Embed/DirectoryListing.xslt
@@ -28,7 +28,7 @@
 	<xsl:template match="ListingEntry[@Type='File']">
 		<a href="{Name}" class="preview-backdrop">
 			<figure class="preview file" style="background-image: url('{Name}?type=thumbnail');">
-				<figcaption><xsl:value-of select="Name" /></figcaption>
+				<figcaption><xsl:value-of select="DisplayName" /></figcaption>
 			</figure>
 		</a>
 	</xsl:template>
@@ -36,7 +36,7 @@
 		<a href="{Name}" class="preview-backdrop">
 			<figure class="preview directory">
 				(coming soon)
-				<figcaption><xsl:value-of select="Name" /></figcaption>
+				<figcaption><xsl:value-of select="DisplayName" /></figcaption>
 			</figure>
 		</a>
 	</xsl:template>
diff --git a/GalleryShare/RequestRouter/RouteDirectoryListing.cs b/GalleryShare/RequestRouter/RouteDirectoryListing.cs
index 57af64e..a0d5ab6 100644
--- a/GalleryShare/RequestRouter/RouteDirectoryListing.cs
+++ b/GalleryShare/RequestRouter/RouteDirectoryListing.cs
@@ -46,7 +46,7 @@ namespace GalleryShare.RequestRouter
 			await xmlData.WriteStartDocumentAsync();
 			await xmlData.WriteProcessingInstructionAsync("xml-stylesheet", "type=\"text/xsl\" href=\"/!Transform-DirListing.xslt\"");
 			await xmlData.WriteStartElementAsync(null, "DirectoryListing", null);
-			await xmlData.WriteElementStringAsync(null, "CurrentDirectory", null, Uri.EscapeDataString(cycle.Request.RawUrl));
+			await xmlData.WriteElementStringAsync(null, "CurrentDirectory", null, cycle.Request.RawUrl);
 			await xmlData.WriteStartElementAsync(null, "Contents", null);
 
 			foreach (string directoryName in dirDirectories)
@@ -54,7 +54,8 @@ namespace GalleryShare.RequestRouter
 				await xmlData.WriteStartElementAsync(null, "ListingEntry", null);
 				await xmlData.WriteAttributeStringAsync(null, "Type", null, "Directory");
 
-				await xmlData.WriteElementStringAsync(null, "Name", null, Uri.EscapeDataString("/" + directoryName.Substring(parentServer.ServingDirectory.Length)));
+				await xmlData.WriteElementStringAsync(null, "Name", null, escapePath("/" + directoryName.Substring(parentServer.ServingDirectory.Length)));
+				await xmlData.WriteElementStringAsync(null, "DisplayName", null, directoryName.Substring(parentServer.ServingDirectory.Length));
 				await xmlData.WriteElementStringAsync(null, "ItemCount", null, Directory.GetFileSystemEntries(directoryName).Length.ToString());
 
 				await xmlData.WriteEndElementAsync();
@@ -64,7 +65,8 @@ namespace GalleryShare.RequestRouter
 				await xmlData.WriteStartElementAsync(null, "ListingEntry", null);
 				await xmlData.WriteAttributeStringAsync(null, "Type", null, "File");
 
-						await xmlData.WriteElementStringAsync(null, "Name", null, Uri.EscapeDataString("/" + filename.Substring(parentServer.ServingDirectory.Length)));
+				await xmlData.WriteElementStringAsync(null, "Name", null, escapePath("/" + filename.Substring(parentServer.ServingDirectory.Length)));
+				await xmlData.WriteElementStringAsync(null, "DisplayName", null, "/" + filename.Substring(parentServer.ServingDirectory.Length));
 
 				await xmlData.WriteEndElementAsync();
 			}
@@ -72,6 +74,17 @@ namespace GalleryShare.RequestRouter
 			await xmlData.WriteEndDocumentAsync();
 			await xmlData.FlushAsync();
 		}
+
+		/// <summary>
+		/// Escapes a path to make it safe for sending to a browser.
+		/// Does not escape forward slashes ('/').
+		/// </summary>
+		/// <param name="path">The path to escape.</param>
+		/// <returns>An escaped version of the given path.</returns>
+		private string escapePath(string path)
+		{
+			return Uri.EscapeDataString(path).Replace("%2F", "/");
+		}
 	}
 }
 
