Exemplo n.º 1
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));
        }
Exemplo n.º 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 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));
        }