コード例 #1
0
        public void LookupFileInfo_ReturnsTrue_IfFileExists()
        {
            // Arrange
            var options      = new StaticFileOptions();
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile("/foo.txt", new TestFileInfo
            {
                LastModified = new DateTimeOffset(2014, 1, 2, 3, 4, 5, TimeSpan.Zero)
            });
            var pathString  = new PathString("/test");
            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path = new PathString("/test/foo.txt");
            var validateResult    = StaticFileMiddleware.ValidatePath(httpContext, pathString, out var subPath);
            var contentTypeResult = StaticFileMiddleware.LookupContentType(new FileExtensionContentTypeProvider(), options, subPath, out var contentType);

            var context = new StaticFileContext(httpContext, options, NullLogger.Instance, fileProvider, contentType, subPath);

            // Act
            var result = context.LookupFileInfo();

            // Assert
            Assert.True(validateResult);
            Assert.True(contentTypeResult);
            Assert.True(result);
        }
コード例 #2
0
        public async Task EnablesHttpsCompression_IfMatched()
        {
            var options      = new StaticFileOptions();
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile("/foo.txt", new TestFileInfo
            {
                LastModified = new DateTimeOffset(2014, 1, 2, 3, 4, 5, TimeSpan.Zero)
            });
            var pathString              = new PathString("/test");
            var httpContext             = new DefaultHttpContext();
            var httpsCompressionFeature = new TestHttpsCompressionFeature();

            httpContext.Features.Set <IHttpsCompressionFeature>(httpsCompressionFeature);
            httpContext.Request.Path = new PathString("/test/foo.txt");
            var context = new StaticFileContext(httpContext, options, pathString, NullLogger.Instance, fileProvider, new FileExtensionContentTypeProvider());

            context.ValidatePath();
            var result = context.LookupFileInfo();

            Assert.True(result);

            await context.SendAsync();

            Assert.Equal(HttpsCompressionMode.Compress, httpsCompressionFeature.Mode);
        }
コード例 #3
0
        /// <summary>
        /// Processes a request to determine if it matches a known file, and if so, serves it.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Invoke(HttpContext context)
        {
            var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider);

            if (!fileContext.ValidateNoEndpoint())
            {
                _logger.EndpointMatched();
            }
            else if (!fileContext.ValidateMethod())
            {
                _logger.RequestMethodNotSupported(context.Request.Method);
            }
            else if (!fileContext.ValidatePath())
            {
                _logger.PathMismatch(fileContext.SubPath);
            }
            else if (!fileContext.LookupContentType())
            {
                _logger.FileTypeNotSupported(fileContext.SubPath);
            }
            else if (!fileContext.LookupFileInfo())
            {
                _logger.FileNotFound(fileContext.SubPath);
            }
            else
            {
                // If we get here, we can try to serve the file
                return(ServeStaticFile(context, fileContext));
            }

            return(_next(context));
        }
コード例 #4
0
        public async Task RequestAborted_DoesntThrow()
        {
            var options      = new StaticFileOptions();
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile("/foo.txt", new TestFileInfo
            {
                LastModified = new DateTimeOffset(2014, 1, 2, 3, 4, 5, TimeSpan.Zero)
            });
            var pathString  = new PathString("/test");
            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path   = new PathString("/test/foo.txt");
            httpContext.RequestAborted = new CancellationToken(canceled: true);
            var body = new MemoryStream();

            httpContext.Response.Body = body;
            var validateResult    = StaticFileMiddleware.ValidatePath(httpContext, pathString, out var subPath);
            var contentTypeResult = StaticFileMiddleware.LookupContentType(new FileExtensionContentTypeProvider(), options, subPath, out var contentType);

            var context = new StaticFileContext(httpContext, options, NullLogger.Instance, fileProvider, contentType, subPath);

            var result = context.LookupFileInfo();

            Assert.True(validateResult);
            Assert.True(contentTypeResult);
            Assert.True(result);

            await context.SendAsync();

            Assert.Equal(0, body.Length);
        }
コード例 #5
0
        public void SkipsHttpsCompression_IfNotMatched()
        {
            var options      = new StaticFileOptions();
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile("/foo.txt", new TestFileInfo
            {
                LastModified = new DateTimeOffset(2014, 1, 2, 3, 4, 5, TimeSpan.Zero)
            });
            var pathString              = new PathString("/test");
            var httpContext             = new DefaultHttpContext();
            var httpsCompressionFeature = new TestHttpsCompressionFeature();

            httpContext.Features.Set <IHttpsCompressionFeature>(httpsCompressionFeature);
            httpContext.Request.Path = new PathString("/test/bar.txt");
            var validateResult    = StaticFileMiddleware.ValidatePath(httpContext, pathString, out var subPath);
            var contentTypeResult = StaticFileMiddleware.LookupContentType(new FileExtensionContentTypeProvider(), options, subPath, out var contentType);

            var context = new StaticFileContext(httpContext, options, NullLogger.Instance, fileProvider, contentType, subPath);

            var result = context.LookupFileInfo();

            Assert.True(validateResult);
            Assert.True(contentTypeResult);
            Assert.False(result);

            Assert.Equal(HttpsCompressionMode.Default, httpsCompressionFeature.Mode);
        }
コード例 #6
0
        /// <summary>
        /// Processes a request to determine if it matches a known file, and if so, serves it.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Invoke(HttpContext context)
        {
            var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider);

            if (!fileContext.ValidateMethod())
            {
                _logger.LogRequestMethodNotSupported(context.Request.Method);
            }
            else if (!fileContext.ValidatePath())
            {
                _logger.LogPathMismatch(fileContext.SubPath);
            }
            else if (!fileContext.LookupContentType())
            {
                _logger.LogFileTypeNotSupported(fileContext.SubPath);
            }
            else if (!fileContext.LookupFileInfo())
            {
                _logger.LogFileNotFound(fileContext.SubPath);
            }
            else
            {
                // If we get here, we can try to serve the file
                fileContext.ComprehendRequestHeaders();

                switch (fileContext.GetPreconditionState())
                {
                case StaticFileContext.PreconditionState.Unspecified:
                case StaticFileContext.PreconditionState.ShouldProcess:
                    if (fileContext.IsHeadMethod)
                    {
                        return(fileContext.SendStatusAsync(Constants.Status200Ok));
                    }
                    if (fileContext.IsRangeRequest)
                    {
                        return(fileContext.SendRangeAsync());
                    }

                    _logger.LogFileServed(fileContext.SubPath, fileContext.PhysicalPath);
                    return(fileContext.SendAsync());

                case StaticFileContext.PreconditionState.NotModified:
                    _logger.LogPathNotModified(fileContext.SubPath);
                    return(fileContext.SendStatusAsync(Constants.Status304NotModified));

                case StaticFileContext.PreconditionState.PreconditionFailed:
                    _logger.LogPreconditionFailed(fileContext.SubPath);
                    return(fileContext.SendStatusAsync(Constants.Status412PreconditionFailed));

                default:
                    var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
                    Debug.Fail(exception.ToString());
                    throw exception;
                }
            }

            return(_next(context));
        }
コード例 #7
0
        public void LookupFileInfo_ReturnsFalse_IfFileDoesNotExist()
        {
            // Arrange
            var options = new StaticFileOptions();
            var context = new StaticFileContext(new DefaultHttpContext(), options, PathString.Empty, NullLogger.Instance, new TestFileProvider(), new FileExtensionContentTypeProvider());

            // Act
            var validateResult = context.ValidatePath();
            var lookupResult   = context.LookupFileInfo();

            // Assert
            Assert.True(validateResult);
            Assert.False(lookupResult);
        }
コード例 #8
0
        private Task TryServeStaticFile(HttpContext context, string?contentType, PathString subPath)
        {
            var fileContext = new StaticFileContext(context, _options, _logger, _fileProvider, contentType, subPath);

            if (!fileContext.LookupFileInfo())
            {
                _logger.FileNotFound(fileContext.SubPath);
            }
            else
            {
                // If we get here, we can try to serve the file
                return(fileContext.ServeStaticFile(context, _next));
            }

            return(_next(context));
        }
コード例 #9
0
        public void LookupFileInfo_ReturnsFalse_IfFileDoesNotExist()
        {
            // Arrange
            var options           = new StaticFileOptions();
            var httpContext       = new DefaultHttpContext();
            var pathString        = PathString.Empty;
            var validateResult    = StaticFileMiddleware.ValidatePath(httpContext, pathString, out var subPath);
            var contentTypeResult = StaticFileMiddleware.LookupContentType(new FileExtensionContentTypeProvider(), options, subPath, out var contentType);
            var context           = new StaticFileContext(httpContext, options, NullLogger.Instance, new TestFileProvider(), contentType, subPath);

            // Act
            var lookupResult = context.LookupFileInfo();

            // Assert
            Assert.True(validateResult);
            Assert.False(contentTypeResult);
            Assert.False(lookupResult);
        }