Improved SinglePage algorithm. Bumped package version to 1.6.6
This commit is contained in:
		@@ -18,6 +18,8 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public string TestFile = null;
 | 
					        public string TestFile = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public string IndexFile = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public ServeFileTests()
 | 
					        public ServeFileTests()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            this.BaseDirectory = Path.Combine(Environment.CurrentDirectory, "test");
 | 
					            this.BaseDirectory = Path.Combine(Environment.CurrentDirectory, "test");
 | 
				
			||||||
@@ -34,12 +36,17 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            if (!File.Exists(this.TestFile))
 | 
					            if (!File.Exists(this.TestFile))
 | 
				
			||||||
                File.WriteAllText(this.TestFile, "hello world");
 | 
					                File.WriteAllText(this.TestFile, "hello world");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            this.IndexFile = Path.Combine(this.BaseDirectory, "index.html");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (!File.Exists(this.IndexFile))
 | 
				
			||||||
 | 
					                File.WriteAllText(this.IndexFile, "hello world");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeMultiple_File_ShouldWork()
 | 
					        public void ServeMultiple_File_ShouldWork()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "/test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "/test.html", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            isDirectory.Should().BeFalse();
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -49,7 +56,7 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeMultiple_File_WithParentDirectory_ShouldWork()
 | 
					        public void ServeMultiple_File_WithParentDirectory_ShouldWork()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "test/test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "test/test.html", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            isDirectory.Should().BeFalse();
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -59,7 +66,7 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeMultiple_Directory_ShouldWork()
 | 
					        public void ServeMultiple_Directory_ShouldWork()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "/test2", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "/test2", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            isDirectory.Should().BeTrue();
 | 
					            isDirectory.Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -69,13 +76,13 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeMultiple_NavigatingUp_Should_NotWork()
 | 
					        public void ServeMultiple_NavigatingUp_Should_NotWork()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "../test.html", null, out string resolvedPath, out bool isDirectory).Should().BeFalse();
 | 
					            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "../test.html", "index.html", out string resolvedPath, out bool isDirectory).Should().BeFalse();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeMultiple_Correct_NavigatingUp_Should_Work()
 | 
					        public void ServeMultiple_Correct_NavigatingUp_Should_Work()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "a/b/../../test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "a/b/../../test.html", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            isDirectory.Should().BeFalse();
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -85,13 +92,23 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeMultiple_NavigatingUp_Multiple_Should_NotWork()
 | 
					        public void ServeMultiple_NavigatingUp_Multiple_Should_NotWork()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "test/../../test.html", null, out string resolvedPath, out bool isDirectory).Should().BeFalse();
 | 
					            HttpListenerResponseExtensions.ResolveMultiPagePath(this.BaseDirectory, "test/../../test.html", "index.html", out string resolvedPath, out bool isDirectory).Should().BeFalse();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeSingle_File_ShouldWork()
 | 
					        public void ServeSingle_Empty_Should_Work()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            resolvedPath.Should().BeEquivalentTo(this.IndexFile);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void ServeSingle_File_Should_Work()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/test.html", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            isDirectory.Should().BeFalse();
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -99,19 +116,9 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeSingle_File_WithParentDirectory_ShouldWork()
 | 
					        public void ServeSingle_Directory_Should_Work()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "test/test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/test2", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					 | 
				
			||||||
            isDirectory.Should().BeFalse();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            resolvedPath.Should().BeEquivalentTo(this.TestFile);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        [Fact]
 | 
					 | 
				
			||||||
        public void ServeSingle_Directory_ShouldWork()
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/test2", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            isDirectory.Should().BeTrue();
 | 
					            isDirectory.Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -119,9 +126,49 @@ namespace Rest.Net.Tests
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void ServeSingle_File_Route_ShouldWork()
 | 
					        public void ServeSingle_File_Route_Should_Work()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/a/b/test.html", null, out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/a/b/test.html", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            resolvedPath.Should().BeEquivalentTo(this.TestFile);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void ServeSingle_Directory_Route_Should_Work()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/a/b/test2", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            isDirectory.Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            resolvedPath.Should().BeEquivalentTo(this.TestDirectory);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void ServeSingle_File_Route_Invalid_Should_Work()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/a/b/c", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            resolvedPath.Should().BeEquivalentTo(this.IndexFile);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void ServeSingle_File_WithoutExtension_Should_Work()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/test", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            resolvedPath.Should().BeEquivalentTo(this.TestFile);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void ServeSingle_File_Route_WithoutExtension_Should_Work()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/a/b/c/test", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            isDirectory.Should().BeFalse();
 | 
					            isDirectory.Should().BeFalse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -672,14 +672,7 @@ namespace MontoyaTech.Rest.Net
 | 
				
			|||||||
            //Break the request 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();
 | 
					            var components = requestPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).ToList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //If the first component is the parent directory, then remove it
 | 
					            //Process the components and handle any directory navigations.
 | 
				
			||||||
            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++)
 | 
					            for (int i = 0; i < components.Count; i++)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (components[i].Trim() == "..")
 | 
					                if (components[i].Trim() == "..")
 | 
				
			||||||
@@ -711,17 +704,50 @@ namespace MontoyaTech.Rest.Net
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (components.Count == 0)
 | 
					            //Check the components and remove any that are invalid.
 | 
				
			||||||
                return false;
 | 
					            while (components.Count > 0)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                string path = Path.Combine(basePath, components[0]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (File.Exists(path) || Directory.Exists(path))
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                //If we have no more components and we are missing an extension and with .html a file exists, then use it.
 | 
				
			||||||
 | 
					                else if (components.Count == 1 && !components[0].Contains('.') && File.Exists(path + ".html"))
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    components[0] = components[0] + ".html";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    components.RemoveAt(0);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //Quirk, if the components is now empty, point to the indexFile
 | 
				
			||||||
 | 
					            if (components.Count == 0)
 | 
				
			||||||
 | 
					                components.Add(indexFile);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //Combine the path into an absolute path
 | 
				
			||||||
            var absolutePath = Path.Combine(basePath, components.Separate(Path.DirectorySeparatorChar));
 | 
					            var absolutePath = Path.Combine(basePath, components.Separate(Path.DirectorySeparatorChar));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //If a file exists, return true
 | 
				
			||||||
            if (File.Exists(absolutePath))
 | 
					            if (File.Exists(absolutePath))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                resolvedPath = absolutePath;
 | 
					                resolvedPath = absolutePath;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return true;
 | 
					                return true;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            //If a file exists with adding .html then use that
 | 
				
			||||||
 | 
					            else if (File.Exists(absolutePath + ".html"))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                resolvedPath = absolutePath + ".html";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                return true;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            //If a directory exists then use that
 | 
				
			||||||
            else if (Directory.Exists(absolutePath))
 | 
					            else if (Directory.Exists(absolutePath))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                resolvedPath = absolutePath;
 | 
					                resolvedPath = absolutePath;
 | 
				
			||||||
@@ -730,40 +756,12 @@ namespace MontoyaTech.Rest.Net
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                return true;
 | 
					                return true;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            //Otherwise redirect to index.html
 | 
				
			||||||
            else
 | 
					            else
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                //Try to components that don't exist and try again
 | 
					                resolvedPath = Path.Combine(basePath, indexFile);
 | 
				
			||||||
                while (components.Count > 0)
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    string path = Path.Combine(basePath, components[0]);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (File.Exists(path) || Directory.Exists(path))
 | 
					                return true;
 | 
				
			||||||
                        break;
 | 
					 | 
				
			||||||
                    else
 | 
					 | 
				
			||||||
                        components.RemoveAt(0);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (components.Count == 0)
 | 
					 | 
				
			||||||
                    return false;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                absolutePath = Path.Combine(basePath, components.Separate(Path.PathSeparator));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (File.Exists(absolutePath))
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    resolvedPath = absolutePath;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    return true;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                else if (Directory.Exists(absolutePath))
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    resolvedPath = absolutePath;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    isDirectory = true;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    return true;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return false;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@
 | 
				
			|||||||
    <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
 | 
					    <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
 | 
				
			||||||
    <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
 | 
					    <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
 | 
				
			||||||
    <GenerateDocumentationFile>True</GenerateDocumentationFile>
 | 
					    <GenerateDocumentationFile>True</GenerateDocumentationFile>
 | 
				
			||||||
    <Version>1.6.5</Version>
 | 
					    <Version>1.6.6</Version>
 | 
				
			||||||
    <PackageReleaseNotes></PackageReleaseNotes>
 | 
					    <PackageReleaseNotes></PackageReleaseNotes>
 | 
				
			||||||
    <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
 | 
					    <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
 | 
				
			||||||
  </PropertyGroup>
 | 
					  </PropertyGroup>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user