Пример #1
0
        protected void InitializeContext()
        {
            _thisHandle = GCHandle.Alloc(this);

            NativeMethods.HttpSetManagedContext(_pInProcessHandler, (IntPtr)_thisHandle);

            Method = GetVerb();

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

            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;

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

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

            ResetFeatureCollection();

            if (!_server.IsWebSocketAvailible(_pInProcessHandler))
            {
                _currentIHttpUpgradeFeature = null;
            }

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


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

            _bodyOutput = new OutputProducer(pipe);
        }
Пример #2
0
        internal unsafe HttpProtocol(PipeFactory pipeFactory, IntPtr pHttpContext)
            : base((HttpApiTypes.HTTP_REQUEST *)NativeMethods.http_get_raw_request(pHttpContext))
        {
            _thisHandle = GCHandle.Alloc(this);

            _pipeFactory  = pipeFactory;
            _pHttpContext = pHttpContext;

            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());

                // TODO: Read this from IIS config
                // See https://github.com/aspnet/IISIntegration/issues/427
                var prefix = "/";
                if (KnownMethod == HttpApiTypes.HTTP_VERB.HttpVerbOPTIONS && string.Equals(RawTarget, "*", StringComparison.Ordinal))
                {
                    PathBase = string.Empty;
                    Path     = string.Empty;
                }
                // These paths are both unescaped already.
                else if (originalPath.Length == prefix.Length - 1)
                {
                    // They matched exactly except for the trailing slash.
                    PathBase = originalPath;
                    Path     = string.Empty;
                }
                else
                {
                    // url: /base/path, prefix: /base/, base: /base, path: /path
                    // url: /, prefix: /, base: , path: /
                    PathBase = originalPath.Substring(0, prefix.Length - 1);
                    Path     = originalPath.Substring(prefix.Length - 1);
                }

                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(); // TODO Optimize for known headers
                ResponseHeaders     = HttpResponseHeaders;

                ResetFeatureCollection();
            }

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

            Input = _pipeFactory.Create(new PipeOptions {
                ReaderScheduler = TaskRunScheduler.Default
            });
            var pipe = _pipeFactory.Create(new PipeOptions {
                ReaderScheduler = TaskRunScheduler.Default
            });

            Output = new OutputProducer(pipe);
        }
Пример #3
0
        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);
        }
Пример #4
0
        internal unsafe IISHttpContext(MemoryPool <byte> memoryPool, IntPtr pInProcessHandler, IISOptions options)
            : base((HttpApiTypes.HTTP_REQUEST *)NativeMethods.http_get_raw_request(pInProcessHandler))
        {
            _thisHandle = GCHandle.Alloc(this);

            _memoryPool        = memoryPool;
            _pInProcessHandler = pInProcessHandler;

            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(); // TODO Optimize for known headers
                ResponseHeaders     = HttpResponseHeaders;

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

                ResetFeatureCollection();
            }

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

            Input = new Pipe(new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.ThreadPool));
            var pipe = new Pipe(new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.ThreadPool));

            Output = new OutputProducer(pipe);
        }