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

/*
 * The ASP.NET runtime services
 * 
 * Copyright (c) 1998 Microsoft Corporation
 */

namespace System.Web.Hosting {
    using System.Runtime.InteropServices;       
    using System.Collections;
    using System.Reflection;
    using System.Threading;
    using System.Web;
    using System.Web.Management;
    using System.Web.Util;
    using System.Globalization;
    using System.Security.Permissions;
    

    /// <devdoc>
    ///    <para>[To be supplied.]</para>
    /// </devdoc>
    /// <internalonly/>
    [ComImport, Guid("08a2c56f-7c16-41c1-a8be-432917a1a2d1"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IISAPIRuntime {

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        [SecurityPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
        void StartProcessing();

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        [SecurityPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
        void StopProcessing();


        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        [SecurityPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
        [return: MarshalAs(UnmanagedType.I4)]
        int ProcessRequest(
                          [In]
                          IntPtr ecb, 
                          [In, MarshalAs(UnmanagedType.I4)]
                          int useProcessModel);

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        [SecurityPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
        void DoGCCollect();
    }

    // DevDiv #195200: Adding link demands to IISAPIRuntime causes partial trust to stop
    // working. Ideally that type should have been internal, but we can't do that since
    // that would be a breaking API change. So instead we introduce a parallel internal
    // type with a different COM GUID. Existing public methods on ISAPIRuntime will be
    // protected by a link demand, but our native layer will go through this specific
    // interface which can bypass the link demands.
    [ComImport, Guid("15eb8d20-d4ed-4855-a276-91a75a696955"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IISAPIRuntime2 {

        void StartProcessing();

        void StopProcessing();

        [return: MarshalAs(UnmanagedType.I4)]
        int ProcessRequest([In]IntPtr ecb, [In, MarshalAs(UnmanagedType.I4)]int useProcessModel);

        void DoGCCollect();

    }

    /// <devdoc>
    ///    <para>[To be supplied.]</para>
    /// </devdoc>
    /// <internalonly/>
    public sealed class ISAPIRuntime : MarshalByRefObject, IISAPIRuntime, IISAPIRuntime2, IRegisteredObject {

        // WARNING: do not modify without making corresponding changes in appdomains.h
        private const int WORKER_REQUEST_TYPE_IN_PROC            = 0x0;
        private const int WORKER_REQUEST_TYPE_OOP                = 0x1;
        private const int WORKER_REQUEST_TYPE_IN_PROC_VERSION_2  = 0x2;

        // to control removal from unmanaged table (to it only once)
        private static int _isThisAppDomainRemovedFromUnmanagedTable;

        [SecurityPermission(SecurityAction.Demand, Unrestricted = true)]
        public ISAPIRuntime() {
            HostingEnvironment.RegisterObject(this);
        }


        public override Object InitializeLifetimeService() {
            return null; // never expire lease
        }


        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        public void StartProcessing() {
            Debug.Trace("ISAPIRuntime", "StartProcessing");
        }

        void IISAPIRuntime2.StartProcessing() {
            StartProcessing();
        }


        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        public void StopProcessing() {
            Debug.Trace("ISAPIRuntime", "StopProcessing");
            HostingEnvironment.UnregisterObject(this);
        }

        void IISAPIRuntime2.StopProcessing() {
            StopProcessing();
        }

        /*
         * Process one ISAPI request
         *
         * @param ecb ECB
         * @param useProcessModel flag set to true when out-of-process
         */

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        public int ProcessRequest(IntPtr ecb, int iWRType) {
            IntPtr pHttpCompletion = IntPtr.Zero;
            if (iWRType == WORKER_REQUEST_TYPE_IN_PROC_VERSION_2) {
                pHttpCompletion = ecb;
                ecb = UnsafeNativeMethods.GetEcb(pHttpCompletion);
            } 
            ISAPIWorkerRequest wr = null;
            try {
                bool useOOP = (iWRType == WORKER_REQUEST_TYPE_OOP);
                wr = ISAPIWorkerRequest.CreateWorkerRequest(ecb, useOOP);
                wr.Initialize();

                // check if app path matches (need to restart app domain?)                
                String wrPath = wr.GetAppPathTranslated();
                String adPath = HttpRuntime.AppDomainAppPathInternal;                
                
                if (adPath == null ||
                    StringUtil.EqualsIgnoreCase(wrPath, adPath)) {
                    
                    HttpRuntime.ProcessRequestNoDemand(wr);
                    return 0;
                }
                else {
                    // need to restart app domain
                    HttpRuntime.ShutdownAppDomain(ApplicationShutdownReason.PhysicalApplicationPathChanged,
                                                  SR.GetString(SR.Hosting_Phys_Path_Changed,
                                                                                   adPath,
                                                                                   wrPath));
                    return 1;
                }
            }
            catch(Exception e) {
                try {
                    WebBaseEvent.RaiseRuntimeError(e, this);
                } catch {}
                
                // Have we called HSE_REQ_DONE_WITH_SESSION?  If so, don't re-throw.
                if (wr != null && wr.Ecb == IntPtr.Zero) {
                    if (pHttpCompletion != IntPtr.Zero) {
                        UnsafeNativeMethods.SetDoneWithSessionCalled(pHttpCompletion);
                    }
                    // if this is a thread abort exception, cancel the abort
                    if (e is ThreadAbortException) {
                        Thread.ResetAbort();
                    }                    
                    // IMPORTANT: if this thread is being aborted because of an AppDomain.Unload,
                    // the CLR will still throw an AppDomainUnloadedException. The native caller
                    // must special case COR_E_APPDOMAINUNLOADED(0x80131014) and not
                    // call HSE_REQ_DONE_WITH_SESSION more than once.
                    return 0;
                }
                
                // re-throw if we have not called HSE_REQ_DONE_WITH_SESSION
                throw;
            }
        }

        int IISAPIRuntime2.ProcessRequest(IntPtr ecb, int iWRType) {
            return ProcessRequest(ecb, iWRType);
        }

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] // DevDiv #180492
        public void DoGCCollect() {
            for (int c = 10; c > 0; c--) {
                System.GC.Collect();
            }
        }

        void IISAPIRuntime2.DoGCCollect() {
            DoGCCollect();
        }


        /// <internalonly/>
        void IRegisteredObject.Stop(bool immediate) {
            RemoveThisAppDomainFromUnmanagedTable();
            HostingEnvironment.UnregisterObject(this);
        }


        internal static void RemoveThisAppDomainFromUnmanagedTable() {
            if (Interlocked.Exchange(ref _isThisAppDomainRemovedFromUnmanagedTable, 1) != 0) {
                return;
            }

            try {
                String appId = HttpRuntime.AppDomainAppId;
                if (appId != null ) {
                    Debug.Trace("ISAPIRuntime", "Calling UnsafeNativeMethods.AppDomainRestart appId=" + appId);

                    UnsafeNativeMethods.AppDomainRestart(appId);
                }

                HttpRuntime.AddAppDomainTraceMessage(SR.GetString(SR.App_Domain_Restart));
            }
            catch {
            }
        }
    }
}