File: VirtualDirectoryMapping.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 (146 lines) | stat: -rw-r--r-- 5,070 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
//------------------------------------------------------------------------------
// <copyright file="VirtualDirectoryMapping.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.Configuration {
    using System;
    using System.Configuration;
    using System.IO;
    using System.Web.Util;
    using System.Security.Permissions;

    //
    // Maps a virtual directory to a physical directory and its config file.
    //
    public sealed class VirtualDirectoryMapping {
        VirtualPath  _virtualDirectory;
        string  _physicalDirectory;
        string  _configFileBaseName;
        bool    _isAppRoot;

        const string DEFAULT_BASE_NAME = "web.config";

        public VirtualDirectoryMapping(string physicalDirectory, bool isAppRoot) 
            : this(null, physicalDirectory, isAppRoot, DEFAULT_BASE_NAME) {

        }

        public VirtualDirectoryMapping(string physicalDirectory, bool isAppRoot, string configFileBaseName) 
            : this(null, physicalDirectory, isAppRoot, configFileBaseName) {
        }

        private VirtualDirectoryMapping(VirtualPath virtualDirectory, string physicalDirectory, bool isAppRoot, string configFileBaseName) {
            _virtualDirectory = virtualDirectory;
            _isAppRoot = isAppRoot;

            PhysicalDirectory = physicalDirectory;
            ConfigFileBaseName = configFileBaseName;
        }

        internal VirtualDirectoryMapping Clone() {
            return new VirtualDirectoryMapping(_virtualDirectory, _physicalDirectory, _isAppRoot, _configFileBaseName);
        }

        //
        // Get the virtual directory.
        // Not settable because it is set when it is added to a collection.
        //
        public string VirtualDirectory {
            get {
                return (_virtualDirectory != null) ? _virtualDirectory.VirtualPathString : string.Empty;
            }
        }

        internal VirtualPath VirtualDirectoryObject {
            get {
                return _virtualDirectory;
            }
        }

        internal void SetVirtualDirectory(VirtualPath virtualDirectory) {
            _virtualDirectory = virtualDirectory;
        }

        //
        // The physical directory.
        //
        public string PhysicalDirectory {
            get {
                return _physicalDirectory;
            }

            set {
                string physicalDirectory = value;
                if (String.IsNullOrEmpty(physicalDirectory)) {
                    physicalDirectory = null;
                }
                else {
                    // remove trailing '\' if any
                    if (UrlPath.PathEndsWithExtraSlash(physicalDirectory)) {
                        physicalDirectory = physicalDirectory.Substring(0, physicalDirectory.Length - 1);
                    }

                    // Throw if the resulting physical path is not canonical, to prevent potential
                    // security issues (VSWhidbey 418125)
                    if (FileUtil.IsSuspiciousPhysicalPath(physicalDirectory)) {
                        throw ExceptionUtil.ParameterInvalid("PhysicalDirectory");
                    }
                }

                _physicalDirectory = physicalDirectory;
            }
        }

        //
        // Indicates whether the virtual directory is the location of an application.
        //
        public bool IsAppRoot {
            get {
                return _isAppRoot;
            }

            set {
                _isAppRoot = value;
            }

        }

        //
        // The base name of the config file.
        // If not specified, "web.config" is used.
        //
        public string ConfigFileBaseName {
            get {
                return _configFileBaseName;
            }

            set {
                if (string.IsNullOrEmpty(value)) {
                    throw ExceptionUtil.PropertyInvalid("ConfigFileBaseName");
                }

                _configFileBaseName = value;
            }
        }

        internal void Validate() {
            if (_physicalDirectory != null) {
                //
                // Ensure that the caller has PathDiscovery to the resulting config file,
                // and that the web.config file does not have ".." that could lead to a 
                // different directory.
                //
                string configFilename = Path.Combine(_physicalDirectory, _configFileBaseName);
                string fullConfigFilename = Path.GetFullPath(configFilename);
                if (    Path.GetDirectoryName(fullConfigFilename) != _physicalDirectory ||
                        Path.GetFileName(fullConfigFilename) != _configFileBaseName ||
                        FileUtil.IsSuspiciousPhysicalPath(configFilename)) {

                    throw ExceptionUtil.ParameterInvalid("configFileBaseName");
                }
            }
        }
    }
}