Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Support to other default pages #216

Merged
merged 2 commits into from
Feb 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/Pretzel/WebHost/FileContentProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.IO;

namespace Pretzel
Expand Down Expand Up @@ -33,6 +34,21 @@ public bool IsAvailable(string request)
return File.Exists(file);
}

/// <summary>
/// Checks if request points to a directory
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public bool IsDirectory(string request)
{
if (string.IsNullOrEmpty(basePath))
{
throw new InvalidOperationException("basePath required");
}

return Directory.Exists(GetFullPath(request));
}

/// <summary>
/// Read file
/// </summary>
Expand Down Expand Up @@ -78,23 +94,40 @@ public byte[] GetBinaryContent(string request)
return fileContents;
}

private static readonly string[] defaultPages = { "index.html", "index.htm", "default.htm" };

/// <summary>
/// Get the path for the page to send to the user
/// </summary>
/// <param name="request"> </param>
/// <returns>Path to file</returns>
private string GetRequestedPage(string request)
{
string requestString = basePath + request;
//#AS:2013/05/02: quick and dirty fix for issue #125
requestString = requestString.Replace("%20", " ");
if (requestString.EndsWith("/", StringComparison.Ordinal))
string requestString = GetFullPath(request);

//if it's a directory, try to find its default page
if (IsDirectory(request))
{
// Load index.html as default
requestString = Path.Combine(requestString, "index.html");
string defaultPage = defaultPages
.Select(page => Path.Combine(requestString, page))
.Where(page => File.Exists(page))
.FirstOrDefault();

if (defaultPage != null)
return defaultPage;
}

return requestString;
}

/// <summary>
/// Combines and unescapes the path for the request
/// </summary>
/// <param name="request"></param>
/// <returns>Path to the file</returns>
private string GetFullPath(string request)
{
return Uri.UnescapeDataString(Path.Combine(basePath + request));
}
}
}
1 change: 1 addition & 0 deletions src/Pretzel/WebHost/IWebContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IWebContent
{
void SetBasePath(string path);
bool IsAvailable(string request);
bool IsDirectory(string request);
string GetContent(string request);
byte[] GetBinaryContent(string request);
}
Expand Down
11 changes: 11 additions & 0 deletions src/Pretzel/WebHost/MemoryContentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,16 @@ public byte[] GetBinaryContent(string request)
{
throw new NotImplementedException();
}

/// <summary>
/// Checks if request points to a directory
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public bool IsDirectory(string request)
{
return false;
}

}
}
6 changes: 6 additions & 0 deletions src/Pretzel/WebHost/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ Task NewServerCallback(IDictionary<string, object> env)
response.Headers["Content-Length"] = new[] { fileContents.Length.ToString(CultureInfo.InvariantCulture) };
response.Write(new ArraySegment<byte>(fileContents));
}
else if (content.IsDirectory(path) && !path.EndsWith("/"))
{
//if path is a directory without trailing slash, redirects to the same url with a trailing slash
var response = new Response(env) { Status = "301 Moved Permanently" };
response.Headers["Location"] = new[] { String.Format("http://localhost:{0}{1}/", Port, path) };
}
else
{
var response = new Response(env) { ContentType = path.MimeType() };
Expand Down