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);
        }
Exemple #2
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 async Task Invoke(ProtoContext 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
                fileContext.ComprehendRequestHeaders();
                switch (fileContext.PreconditionState)
                {
                case StaticFileContext.PreconditionState.Unspecified:
                case StaticFileContext.PreconditionState.ShouldProcess:
                    if (fileContext.IsHeadMethod)
                    {
                        await fileContext.SendStatusAsync(Constants.Status200Ok);

                        return;
                    }

                    try
                    {
                        if (fileContext.IsRangeRequest)
                        {
                            await fileContext.SendRangeAsync();

                            return;
                        }

                        await fileContext.SendAsync();

                        _logger.FileServed(fileContext.SubPath, fileContext.PhysicalPath);
                        return;
                    }
                    catch (FileNotFoundException)
                    {
                        context.Response.Clear();
                    }
                    break;

                case StaticFileContext.PreconditionState.NotModified:
                    _logger.FileNotModified(fileContext.SubPath);
                    await fileContext.SendStatusAsync(Constants.Status304NotModified);

                    return;

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

                    return;

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

            await _next(context);
        }