Exemple #1
0
 public IISContextFactory(MemoryPool <byte> memoryPool, IHttpApplication <T> application, IISOptions options, IISHttpServer server)
 {
     _application = application;
     _memoryPool  = memoryPool;
     _options     = options;
     _server      = server;
 }
 internal unsafe IISHttpContext(MemoryPool <byte> memoryPool, IntPtr pInProcessHandler, IISOptions options, IISHttpServer server)
     : base((HttpApiTypes.HTTP_REQUEST *)NativeMethods.HttpGetRawRequest(pInProcessHandler))
 {
     _memoryPool        = memoryPool;
     _pInProcessHandler = pInProcessHandler;
     _options           = options;
     _server            = server;
 }
        internal unsafe IISHttpContext(MemoryPool <byte> memoryPool, IntPtr pInProcessHandler, IISOptions options, IISHttpServer server)
            : base((HttpApiTypes.HTTP_REQUEST *)NativeMethods.http_get_raw_request(pInProcessHandler))
        {
            _thisHandle = GCHandle.Alloc(this);

            _memoryPool        = memoryPool;
            _pInProcessHandler = pInProcessHandler;
            _server            = server;

            NativeMethods.http_set_managed_context(pInProcessHandler, (IntPtr)_thisHandle);
            unsafe
            {
                Method = GetVerb();

                RawTarget = GetRawUrl();
                // TODO version is slow.
                HttpVersion = GetVersion();
                Scheme      = SslStatus != SslStatus.Insecure ? Constants.HttpsScheme : Constants.HttpScheme;
                KnownMethod = VerbId;

                var originalPath = RequestUriBuilder.DecodeAndUnescapePath(GetRawUrlInBytes());

                if (KnownMethod == HttpApiTypes.HTTP_VERB.HttpVerbOPTIONS && string.Equals(RawTarget, "*", StringComparison.Ordinal))
                {
                    PathBase = string.Empty;
                    Path     = string.Empty;
                }
                else
                {
                    // Path and pathbase are unescaped by RequestUriBuilder
                    // The UsePathBase middleware will modify the pathbase and path correctly
                    PathBase = string.Empty;
                    Path     = originalPath;
                }

                var cookedUrl = GetCookedUrl();
                QueryString = cookedUrl.GetQueryString() ?? string.Empty;

                // TODO: Avoid using long.ToString, it's pretty slow
                RequestConnectionId = ConnectionId.ToString(CultureInfo.InvariantCulture);

                // Copied from WebListener
                // This is the base GUID used by HTTP.SYS for generating the activity ID.
                // HTTP.SYS overwrites the first 8 bytes of the base GUID with RequestId to generate ETW activity ID.
                // The requestId should be set by the NativeRequestContext
                var guid = new Guid(0xffcb4c93, 0xa57f, 0x453c, 0xb6, 0x3f, 0x84, 0x71, 0xc, 0x79, 0x67, 0xbb);
                *((ulong *)&guid) = RequestId;

                // TODO: Also make this not slow
                TraceIdentifier = guid.ToString();

                var localEndPoint = GetLocalEndPoint();
                LocalIpAddress = localEndPoint.GetIPAddress();
                LocalPort      = localEndPoint.GetPort();

                var remoteEndPoint = GetRemoteEndPoint();
                RemoteIpAddress = remoteEndPoint.GetIPAddress();
                RemotePort      = remoteEndPoint.GetPort();
                StatusCode      = 200;

                RequestHeaders      = new RequestHeaders(this);
                HttpResponseHeaders = new HeaderCollection();
                ResponseHeaders     = HttpResponseHeaders;

                if (options.ForwardWindowsAuthentication)
                {
                    WindowsUser = GetWindowsPrincipal();
                    if (options.AutomaticAuthentication)
                    {
                        User = WindowsUser;
                    }
                }

                ResetFeatureCollection();

                // Check if the Http upgrade feature is available in IIS.
                // To check this, we can look at the server variable WEBSOCKET_VERSION
                // And see if there is a version. Same check that Katana did:
                // https://github.com/aspnet/AspNetKatana/blob/9f6e09af6bf203744feb5347121fe25f6eec06d8/src/Microsoft.Owin.Host.SystemWeb/OwinAppContext.cs#L125
                // Actively not locking here as acquiring a lock on every request will hurt perf more than checking the
                // server variables a few extra times if a bunch of requests hit the server at the same time.
                if (_websocketAvailability == WebsocketAvailabilityStatus.Uninitialized)
                {
                    NativeMethods.http_get_server_variable(pInProcessHandler, WebSocketVersionString, out var webSocketsSupported);
                    var webSocketsAvailable = !string.IsNullOrEmpty(webSocketsSupported);
                    _websocketAvailability = webSocketsAvailable ?
                                             WebsocketAvailabilityStatus.Available :
                                             WebsocketAvailabilityStatus.NotAvailable;
                }

                if (_websocketAvailability == WebsocketAvailabilityStatus.NotAvailable)
                {
                    _currentIHttpUpgradeFeature = null;
                }
            }

            RequestBody  = new IISHttpRequestBody(this);
            ResponseBody = new IISHttpResponseBody(this);

            Input = new Pipe(new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.ThreadPool, minimumSegmentSize: MinAllocBufferSize));
            var pipe = new Pipe(new PipeOptions(
                                    _memoryPool,
                                    readerScheduler: PipeScheduler.ThreadPool,
                                    pauseWriterThreshold: PauseWriterThreshold,
                                    resumeWriterThreshold: ResumeWriterTheshold,
                                    minimumSegmentSize: MinAllocBufferSize));

            Output = new OutputProducer(pipe);
        }
Exemple #4
0
 public IISHttpContextOfT(MemoryPool <byte> memoryPool, IHttpApplication <TContext> application, IntPtr pInProcessHandler, IISOptions options, IISHttpServer server)
     : base(memoryPool, pInProcessHandler, options, server)
 {
     _application = application;
 }