File: HostedHttpRequestAsyncResult.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 (1026 lines) | stat: -rw-r--r-- 44,530 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Activation
{
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Net;
    using System.Runtime;
    using System.Runtime.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Security.Authentication.ExtendedProtection;
    using System.Security.Cryptography.X509Certificates;
    using System.Security.Permissions;
    using System.Security.Principal;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Activation.Configuration;
    using System.ServiceModel.Activation.Diagnostics;
    using System.Threading;
    using System.Web;
    using System.Web.Management;
    using System.Web.Routing;
    using TD2 = System.ServiceModel.Diagnostics.Application.TD;

    class HostedHttpRequestAsyncResult : AsyncResult, HttpChannelListener.IHttpAuthenticationContext
    {
        [Fx.Tag.SecurityNote(Critical = "Stores the securitycritical callback values, we need to protect these values")]
        [SecurityCritical]
        static WindowsIdentity anonymousIdentity;
        [SecurityCritical]
        static Action<object> waitOnBeginRequest;
        [SecurityCritical]
        static Action<object> waitOnBeginRequestWithFlow;
        [SecurityCritical]
        static ContextCallback contextOnBeginRequest;
        [SecurityCritical]
        static AsyncCallback processRequestCompleteCallback;


        [ThreadStatic]
        static AutoResetEvent waitObject;

        static Nullable<bool> iisSupportsExtendedProtection;

        [Fx.Tag.SecurityNote(Critical = "Keeps track of impersonated user, caller must use with care")]
        [SecurityCritical]
        HostedImpersonationContext impersonationContext;

        [Fx.Tag.SecurityNote(Critical = "Keeps track of thread static data (HttpContext, CurrentCulture, CurrentUICulture) that is used for AspNetCompatibility mode, caller must use with care")]
        [SecurityCritical]
        HostedThreadData hostedThreadData;

        [Fx.Tag.SecurityNote(Critical =
            "This field is used to manipulate request/responses using APIs protected by LinkDemand." +
            "It is critical because we use it to determine whether we believe we're being hosted in ASP.NET or not." +
            "The field is set in the constructor of this class and we deem it safe because:" +
            "    1) all paths that lead to the .ctor are SecurityCritical and" +
            "    2) those paths have called ServiceHostingEnvironment.EnsureInitialized (which is also critical)" +
            "So if the field is non-null, it's safe to say that we're hosted in ASP.NET, hence all the helper methods in this class that touch this field can be SecurityTreatAsSafe")]
        [SecurityCritical]
        HttpApplication context;

        int state;

        int streamedReadState;

        [Fx.Tag.SecurityNote(Critical = "Determines whether to set the HttpContext on the outgoing thread.")]
        [SecurityCritical]
        bool flowContext;
        bool ensureWFService;
        string configurationBasedServiceVirtualPath;
        EventTraceActivity eventTraceActivity;

        readonly bool isWebSocketRequest;

        [Fx.Tag.SecurityNote(Critical = "Captures HostedImpersonationContext which must be done in the right place, and calls unsafe" +
        "ScheduleCallbackLowPriNoFlow and ScriptTimeout.  Called outside of user security context.")]
        [SecurityCritical]
        public HostedHttpRequestAsyncResult(HttpApplication context, bool flowContext, bool ensureWFService, AsyncCallback callback, object state) :
            this(context, null, flowContext, ensureWFService, callback, state)
        {
        }

        [Fx.Tag.SecurityNote(Critical = "Captures HostedImpersonationContext which must be done in the right place, and calls unsafe" +
            "ScheduleCallbackLowPriNoFlow and ScriptTimeout.  Called outside of user security context.")]
        [SecurityCritical]
        public HostedHttpRequestAsyncResult(HttpApplication context, string aspNetRouteServiceVirtualPath, bool flowContext, bool ensureWFService, AsyncCallback callback, object state) :
            base(callback, state)
        {
            if (context == null)
            {
                throw FxTrace.Exception.ArgumentNull("context");
            }

            AspNetPartialTrustHelpers.FailIfInPartialTrustOutsideAspNet();

            HostedAspNetEnvironment.TrySetWebSocketVersion(context);
            this.context = context;
            // WebSockets require the integrated pipeline mode and the WebSocket IIS module to be loaded. If these conditions 
            // are not met, the HttpContext.IsWebSocketRequest property throws. Also, if these conditions are not met,
            // we do not let WebSocket listeners to be started (we fail the service activation), so setting the 'isWebSocketRequest' flag 
            // to false in this case will not create confusion (or make troubleshooting difficult).
            this.isWebSocketRequest = HttpRuntime.UsingIntegratedPipeline && AspNetEnvironment.Current.IsWebSocketModuleLoaded && this.context.Context.IsWebSocketRequest;
            this.flowContext = flowContext;
            if (ensureWFService)
            {
                // check for CBA scenario. if true, service should be handled by WCF instead of WF, 
                // set this.ensureWFservice to false
                if (ServiceHostingEnvironment.IsConfigurationBasedService(context, out this.configurationBasedServiceVirtualPath))
                {
                    this.ensureWFService = false;
                }
                else
                {
                    this.ensureWFService = true;
                }
            }

            if (!string.IsNullOrEmpty(aspNetRouteServiceVirtualPath))
            {
                // aspnet routing can hijack CBA request as we append {*pathInfo} to urlpattern and there is no real file for CBA 
                // check for CBA scenario. if the request is hijacked. i.e., 
                // 1) route maps to a virtual directory:
                // aspNetRouteServiceVirtualPath <> context.Request.AppRelativeCurrentExecutionFilePath == configurationBasedServiceVirtualPath 
                // if RouteExistingFiles <> true, set aspnetRouteServiceVirtualPath to null so that the request will be treated as CBA
                // if RouteExistingFiles == true, this hijack is by-design, do nothing
                // 2) route maps to a CBA entry:
                // aspNetRouteServiceVirtualPath == context.Request.AppRelativeCurrentExecutionFilePath == configurationBasedServiceVirtualPath
                // we will use RouteExistingFiles to decide which service should be activated. We do it in ServiceHostingEnviroment.HostingManager, 
                // as we cannot pass this info to the latter. 
                if (!RouteTable.Routes.RouteExistingFiles &&
                    ServiceHostingEnvironment.IsConfigurationBasedService(context, out this.configurationBasedServiceVirtualPath))
                {
                    this.AspNetRouteServiceVirtualPath = null;
                }
                else
                {
                    this.AspNetRouteServiceVirtualPath = aspNetRouteServiceVirtualPath;
                }
            }

            // If this is a DEBUG request, complete right away and let ASP.NET handle it.
            string method = context.Request.HttpMethod ?? "";
            char firstMethodChar = method.Length == 5 ? method[0] : '\0';
            if ((firstMethodChar == 'd' || firstMethodChar == 'D') &&
                string.Compare(method, "DEBUG", StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (DiagnosticUtility.ShouldTraceVerbose)
                {
                    TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.WebHostDebugRequest, SR.TraceCodeWebHostDebugRequest, this);
                }

                this.state = State.Completed;
                Complete(true, null);
                return;
            }

            this.impersonationContext = new HostedImpersonationContext();

            if (flowContext)
            {
                if (ServiceHostingEnvironment.AspNetCompatibilityEnabled)
                {
                    // Capture HttpContext/culture context if necessary.  Can be used later by HostedHttpInput to re-apply
                    // the culture during dispatch.  Also flowed here.
                    hostedThreadData = new HostedThreadData();
                }
            }

            // Set this up before calling IncrementRequestCount so if it fails, we don't leak a count.
            Action<object> iotsCallback = (AspNetPartialTrustHelpers.NeedPartialTrustInvoke || flowContext) ?
                WaitOnBeginRequestWithFlow : WaitOnBeginRequest;

            // Tell ASPNET to by-pass all the other events so no other http modules will
            // be invoked, Indigo basically takes over the request completely. This should
            // only be called in non-AspNetCompatibilityEnabled mode.
            if (!ServiceHostingEnvironment.AspNetCompatibilityEnabled && !this.ensureWFService)
            {
                context.CompleteRequest();
            }

            // Prevent ASP.NET from generating thread aborts in relation to this request.
            context.Server.ScriptTimeout = int.MaxValue;

            ServiceHostingEnvironment.IncrementRequestCount(ref this.eventTraceActivity, context.Request.AppRelativeCurrentExecutionFilePath);

            IOThreadScheduler.ScheduleCallbackLowPriNoFlow(iotsCallback, this);
        }

        public static WindowsIdentity AnonymousIdentity
        {
            [Fx.Tag.SecurityNote(Critical = "Access the value of corresponding static field and prevent someone from changing its value")]
            [SecuritySafeCritical]
            get
            {
                if (anonymousIdentity == null)
                {
                    anonymousIdentity = WindowsIdentity.GetAnonymous();
                }
                return anonymousIdentity;
            }
        }

        public static Action<object> WaitOnBeginRequest
        {
            [Fx.Tag.SecurityNote(Critical = "Access the value of corresponding static field and prevent someone from changing its value")]
            [SecuritySafeCritical]
            get
            {
                if (waitOnBeginRequest == null)
                {
                    waitOnBeginRequest = new Action<object>(OnBeginRequest);
                }
                return waitOnBeginRequest;
            }
        }

        public static Action<object> WaitOnBeginRequestWithFlow
        {
            [Fx.Tag.SecurityNote(Critical = "Access the value of corresponding static field and prevent someone from changing its value")]
            [SecuritySafeCritical]
            get
            {
                if (waitOnBeginRequestWithFlow == null)
                {
                    waitOnBeginRequestWithFlow = new Action<object>(OnBeginRequestWithFlow);
                }
                return waitOnBeginRequestWithFlow;
            }
        }

        public static ContextCallback ContextOnBeginRequest
        {
            [Fx.Tag.SecurityNote(Critical = "Access the value of corresponding static field and prevent someone from changing its value")]
            [SecuritySafeCritical]
            get
            {
                if (contextOnBeginRequest == null)
                {
                    contextOnBeginRequest = new ContextCallback(OnBeginRequest);
                }
                return contextOnBeginRequest;
            }
        }

        public static AsyncCallback ProcessRequestCompleteCallback
        {
            [Fx.Tag.SecurityNote(Critical = "Access the value of corresponding static field and prevent someone from changing its value")]
            [SecuritySafeCritical]
            get
            {
                if (processRequestCompleteCallback == null)
                {
                    processRequestCompleteCallback = Fx.ThunkCallback(new AsyncCallback(ProcessRequestComplete));
                }
                return processRequestCompleteCallback;
            }
        }

        public bool IISSupportsExtendedProtection
        {
            get
            {
                if (HostedHttpRequestAsyncResult.iisSupportsExtendedProtection == null)
                {
                    HostedHttpRequestAsyncResult.iisSupportsExtendedProtection = this.IISSupportsExtendedProtectionInternal();
                }
                return HostedHttpRequestAsyncResult.iisSupportsExtendedProtection.Value;
            }
        }

        public bool IsWebSocketRequest
        {
            get { return this.isWebSocketRequest; }
        }

        [Fx.Tag.SecurityNote(Critical = "Touches critical field context.", Safe = "Does not leak control or data, no potential for harm.")]
        [SecuritySafeCritical]
        [MethodImpl(MethodImplOptions.NoInlining)]
        [PermissionSetAttribute(SecurityAction.Assert, Unrestricted = true)]
        private bool IISSupportsExtendedProtectionInternal()
        {
            DiagnosticUtility.DebugAssert(ExtendedProtectionPolicy.OSSupportsExtendedProtection, "OS must support ExtendedProtection");

            try
            {
                ChannelBinding cbt = this.context.Request.HttpChannelBinding;
                return true;
            }
            catch (PlatformNotSupportedException)
            {
                // contract with Asp.Net is that they will always throw a PlatformNotSupportedException if IIS is not patched for CBT yet
                return false;
            }
            catch (COMException)
            {
                // If IIS is patched for CBT and an error occurs when trying to retrieve the token a COMException is thrown. Even in this
                // case we know that IIS is patched for CBT.
                return true;
            }
        }


        [Fx.Tag.SecurityNote(Critical = "Captures HostedImpersonationContext which must be done in the right place, and calls unsafe" +
            "ScheduleCallbackLowPriNoFlow and ScriptTimeout.  Called outside of user security context." +
            "Callers of this function must call ServiceHostingEnvironment.EnsureInitialized")]
        [SecurityCritical]
        public static void ExecuteSynchronous(HttpApplication context, bool flowContext, bool ensureWFService)
        {
            ExecuteSynchronous(context, null, flowContext, ensureWFService);
        }

        [Fx.Tag.SecurityNote(Critical = "Captures HostedImpersonationContext which must be done in the right place, and calls unsafe" +
            "ScheduleCallbackLowPriNoFlow and ScriptTimeout.  Called outside of user security context." +
            "Callers of this function must call ServiceHostingEnvironment.EnsureInitialized")]
        [SecurityCritical]
        public static void ExecuteSynchronous(HttpApplication context, string routeServiceVirtualPath, bool flowContext, bool ensureWFService)
        {
            AutoResetEvent wait = HostedHttpRequestAsyncResult.waitObject;
            if (wait == null)
            {
                wait = new AutoResetEvent(false);
                HostedHttpRequestAsyncResult.waitObject = wait;
            }

            HostedHttpRequestAsyncResult result;
            try
            {
                result = new HostedHttpRequestAsyncResult(context, routeServiceVirtualPath, flowContext, ensureWFService, ProcessRequestCompleteCallback, wait);
                if (!result.CompletedSynchronously)
                {
                    wait.WaitOne();
                }
                wait = null;
            }
            finally
            {
                if (wait != null)
                {
                    // Not sure of the state anymore.
                    HostedHttpRequestAsyncResult.waitObject = null;
                    wait.Close();
                }
            }

            HostedHttpRequestAsyncResult.End(result);
        }

        [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview - Can be called outside of a user context.")]
        static void ProcessRequestComplete(IAsyncResult result)
        {
            if (!result.CompletedSynchronously)
            {
                try
                {
                    ((AutoResetEvent)result.AsyncState).Set();
                }
                catch (ObjectDisposedException exception)
                {
                    DiagnosticUtility.TraceHandledException(exception, TraceEventType.Warning);
                }
            }
        }

        [Fx.Tag.SecurityNote(Critical = "Can be called outside of user context, accesses hostedThreadData.",
            Safe = "Uses hostedThreadData to set HttpContext.Current, cultures to the one attached to this async-result instance.")]
        [SecuritySafeCritical]
        static void OnBeginRequestWithFlow(object state)
        {
            HostedHttpRequestAsyncResult self = (HostedHttpRequestAsyncResult)state;

            IDisposable hostedThreadContext = null;
            try
            {
                if (self.flowContext)
                {
                    // In AspCompat case, these are the three things that need to be flowed.  See HostedHttpInput.
                    if (self.hostedThreadData != null)
                    {
                        hostedThreadContext = self.hostedThreadData.CreateContext();
                    }
                }

                // In full-trust, this simply calls the delegate.
                AspNetPartialTrustHelpers.PartialTrustInvoke(ContextOnBeginRequest, self);
            }
            finally
            {
                if (hostedThreadContext != null)
                {
                    hostedThreadContext.Dispose();
                }
            }
        }

        static void OnBeginRequest(object state)
        {
            HostedHttpRequestAsyncResult self = (HostedHttpRequestAsyncResult)state;

            Exception completionException = null;
            try
            {
                self.BeginRequest();
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                completionException = e;
            }

            if (completionException != null)
            {
                self.CompleteOperation(completionException);
            }
        }

        void BeginRequest()
        {
            try
            {
                HandleRequest();
            }
            catch (EndpointNotFoundException exception)
            {
                // HTTP-GET is special cased to avoid that the ServiceActivation-HTTP-response is treated as service response.
                // For WebSocket requests we treat the ServiceActivation in the same way like for SOAP (HTTP-POST) requests.
                if (string.Compare(GetHttpMethod(), "GET", StringComparison.OrdinalIgnoreCase) == 0 &&
                    !this.isWebSocketRequest)
                {
                    // Wrap the exception into HttpException
                    throw FxTrace.Exception.AsError(new HttpException((int)HttpStatusCode.NotFound, exception.Message, exception));
                }

                SetStatusCode((int)HttpStatusCode.NotFound);
                CompleteOperation(null);
            }
            catch (ServiceActivationException exception)
            {
                // HTTP-GET is special cased to avoid that the ServiceActivation-HTTP-response is treated as service response.
                // For WebSocket requests we treat the ServiceActivation in the same way like for SOAP (HTTP-POST) requests.
                if (string.Compare(GetHttpMethod(), "GET", StringComparison.OrdinalIgnoreCase) == 0 &&
                    !this.isWebSocketRequest)
                {
                    if (exception.InnerException is HttpException)
                    {
                        throw exception.InnerException;
                    }
                    else
                    {
                        throw;
                    }
                }

                SetStatusCode((int)HttpStatusCode.InternalServerError);
                SetStatusDescription(
                    HttpChannelUtilities.StatusDescriptionStrings.HttpStatusServiceActivationException);
                CompleteOperation(null);
            }
            finally
            {
                ReleaseImpersonation();
            }
        }

        public WindowsIdentity LogonUserIdentity
        {
            get
            {
                IPrincipal user = this.Application.User;
                if (user == null)
                {
                    return AnonymousIdentity;
                }

                WindowsIdentity identity = user.Identity as WindowsIdentity;
                if (identity == null)
                {
                    return AnonymousIdentity;
                }

                return identity;
            }
        }

        WindowsIdentity HttpChannelListener.IHttpAuthenticationContext.LogonUserIdentity
        {
            get
            {
                return this.LogonUserIdentity;
            }
        }

        public HostedImpersonationContext ImpersonationContext
        {
            [Fx.Tag.SecurityNote(Critical = "Keeps track of impersonated user, caller must use with care.",
                Safe = "Safe for Get, individual members of HostedImpersonationContext are protected.")]
            [SecuritySafeCritical]
            get
            {
                return this.impersonationContext;
            }
        }

        public HostedThreadData HostedThreadData
        {
            [Fx.Tag.SecurityNote(Critical = "Keeps track of impersonated user, caller must use with care.",
                Safe = "Safe for Get, individual members of HostedThreadData are protected.")]
            [SecuritySafeCritical]
            get
            {
                return this.hostedThreadData;
            }
        }

        public EventTraceActivity EventTraceActivity
        {
            get
            {
                return this.eventTraceActivity;
            }
        }

        public Uri OriginalRequestUri
        {
            get;
            private set;

        }

        public Uri RequestUri
        {
            get;
            private set;
        }

        public HttpApplication Application
        {
            [Fx.Tag.SecurityNote(Critical = "Touches critical field context.", Safe = "Does not leak control or data, no potential for harm.")]
            [SecuritySafeCritical]
            get
            {
                return this.context;
            }
        }

        public string AspNetRouteServiceVirtualPath
        {
            get;
            private set;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.", Safe = "Does not leak control or data, no potential for harm.")]
        [SecuritySafeCritical]
        public Stream GetInputStream()
        {
            try
            {
                // CSDMain #133228: "Consume GetBufferlessInputStream"
                // The ReadEntityBodyMode property on the HttpRequest keeps track of whether the request stream has already been accessed, and if so, what API was used to access the request.
                //     - "None" means that the request stream hasn't been accessed.
                //     - "Bufferless" means that GetBufferlessInputStream() was used to access it.
                //     - "Buffered" means GetBufferedInputStream() was used to access it.
                //     - "Classic" means that either the InputStream, Form, Files, or BinaryRead APIs were invoked already.
                // In general, these values are incompatible with one another, meaning that once the request was accessed in a "Classic" way, only "Classic" APIs can be invoked on the HttpRequest.
                // If incompatible APIs are invoked, an HttpException is thrown.
                // In order to prevent HttpExceptions from being thrown for this reason, we will check the ReadEntityBodyMode, and access the request stream with the corresponding API
                // If the request stream hasn't been accessed yet (eg, by an HttpModule which executed earlier), then we will use GetBufferlessInputStream by default.
                ReadEntityBodyMode mode = this.context.Request.ReadEntityBodyMode;
                Fx.Assert(mode == ReadEntityBodyMode.None || mode == ReadEntityBodyMode.Bufferless || mode == ReadEntityBodyMode.Buffered || mode == ReadEntityBodyMode.Classic,
                    "Unknown value for System.Web.ReadEntityBodyMode enum");

                if (mode == ReadEntityBodyMode.None && ServiceHostingEnvironment.AspNetCompatibilityEnabled && AppSettings.UseClassicReadEntityMode)
                {
                    mode = ReadEntityBodyMode.Classic;
                }

                switch (mode)
                {
                    case ReadEntityBodyMode.None:
                    case ReadEntityBodyMode.Bufferless:
                        return this.context.Request.GetBufferlessInputStream(true);  // ignores system.web/httpRuntime/maxRequestLength
                    case ReadEntityBodyMode.Buffered:
                        return this.context.Request.GetBufferedInputStream();
                    default: 
                        // ReadEntityBodyMode.Classic:
                        return this.context.Request.InputStream;
                }
            }
            catch (HttpException hostedException)
            {
                if (hostedException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
                {
                    throw FxTrace.Exception.AsError(HttpInput.CreateHttpProtocolException(SR.Hosting_MaxRequestLengthExceeded, HttpStatusCode.RequestEntityTooLarge, null, hostedException));
                }
                else
                {
                    throw FxTrace.Exception.AsError(new CommunicationException(hostedException.Message, hostedException));
                }
            }
        }

        public void OnReplySent()
        {
            CompleteOperation(null);
        }

        internal void CompleteOperation(Exception exception)
        {
            if (this.state == State.Running &&
                Interlocked.CompareExchange(ref this.state, State.Completed, State.Running) == State.Running)
            {
                this.CompleteAsynchronously(exception);
            }
        }

        public void Abort()
        {
            if (this.state == State.Running &&
                Interlocked.CompareExchange(ref this.state, State.Aborted, State.Running) == State.Running)
            {
                int currentStreamedReadState = Interlocked.Exchange(ref this.streamedReadState, StreamedReadState.AbortStarted);
                // Closes the socket connection to the client
                if (HttpRuntime.UsingIntegratedPipeline)
                {
                    Application.Request.Abort();
                }
                else
                {
                    Application.Response.Close();
                }

                if (currentStreamedReadState == StreamedReadState.None)
                {
                    this.CompleteAsynchronously(null);
                }
                else
                {
                    Fx.Assert(currentStreamedReadState == StreamedReadState.ReceiveStarted, string.Format(CultureInfo.InvariantCulture, "currentStramedReadState is not ReceivedStarted: {0}", currentStreamedReadState));
                    if (Interlocked.CompareExchange(ref this.streamedReadState, StreamedReadState.Aborted, StreamedReadState.AbortStarted) == StreamedReadState.AbortStarted)
                    {
                        return;
                    }

                    Fx.Assert(this.streamedReadState == StreamedReadState.ReceiveFinishedAfterAbortStarted, string.Format(CultureInfo.InvariantCulture, "currentStramedReadState is not ReceiveFinished: {0}", this.streamedReadState));
                    this.CompleteAsynchronously(null);
                }
            }
        }

        void CompleteAsynchronously(Exception ex)
        {
            Complete(false, ex);
            ServiceHostingEnvironment.DecrementRequestCount(this.eventTraceActivity);
        }

        internal bool TryStartStreamedRead()
        {
            return Interlocked.CompareExchange(ref this.streamedReadState, StreamedReadState.ReceiveStarted, StreamedReadState.None) == StreamedReadState.None;
        }

        internal void SetStreamedReadFinished()
        {
            if (Interlocked.CompareExchange(ref this.streamedReadState, StreamedReadState.None, StreamedReadState.ReceiveStarted) == StreamedReadState.ReceiveStarted)
            {
                return;
            }

            if (Interlocked.CompareExchange(ref this.streamedReadState, StreamedReadState.ReceiveFinishedAfterAbortStarted, StreamedReadState.AbortStarted) == StreamedReadState.AbortStarted)
            {
                return;
            }

            Fx.Assert(this.streamedReadState == StreamedReadState.Aborted, string.Format(CultureInfo.InvariantCulture, "currentStramedReadState is not Aborted: {0}", this.streamedReadState));

            this.CompleteAsynchronously(null);
        }

        [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview - Can be called outside of a user context.")]
        public static void End(IAsyncResult result)
        {
            try
            {
                AsyncResult.End<HostedHttpRequestAsyncResult>(result);
            }
            catch (Exception exception)
            {
                if (!Fx.IsFatal(exception))
                {
                    // Log the exception.
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.WebHost, 
                        (uint)System.Runtime.Diagnostics.EventLogEventId.WebHostFailedToProcessRequest,
                      TraceUtility.CreateSourceString(result),
                      exception == null ? string.Empty : exception.ToString());
                }
                throw;
            }
        }

        X509Certificate2 HttpChannelListener.IHttpAuthenticationContext.GetClientCertificate(out bool isValidCertificate)
        {
            HttpClientCertificate certificateInfo = this.Application.Request.ClientCertificate;
            isValidCertificate = certificateInfo.IsValid;
            if (certificateInfo.IsPresent)
            {
                return new X509Certificate2(certificateInfo.Certificate);
            }
            else
            {
                return null;
            }
        }

        TraceRecord HttpChannelListener.IHttpAuthenticationContext.CreateTraceRecord()
        {
            return new System.ServiceModel.Diagnostics.HttpRequestTraceRecord(this.Application.Request);
        }

        void HandleRequest()
        {
            this.OriginalRequestUri = GetUrl();
            string relativeVirtualPath;
            if (!string.IsNullOrEmpty(this.AspNetRouteServiceVirtualPath))
            {
                relativeVirtualPath = this.AspNetRouteServiceVirtualPath;
            }
            else if (!string.IsNullOrEmpty(this.configurationBasedServiceVirtualPath))
            {
                relativeVirtualPath = this.configurationBasedServiceVirtualPath;

            }
            else
            {
                relativeVirtualPath = GetAppRelativeCurrentExecutionFilePath();
            }

            if (ensureWFService)
            {
                bool bypass = false;
                try
                {
                    if (!ServiceHostingEnvironment.EnsureWorkflowService(relativeVirtualPath))
                    {
                        CompleteOperation(null);
                        bypass = true;
                        return;
                    }
                }
                finally
                {
                    if (!bypass)
                    {
                        CompleteRequest();
                    }
                }
            }

            // Support for Cassini.
            if (ServiceHostingEnvironment.IsSimpleApplicationHost)
            {
                HostedTransportConfigurationManager.EnsureInitializedForSimpleApplicationHost(this);
            }

            HttpHostedTransportConfiguration transportConfiguration = HostedTransportConfigurationManager.GetConfiguration(this.OriginalRequestUri.Scheme)
                as HttpHostedTransportConfiguration;
            HostedHttpTransportManager transportManager = null;

            // There must be a transport binding that matches the request.
            if (transportConfiguration != null)
            {
                transportManager = transportConfiguration.GetHttpTransportManager(this.OriginalRequestUri);
            }

            if (transportManager == null)
            {
                InvalidOperationException invalidOpException = new InvalidOperationException(SR.Hosting_TransportBindingNotFound(OriginalRequestUri.ToString()));

                ServiceActivationException activationException = new ServiceActivationException(invalidOpException.Message, invalidOpException);

                LogServiceActivationException(activationException);

                throw FxTrace.Exception.AsError(activationException);
            }

            this.RequestUri = new Uri(transportManager.ListenUri, this.OriginalRequestUri.PathAndQuery);
            Fx.Assert(
                object.ReferenceEquals(this.RequestUri.Scheme, Uri.UriSchemeHttp) || object.ReferenceEquals(this.RequestUri.Scheme, Uri.UriSchemeHttps),
                "Scheme must be Http or Https.");

            ServiceHostingEnvironment.EnsureServiceAvailableFast(relativeVirtualPath, this.eventTraceActivity);

            transportManager.HttpContextReceived(this);
        }

        [Fx.Tag.SecurityNote(Critical = "Calls into an unsafe UnsafeLogEvent method",
            Safe = "Event identities cannot be spoofed as they are constants determined inside the method")]
        [SecuritySafeCritical]
        void LogServiceActivationException(ServiceActivationException activationException)
        {
            if (TD2.ServiceExceptionIsEnabled())
            {
                TD2.ServiceException(this.eventTraceActivity, activationException.ToString(), typeof(ServiceActivationException).FullName);
            }

            if (TD.ServiceActivationExceptionIsEnabled())
            {
                TD.ServiceActivationException(activationException != null ? activationException.ToString() : string.Empty, activationException);
            }
            DiagnosticUtility.UnsafeEventLog.UnsafeLogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.WebHost, 
                (uint)System.Runtime.Diagnostics.EventLogEventId.WebHostFailedToProcessRequest, true,
                    TraceUtility.CreateSourceString(this), activationException.ToString());
        }

        [Fx.Tag.SecurityNote(Critical = "manipulates impersonation object",
           Safe = "Does not leak control or mutable/harmful data, no potential for harm except memory leak.")]
        [SecuritySafeCritical]
        internal void AddRefForImpersonation()
        {
            if (this.impersonationContext != null)
            {
                this.impersonationContext.AddRef();
            }
        }

        [Fx.Tag.SecurityNote(Critical = "manipulates impersonation object",
            Safe = "Releasing the SafeHandle early could only cause a future impersonation attempt to fail. We have to handle impersonation failures well already.")]
        [SecuritySafeCritical]
        internal void ReleaseImpersonation()
        {
            if (this.impersonationContext != null)
            {
                this.impersonationContext.Release();
            }
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, changes properties of the HTTP response.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void SetContentType(string contentType)
        {
            this.context.Response.ContentType = contentType;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, changes properties of the HTTP response.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void SetContentEncoding(string contentEncoding)
        {
            this.context.Response.AddHeader(HttpChannelUtilities.ContentEncodingHeader, contentEncoding);
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, completes the request.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void CompleteRequest()
        {
            this.context.CompleteRequest();
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, changes properties of the HTTP response.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void SetTransferModeToStreaming()
        {
            this.context.Response.BufferOutput = false;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, changes properties of the HTTP response.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void AppendHeader(string name, string value)
        {
            this.context.Response.AppendHeader(name, value);
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, changes properties of the HTTP response.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void SetStatusCode(int statusCode)
        {
            this.context.Response.TrySkipIisCustomErrors = true;
            this.context.Response.StatusCode = statusCode;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, changes properties of the HTTP response.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void SetStatusDescription(string statusDescription)
        {
            this.context.Response.StatusDescription = statusDescription;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects, changes properties of the HTTP response.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal void SetConnectionClose()
        {
            this.context.Response.AppendHeader("Connection", "close");
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal byte[] GetPrereadBuffer(ref int contentLength)
        {
            byte[] preReadBuffer = new byte[1];
            if (this.GetInputStream().Read(preReadBuffer, 0, 1) > 0)
            {
                contentLength = -1;
                return preReadBuffer;
            }
            return null;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal Stream GetOutputStream()
        {
            return this.context.Response.OutputStream;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal string GetHttpMethod()
        {
            return this.context.Request.HttpMethod;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal string GetContentType()
        {
            const string ContentTypeHeaderName = "Content-Type";
            return this.context.Request.Headers[ContentTypeHeaderName];
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal string GetAcceptEncoding()
        {
            return this.context.Request.Headers[HttpChannelUtilities.AcceptEncodingHeader];
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal string GetContentTypeFast()
        {
            return this.context.Request.ContentType;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal int GetContentLength()
        {
            return this.context.Request.ContentLength;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal string GetSoapAction()
        {
            const string SoapActionHeaderName = "SOAPAction";
            return this.context.Request.Headers[SoapActionHeaderName];
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        internal ChannelBinding GetChannelBinding()
        {
            if (!this.IISSupportsExtendedProtection)
            {
                return null;
            }

            return this.context.Request.HttpChannelBinding;
        }


        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        string GetAppRelativeCurrentExecutionFilePath()
        {
            return this.context.Request.AppRelativeCurrentExecutionFilePath;
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical]
        Uri GetUrl()
        {
            return this.context.Request.Url;
        }

        static class State
        {
            internal const int Running = 0;
            internal const int Completed = 1;
            internal const int Aborted = 2;
        }

        static class StreamedReadState
        {
            internal const int None = 0;
            internal const int ReceiveStarted = 1;
            internal const int ReceiveFinishedAfterAbortStarted = 2;
            internal const int AbortStarted = 3;
            internal const int Aborted = 4;
        }
    }
}