Example #1
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);
    }
Example #2
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);
    }
Example #3
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);
    }
Example #4
0
 public Task Invoke(HtcHttpContext httpContext)
 {
     foreach (string fileName in _files)
     {
         if (fileName.Equals("/$internal"))
         {
             foreach (string fileName2 in httpContext.Site.Indexes)
             {
                 var    path2     = httpContext.Request.Path.Add(fileName2);
                 string extension = Path.GetExtension(path2);
                 var    fileInfo  = httpContext.Site.FileProvider.GetFileInfo(path2);
                 if (fileInfo.Exists && httpContext.Site.FileExtensions.TryGetValue(extension, out var extensionProcessor))
                 {
                     return(extensionProcessor.OnHttpExtensionProcess(_next, httpContext, path2, extension));
                 }
             }
         }
         var path = httpContext.Request.Path.Add(fileName);
         var staticFileContext = new StaticFileContext(httpContext, httpContext.Site.FileProvider, path);
         if (!staticFileContext.LookupFileInfo())
         {
             continue;
         }
         return(staticFileContext.ServeStaticFile(httpContext, _next));
     }
     return(_next(httpContext));
 }
Example #5
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));
        }
Example #6
0
 public Task Invoke(HtcHttpContext httpContext)
 {
     foreach (string replaceName in _paths)
     {
         PathString fileName          = replaceName.Replace("$uri", httpContext.Request.Path.Value);
         var        staticFileContext = new StaticFileContext(httpContext, httpContext.Site.FileProvider, fileName);
         if (!staticFileContext.LookupFileInfo())
         {
             continue;
         }
         return(staticFileContext.ServeStaticFile(httpContext, _next));
     }
     return(_next(httpContext));
 }
    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));
    }
Example #8
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);
    }