Exemplo n.º 1
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 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(HttpsCompressionMode.Compress, httpsCompressionFeature.Mode);
    }
Exemplo n.º 2
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);
    }
Exemplo n.º 3
0
        public override Task ExecuteResultAsync(ActionContext context)
        {
            var logger = context.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>().CreateLogger <StaticVirtualFileResult>();

            var fileContext = new StaticFileContext(context.HttpContext, _contentTypeProvider, logger, _fileInfo, _contentType);

            if (!fileContext.ValidateMethod())
            {
                LoggerExtensions.LogRequestMethodNotSupported(logger, context.HttpContext.Request.Method);
            }
            else if (!fileContext.LookupContentType())
            {
                LoggerExtensions.LogFileTypeNotSupported(logger, fileContext.SubPath);
            }
            else if (!fileContext.LookupFileInfo())
            {
                LoggerExtensions.LogFileNotFound(logger, 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());
                    }

                    LoggerExtensions.LogFileServed(logger, fileContext.SubPath, fileContext.PhysicalPath);
                    return(fileContext.SendAsync());

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

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

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

            return(Task.FromResult(0));
        }
        public override Task Invoke(IOwinContext context)
        {
            var path        = context.Request.Path.ToString();
            var fileContext = new StaticFileContext(context);

            if (fileContext.ValidateMethod())
            {
                var fileInfo = FindFile(path);
                if (fileInfo != null)
                {
                    var contentType = FindContentType(path);
                    fileContext.SetPayload(fileInfo, contentType);
                    fileContext.ComprehendRequestHeaders();

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

                        return(fileContext.SendAsync());

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

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

                    default:
                        throw new NotImplementedException(fileContext.GetPreconditionState().ToString());
                    }
                }
            }

            return(Next.Invoke(context));
        }