public async Task OnRequest(HttpContext context)
        {
            var request     = context.Request;
            var requestPath = request.Path;

            if (string.Equals(requestPath, "/favicon.ico", StringComparison.OrdinalIgnoreCase))
            {
                context.Response.StatusCode = 200;
                await context.Response.Body.WriteAsync(Resources.Favicon);

                return;
            }

            var decodedPath  = HttpUtility.UrlDecode(requestPath);
            var physicalPath = _webHostEnvironment.ContentRootFileProvider.GetFileInfo(decodedPath).PhysicalPath;

            if (Directory.Exists(physicalPath) && request.Method == "GET" || request.Method == "HEAD")
            {
                var url = $"{request.Scheme}://{request.Host}{requestPath}";
                var uri = new Uri(url);

                var isRootPath  = string.Equals(requestPath, "/") || true;
                var mainPayload = _tinfoilIndexBuilder.Build(physicalPath, uri, _appSettings.IndexType, isRootPath ? _appSettings.MessageOfTheDay : null);

                var json = JsonSerializer.Serialize(mainPayload, new JsonSerializerOptions {
                    WriteIndented = true
                });

                context.Response.StatusCode  = 200;
                context.Response.ContentType = "application/json";

                await context.Response.WriteAsync(json, Encoding.UTF8);
            }
            else if (_fileFilter.IsFileAllowed(physicalPath) && File.Exists(physicalPath) && request.Method == "GET" || request.Method == "HEAD")
            {
                var rangeHeader = new RangeHeader
                {
                    RawValue = request.Headers["range"]
                };

                var ranges = rangeHeader.Ranges;
                var range  = ranges.Count == 1 ? ranges[0] : null;

                await context.Response.WriteFile(physicalPath, range : range);
            }
            else
            {
                context.Response.StatusCode = 404;
                await context.Response.WriteAsync("<!DOCTYPE html><http><head><title>Oops!</title></head><body style='text-align:center'>404<br>Not found...</body></html>");
            }
        }
Esempio n. 2
0
        private void AppendFlatten(Dir dir, TinfoilIndex tinfoilIndex)
        {
            var urlCombiner = _urlCombinerFactory.Create(dir.CorrespondingUrl);

            var localDirPath = dir.Path;

            if (!Directory.Exists(localDirPath))
            {
                return;
            }

            var filePaths = Directory.GetFiles(localDirPath, "*.*", SearchOption.AllDirectories);

            foreach (var filePath in filePaths)
            {
                if (!_fileFilter.IsFileAllowed(filePath))
                {
                    continue;
                }

                var relFilePath = filePath[localDirPath.Length..]; // SubString from dirPath.Length to the end
Esempio n. 3
0
        public TinfoilIndex Build(string directory, Uri correspondingUri, TinfoilIndexType indexType, string?messageOfTheDay)
        {
            var rooDirUri = correspondingUri.OriginalString.EndsWith('/') ? correspondingUri : new Uri(correspondingUri.OriginalString + "/");

            var tinfoilIndex = new TinfoilIndex
            {
                Success = messageOfTheDay,
            };

            switch (indexType)
            {
            case TinfoilIndexType.Flatten:
                var filePaths = Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories);
                foreach (var filePath in filePaths)
                {
                    if (!_fileFilter.IsFileAllowed(filePath))
                    {
                        continue;
                    }

                    var relFilePath = filePath[directory.Length..];