File: VirtualPathProvider.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (422 lines) | stat: -rw-r--r-- 13,202 bytes parent folder | download | duplicates (7)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//------------------------------------------------------------------------------
// <copyright file="VirtualPathProvider.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.Hosting {

using System;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Configuration;
using System.Web.Caching;
using System.Web.Util;
using Util=System.Web.UI.Util;
using System.Security.Permissions;

/*
 * Base class for virtual path providers
 */

public abstract class VirtualPathProvider: MarshalByRefObject {

    private VirtualPathProvider _previous;


    public override Object InitializeLifetimeService(){
        return null; // never expire lease
    }

    internal virtual void Initialize(VirtualPathProvider previous) {
        _previous = previous;
        Initialize();
    }

    /*
     * Initialize is called on the provider after it is registered.
     */

    protected virtual void Initialize() {
    }

    /*
     * Gives the provider access to the Previous provider.  It can be used to delegate some of the calls 
     * (e.g. as a way of having some files comes from the file system, and others from the database)
     */

    protected internal VirtualPathProvider Previous { get { return _previous; } }

    /*
     * Asks the provider for a hash string based on the state of a set of virtual paths.
     * The primary virtualPath is also passed in by itself.
     * If they match, the cached data held by the user of the provider is still
     * valid.  Otherwise, it should be discarded, and a new version needs to be
     * obtained via GetFile/GetDirectory.
     */

    public virtual string GetFileHash(string virtualPath, IEnumerable virtualPathDependencies) {

        // Delegate to the previous VirtualPathProvider, if any

        if (_previous == null)
            return null;

        return _previous.GetFileHash(virtualPath, virtualPathDependencies);
    }

    internal string GetFileHash(VirtualPath virtualPath, IEnumerable virtualPathDependencies) {
        return GetFileHash(virtualPath.VirtualPathString, virtualPathDependencies);
    }

    /*
     * Asks the provider for a CacheDependency that will be invalidated if any of the
     * input files become invalid.
     * utcStart contains the time (UTC) at which the files were read.  Any change to the file
     * made after that time (even if the change is in the past) should invalidate the
     * CacheDependency.
     * If the provider doesn't support using a CacheDependency, it should return null,
     * or simply not override GetCacheDependency (the base implementation returns null).
     */

    public virtual CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
        // Delegate to the previous VirtualPathProvider, if any
        if (_previous == null) {
            return null;
        }

        return _previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }

    internal CacheDependency GetCacheDependency(VirtualPath virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
        return GetCacheDependency(virtualPath.VirtualPathString, virtualPathDependencies, utcStart);
    }

    /*
     * Returns whether the file described by the virtual path exists from
     * the point of view of this provider.
     */

    public virtual bool FileExists(string virtualPath) {

        // Delegate to the previous VirtualPathProvider, if any

        if (_previous == null)
            return false;

        return _previous.FileExists(virtualPath);
    }

    internal bool FileExists(VirtualPath virtualPath) {
        return FileExists(virtualPath.VirtualPathString);
    }

    /*
     * Returns whether the directory described by the virtual path exists from
     * the point of view of this provider.
     */

    public virtual bool DirectoryExists(string virtualDir) {

        // Delegate to the previous VirtualPathProvider, if any

        if (_previous == null)
            return false;

        return _previous.DirectoryExists(virtualDir);
    }

    internal bool DirectoryExists(VirtualPath virtualDir) {
        return DirectoryExists(virtualDir.VirtualPathString);
    }

    /*
     * Returns a VirtualFile object for the passed in virtual path
     */

    public virtual VirtualFile GetFile(string virtualPath) {

        // Delegate to the previous VirtualPathProvider, if any

        if (_previous == null)
            return null;

        return _previous.GetFile(virtualPath);
    }

    internal VirtualFile GetFile(VirtualPath virtualPath) {
        return GetFileWithCheck(virtualPath.VirtualPathString);
    }

    internal VirtualFile GetFileWithCheck(string virtualPath) {

        VirtualFile virtualFile = GetFile(virtualPath);

        if (virtualFile == null)
            return null;

        // Make sure the VirtualFile's path is the same as what was passed to GetFile
        if (!StringUtil.EqualsIgnoreCase(virtualPath, virtualFile.VirtualPath)) {
            throw new HttpException(SR.GetString(SR.Bad_VirtualPath_in_VirtualFileBase,
                "VirtualFile", virtualFile.VirtualPath, virtualPath));
        }

        return virtualFile;
    }


    /*
     * Returns a VirtualDirectory object for the passed in virtual path
     */

    public virtual VirtualDirectory GetDirectory(string virtualDir) {

        // Delegate to the previous VirtualPathProvider, if any

        if (_previous == null)
            return null;

        return _previous.GetDirectory(virtualDir);
    }

    internal VirtualDirectory GetDirectory(VirtualPath virtualDir) {
        Debug.Assert(virtualDir.HasTrailingSlash);
        return GetDirectoryWithCheck(virtualDir.VirtualPathString);
    }

    internal VirtualDirectory GetDirectoryWithCheck(string virtualPath) {

        VirtualDirectory virtualDir = GetDirectory(virtualPath);

        if (virtualDir == null)
            return null;

        // Make sure the VirtualDirectory's path is the same as what was passed to GetDirectory
        if (!StringUtil.EqualsIgnoreCase(virtualPath, virtualDir.VirtualPath)) {
            throw new HttpException(SR.GetString(SR.Bad_VirtualPath_in_VirtualFileBase,
                "VirtualDirectory", virtualDir.VirtualPath, virtualPath));
        }

        return virtualDir;
    }

#if OLD
    /*
     * Allow the file provider to replace a virtual path by a different (ghosted) one.  This can
     * be used to have several virtual paths be mapped to the same compilation, hence saving
     * resources.  If the path is not ghosted, this method must return null.
     */

    public virtual string GetGhostedVirtualPath(string virtualPath) {

        // By default, it's not supported
        return null;
    }
#endif

    /*
     * Returns a cache key to be used for this virtual path.  If not overridden, this returns
     * null meaning that the virtual path itself should be used as the cache key.
     * This should only be overridden to achieve Sharepoint like ghosting behavior.
     */

    public virtual string GetCacheKey(string virtualPath) {

        // By default, return null, meaning use a key based on the virtual path
        return null;
    }

    internal string GetCacheKey(VirtualPath virtualPath) {
        return GetCacheKey(virtualPath.VirtualPathString);
    }

    /*
     * Allows the VirtualPathProvider to use custom logic to combine virtual path.
     * This can be used to give a special meaning to app relative paths (DevDiv 31438).
     * basePath is the path to the file in which the relative reference was found.
     */
    public virtual string CombineVirtualPaths(string basePath, string relativePath) {

        string baseDir = null;
        if (!String.IsNullOrEmpty(basePath))
            baseDir = UrlPath.GetDirectory(basePath);

        // By default, just combine them normally
        return UrlPath.Combine(baseDir, relativePath);
    }

    internal VirtualPath CombineVirtualPaths(VirtualPath basePath, VirtualPath relativePath) {
        string virtualPath = CombineVirtualPaths(basePath.VirtualPathString,
            relativePath.VirtualPathString);
        return VirtualPath.Create(virtualPath);
    }

    /*
     * Helper method to open a file from its virtual path
     */

    public static Stream OpenFile(string virtualPath) {
        VirtualPathProvider vpathProvider = HostingEnvironment.VirtualPathProvider;
        VirtualFile vfile = vpathProvider.GetFileWithCheck(virtualPath);
        return vfile.Open();
    }

    internal static Stream OpenFile(VirtualPath virtualPath) {
        return OpenFile(virtualPath.VirtualPathString);
    }

    internal static CacheDependency GetCacheDependency(VirtualPath virtualPath) {
        VirtualPathProvider vpathProvider = HostingEnvironment.VirtualPathProvider;
        return vpathProvider.GetCacheDependency(virtualPath,
            new SingleObjectCollection(virtualPath.VirtualPathString), DateTime.MaxValue);
    }

    /*
     * Helper method to call CombineVirtualPaths if there is a VirtualPathProvider
     */
    internal static VirtualPath CombineVirtualPathsInternal(VirtualPath basePath, VirtualPath relativePath) {

        VirtualPathProvider vpathProvider = HostingEnvironment.VirtualPathProvider;
        if (vpathProvider != null) {
            return vpathProvider.CombineVirtualPaths(basePath, relativePath);
        }

        // If there is no provider, just combine them normally
        return basePath.Parent.Combine(relativePath);
    }

    internal static bool DirectoryExistsNoThrow(string virtualDir) {
        try {
            return HostingEnvironment.VirtualPathProvider.DirectoryExists(virtualDir);
        }
        catch {
            // If it throws, act is if it doesn't exist
            return false;
        }
    }

    internal static bool DirectoryExistsNoThrow(VirtualPath virtualDir) {
        return DirectoryExistsNoThrow(virtualDir.VirtualPathString);
    }
}

/*
 * Base class for VirtualFile and VirtualDirectory.  This is analogous to
 * System.IO.FileSystemInfo, but for virtual paths instead of physical.
 */

public abstract class VirtualFileBase: MarshalByRefObject {

    internal VirtualPath _virtualPath;

    public override Object InitializeLifetimeService(){
        return null; // never expire lease
    }

    /*
     * Returns the name of the file or directory, without any path info.
     * e.g. if the virtual path is /app/sub/foo.aspx, this returns foo.aspx.
     * Note that this is expected to return the name in the correct casing,
     * which may be different from the casing in the original virtual path.
     */

    public virtual string Name {
        get {
            // By default, return the last chunk of the virtual path
            return _virtualPath.FileName;
        }
    }

    /*
     * Returns the virtual path to the file or directory that this object
     * represents.  This is typically the path passed in to the constructor.
     */

    public string VirtualPath {
        get { return _virtualPath.VirtualPathString; }
    }

    internal VirtualPath VirtualPathObject {
        get { return _virtualPath; }
    }

    /*
     * Returns true if this is a directory, and false if its a file
     */

    public abstract bool IsDirectory {get;}
}

/*
 * Object that represents a virtual file.  This is analogous to
 * System.IO.FileInfo, but for virtual paths instead of physical.
 */

public abstract class VirtualFile: VirtualFileBase {

    /*
     * Contructs a VirtualFile, passing it the virtual path to the
     * file it represents
     */

    protected VirtualFile(string virtualPath) {
        _virtualPath = System.Web.VirtualPath.Create(virtualPath);
    }


    public override bool IsDirectory {
        get { return false; }
    }

    /*
     * Returns a readonly stream to the file
     */

    public abstract Stream Open();
}

/*
 * Object that represents a virtual directory.  This is analogous to
 * System.IO.DirectoryInfo, but for virtual paths instead of physical.
 */

public abstract class VirtualDirectory: VirtualFileBase {

    /*
     * Contructs a VirtualDirectory, passing it the virtual path to the
     * directory it represents
     */

    protected VirtualDirectory(string virtualPath) {
        // Make sure it always has a trailing slash
        _virtualPath = System.Web.VirtualPath.CreateTrailingSlash(virtualPath);
    }


    public override bool IsDirectory {
        get { return true; }
    }

    /*
     * Returns an object that enumerates all the children VirtualDirectory's
     * of this directory.
     */

    public abstract IEnumerable Directories {get;}

    /*
     * Returns an object that enumerates all the children VirtualFile's
     * of this directory.
     */

    public abstract IEnumerable Files {get;}

    /*
     * Returns an object that enumerates all the children VirtualDirectory's
     * and VirtualFiles of this directory.
     */

    public abstract IEnumerable Children {get;}
}
}