File: UrlPath.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (174 lines) | stat: -rw-r--r-- 5,750 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
//------------------------------------------------------------------------------
// <copyright file="UrlPath.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration {
    using System.Collections;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Globalization;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using System.Security;
    using System.Text;
    using System.Xml;
    using Microsoft.Win32;

    static internal class UrlPath {
    
        internal static string GetDirectoryOrRootName(string path) {
            string dir;
    
            dir = Path.GetDirectoryName(path);
            if (dir == null) {
                // dir == null if path = "c:\"
                dir = Path.GetPathRoot(path);
            }
    
            return dir;
        }

        //
        // Determine if subdir is equal to or a subdirectory of dir.
        // For example, c:\mydir\subdir is a subdirectory of c:\mydir
        // Account for optional trailing backslashes.
        //
        internal static bool IsEqualOrSubdirectory(string dir, string subdir) {
            if (String.IsNullOrEmpty(dir))
                return true;

            if (String.IsNullOrEmpty(subdir))
                return false;

            //
            // Compare up to but not including trailing backslash
            //
            int lDir = dir.Length;
            if (dir[lDir - 1] == '\\') {
                lDir -= 1;
            }

            int lSubdir = subdir.Length;
            if (subdir[lSubdir - 1] == '\\') {
                lSubdir -= 1;
            }

            if (lSubdir < lDir)
                return false;

            if (String.Compare(dir, 0, subdir, 0, lDir, StringComparison.OrdinalIgnoreCase) != 0)
                return false;

            // Check subdir that character following length of dir is a backslash
            if (lSubdir > lDir && subdir[lDir] != '\\')
                return false;

            return true;
        }

        //
        // NOTE: This function is also present in fx\src\xsp\system\web\util\urlpath.cs
        // Please propagate any changes to that file.
        //
        // Determine if subpath is equal to or a subpath of path.
        // For example, /myapp/foo.aspx is a subpath of /myapp
        // Account for optional trailing slashes.
        //
        internal static bool IsEqualOrSubpath(string path, string subpath) {
            return IsEqualOrSubpathImpl(path, subpath, false);
        }


        //
        // Determine if subpath is a subpath of path, but return
        // false if subpath & path are the same.
        // For example, /myapp/foo.aspx is a subpath of /myapp
        // Account for optional trailing slashes.
        //
        internal static bool IsSubpath(string path, string subpath) {
            return IsEqualOrSubpathImpl(path, subpath, true);
        }
        
        private static bool IsEqualOrSubpathImpl(string path, string subpath, bool excludeEqual) {
            if (String.IsNullOrEmpty(path))
                return true;

            if (String.IsNullOrEmpty(subpath))
                return false;

            //
            // Compare up to but not including trailing slash
            //
            int lPath = path.Length;
            if (path[lPath - 1] == '/') {
                lPath -= 1;
            }

            int lSubpath = subpath.Length;
            if (subpath[lSubpath - 1] == '/') {
                lSubpath -= 1;
            }

            if (lSubpath < lPath)
                return false;

            if (excludeEqual && lSubpath == lPath)
                return false;

            if (String.Compare(path, 0, subpath, 0, lPath, StringComparison.OrdinalIgnoreCase) != 0)
                return false;

            // Check subpath that character following length of path is a slash
            if (lSubpath > lPath && subpath[lPath] != '/')
                return false;

            return true;
        }

        private static bool IsDirectorySeparatorChar(char ch) {
            return (ch == '\\' || ch == '/');
        }

        private static bool IsAbsoluteLocalPhysicalPath(string path) {
            if (path == null || path.Length < 3)
                return false;

            // e.g c:\foo
            return (path[1] == ':' && IsDirectorySeparatorChar(path[2]));
        }

        private static bool IsAbsoluteUNCPhysicalPath(string path) {
            if (path == null || path.Length < 3)
                return false;

            // e.g \\server\share\foo or //server/share/foo
            return (IsDirectorySeparatorChar(path[0]) && IsDirectorySeparatorChar(path[1]));
        }

        const string FILE_URL_LOCAL = "file:///";
        const string FILE_URL_UNC = "file:";

        internal static string ConvertFileNameToUrl(string fileName) {
            string prefix;

            if (IsAbsoluteLocalPhysicalPath(fileName)) {
                prefix = FILE_URL_LOCAL;
            }
            else if (IsAbsoluteUNCPhysicalPath(fileName)) {
                prefix = FILE_URL_UNC;
            }
            else {
                // We should never get here, but if we do we are likely to have
                // serious security problems, so throw an exception rather than simply 
                // asserting.
                throw ExceptionUtil.ParameterInvalid("filename");
            }

            string newFileName = prefix + fileName.Replace('\\', '/');
            return newFileName;
        }
    }
}