diff --git a/Rest.Net.Tests/ServeFileTests.cs b/Rest.Net.Tests/ServeFileTests.cs
index 1595b39..32373ea 100644
--- a/Rest.Net.Tests/ServeFileTests.cs
+++ b/Rest.Net.Tests/ServeFileTests.cs
@@ -18,6 +18,8 @@ namespace Rest.Net.Tests
public string TestFile = null;
+ public string IndexFile = null;
+
public ServeFileTests()
{
this.BaseDirectory = Path.Combine(Environment.CurrentDirectory, "test");
@@ -34,12 +36,17 @@ namespace Rest.Net.Tests
if (!File.Exists(this.TestFile))
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]
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();
@@ -49,7 +56,7 @@ namespace Rest.Net.Tests
[Fact]
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();
@@ -59,7 +66,7 @@ namespace Rest.Net.Tests
[Fact]
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();
@@ -69,13 +76,13 @@ namespace Rest.Net.Tests
[Fact]
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]
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();
@@ -85,13 +92,23 @@ namespace Rest.Net.Tests
[Fact]
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]
- 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();
@@ -99,19 +116,9 @@ namespace Rest.Net.Tests
}
[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();
-
- 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();
+ HttpListenerResponseExtensions.ResolveSinglePagePath(this.BaseDirectory, "/test2", "index.html", out string resolvedPath, out bool isDirectory).Should().BeTrue();
isDirectory.Should().BeTrue();
@@ -119,9 +126,49 @@ namespace Rest.Net.Tests
}
[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();
diff --git a/Rest.Net/HttpListenerResponseExtensions.cs b/Rest.Net/HttpListenerResponseExtensions.cs
index a662947..153b1cf 100644
--- a/Rest.Net/HttpListenerResponseExtensions.cs
+++ b/Rest.Net/HttpListenerResponseExtensions.cs
@@ -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.
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);
-
+ //Process the components and handle any directory navigations.
for (int i = 0; i < components.Count; i++)
{
if (components[i].Trim() == "..")
@@ -711,17 +704,50 @@ namespace MontoyaTech.Rest.Net
}
}
- if (components.Count == 0)
- return false;
+ //Check the components and remove any that are invalid.
+ 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));
+ //If a file exists, return true
if (File.Exists(absolutePath))
{
resolvedPath = absolutePath;
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))
{
resolvedPath = absolutePath;
@@ -730,40 +756,12 @@ namespace MontoyaTech.Rest.Net
return true;
}
+ //Otherwise redirect to index.html
else
{
- //Try to components that don't exist and try again
- while (components.Count > 0)
- {
- string path = Path.Combine(basePath, components[0]);
+ resolvedPath = Path.Combine(basePath, indexFile);
- if (File.Exists(path) || Directory.Exists(path))
- 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;
+ return true;
}
}
}
diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj
index 9c3467e..88c896a 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.5
+ 1.6.6
Logo_Symbol_Black_Outline.png