Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UrlPath.cs « Mobile « System.Web.Mobile « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 408bf43925aa455847f62ad7ceced0d6c60613d1 (plain)
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
//------------------------------------------------------------------------------
// <copyright file="UrlPath.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

/*
 * UrlPath class.
 * 
 * Copyright (c) 1999 Microsoft Corporation
 */

using System.Text;
using System.Runtime.Serialization.Formatters;
using System.Runtime.InteropServices;
using System.Collections;
using System.Diagnostics;

namespace System.Web.Mobile
{
    /*
     * URL Path library.
     */
    [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
    internal static class UrlPath
    {
        private const char appRelativeCharacter = '~';

        internal static bool IsRooted(String basepath)
        {
            return(basepath == null || basepath.Length == 0 || basepath[0] == '/' || basepath[0] == '\\');
        }

        internal static bool IsRelativeUrl(string url)
        {
            // If it has a protocol, it's not relative
            if (url.IndexOf(":", StringComparison.Ordinal) != -1)
            {
                return false;
            }

            return !IsRooted(url);
        }

        internal static String GetDirectory(String path)
        {
            if (path == null || path.Length == 0)
            {
                throw new ArgumentException(SR.GetString(SR.UrlPath_EmptyPathHasNoDirectory));
            }

            if (path[0] != '/' && path[0] != appRelativeCharacter)
            {
                throw new ArgumentException(SR.GetString(SR.UrlPath_PathMustBeRooted));
            }

            // Make sure there is a filename after the last '/'
            Debug.Assert(path[path.Length-1] != '/', "Path should not end with a /");

            string dir = path.Substring(0, path.LastIndexOf('/'));

            // If it's the root dir, we would end up with "".  Return "/" instead
            if (dir.Length == 0)
            {
                return "/";
            }

            return dir;
        }

        private static void FailIfPhysicalPath(string path)
        {
            if (path == null || path.Length < 4)
            {
                return;
            }

            if (path[1] == ':' || (path[0] == '\\' && path[1] == '\\'))
            {
                throw new Exception(SR.GetString(SR.UrlPath_PhysicalPathNotAllowed, path));
            }
        }

        internal static String Combine(String basepath, String relative)
        {
            String path;

            // Make sure the relative path is not a physical path (
            FailIfPhysicalPath(relative);

            if (IsRooted(relative))
            {
                path = relative;
                if (path == null || path.Length == 0)
                {
                    return String.Empty;
                }
            }
            else
            {
                // If the relative path starts with "~/" or "~\", treat it as app root
                // relative (
                if (relative.Length >=3 && relative[0] == appRelativeCharacter && (relative[1] == '/' || relative[1] == '\\'))
                {
                    String appPath = HttpRuntime.AppDomainAppVirtualPath;
                    if (appPath.Length > 1)
                    {
                        path = appPath + "/" + relative.Substring(2);
                    }
                    else
                    {
                        path = "/" + relative.Substring(2);
                    }
                }
                else
                {
                    if (basepath == null || (basepath.Length == 1 && basepath[0] == '/'))
                    {
                        basepath = String.Empty;
                    }

                    path = basepath + "/" + relative;
                }
            }

            return Reduce(path);
        }

        internal static String Reduce(String path)
        {
            // ignore query string
            String queryString = null;
            if (path != null)
            {
                int iqs = path.IndexOf('?');
                if (iqs >= 0)
                {
                    queryString = path.Substring(iqs);
                    path = path.Substring(0, iqs);
                }
            }

            int length = path.Length;
            int examine;

            // Make sure we don't have any back slashes
            path = path.Replace('\\', '/');

            // quickly rule out situations in which there are no . or ..

            for (examine = 0; ; examine++)
            {
                examine = path.IndexOf('.', examine);
                if (examine < 0)
                {
                    return (queryString != null) ? (path + queryString) : path;
                }

                if ((examine == 0 || path[examine - 1] == '/')
                    && (examine + 1 == length || path[examine + 1] == '/' ||
                        (path[examine + 1] == '.' && (examine + 2 == length || path[examine + 2] == '/'))))
                {
                    break;
                }
            }

            // OK, we found a . or .. so process it:

            ArrayList list = new ArrayList();
            StringBuilder sb = new StringBuilder();
            int start;
            examine = 0;

            for (;;)
            {
                start = examine;
                examine = path.IndexOf('/', start + 1);

                if (examine < 0)
                {
                    examine = length;
                }

                if (examine - start <= 3 &&
                    (examine < 1 || path[examine - 1] == '.') &&
                    (start + 1 >= length || path[start + 1] == '.'))
                {
                    if (examine - start == 3)
                    {
                        if (list.Count == 0)
                        {
                            throw new Exception(SR.GetString(SR.UrlPath_CannotExitUpTopDirectory));
                        }

                        sb.Length = (int)list[list.Count - 1];
                        list.RemoveRange(list.Count - 1, 1);
                    }
                }
                else
                {
                    list.Add(sb.Length);

                    sb.Append(path, start, examine - start);
                }

                if (examine == length)
                {
                    break;
                }
            }

            return sb.ToString() + queryString;
        }

        private const string dummyProtocolAndServer = "http://foo";

        // Return the relative vpath path from one rooted vpath to another
        internal static string MakeRelative(string from, string to)
        {
            // If either path is app relative (~/...), make it absolute, since the Uri
            // class wouldn't know how to deal with it.
            from = MakeVirtualPathAppAbsolute(from);
            to = MakeVirtualPathAppAbsolute(to);

            // Make sure both virtual paths are rooted
            Debug.Assert(IsRooted(from));
            Debug.Assert(IsRooted(to));

            // Uri's need full url's so, we use a dummy root
            Uri fromUri = new Uri(dummyProtocolAndServer + from);
            Uri toUri = new Uri(dummyProtocolAndServer + to);
            return fromUri.MakeRelative(toUri);
        }

        // If a virtual path is app relative (i.e. starts with ~/), change it to
        // start with the actuall app path.
        // E.g. ~/Sub/foo.aspx --> /MyApp/Sub/foo.aspx
        internal static string MakeVirtualPathAppAbsolute(string virtualPath) {

            // If the path is exactly "~", just return the app root path
            if (virtualPath.Length == 1 && virtualPath[0] == appRelativeCharacter)
                return HttpRuntime.AppDomainAppVirtualPath;

            // If the virtual path starts with "~/" or "~\", replace with the app path
            // relative (ASURT 68628)
            if (virtualPath.Length >=2 && virtualPath[0] == appRelativeCharacter &&
                (virtualPath[1] == '/' || virtualPath[1] == '\\')) {

                string appPath = HttpRuntime.AppDomainAppVirtualPath;

                if (appPath.Length > 1)
                    return appPath + "/" + virtualPath.Substring(2);
                else
                    return "/" + virtualPath.Substring(2);
            }

            // Return it unchanged
            return virtualPath;
        }
    }
}