-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DefaultVirtualPathProvider.cs
184 lines (158 loc) · 7.14 KB
/
DefaultVirtualPathProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using Orchard.Logging;
using Orchard.Exceptions;
namespace Orchard.FileSystems.VirtualPath {
public class DefaultVirtualPathProvider : IVirtualPathProvider {
public DefaultVirtualPathProvider() {
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
public virtual string GetDirectoryName(string virtualPath) {
return Path.GetDirectoryName(virtualPath).Replace(Path.DirectorySeparatorChar, '/');
}
public virtual IEnumerable<string> ListFiles(string path) {
return HostingEnvironment
.VirtualPathProvider
.GetDirectory(path)
.Files
.OfType<VirtualFile>()
.Select(f => VirtualPathUtility.ToAppRelative(f.VirtualPath));
}
public virtual IEnumerable<string> ListDirectories(string path) {
return HostingEnvironment
.VirtualPathProvider
.GetDirectory(path)
.Directories
.OfType<VirtualDirectory>()
.Select(d => VirtualPathUtility.ToAppRelative(d.VirtualPath));
}
public virtual string Combine(params string[] paths) {
return Path.Combine(paths).Replace(Path.DirectorySeparatorChar, '/');
}
public virtual string ToAppRelative(string virtualPath) {
if (IsMalformedVirtualPath(virtualPath))
return null;
try {
string result = VirtualPathUtility.ToAppRelative(virtualPath);
// In some cases, ToAppRelative doesn't normalize the path. In those cases,
// the path is invalid.
// Example:
// ApplicationPath: /Foo
// VirtualPath : ~/Bar/../Blah/Blah2
// Result : /Blah/Blah2 <= that is not an app relative path!
if (!result.StartsWith("~/")) {
Logger.Information("Path '{0}' cannot be made app relative: Path returned ('{1}') is not app relative.", virtualPath, result);
return null;
}
return result;
}
catch (Exception ex) {
if (ex.IsFatal()) {
throw;
}
// The initial path might have been invalid (e.g. path indicates a path outside the application root)
Logger.Information(ex, "Path '{0}' cannot be made app relative", virtualPath);
return null;
}
}
/// <summary>
/// We want to reject path that contains ".." going outside of the application root.
/// ToAppRelative does that already, but we want to do the same while avoiding exceptions.
///
/// Note: This method doesn't detect all cases of malformed paths, it merely checks
/// for *some* cases of malformed paths, so this is not a replacement for full virtual path
/// verification through VirtualPathUtilty methods.
/// In other words, !IsMalformed does *not* imply "IsWellformed".
/// </summary>
public bool IsMalformedVirtualPath(string virtualPath) {
if (string.IsNullOrEmpty(virtualPath))
return true;
if (virtualPath.IndexOf("..") >= 0) {
virtualPath = virtualPath.Replace(Path.DirectorySeparatorChar, '/');
string rootPrefix = virtualPath.StartsWith("~/") ? "~/" : virtualPath.StartsWith("/") ? "/" : "";
if (!string.IsNullOrEmpty(rootPrefix)) {
string[] terms = virtualPath.Substring(rootPrefix.Length).Split('/');
int depth = 0;
foreach (var term in terms) {
if (term == "..") {
if (depth == 0) {
Logger.Information("Path '{0}' cannot be made app relative: Too many '..'", virtualPath);
return true;
}
depth--;
}
else {
depth++;
}
}
}
}
return false;
}
public virtual Stream OpenFile(string virtualPath) {
return HostingEnvironment.VirtualPathProvider.GetFile(virtualPath).Open();
}
public virtual StreamWriter CreateText(string virtualPath) {
return File.CreateText(MapPath(virtualPath));
}
public virtual Stream CreateFile(string virtualPath) {
return File.Create(MapPath(virtualPath));
}
public virtual DateTime GetFileLastWriteTimeUtc(string virtualPath) {
#if true
// This code is less "pure" than the code below, but performs fewer file I/O, and it
// has been measured to make a significant difference (4x) on slow file systems.
return File.GetLastWriteTimeUtc(MapPath(virtualPath));
#else
var dependency = HostingEnvironment.VirtualPathProvider.GetCacheDependency(virtualPath, new[] { virtualPath }, DateTime.UtcNow);
if (dependency == null) {
throw new Exception(string.Format("Invalid virtual path: '{0}'", virtualPath));
}
return dependency.UtcLastModified;
#endif
}
public string GetFileHash(string virtualPath) {
return GetFileHash(virtualPath, new[] { virtualPath });
}
public string GetFileHash(string virtualPath, IEnumerable<string> dependencies) {
return HostingEnvironment.VirtualPathProvider.GetFileHash(virtualPath, dependencies);
}
public virtual void DeleteFile(string virtualPath) {
File.Delete(MapPath(virtualPath));
}
public virtual string MapPath(string virtualPath) {
return HostingEnvironment.MapPath(virtualPath);
}
public virtual bool FileExists(string virtualPath) {
return HostingEnvironment.VirtualPathProvider.FileExists(virtualPath);
}
public virtual bool TryFileExists(string virtualPath) {
if (IsMalformedVirtualPath(virtualPath))
return false;
try {
return FileExists(virtualPath);
}
catch (Exception ex) {
if (ex.IsFatal()) {
throw;
}
Logger.Information(ex, "File '{0}' can not be checked for existence. Assuming doesn't exist.", virtualPath);
return false;
}
}
public virtual bool DirectoryExists(string virtualPath) {
return HostingEnvironment.VirtualPathProvider.DirectoryExists(virtualPath);
}
public virtual void CreateDirectory(string virtualPath) {
Directory.CreateDirectory(MapPath(virtualPath));
}
public virtual void DeleteDirectory(string virtualPath) {
Directory.Delete(MapPath(virtualPath));
}
}
}