forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathUtil.cs
More file actions
76 lines (71 loc) · 2.84 KB
/
Copy pathPathUtil.cs
File metadata and controls
76 lines (71 loc) · 2.84 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
namespace System.Web.WebPages
{
internal static class PathUtil
{
/// <summary>
/// Path.GetExtension performs a CheckInvalidPathChars(path) which blows up for paths that do not translate to valid physical paths but are valid paths in ASP.NET
/// This method is a near clone of Path.GetExtension without a call to CheckInvalidPathChars(path);
/// </summary>
internal static string GetExtension(string path)
{
if (String.IsNullOrEmpty(path))
{
return path;
}
int current = path.Length;
while (--current >= 0)
{
char ch = path[current];
if (ch == '.')
{
if (current == path.Length - 1)
{
break;
}
return path.Substring(current);
}
if (ch == Path.DirectorySeparatorChar || ch == Path.AltDirectorySeparatorChar)
{
break;
}
}
return String.Empty;
}
internal static bool IsWithinAppRoot(string appDomainAppVirtualPath, string virtualPath)
{
if (appDomainAppVirtualPath == null)
{
// If the runtime has not been initialized, just return true.
return true;
}
var absPath = virtualPath;
if (!VirtualPathUtility.IsAbsolute(absPath))
{
absPath = VirtualPathUtility.ToAbsolute(absPath);
}
// We need to call this overload because it returns null if the path is not within the application root.
// The overload calls into MakeVirtualPathAppRelative(string virtualPath, string applicationPath, bool nullIfNotInApp), with
// nullIfNotInApp set to true.
return VirtualPathUtility.ToAppRelative(absPath, appDomainAppVirtualPath) != null;
}
/// <summary>
/// Determines true if the path is simply "MyPath", and not app-relative "~/MyPath" or absolute "/MyApp/MyPath" or relative "../Test/MyPath"
/// </summary>
/// <returns>True if it is a not app-relative, absolute or relative.</returns>
internal static bool IsSimpleName(string path)
{
if (VirtualPathUtility.IsAbsolute(path) || VirtualPathUtility.IsAppRelative(path))
{
return false;
}
if (path.StartsWith(".", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return true;
}
}
}