File: AspNetEnvironment.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (370 lines) | stat: -rw-r--r-- 13,332 bytes parent folder | download | duplicates (9)
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
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//----------------------------------------------------------------------------

namespace System.ServiceModel.Activation
{
    using System.Collections.Generic;
    using System.Configuration;
    using System.Net;
    using System.Runtime;
    using System.Security;
    using System.Security.Authentication.ExtendedProtection;
    using System.Security.Permissions;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;

    class AspNetEnvironment
    {
        static readonly object thisLock = new object();

        // Double-checked locking pattern requires volatile for read/write synchronization
        static volatile AspNetEnvironment current;
        static bool isEnabled;

        static bool? isApplicationDomainHosted;

        protected AspNetEnvironment()
        {
        }

        public static AspNetEnvironment Current
        {
            get
            {
                if (current == null)
                {
                    lock (thisLock)
                    {
                        if (current == null)
                        {
                            current = new AspNetEnvironment();
                        }
                    }
                }
                return current;
            }
            // AspNetEnvironment.Current is set by System.ServiceModel.Activation when it is brought into memory through
            // the ASP.Net hosting environment. It is the only "real" implementer of this class.
            protected set
            {
                Fx.Assert(!isEnabled, "should only be explicitly set once");
                Fx.Assert(value != null, "should only be set to a valid environment");
                current = value;
                isEnabled = true;
            }
        }

        public static bool Enabled
        {
            get
            {
                return isEnabled;
            }
        }

        public bool RequiresImpersonation
        {
            get
            {
                return AspNetCompatibilityEnabled;
            }
        }

        public virtual bool AspNetCompatibilityEnabled
        {
            get
            {
                // subclass will check AspNetCompatibility mode
                return false;
            }
        }

        public virtual string ConfigurationPath
        {
            get
            {
                return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            }
        }

        // these ideally would be replaced with public APIs
        public virtual bool IsConfigurationBased
        {
            get
            {
                // subclass will check ServiceActivationElement presence
                return false;
            }
        }

        public virtual string CurrentVirtualPath
        {
            get
            {
                // subclass will use calculation from HostingManager.CreateService
                return null;
            }
        }

        public virtual string XamlFileBaseLocation
        {
            get
            {
                // subclass will use calculation from HostingManager.CreateService
                return null;
            }
        }

        public virtual bool UsingIntegratedPipeline
        {
            get
            {
                return false;
            }
        }

        public virtual string WebSocketVersion
        {
            get
            {
                return null;
            }
        }

        // Indicates if the WebSocket module is loaded. When IIS hosted, it throws an exception when called before we determined if the module is loaded or not.
        public bool IsWebSocketModuleLoaded
        {
            get
            {
                return this.WebSocketVersion != null;
            }
        }

        public virtual void AddHostingBehavior(ServiceHostBase serviceHost, ServiceDescription description)
        {
            // subclass will add HostedBindingBehavior
        }

        public virtual bool IsWindowsAuthenticationConfigured()
        {
            // subclass will check Asp.Net authentication mode
            return false;
        }

        public virtual List<Uri> GetBaseAddresses(Uri addressTemplate)
        {
            // subclass will provide multiple base address support 
            return null;
        }

        // check if ((System.Web.Configuration.WebContext)configHostingContext).ApplicationLevel == WebApplicationLevel.AboveApplication
        public virtual bool IsWebConfigAboveApplication(object configHostingContext)
        {
            // there are currently only two known implementations of HostingContext, so we are 
            // pretty much guaranteed to be hosted in ASP.Net here. However, it may not be
            // through our BuildProvider, so we still need to do some work in the base class.
            // The HostedAspNetEnvironment subclass can perform more efficiently using reflection
            return SystemWebHelper.IsWebConfigAboveApplication(configHostingContext);
        }
        public virtual void EnsureCompatibilityRequirements(ServiceDescription description)
        {
            // subclass will ensure AspNetCompatibilityRequirementsAttribute is in the behaviors collection
        }

        public virtual bool TryGetFullVirtualPath(out string virtualPath)
        {
            // subclass will use the virtual path from the compiled string
            virtualPath = null;
            return false;
        }

        public virtual string GetAnnotationFromHost(ServiceHostBase host)
        {
            // subclass will return "Website name\Application Virtual Path|\relative service virtual path|serviceName"
            return string.Empty;
        }

        public virtual void EnsureAllReferencedAssemblyLoaded()
        {
        }

        public virtual BaseUriWithWildcard GetBaseUri(string transportScheme, Uri listenUri)
        {
            return null;
        }

        public virtual void ValidateHttpSettings(string virtualPath, bool isMetadataListener, bool usingDefaultSpnList, ref AuthenticationSchemes supportedSchemes, ref ExtendedProtectionPolicy extendedProtectionPolicy, ref string realm)
        {
        }

        // returns whether or not the caller should use the hosted client certificate mapping
        public virtual bool ValidateHttpsSettings(string virtualPath, ref bool requireClientCertificate)
        {
            return false;
        }

        public virtual void ProcessNotMatchedEndpointAddress(Uri uri, string endpointName)
        {
            // subclass will throw as appropriate for compat mode
        }

        public virtual void ValidateCompatibilityRequirements(AspNetCompatibilityRequirementsMode compatibilityMode)
        {
            // validate that hosting settings are compatible with the requested requirements
            if (compatibilityMode == AspNetCompatibilityRequirementsMode.Required)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.Hosting_CompatibilityServiceNotHosted)));
            }
        }

        public virtual IAspNetMessageProperty GetHostingProperty(Message message)
        {
            // subclass will gen up a HostingMessageProperty
            return null;
        }

        public virtual IAspNetMessageProperty GetHostingProperty(Message message, bool removeFromMessage)
        {
            // subclass will return the hosting property from the message
            // and remove it from the message's properties.
            return null;
        }

        public virtual void PrepareMessageForDispatch(Message message)
        {
            // subclass will wrap ReceiveContext for Busy Count 
        }

        public virtual void ApplyHostedContext(TransportChannelListener listener, BindingContext context)
        {
            // subclass will push hosted information to the transport listeners
        }

        internal virtual void AddMetadataBindingParameters(Uri listenUri, KeyedByTypeCollection<IServiceBehavior> serviceBehaviors, BindingParameterCollection bindingParameters)
        {
            bindingParameters.Add(new ServiceMetadataExtension.MetadataBindingParameter());
        }

        internal virtual bool IsMetadataListener(BindingParameterCollection bindingParameters)
        {
            return bindingParameters.Find<ServiceMetadataExtension.MetadataBindingParameter>() != null;
        }

        public virtual void IncrementBusyCount()
        {
            // subclass will increment HostingEnvironment.BusyCount
        }

        public virtual void DecrementBusyCount()
        {
            // subclass will decrement HostingEnvironment.BusyCount
        }
        public virtual bool TraceIncrementBusyCountIsEnabled()
        {
            // subclass will return true if tracing for IncrementBusyCount is enabled.
            //kept as a separate check from TraceIncrementBusyCount to avoid creating source string if Tracing is not enabled.
            return false;
        }
        public virtual bool TraceDecrementBusyCountIsEnabled()
        {
            // subclass will return true if tracing for DecrementBusyCount is enabled.
            //kept as a separate check from TraceDecrementBusyCount to avoid creating source string if Tracing is not enabled.
            return false;
        }
        public virtual void TraceIncrementBusyCount(string data)
        {
            //callers are expected to check if TraceIncrementBusyCountIsEnabled() is true
            //before calling this method

            // subclass will emit trace for IncrementBusyCount 
            // data is emitted in the Trace as the source of the call to Increment.
        }

        public virtual void TraceDecrementBusyCount(string data)
        {
            //callers are expected to check if TraceDecrementBusyCountIsEnabled() is true
            //before calling this method

            // subclass will emit trace for DecrementBusyCount 
            // data is emitted in the Trace as the source of the call to Decrement.
        }
        public virtual object GetConfigurationSection(string sectionPath)
        {
            // subclass will interact with web.config system
            return ConfigurationManager.GetSection(sectionPath);
        }

        // Be sure to update UnsafeGetSection if you modify this method
        [Fx.Tag.SecurityNote(Critical = "Uses SecurityCritical method UnsafeGetSectionFromConfigurationManager which elevates.")]
        [SecurityCritical]
        public virtual object UnsafeGetConfigurationSection(string sectionPath)
        {
            // subclass will interact with web.config system
            return UnsafeGetSectionFromConfigurationManager(sectionPath);
        }

        public virtual AuthenticationSchemes GetAuthenticationSchemes(Uri baseAddress)
        {
            // subclass will grab settings from the metabase
            return AuthenticationSchemes.None;
        }

        public virtual bool IsSimpleApplicationHost
        {
            get
            {
                // subclass will grab settings from the ServiceHostingEnvironment
                return false;
            }
        }

        [Fx.Tag.SecurityNote(Critical = "Asserts ConfigurationPermission in order to fetch config from ConfigurationManager,"
        + "caller must guard return value.")]
        [SecurityCritical]
        [ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)]
        static object UnsafeGetSectionFromConfigurationManager(string sectionPath)
        {
            return ConfigurationManager.GetSection(sectionPath);
        }

        public virtual bool IsWithinApp(string absoluteVirtualPath)
        {
            return true;
        }

        internal static bool IsApplicationDomainHosted()
        {
            if (!AspNetEnvironment.isApplicationDomainHosted.HasValue)
            {
                lock (AspNetEnvironment.thisLock)
                {
                    if (!AspNetEnvironment.isApplicationDomainHosted.HasValue)
                    {
                        bool isApplicationDomainHosted = false;
                        if (AspNetEnvironment.Enabled)
                        {
                            isApplicationDomainHosted = AspNetEnvironment.IsSystemWebAssemblyLoaded();
                        }
                        AspNetEnvironment.isApplicationDomainHosted = isApplicationDomainHosted;
                    }
                }
            }
            return AspNetEnvironment.isApplicationDomainHosted.Value;
        }

        private static bool IsSystemWebAssemblyLoaded()
        {
            const string systemWebName = "System.Web,";
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (assembly.FullName.StartsWith(systemWebName, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }
    }
}