forked from microsoft/referencesource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpApplication.cs
4473 lines (3686 loc) · 197 KB
/
HttpApplication.cs
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <copyright file="HttpApplication.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web {
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization.Formatters;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Compilation;
using System.Web.Configuration;
using System.Web.Configuration.Common;
using System.Web.Hosting;
using System.Web.Management;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.Util;
using IIS = System.Web.Hosting.UnsafeIISMethods;
//
// Async EventHandler support
//
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public delegate IAsyncResult BeginEventHandler(object sender, EventArgs e, AsyncCallback cb, object extraData);
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public delegate void EndEventHandler(IAsyncResult ar);
// Represents an event handler using TAP (Task Asynchronous Pattern).
public delegate Task TaskEventHandler(object sender, EventArgs e);
/// <devdoc>
/// <para>
/// The HttpApplication class defines the methods, properties and events common to all
/// HttpApplication objects within the ASP.NET Framework.
/// </para>
/// </devdoc>
[
ToolboxItem(false)
]
public class HttpApplication : IComponent, IHttpAsyncHandler, IRequestCompletedNotifier, ISyncContext {
// application state dictionary
private HttpApplicationState _state;
// context during init for config lookups
private HttpContext _initContext;
// async support
private HttpAsyncResult _ar; // currently pending async result for call into application
// list of modules
private static readonly DynamicModuleRegistry _dynamicModuleRegistry = new DynamicModuleRegistry();
private HttpModuleCollection _moduleCollection;
// event handlers
private static readonly object EventDisposed = new object();
private static readonly object EventErrorRecorded = new object();
private static readonly object EventRequestCompleted = new object();
private static readonly object EventPreSendRequestHeaders = new object();
private static readonly object EventPreSendRequestContent = new object();
private static readonly object EventBeginRequest = new object();
private static readonly object EventAuthenticateRequest = new object();
private static readonly object EventDefaultAuthentication = new object();
private static readonly object EventPostAuthenticateRequest = new object();
private static readonly object EventAuthorizeRequest = new object();
private static readonly object EventPostAuthorizeRequest = new object();
private static readonly object EventResolveRequestCache = new object();
private static readonly object EventPostResolveRequestCache = new object();
private static readonly object EventMapRequestHandler = new object();
private static readonly object EventPostMapRequestHandler = new object();
private static readonly object EventAcquireRequestState = new object();
private static readonly object EventPostAcquireRequestState = new object();
private static readonly object EventPreRequestHandlerExecute = new object();
private static readonly object EventPostRequestHandlerExecute = new object();
private static readonly object EventReleaseRequestState = new object();
private static readonly object EventPostReleaseRequestState = new object();
private static readonly object EventUpdateRequestCache = new object();
private static readonly object EventPostUpdateRequestCache = new object();
private static readonly object EventLogRequest = new object();
private static readonly object EventPostLogRequest = new object();
private static readonly object EventEndRequest = new object();
internal static readonly string AutoCulture = "auto";
private EventHandlerList _events;
private AsyncAppEventHandlersTable _asyncEvents;
// execution steps
private StepManager _stepManager;
// callback for Application ResumeSteps
#pragma warning disable 0649
private WaitCallback _resumeStepsWaitCallback;
#pragma warning restore 0649
// event passed to modules
private EventArgs _appEvent;
// list of handler mappings
private Hashtable _handlerFactories = new Hashtable();
// list of handler/factory pairs to be recycled
private ArrayList _handlerRecycleList;
// flag to hide request and response intrinsics
private bool _hideRequestResponse;
// application execution variables
private HttpContext _context;
private Exception _lastError; // placeholder for the error when context not avail
private bool _timeoutManagerInitialized;
// session (supplied by session-on-end outside of context)
private HttpSessionState _session;
// culture (needs to be set per thread)
private CultureInfo _appLevelCulture;
private CultureInfo _appLevelUICulture;
private CultureInfo _savedAppLevelCulture;
private CultureInfo _savedAppLevelUICulture;
private bool _appLevelAutoCulture;
private bool _appLevelAutoUICulture;
// pipeline event mappings
private Dictionary<string, RequestNotification> _pipelineEventMasks;
// IComponent support
private ISite _site;
// IIS7 specific fields
internal const string MANAGED_PRECONDITION = "managedHandler";
internal const string IMPLICIT_FILTER_MODULE = "AspNetFilterModule";
internal const string IMPLICIT_HANDLER = "ManagedPipelineHandler";
// map modules to their index
private static Hashtable _moduleIndexMap = new Hashtable();
private static bool _initSpecialCompleted;
private bool _initInternalCompleted;
private RequestNotification _appRequestNotifications;
private RequestNotification _appPostNotifications;
// Set the current module init key to the global.asax module to enable
// the custom global.asax derivation constructor to register event handlers
private string _currentModuleCollectionKey = HttpApplicationFactory.applicationFileName;
// module config is read once per app domain and used to initialize the per-instance _moduleContainers array
private static List<ModuleConfigurationInfo> _moduleConfigInfo;
// this is the per instance list that contains the events for each module
private PipelineModuleStepContainer[] _moduleContainers;
// Byte array to be used by HttpRequest.GetEntireRawContent. Windows OS Bug 1632921
private byte[] _entityBuffer;
// Counts the number of code paths consuming this HttpApplication instance. When the counter hits zero,
// it is safe to release this HttpApplication instance back into the HttpApplication pool.
// This counter can be null if we're not using the new Task-friendly code paths.
internal CountdownTask ApplicationInstanceConsumersCounter;
private IAllocatorProvider _allocator;
//
// Public Application properties
//
/// <devdoc>
/// <para>
/// HTTPRuntime provided context object that provides access to additional
/// pipeline-module exposed objects.
/// </para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public HttpContext Context {
get {
return(_context != null) ? _context : _initContext;
}
}
private bool IsContainerInitalizationAllowed {
get {
if (HttpRuntime.UseIntegratedPipeline && _initSpecialCompleted && !_initInternalCompleted) {
// return true if
// i) this is integrated pipeline mode,
// ii) InitSpecial has been called at least once in this AppDomain to register events with IIS,
// iii) InitInternal has not been invoked yet or is currently executing
return true;
}
return false;
}
}
private void ThrowIfEventBindingDisallowed() {
if (HttpRuntime.UseIntegratedPipeline && _initSpecialCompleted && _initInternalCompleted) {
// throw if we're using the integrated pipeline and both InitSpecial and InitInternal have completed.
throw new InvalidOperationException(SR.GetString(SR.Event_Binding_Disallowed));
}
}
private PipelineModuleStepContainer[] ModuleContainers {
get {
if (_moduleContainers == null) {
Debug.Assert(_moduleIndexMap != null && _moduleIndexMap.Count > 0, "_moduleIndexMap != null && _moduleIndexMap.Count > 0");
// At this point, all modules have been registered with IIS via RegisterIntegratedEvent.
// Now we need to create a container for each module and add execution steps.
// The number of containers is the same as the number of modules that have been
// registered (_moduleIndexMap.Count).
_moduleContainers = new PipelineModuleStepContainer[_moduleIndexMap.Count];
for (int i = 0; i < _moduleContainers.Length; i++) {
_moduleContainers[i] = new PipelineModuleStepContainer();
}
}
return _moduleContainers;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public event EventHandler Disposed {
add {
Events.AddHandler(EventDisposed, value);
}
remove {
Events.RemoveHandler(EventDisposed, value);
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
protected EventHandlerList Events {
get {
if (_events == null) {
_events = new EventHandlerList();
}
return _events;
}
}
internal IExecutionStep CreateImplicitAsyncPreloadExecutionStep() {
ImplicitAsyncPreloadModule implicitAsyncPreloadModule = new ImplicitAsyncPreloadModule();
BeginEventHandler beginHandler = null;
EndEventHandler endHandler = null;
implicitAsyncPreloadModule.GetEventHandlers(out beginHandler, out endHandler);
return new AsyncEventExecutionStep(this, beginHandler, endHandler, null);
}
private AsyncAppEventHandlersTable AsyncEvents {
get {
if (_asyncEvents == null)
_asyncEvents = new AsyncAppEventHandlersTable();
return _asyncEvents;
}
}
// Last error during the processing of the current request.
internal Exception LastError {
get {
// only temporaraly public (will be internal and not related context)
return (_context != null) ? _context.Error : _lastError;
}
}
// Used by HttpRequest.GetEntireRawContent. Windows OS Bug 1632921
internal byte[] EntityBuffer
{
get
{
if (_entityBuffer == null)
{
_entityBuffer = new byte[8 * 1024];
}
return _entityBuffer;
}
}
// Provides fixed size reusable buffers per request
// Benefit:
// 1) Eliminates global locks - access to HttpApplication instance is lock free and no concurrent access is expected (by design).
// 36+ cores show really bad spin lock characteristics for short locks.
// 2) Better lifetime dynamics - Buffers increase/decrease as HttpApplication instances grow/shrink on demand.
internal IAllocatorProvider AllocatorProvider {
get {
if (_allocator == null) {
AllocatorProvider alloc = new AllocatorProvider();
alloc.CharBufferAllocator = new SimpleBufferAllocator<char>(BufferingParams.CHAR_BUFFER_SIZE);
alloc.IntBufferAllocator = new SimpleBufferAllocator<int>(BufferingParams.INT_BUFFER_SIZE);
alloc.IntPtrBufferAllocator = new SimpleBufferAllocator<IntPtr>(BufferingParams.INTPTR_BUFFER_SIZE);
Interlocked.CompareExchange(ref _allocator, alloc, null);
}
return _allocator;
}
}
internal void ClearError() {
_lastError = null;
}
/// <devdoc>
/// <para>HTTPRuntime provided request intrinsic object that provides access to incoming HTTP
/// request data.</para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public HttpRequest Request {
get {
HttpRequest request = null;
if (_context != null && !_hideRequestResponse)
request = _context.Request;
if (request == null)
throw new HttpException(SR.GetString(SR.Request_not_available));
return request;
}
}
/// <devdoc>
/// <para>HTTPRuntime provided
/// response intrinsic object that allows transmission of HTTP response data to a
/// client.</para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public HttpResponse Response {
get {
HttpResponse response = null;
if (_context != null && !_hideRequestResponse)
response = _context.Response;
if (response == null)
throw new HttpException(SR.GetString(SR.Response_not_available));
return response;
}
}
/// <devdoc>
/// <para>
/// HTTPRuntime provided session intrinsic.
/// </para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public HttpSessionState Session {
get {
HttpSessionState session = null;
if (_session != null)
session = _session;
else if (_context != null)
session = _context.Session;
if (session == null)
throw new HttpException(SR.GetString(SR.Session_not_available));
return session;
}
}
/// <devdoc>
/// <para>
/// Returns
/// a reference to an HTTPApplication state bag instance.
/// </para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public HttpApplicationState Application {
get {
Debug.Assert(_state != null); // app state always available
return _state;
}
}
/// <devdoc>
/// <para>Provides the web server Intrinsic object.</para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public HttpServerUtility Server {
get {
if (_context != null)
return _context.Server;
else
return new HttpServerUtility(this); // special Server for application only
}
}
/// <devdoc>
/// <para>Provides the User Intrinsic object.</para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public IPrincipal User {
get {
if (_context == null)
throw new HttpException(SR.GetString(SR.User_not_available));
return _context.User;
}
}
/// <devdoc>
/// <para>
/// Collection
/// of all IHTTPModules configured for the current application.
/// </para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public HttpModuleCollection Modules {
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.High)]
get {
if (_moduleCollection == null)
_moduleCollection = new HttpModuleCollection();
return _moduleCollection;
}
}
// event passed to all modules
internal EventArgs AppEvent {
get {
if (_appEvent == null)
_appEvent = EventArgs.Empty;
return _appEvent;
}
set {
_appEvent = null;
}
}
private ISessionStateModule FindISessionStateModule() {
if (!HttpRuntime.UseIntegratedPipeline)
return null;
if (_moduleCollection != null) {
for (int i = 0; i < _moduleCollection.Count; i++) {
ISessionStateModule module = _moduleCollection.Get(i) as ISessionStateModule;
if (module != null) {
return module;
}
}
}
return null;
}
// DevDiv Bugs 151914: Release session state before executing child request
internal void EnsureReleaseState() {
ISessionStateModule module = FindISessionStateModule();
if (module != null) {
module.ReleaseSessionState(Context);
}
}
internal Task EnsureReleaseStateAsync() {
ISessionStateModule module = FindISessionStateModule();
if (module != null) {
return module.ReleaseSessionStateAsync(Context);
}
return TaskAsyncHelper.CompletedTask;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void CompleteRequest() {
//
// Request completion (force skipping all steps until RequestEnd
//
_stepManager.CompleteRequest();
}
internal bool IsRequestCompleted {
get {
if (null == _stepManager) {
return false;
}
return _stepManager.IsCompleted;
}
}
bool IRequestCompletedNotifier.IsRequestCompleted {
get {
return IsRequestCompleted;
}
}
// Dev10 745301: Asynchronous pipeline steps can start a new thread that triggers
// a SendResponse notification. E.g., it might call Flush when a module is registered
// for PreSendRequestHeaders/Content. If the async pipeline step returns from ExecuteStep
// while the SendResponse notification is executing, the NotificationContext can
// be corrupted. To fix this, a lock is now taken to prevent multi-threaded access when
// the async pipeline step sets the NotificationContext.PendingAsyncCompletion field.
// The SendResponse notification also acquires the lock when it enters managed code and
// releases the lock when it leaves.
internal void AcquireNotifcationContextLock(ref bool locked) {
Debug.Assert(HttpRuntime.UseIntegratedPipeline, "HttpRuntime.UseIntegratedPipeline");
Monitor.Enter(_stepManager, ref locked);
}
internal void ReleaseNotifcationContextLock() {
Debug.Assert(HttpRuntime.UseIntegratedPipeline, "HttpRuntime.UseIntegratedPipeline");
Monitor.Exit(_stepManager);
}
// Some frameworks built on top of the integrated pipeline call Flush() on background thread which will trigger nested
// RQ_SEND_RESPONSE notification and replace the old context.NotificationContext with the new context.NotificationContext.
// In order to maintain proper synchronization logic at the time when the completion callback is called we need to make sure
// we access the original context.NotificationContext (and don't touch the nested one).
// It will make sure that we read the correct NotificationContext
[MethodImpl(MethodImplOptions.NoInlining)]
private void GetNotifcationContextPropertiesUnderLock(ref bool isReentry, ref int eventCount) {
bool locked = false;
try {
AcquireNotifcationContextLock(ref locked);
isReentry = Context.NotificationContext.IsReEntry;
eventCount = CurrentModuleContainer.GetEventCount(Context.CurrentNotification, Context.IsPostNotification) - 1;
}
finally {
if (locked) {
ReleaseNotifcationContextLock();
}
}
}
[MethodImpl(MethodImplOptions.NoInlining)] // Iniling this causes throughtput regression in ResumeStep
private void GetNotifcationContextProperties(ref bool isReentry, ref int eventCount) {
// Read optimistically (without lock)
var nc = Context.NotificationContext;
isReentry = nc.IsReEntry;
// We can continue optimistic read only if this is not reentry
if (!isReentry) {
eventCount = ModuleContainers[nc.CurrentModuleIndex].GetEventCount(nc.CurrentNotification, nc.IsPostNotification) - 1;
// Check if the optimistic read was consistent
if (object.ReferenceEquals(nc, Context.NotificationContext)) {
return;
}
}
GetNotifcationContextPropertiesUnderLock(ref isReentry, ref eventCount);
}
private void RaiseOnError() {
EventHandler handler = (EventHandler)Events[EventErrorRecorded];
if (handler != null) {
try {
handler(this, AppEvent);
}
catch (Exception e) {
if (_context != null) {
_context.AddError(e);
}
}
}
}
private void RaiseOnRequestCompleted() {
EventHandler handler = (EventHandler)Events[EventRequestCompleted];
if (handler != null) {
try {
handler(this, AppEvent);
}
catch (Exception e) {
WebBaseEvent.RaiseRuntimeError(e, this);
}
}
}
internal void RaiseOnPreSendRequestHeaders() {
EventHandler handler = (EventHandler)Events[EventPreSendRequestHeaders];
if (handler != null) {
try {
handler(this, AppEvent);
}
catch (Exception e) {
RecordError(e);
}
}
}
internal void RaiseOnPreSendRequestContent() {
EventHandler handler = (EventHandler)Events[EventPreSendRequestContent];
if (handler != null) {
try {
handler(this, AppEvent);
}
catch (Exception e) {
RecordError(e);
}
}
}
internal HttpAsyncResult AsyncResult {
get {
if (HttpRuntime.UseIntegratedPipeline) {
return (_context.NotificationContext != null) ? _context.NotificationContext.AsyncResult : null;
}
else {
return _ar;
}
}
set {
if (HttpRuntime.UseIntegratedPipeline) {
_context.NotificationContext.AsyncResult = value;
}
else {
_ar = value;
}
}
}
internal void AddSyncEventHookup(object key, Delegate handler, RequestNotification notification) {
AddSyncEventHookup(key, handler, notification, false);
}
private PipelineModuleStepContainer CurrentModuleContainer { get { return ModuleContainers[_context.CurrentModuleIndex]; } }
private PipelineModuleStepContainer GetModuleContainer(string moduleName) {
object value = _moduleIndexMap[moduleName];
if (value == null) {
return null;
}
int moduleIndex = (int)value;
#if DBG
Debug.Trace("PipelineRuntime", "GetModuleContainer: moduleName=" + moduleName + ", index=" + moduleIndex.ToString(CultureInfo.InvariantCulture) + "\r\n");
Debug.Assert(moduleIndex >= 0 && moduleIndex < ModuleContainers.Length, "moduleIndex >= 0 && moduleIndex < ModuleContainers.Length");
#endif
PipelineModuleStepContainer container = ModuleContainers[moduleIndex];
Debug.Assert(container != null, "container != null");
return container;
}
private void AddSyncEventHookup(object key, Delegate handler, RequestNotification notification, bool isPostNotification) {
ThrowIfEventBindingDisallowed();
// add the event to the delegate invocation list
// this keeps non-pipeline ASP.NET hosts working
Events.AddHandler(key, handler);
// For integrated pipeline mode, add events to the IExecutionStep containers only if
// InitSpecial has completed and InitInternal has not completed.
if (IsContainerInitalizationAllowed) {
// lookup the module index and add this notification
PipelineModuleStepContainer container = GetModuleContainer(CurrentModuleCollectionKey);
//WOS 1985878: HttpModule unsubscribing an event handler causes AV in Integrated Mode
if (container != null) {
#if DBG
container.DebugModuleName = CurrentModuleCollectionKey;
#endif
SyncEventExecutionStep step = new SyncEventExecutionStep(this, (EventHandler)handler);
container.AddEvent(notification, isPostNotification, step);
}
}
}
internal void RemoveSyncEventHookup(object key, Delegate handler, RequestNotification notification) {
RemoveSyncEventHookup(key, handler, notification, false);
}
internal void RemoveSyncEventHookup(object key, Delegate handler, RequestNotification notification, bool isPostNotification) {
ThrowIfEventBindingDisallowed();
Events.RemoveHandler(key, handler);
if (IsContainerInitalizationAllowed) {
PipelineModuleStepContainer container = GetModuleContainer(CurrentModuleCollectionKey);
//WOS 1985878: HttpModule unsubscribing an event handler causes AV in Integrated Mode
if (container != null) {
container.RemoveEvent(notification, isPostNotification, handler);
}
}
}
private void AddSendResponseEventHookup(object key, Delegate handler) {
ThrowIfEventBindingDisallowed();
// add the event to the delegate invocation list
// this keeps non-pipeline ASP.NET hosts working
Events.AddHandler(key, handler);
// For integrated pipeline mode, add events to the IExecutionStep containers only if
// InitSpecial has completed and InitInternal has not completed.
if (IsContainerInitalizationAllowed) {
// lookup the module index and add this notification
PipelineModuleStepContainer container = GetModuleContainer(CurrentModuleCollectionKey);
//WOS 1985878: HttpModule unsubscribing an event handler causes AV in Integrated Mode
if (container != null) {
#if DBG
container.DebugModuleName = CurrentModuleCollectionKey;
#endif
bool isHeaders = (key == EventPreSendRequestHeaders);
SendResponseExecutionStep step = new SendResponseExecutionStep(this, (EventHandler)handler, isHeaders);
container.AddEvent(RequestNotification.SendResponse, false /*isPostNotification*/, step);
}
}
}
private void RemoveSendResponseEventHookup(object key, Delegate handler) {
ThrowIfEventBindingDisallowed();
Events.RemoveHandler(key, handler);
if (IsContainerInitalizationAllowed) {
PipelineModuleStepContainer container = GetModuleContainer(CurrentModuleCollectionKey);
//WOS 1985878: HttpModule unsubscribing an event handler causes AV in Integrated Mode
if (container != null) {
container.RemoveEvent(RequestNotification.SendResponse, false /*isPostNotification*/, handler);
}
}
}
//
// Sync event hookup
//
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler BeginRequest {
add { AddSyncEventHookup(EventBeginRequest, value, RequestNotification.BeginRequest); }
remove { RemoveSyncEventHookup(EventBeginRequest, value, RequestNotification.BeginRequest); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler AuthenticateRequest {
add { AddSyncEventHookup(EventAuthenticateRequest, value, RequestNotification.AuthenticateRequest); }
remove { RemoveSyncEventHookup(EventAuthenticateRequest, value, RequestNotification.AuthenticateRequest); }
}
// internal - for back-stop module only
internal event EventHandler DefaultAuthentication {
add { AddSyncEventHookup(EventDefaultAuthentication, value, RequestNotification.AuthenticateRequest); }
remove { RemoveSyncEventHookup(EventDefaultAuthentication, value, RequestNotification.AuthenticateRequest); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostAuthenticateRequest {
add { AddSyncEventHookup(EventPostAuthenticateRequest, value, RequestNotification.AuthenticateRequest, true); }
remove { RemoveSyncEventHookup(EventPostAuthenticateRequest, value, RequestNotification.AuthenticateRequest, true); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler AuthorizeRequest {
add { AddSyncEventHookup(EventAuthorizeRequest, value, RequestNotification.AuthorizeRequest); }
remove { RemoveSyncEventHookup(EventAuthorizeRequest, value, RequestNotification.AuthorizeRequest); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostAuthorizeRequest {
add { AddSyncEventHookup(EventPostAuthorizeRequest, value, RequestNotification.AuthorizeRequest, true); }
remove { RemoveSyncEventHookup(EventPostAuthorizeRequest, value, RequestNotification.AuthorizeRequest, true); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler ResolveRequestCache {
add { AddSyncEventHookup(EventResolveRequestCache, value, RequestNotification.ResolveRequestCache); }
remove { RemoveSyncEventHookup(EventResolveRequestCache, value, RequestNotification.ResolveRequestCache); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostResolveRequestCache {
add { AddSyncEventHookup(EventPostResolveRequestCache, value, RequestNotification.ResolveRequestCache, true); }
remove { RemoveSyncEventHookup(EventPostResolveRequestCache, value, RequestNotification.ResolveRequestCache, true); }
}
public event EventHandler MapRequestHandler {
add {
if (!HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode));
}
AddSyncEventHookup(EventMapRequestHandler, value, RequestNotification.MapRequestHandler);
}
remove {
if (!HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode));
}
RemoveSyncEventHookup(EventMapRequestHandler, value, RequestNotification.MapRequestHandler);
}
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostMapRequestHandler {
add { AddSyncEventHookup(EventPostMapRequestHandler, value, RequestNotification.MapRequestHandler, true); }
remove { RemoveSyncEventHookup(EventPostMapRequestHandler, value, RequestNotification.MapRequestHandler); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler AcquireRequestState {
add { AddSyncEventHookup(EventAcquireRequestState, value, RequestNotification.AcquireRequestState); }
remove { RemoveSyncEventHookup(EventAcquireRequestState, value, RequestNotification.AcquireRequestState); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostAcquireRequestState {
add { AddSyncEventHookup(EventPostAcquireRequestState, value, RequestNotification.AcquireRequestState, true); }
remove { RemoveSyncEventHookup(EventPostAcquireRequestState, value, RequestNotification.AcquireRequestState, true); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PreRequestHandlerExecute {
add { AddSyncEventHookup(EventPreRequestHandlerExecute, value, RequestNotification.PreExecuteRequestHandler); }
remove { RemoveSyncEventHookup(EventPreRequestHandlerExecute, value, RequestNotification.PreExecuteRequestHandler); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostRequestHandlerExecute {
add { AddSyncEventHookup(EventPostRequestHandlerExecute, value, RequestNotification.ExecuteRequestHandler, true); }
remove { RemoveSyncEventHookup(EventPostRequestHandlerExecute, value, RequestNotification.ExecuteRequestHandler, true); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler ReleaseRequestState {
add { AddSyncEventHookup(EventReleaseRequestState, value, RequestNotification.ReleaseRequestState ); }
remove { RemoveSyncEventHookup(EventReleaseRequestState, value, RequestNotification.ReleaseRequestState); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostReleaseRequestState {
add { AddSyncEventHookup(EventPostReleaseRequestState, value, RequestNotification.ReleaseRequestState, true); }
remove { RemoveSyncEventHookup(EventPostReleaseRequestState, value, RequestNotification.ReleaseRequestState, true); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler UpdateRequestCache {
add { AddSyncEventHookup(EventUpdateRequestCache, value, RequestNotification.UpdateRequestCache); }
remove { RemoveSyncEventHookup(EventUpdateRequestCache, value, RequestNotification.UpdateRequestCache); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PostUpdateRequestCache {
add { AddSyncEventHookup(EventPostUpdateRequestCache, value, RequestNotification.UpdateRequestCache, true); }
remove { RemoveSyncEventHookup(EventPostUpdateRequestCache, value, RequestNotification.UpdateRequestCache, true); }
}
public event EventHandler LogRequest {
add {
if (!HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode));
}
AddSyncEventHookup(EventLogRequest, value, RequestNotification.LogRequest);
}
remove {
if (!HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode));
}
RemoveSyncEventHookup(EventLogRequest, value, RequestNotification.LogRequest);
}
}
public event EventHandler PostLogRequest {
add {
if (!HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode));
}
AddSyncEventHookup(EventPostLogRequest, value, RequestNotification.LogRequest, true);
}
remove {
if (!HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode));
}
RemoveSyncEventHookup(EventPostLogRequest, value, RequestNotification.LogRequest, true);
}
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler EndRequest {
add { AddSyncEventHookup(EventEndRequest, value, RequestNotification.EndRequest); }
remove { RemoveSyncEventHookup(EventEndRequest, value, RequestNotification.EndRequest); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler Error {
add { Events.AddHandler(EventErrorRecorded, value); }
remove { Events.RemoveHandler(EventErrorRecorded, value); }
}
// Dev10 902404: a new HttpApplication.RequestCompleted event raised when the managed objects associated with
// the request are being released. It allows modules to cleanup resources after all managed modules and handlers
// have executed. This may occur before the native processing of the request has completed; for example, before
// the final response bytes have been sent to the client. The HttpContext is not available during this event
// because it has already been released.
public event EventHandler RequestCompleted {
add { Events.AddHandler(EventRequestCompleted, value); }
remove { Events.RemoveHandler(EventRequestCompleted, value); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PreSendRequestHeaders {
add { AddSendResponseEventHookup(EventPreSendRequestHeaders, value); }
remove { RemoveSendResponseEventHookup(EventPreSendRequestHeaders, value); }
}
/// <devdoc><para>[To be supplied.]</para></devdoc>
public event EventHandler PreSendRequestContent {
add { AddSendResponseEventHookup(EventPreSendRequestContent, value); }
remove { RemoveSendResponseEventHookup(EventPreSendRequestContent, value); }
}
//
// Async event hookup
//
public void AddOnBeginRequestAsync(BeginEventHandler bh, EndEventHandler eh) {
AddOnBeginRequestAsync(bh, eh, null);
}