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

MetabaseServerConfig.cs « Configuration « System.Web « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b1c78a28ffbc44cceb9b298e461bee80de80df4 (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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//------------------------------------------------------------------------------
// <copyright file="MetabaseServerConfig.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.Configuration {

    using System.Configuration;
    using System.Collections;
    using System.Globalization;
    using System.Text;
    using System.IO;
    using System.Web.Util;
    using System.Web.Hosting;
    using System.Web.Caching;
    using Microsoft.Win32;

    class MetabaseServerConfig : IServerConfig, IConfigMapPath, IConfigMapPath2 {
        private const string    DEFAULT_SITEID = "1";
        private const string    DEFAULT_ROOTAPPID = "/LM/W3SVC/1/ROOT";
        private const int       MAX_PATH=260;
        private const int       BUFSIZE = MAX_PATH + 1;
        private const string    LMW3SVC_PREFIX = "/LM/W3SVC/";
        private const string    ROOT_SUFFIX="/ROOT";

        static private MetabaseServerConfig s_instance;
        static private object               s_initLock = new Object();

        string _defaultSiteName;
        string _siteIdForCurrentApplication;

        static internal IServerConfig GetInstance() {
            if (s_instance == null) {
                lock (s_initLock) {
                    if (s_instance == null) {
                        s_instance = new MetabaseServerConfig();
                    }
                }
            }

            return s_instance;
        }

        private MetabaseServerConfig() {
            HttpRuntime.ForceStaticInit(); // force webengine.dll to load

            // Get the default site information
            bool found = MBGetSiteNameFromSiteID(DEFAULT_SITEID, out _defaultSiteName);
            _siteIdForCurrentApplication = HostingEnvironment.SiteID;
            if (_siteIdForCurrentApplication == null) {
                _siteIdForCurrentApplication = DEFAULT_SITEID;
            }
        }

        string IServerConfig.GetSiteNameFromSiteID(string siteID) {
            if (StringUtil.EqualsIgnoreCase(siteID, DEFAULT_SITEID))
                return _defaultSiteName;

            string siteName;
            bool found = MBGetSiteNameFromSiteID(siteID, out siteName);
            return siteName;
        }

        // if appHost is null, we use the site ID for the current application
        string IServerConfig.MapPath(IApplicationHost appHost, VirtualPath path) {
            string siteID = (appHost == null) ? _siteIdForCurrentApplication : appHost.GetSiteID();
            return MapPathCaching(siteID, path);
        }

        string[] IServerConfig.GetVirtualSubdirs(VirtualPath path, bool inApp) {
            string aboPath = GetAboPath(_siteIdForCurrentApplication, path.VirtualPathString);
            return MBGetVirtualSubdirs(aboPath, inApp);
        }

        bool IServerConfig.GetUncUser(IApplicationHost appHost, VirtualPath path, out string username, out string password) {
            string aboPath = GetAboPath(appHost.GetSiteID(), path.VirtualPathString);
            return MBGetUncUser(aboPath, out username, out password);
        }

        long IServerConfig.GetW3WPMemoryLimitInKB() {
            return (long) MBGetW3WPMemoryLimitInKB();
        }

        string IConfigMapPath.GetMachineConfigFilename() {
            return HttpConfigurationSystem.MachineConfigurationFilePath;
        }

        string IConfigMapPath.GetRootWebConfigFilename() {
            return HttpConfigurationSystem.RootWebConfigurationFilePath;
        }

        private void GetPathConfigFilenameWorker(string siteID, VirtualPath path, out string directory, out string baseName) {
            directory = MapPathCaching(siteID, path);
            if (directory != null) {
                baseName = HttpConfigurationSystem.WebConfigFileName;
            }
            else {
                baseName = null;
            }
        }

        // Based on <siteID, path>, return:
        // directory - the physical directory of the path (vpath)
        // baseName - name of the configuration file to look for.
        // E.g. if siteID="1" and path="/", directory="c:\inetpub\wwwroot" and baseName="web.config"
        void IConfigMapPath.GetPathConfigFilename(
                string siteID, string path, out string directory, out string baseName) {
            GetPathConfigFilenameWorker(siteID, VirtualPath.Create(path), out directory, out baseName);
        }

        void IConfigMapPath2.GetPathConfigFilename(
                string siteID, VirtualPath path, out string directory, out string baseName) {
            GetPathConfigFilenameWorker(siteID, path, out directory, out baseName);
        }

        void IConfigMapPath.GetDefaultSiteNameAndID(out string siteName, out string siteID) {
            siteName = _defaultSiteName;
            siteID = DEFAULT_SITEID;
        }

        void IConfigMapPath.ResolveSiteArgument(string siteArgument, out string siteName, out string siteID) {
            if (    String.IsNullOrEmpty(siteArgument) ||
                    StringUtil.EqualsIgnoreCase(siteArgument, DEFAULT_SITEID) ||
                    StringUtil.EqualsIgnoreCase(siteArgument, _defaultSiteName)) {

                siteName = _defaultSiteName;
                siteID = DEFAULT_SITEID;
            }
            else {
                siteName = String.Empty;
                siteID = String.Empty;

                bool found = false;
                if (IISMapPath.IsSiteId(siteArgument)) {
                    found = MBGetSiteNameFromSiteID(siteArgument, out siteName);
                }

                if (found) {
                    siteID = siteArgument;
                }
                else {
                    found = MBGetSiteIDFromSiteName(siteArgument, out siteID);
                    if (found) {
                        siteName = siteArgument;
                    }
                    else {
                        siteName = siteArgument;
                        siteID = String.Empty;
                    }
                }
            }
        }

        // Map from <siteID, path> to a physical file path
        string IConfigMapPath.MapPath(string siteID, string vpath) {
            return MapPathCaching(siteID, VirtualPath.Create(vpath));
        }

        // IConfigMapPath2 VirtualPath fast path
        string IConfigMapPath2.MapPath(string siteID, VirtualPath vpath) {
            return MapPathCaching(siteID, vpath);
        }

        VirtualPath GetAppPathForPathWorker(string siteID, VirtualPath vpath) {
            string aboPath = GetAboPath(siteID, vpath.VirtualPathString);
            string appAboPath = MBGetAppPath(aboPath);
            if (appAboPath == null)
                return VirtualPath.RootVirtualPath;

            string rootAboPath = GetRootAppIDFromSiteID(siteID);
            if (StringUtil.EqualsIgnoreCase(rootAboPath, appAboPath)) {
                return VirtualPath.RootVirtualPath;
            }

            string appPath = appAboPath.Substring(rootAboPath.Length);
            return VirtualPath.CreateAbsolute(appPath);
        }

        string IConfigMapPath.GetAppPathForPath(string siteID, string vpath) {
            VirtualPath resolved = GetAppPathForPathWorker(siteID, VirtualPath.Create(vpath));
            return resolved.VirtualPathString;
        }

        // IConfigMapPath2 VirtualPath fast path
        VirtualPath IConfigMapPath2.GetAppPathForPath(string siteID, VirtualPath vpath) {
            return GetAppPathForPathWorker(siteID, vpath);
        }

        private string MatchResult(VirtualPath path, string result) {
            if (string.IsNullOrEmpty(result)) {
                return result;
            }

            result = result.Replace('/', '\\');

            // ensure extra '\\' in the physical path if the virtual path had extra '/'
            // and the other way -- no extra '\\' in physical if virtual didn't have it.
            if (path.HasTrailingSlash) {
                if (!UrlPath.PathEndsWithExtraSlash(result) && !UrlPath.PathIsDriveRoot(result)) {
                    result = result + "\\";
                }
            }
            else {
                if (UrlPath.PathEndsWithExtraSlash(result) && !UrlPath.PathIsDriveRoot(result)) {
                    result = result.Substring(0, result.Length - 1);
                }
            }

            return result;
        }

        private string MapPathCaching(string siteID, VirtualPath path) {
            // UrlMetaDataSlidingExpiration config setting controls 
            // the duration of all cached items related to url metadata.
            bool doNotCache = CachedPathData.DoNotCacheUrlMetadata;
            TimeSpan slidingExpiration = CachedPathData.UrlMetadataSlidingExpiration; 

            // store a single variation of the path
            VirtualPath originalPath = path;
            MapPathCacheInfo cacheInfo;

            if (doNotCache) {
                cacheInfo = new MapPathCacheInfo();
            }
            else {
                // Check if it's in the cache
                String cacheKey = CacheInternal.PrefixMapPath + siteID + path.VirtualPathString;
                cacheInfo = (MapPathCacheInfo)HttpRuntime.CacheInternal.Get(cacheKey);

                // If not in cache, add it to the cache
                if (cacheInfo == null) {
                    cacheInfo = new MapPathCacheInfo();
                    // Add to the cache.
                    // No need to have a lock here. UtcAdd will add the entry if it doesn't exist. 
                    // If it does exist, the existing value will be returned (Dev10 
                    object existingEntry = HttpRuntime.CacheInternal.UtcAdd(
                        cacheKey, cacheInfo, null, Cache.NoAbsoluteExpiration, slidingExpiration, CacheItemPriority.Default, null);
                    if (existingEntry != null) {
                        cacheInfo = existingEntry as MapPathCacheInfo;
                    }
                }
            }

            // If not been evaluated, then evaluate it
            if (!cacheInfo.Evaluated) {
                lock(cacheInfo) {

                    if (!cacheInfo.Evaluated && HttpRuntime.IsMapPathRelaxed) {
                        //////////////////////////////////////////////////////////////////
                        // Verify that the parent path is valid. If parent is invalid, then set this to invalid
                        if (path.VirtualPathString.Length > 1) {
                            VirtualPath vParent = path.Parent;
                            if (vParent != null) {
                                string parentPath = vParent.VirtualPathString;
                                if (parentPath.Length > 1 && StringUtil.StringEndsWith(parentPath, '/')) { // Trim the extra trailing / if there is one
                                    vParent = VirtualPath.Create(parentPath.Substring(0, parentPath.Length - 1));
                                }
                                try {
                                    string parentMapPathResult = MapPathCaching(siteID, vParent);
                                    if (parentMapPathResult == HttpRuntime.GetRelaxedMapPathResult(null)) {
                                        // parent is invalid!
                                        cacheInfo.MapPathResult = parentMapPathResult;
                                        cacheInfo.Evaluated = true;
                                    }
                                } catch {
                                    cacheInfo.MapPathResult = HttpRuntime.GetRelaxedMapPathResult(null);
                                    cacheInfo.Evaluated = true;
                                }
                            }
                        }
                    }

                    if (!cacheInfo.Evaluated) {
                        string physicalPath = null;

                        try {
                            physicalPath = MapPathActual(siteID, path);

                            if (HttpRuntime.IsMapPathRelaxed) {
                                physicalPath = HttpRuntime.GetRelaxedMapPathResult(physicalPath);
                            }

                            // Throw if the resulting physical path is not canonical, to prevent potential
                            // security issues (VSWhidbey 418125)
                            if (FileUtil.IsSuspiciousPhysicalPath(physicalPath)) {
                                if (HttpRuntime.IsMapPathRelaxed) {
                                    physicalPath = HttpRuntime.GetRelaxedMapPathResult(null);
                                } else {
                                    throw new HttpException(SR.GetString(SR.Cannot_map_path, path));
                                }
                            }

                        } catch (Exception e) {
                            if (HttpRuntime.IsMapPathRelaxed) {
                                physicalPath = HttpRuntime.GetRelaxedMapPathResult(null);
                            } else {
                                cacheInfo.CachedException = e;
                                cacheInfo.Evaluated=true;
                                throw;
                            }
                        }

                        if ( physicalPath != null ) {
                            // Only cache if we got a good value
                            cacheInfo.MapPathResult = physicalPath;
                            cacheInfo.Evaluated = true;
                        }
                    }
                }
            }

            // Throw an exception if required
            if (cacheInfo.CachedException != null) {
                throw cacheInfo.CachedException;
            }

            return MatchResult(originalPath, cacheInfo.MapPathResult);
        }

        private string MapPathActual(string siteID, VirtualPath path) {
            string appID = GetRootAppIDFromSiteID(siteID);
            string physicalPath = MBMapPath(appID, path.VirtualPathString);
            return physicalPath;
        }

        private string GetRootAppIDFromSiteID(string siteId) {
            return LMW3SVC_PREFIX + siteId + ROOT_SUFFIX;
        }

        private string GetAboPath(string siteID, string path) {
            string rootAppID = GetRootAppIDFromSiteID(siteID);
            string aboPath = rootAppID + FixupPathSlash(path);
            return aboPath;
        }

        private string FixupPathSlash(string path) {
            if (path == null) {
                return null;
            }

            int l = path.Length;
            if (l == 0 || path[l-1] != '/') {
                return path;
            }

            return path.Substring(0, l-1);
        }

        //
        // Metabase access functions
        //
        private bool MBGetSiteNameFromSiteID(string siteID, out string siteName) {
            string appID = GetRootAppIDFromSiteID(siteID);
            StringBuilder sb = new StringBuilder(BUFSIZE);
            int r = UnsafeNativeMethods.IsapiAppHostGetSiteName(appID, sb, sb.Capacity);
            if (r == 1) {
                siteName = sb.ToString();
                return true;
            }
            else {
                siteName = String.Empty;
                return false;
            }
        }

        private bool MBGetSiteIDFromSiteName(string siteName, out string siteID) {
            StringBuilder sb = new StringBuilder(BUFSIZE);
            int r = UnsafeNativeMethods.IsapiAppHostGetSiteId(siteName, sb, sb.Capacity);
            if (r == 1) {
                siteID = sb.ToString();
                return true;
            }
            else {
                siteID = String.Empty;
                return false;
            }
        }


        private string MBMapPath(string appID, string path) {
            // keep growing the buffer to support paths longer than MAX_PATH
            int bufSize = BUFSIZE;
            StringBuilder sb;
            int r;

            for (;;) {
                sb = new StringBuilder(bufSize);
                r = UnsafeNativeMethods.IsapiAppHostMapPath(appID, path, sb, sb.Capacity);
                Debug.Trace("MapPath", "IsapiAppHostMapPath(" + path + ") returns " + r);

                if (r == -2) {
                    // insufficient buffer
                    bufSize *= 2;
                }
                else {
                    break;
                }
            }

            if (r == -1) {
                // special case access denied error
                throw new HostingEnvironmentException(
                    SR.GetString(SR.Cannot_access_mappath_title),
                    SR.GetString(SR.Cannot_access_mappath_details));
            }

            string physicalPath;
            if (r == 1) {
                physicalPath = sb.ToString();
            }
            else {
                physicalPath = null;
            }

            return physicalPath;
        }

        private string[] MBGetVirtualSubdirs(string aboPath, bool inApp) {
            StringBuilder sb = new StringBuilder(BUFSIZE);
            int index = 0;
            ArrayList list = new ArrayList();
            for (;;) {
                sb.Length = 0;
                int r = UnsafeNativeMethods.IsapiAppHostGetNextVirtualSubdir(aboPath, inApp, ref index, sb, sb.Capacity);
                if (r == 0)
                    break;

                string subdir = sb.ToString();
                list.Add(subdir);
            }

            string[] subdirs = new string[list.Count];
            list.CopyTo(subdirs);
            return subdirs;
        }

        private bool MBGetUncUser(string aboPath, out string username, out string password) {
            StringBuilder usr = new StringBuilder(BUFSIZE);
            StringBuilder pwd = new StringBuilder(BUFSIZE);
            int r = UnsafeNativeMethods.IsapiAppHostGetUncUser(aboPath, usr, usr.Capacity, pwd, pwd.Capacity);
            if (r == 1) {
                username = usr.ToString();
                password = pwd.ToString();
                return true;
            }
            else {
                username = null;
                password = null;
                return false;
            }
        }

        private int MBGetW3WPMemoryLimitInKB() {
            return UnsafeNativeMethods.GetW3WPMemoryLimitInKB();
        }

        private string MBGetAppPath(string aboPath) {
            StringBuilder buf = new StringBuilder(aboPath.Length + 1);
            int r = UnsafeNativeMethods.IsapiAppHostGetAppPath(aboPath, buf, buf.Capacity);
            string appAboPath;
            if (r == 1) {
                appAboPath = buf.ToString();
            }
            else {
                appAboPath = null;
            }

            return appAboPath;
        }
    }
}