From 4617f861fcc7d86bb83cf2c35fc62e863638f0da Mon Sep 17 00:00:00 2001 From: MattMo Date: Thu, 29 Jun 2023 08:21:54 -0700 Subject: [PATCH] Added support for cases where the parent directory is included in the request path. Added unit tests to check this case. Bumped package version to 1.6.4 --- Rest.Net.Tests/ServeFileTests.cs | 20 ++++++++++++++++++++ Rest.Net/HttpListenerResponseExtensions.cs | 15 +++++++++++++-- Rest.Net/Rest.Net.csproj | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Rest.Net.Tests/ServeFileTests.cs b/Rest.Net.Tests/ServeFileTests.cs index 77478fa..1595b39 100644 --- a/Rest.Net.Tests/ServeFileTests.cs +++ b/Rest.Net.Tests/ServeFileTests.cs @@ -46,6 +46,16 @@ namespace Rest.Net.Tests resolvedPath.Should().BeEquivalentTo(this.TestFile); } + [Fact] + public void ServeMultiple_File_WithParentDirectory_ShouldWork() + { + HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "test/test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue(); + + isDirectory.Should().BeFalse(); + + resolvedPath.Should().BeEquivalentTo(this.TestFile); + } + [Fact] public void ServeMultiple_Directory_ShouldWork() { @@ -88,6 +98,16 @@ namespace Rest.Net.Tests resolvedPath.Should().BeEquivalentTo(this.TestFile); } + [Fact] + public void ServeSingle_File_WithParentDirectory_ShouldWork() + { + HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "test/test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue(); + + isDirectory.Should().BeFalse(); + + resolvedPath.Should().BeEquivalentTo(this.TestFile); + } + [Fact] public void ServeSingle_Directory_ShouldWork() { diff --git a/Rest.Net/HttpListenerResponseExtensions.cs b/Rest.Net/HttpListenerResponseExtensions.cs index bbff058..4f76d09 100644 --- a/Rest.Net/HttpListenerResponseExtensions.cs +++ b/Rest.Net/HttpListenerResponseExtensions.cs @@ -547,9 +547,20 @@ namespace MontoyaTech.Rest.Net if (string.IsNullOrWhiteSpace(requestPath) || requestPath == "/" || requestPath == ".") requestPath = indexFile; - //Break th erequest path into it's components so we can enfore staying in the base path. + //See if there is a parent directory in the basePath, if so get it + var parentDirectory = basePath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); + + //Break the request path into it's components so we can enfore staying in the base path. var components = requestPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).ToList(); + //If the first component is the parent directory, then remove it + if (components.Count > 0 && components[0].Equals(parentDirectory, StringComparison.CurrentCultureIgnoreCase)) + components.RemoveAt(0); + + //Quirk, if the components is now empty, point to the indexFile + if (components.Count == 0) + components.Add(indexFile); + for (int i = 0; i < components.Count; i++) { if (components[i].Trim() == "..") @@ -655,7 +666,7 @@ namespace MontoyaTech.Rest.Net if (string.IsNullOrWhiteSpace(requestPath) || requestPath == "/" || requestPath == ".") requestPath = indexFile; - //Break th erequest path into it's components so we can enfore staying in the base path. + //Break the request path into it's components so we can enfore staying in the base path. var components = requestPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).ToList(); for (int i = 0; i < components.Count; i++) diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index a77cf3b..fc3550e 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.6.3 + 1.6.4 Logo_Symbol_Black_Outline.png