Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Tweaked logic on static view conventions
Browse files Browse the repository at this point in the history
Now it always does a case insensitive replace of the "requestedPath"
with the "contentPath" so mapping moo->Content works (added to the
main demo) and also using /content on Linux correctly maps to the
Content directory (correct casing is applied).
  • Loading branch information
grumpydev committed Oct 2, 2011
1 parent c5264bc commit 5c4013b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/Nancy.Demo.Hosting.Aspnet/DemoBootstrapper.cs
@@ -1,6 +1,7 @@
namespace Nancy.Demo.Hosting.Aspnet
{
using System.Collections.Generic;
using Conventions;
using Nancy.Session;
using Nancy.ViewEngines.Razor;

Expand All @@ -27,6 +28,8 @@ protected override void InitialiseInternal(TinyIoC.TinyIoCContainer container)
{
base.InitialiseInternal(container);

this.Conventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("moo", "Content"));

CookieBasedSessions.Enable(this);

this.AfterRequest += (ctx) =>
Expand Down
1 change: 1 addition & 0 deletions src/Nancy.Demo.Hosting.Aspnet/Views/routes.cshtml
Expand Up @@ -16,6 +16,7 @@
}
<li><a href='thiswillgivea404'>404</a></li>
<li><a href='@Url.Content("~/content/face.png")'>An image</a></li>
<li><a href='@Url.Content("~/moo/face.png")'>An image using alternative content convention</a></li>
<li><a href='@Url.Content("~/content/main.css")'>CSS</a></li>
<li><a href='@Url.Content("~/content/scripts.js")'>Some javascript</a></li>
</ul>
Expand Down
18 changes: 8 additions & 10 deletions src/Nancy/Conventions/StaticContentConventionBuilder.cs
Expand Up @@ -4,6 +4,7 @@ namespace Nancy.Conventions
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Responses;

/// <summary>
Expand All @@ -21,30 +22,30 @@ static StaticContentConventionBuilder()
/// <summary>
/// Adds a directory-based convention for static convention.
/// </summary>
/// <param name="requestPath">The path that should be matched with the request.</param>
/// <param name="contentPath">The path to where the content is stored in your application, relative to the root. If this is <see langword="null" /> then it will be the same as <paramref name="requestPath"/>.</param>
/// <param name="requestedPath">The path that should be matched with the request.</param>
/// <param name="contentPath">The path to where the content is stored in your application, relative to the root. If this is <see langword="null" /> then it will be the same as <paramref name="requestedPath"/>.</param>
/// <param name="allowedExtensions">A list of extensions that is valid for the conventions. If not supplied, all extensions are valid.</param>
/// <returns>A <see cref="GenericFileResponse"/> instance for the requested static contents if it was found, otherwise <see langword="null"/>.</returns>
public static Func<NancyContext, string, Response> AddDirectory(string requestPath, string contentPath = null, params string[] allowedExtensions)
public static Func<NancyContext, string, Response> AddDirectory(string requestedPath, string contentPath = null, params string[] allowedExtensions)
{
return (ctx, root) =>
{
var path =
ctx.Request.Path.TrimStart(new[] { '/' });
if (!path.StartsWith(requestPath, StringComparison.OrdinalIgnoreCase))
if (!path.StartsWith(requestedPath, StringComparison.OrdinalIgnoreCase))
{
return null;
}
var responseFactory =
ResponseFactoryCache.GetOrAdd(path, BuildContentDelegate(root, contentPath ?? requestPath, allowedExtensions));
ResponseFactoryCache.GetOrAdd(path, BuildContentDelegate(root, requestedPath, contentPath ?? requestedPath, allowedExtensions));
return responseFactory.Invoke();
};
}

private static Func<string, Func<Response>> BuildContentDelegate(string applicationRootPath, string contentPath, string[] allowedExtensions)
private static Func<string, Func<Response>> BuildContentDelegate(string applicationRootPath, string requestedPath, string contentPath, string[] allowedExtensions)
{
return requestPath =>
{
Expand All @@ -60,10 +61,7 @@ static StaticContentConventionBuilder()
return () => null;
}
if(!requestPath.StartsWith(contentPath, StringComparison.OrdinalIgnoreCase))
{
requestPath = String.Concat(contentPath, requestPath.Substring(requestPath.IndexOf("/")));
}
requestPath = Regex.Replace(requestPath, requestedPath, Regex.Escape(contentPath), RegexOptions.IgnoreCase);
var fileName = Path.Combine(applicationRootPath, requestPath);
Expand Down

0 comments on commit 5c4013b

Please sign in to comment.