-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add VirtualPathProvider infrastructure (#490)
This enables usage of these types, but does not hook it up to a ASP.NET Core related concept. For now, that is left to the user if they need the API.
- Loading branch information
1 parent
cc26216
commit 6af1a41
Showing
11 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Microsoft.AspNetCore.SystemWebAdapters/Hosting/VirtualDirectory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
|
||
namespace System.Web.Hosting; | ||
|
||
public abstract class VirtualDirectory : VirtualFileBase | ||
{ | ||
protected VirtualDirectory(string virtualPath) | ||
: base(VirtualPathUtility.AppendTrailingSlash(virtualPath)) | ||
{ | ||
} | ||
|
||
public override bool IsDirectory => true; | ||
|
||
/// <summary> | ||
/// Returns an object that enumerates all the children VirtualDirectory's of this directory. | ||
/// </summary> | ||
public abstract IEnumerable Directories { get; } | ||
|
||
/// <summary> | ||
///Returns an object that enumerates all the children VirtualFile's of this directory. | ||
/// </summary> | ||
public abstract IEnumerable Files { get; } | ||
|
||
/// <summary> | ||
/// Returns an object that enumerates all the children VirtualDirectory's and VirtualFiles of this directory. | ||
/// </summary> | ||
public abstract IEnumerable Children { get; } | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Microsoft.AspNetCore.SystemWebAdapters/Hosting/VirtualFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO; | ||
|
||
namespace System.Web.Hosting; | ||
|
||
public abstract class VirtualFile : VirtualFileBase | ||
{ | ||
protected VirtualFile(string virtualPath) | ||
: base(virtualPath) | ||
{ | ||
} | ||
|
||
public override bool IsDirectory => false; | ||
|
||
public abstract Stream Open(); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Microsoft.AspNetCore.SystemWebAdapters/Hosting/VirtualFileBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Web.Hosting; | ||
|
||
public abstract class VirtualFileBase | ||
{ | ||
private protected VirtualFileBase(string virtualPath) | ||
{ | ||
VirtualPath = virtualPath; | ||
} | ||
|
||
public virtual string Name => VirtualPathUtility.GetFileName(VirtualPath); | ||
|
||
public string VirtualPath { get; } | ||
|
||
public abstract bool IsDirectory { get; } | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Microsoft.AspNetCore.SystemWebAdapters/Hosting/VirtualPathProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Web.Caching; | ||
using System.Web.Util; | ||
|
||
namespace System.Web.Hosting; | ||
|
||
public abstract class VirtualPathProvider | ||
{ | ||
internal virtual void Initialize(VirtualPathProvider? previous) | ||
{ | ||
Previous = previous; | ||
Initialize(); | ||
} | ||
|
||
protected virtual void Initialize() | ||
{ | ||
} | ||
|
||
protected internal VirtualPathProvider? Previous { get; private set; } | ||
|
||
public virtual string? GetFileHash(string virtualPath, IEnumerable virtualPathDependencies) | ||
=> Previous?.GetFileHash(virtualPath, virtualPathDependencies); | ||
|
||
public virtual CacheDependency? GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) | ||
=> Previous?.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); | ||
|
||
public virtual bool FileExists(string virtualPath) => Previous != null && Previous.FileExists(virtualPath); | ||
|
||
public virtual bool DirectoryExists(string virtualDir) => Previous != null && Previous.DirectoryExists(virtualDir); | ||
|
||
public virtual VirtualFile? GetFile(string virtualPath) => Previous?.GetFile(virtualPath); | ||
|
||
internal VirtualFile? GetFileWithCheck(string virtualPath) | ||
{ | ||
var virtualFile = GetFile(virtualPath); | ||
|
||
if (virtualFile == null) | ||
{ | ||
return null; | ||
} | ||
|
||
// Make sure the VirtualFile's path is the same as what was passed to GetFile | ||
if (!string.Equals(virtualPath, virtualFile.VirtualPath, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
throw new HttpException($"Bad virtual path {virtualFile} in VirtuaPathBase"); | ||
} | ||
|
||
return virtualFile; | ||
} | ||
|
||
public virtual VirtualDirectory? GetDirectory(string virtualDir) => Previous?.GetDirectory(virtualDir); | ||
|
||
public virtual string CombineVirtualPaths(string basePath, string relativePath) | ||
{ | ||
if (string.IsNullOrEmpty(basePath)) | ||
{ | ||
throw new ArgumentException($"'{nameof(basePath)}' cannot be null or empty.", nameof(basePath)); | ||
} | ||
|
||
var baseDir = UrlPath.GetDirectory(basePath); | ||
|
||
// By default, just combine them normally | ||
return VirtualPathUtility.Combine(baseDir, relativePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters