Beispiel #1
0
 public IISContextFactory(MemoryPool <byte> memoryPool, IHttpApplication <T> application, IISOptions options, IISHttpServer server)
 {
     _application = application;
     _memoryPool  = memoryPool;
     _options     = options;
     _server      = server;
 }
        public IISMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions <IISOptions> options, string pairingToken, IAuthenticationSchemeProvider authentication, IApplicationLifetime applicationLifetime)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (applicationLifetime == null)
            {
                throw new ArgumentNullException(nameof(applicationLifetime));
            }
            if (string.IsNullOrEmpty(pairingToken))
            {
                throw new ArgumentException("Missing or empty pairing token.");
            }

            _next    = next;
            _options = options.Value;

            if (_options.ForwardWindowsAuthentication)
            {
                authentication.AddScheme(new AuthenticationScheme(IISDefaults.AuthenticationScheme, _options.AuthenticationDisplayName, typeof(AuthenticationHandler)));
            }

            _pairingToken        = pairingToken;
            _applicationLifetime = applicationLifetime;
            _logger = loggerFactory.CreateLogger <IISMiddleware>();
        }
 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;
 }
Beispiel #4
0
        public IISHttpServer(IApplicationLifetime applicationLifetime, IAuthenticationSchemeProvider authentication, IOptions <IISOptions> options)
        {
            _applicationLifetime = applicationLifetime;
            _authentication      = authentication;
            _options             = options.Value;

            if (_options.ForwardWindowsAuthentication)
            {
                authentication.AddScheme(new AuthenticationScheme(IISDefaults.AuthenticationScheme, _options.AuthenticationDisplayName, typeof(IISServerAuthenticationHandler)));
            }
        }
Beispiel #5
0
 private Regex?GetRegex(IISOptions options)
 {
     if (!string.IsNullOrEmpty(options.IncludePattern))
     {
         return(new Regex(PatternToRegex(options.IncludePattern)));
     }
     if (options.IncludeHosts != null && options.IncludeHosts.Any())
     {
         return(new Regex(HostsToRegex(options.IncludeHosts)));
     }
     return(options.IncludeRegex);
 }
        public IISMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions <IISOptions> options, string pairingToken)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrEmpty(pairingToken))
            {
                throw new ArgumentException("Missing or empty pairing token.");
            }

            _next         = next;
            _options      = options.Value;
            _pairingToken = pairingToken;
            _logger       = loggerFactory.CreateLogger <IISMiddleware>();
        }
Beispiel #7
0
        private Target Target(IISOptions options)
        {
            var plugin = new IIS(log, userRoleService, helper, options);

            return(plugin.Generate().Result);
        }
        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);
        }
Beispiel #9
0
        internal List <IISBindingOption> FilterBindings(List <IISBindingOption> bindings, IISOptions options)
        {
            // Check if we have any bindings
            _log.Verbose("{0} named bindings found in IIS", bindings.Count());
            if (options.IncludeSiteIds != null && options.IncludeSiteIds.Any())
            {
                _log.Debug("Filtering by site(s) {0}", options.IncludeSiteIds);
                bindings = bindings.Where(x => options.IncludeSiteIds.Contains(x.SiteId)).ToList();
                _log.Verbose("{0} bindings remaining after site filter", bindings.Count());
            }
            else
            {
                _log.Verbose("No site filter applied");
            }

            // Filter by pattern
            var regex = GetRegex(options);

            if (regex != null)
            {
                _log.Debug("Filtering by host: {regex}", regex);
                bindings = bindings.Where(x => Matches(x, regex)).ToList();
                _log.Verbose("{0} bindings remaining after host filter", bindings.Count());
            }
            else
            {
                _log.Verbose("No host filter applied");
            }

            // Remove exlusions
            if (options.ExcludeHosts != null && options.ExcludeHosts.Any())
            {
                bindings = bindings.Where(x => !options.ExcludeHosts.Contains(x.HostUnicode)).ToList();
                _log.Verbose("{0} named bindings remaining after explicit exclusions", bindings.Count());
            }

            // Check if we have anything left
            _log.Verbose("{0} matching bindings found", bindings.Count());
            return(bindings.ToList());
        }
Beispiel #10
0
 public IISContextFactory(MemoryPool <byte> memoryPool, IHttpApplication <T> application, IISOptions options)
 {
     _application = application;
     _memoryPool  = memoryPool;
     _options     = options;
 }
Beispiel #11
0
 public IISHttpContextOfT(MemoryPool <byte> memoryPool, IHttpApplication <TContext> application, IntPtr pInProcessHandler, IISOptions options, IISHttpServer server)
     : base(memoryPool, pInProcessHandler, options, server)
 {
     _application = application;
 }
Beispiel #12
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);
        }
 internal AuthenticationHandler(HttpContext httpContext, IISOptions options, ClaimsPrincipal user)
 {
     HttpContext = httpContext;
     User        = user;
     Options     = options;
 }