Skip to content

Commit

Permalink
Fix #278 (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeago authored and geoperez committed Apr 23, 2019
1 parent b611869 commit 006a357
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Unosquare.Labs.EmbedIO/Core/VirtualPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ public VirtualPath(string baseUrlPath, string baseLocalPath)

public string BaseLocalPath { get; }

internal bool CanMapUrlPath(string urlPath) => urlPath.StartsWith(BaseUrlPath, StringComparison.Ordinal);
// Base paths are forced to end with a slash,
// while requested paths are forced to NOT end with a slash.
// Virtual path "/media/" can map "/media/file.jpg"
// but it can also map "/media" (without the slash).

internal bool CanMapUrlPath(string urlPath)
=> urlPath.StartsWith(BaseUrlPath, StringComparison.Ordinal)
|| (urlPath.Length == BaseUrlPath.Length - 1 && BaseUrlPath.StartsWith(urlPath, StringComparison.Ordinal));

internal bool TryMapUrlPathLoLocalPath(string urlPath, out string localPath)
{
Expand All @@ -34,7 +41,11 @@ internal bool TryMapUrlPathLoLocalPath(string urlPath, out string localPath)
return false;
}

var relativeUrlPath = urlPath.Substring(BaseUrlPath.Length);
// The only case where CanMapUrlPath returns true for a path shorter than BaseUrlPath
// is urlPath == (BaseUrlPath minus the final slash).
var relativeUrlPath = urlPath.Length < BaseUrlPath.Length
? string.Empty
: urlPath.Substring(BaseUrlPath.Length);
localPath = Path.Combine(BaseLocalPath, relativeUrlPath.Replace('/', Path.DirectorySeparatorChar));
return true;
}
Expand Down

0 comments on commit 006a357

Please sign in to comment.