From 1bc4e21c22797a31c243f4323c57b2f7db091778 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Wed, 26 Nov 2014 12:24:56 -0800 Subject: [PATCH] Addressing IFileSystem breaking changes This PR is to address the breaking changes introduced with https://github.com/aspnet/FileSystem/pull/20 @Tratcher @pranavkm --- .../DefaultFilesMiddleware.cs | 40 ++++++++++--------- .../DirectoryBrowserMiddleware.cs | 7 ++-- .../StaticFileContext.cs | 6 +-- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs index 4cf577c429..6373705ca9 100644 --- a/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs @@ -48,32 +48,34 @@ namespace Microsoft.AspNet.StaticFiles /// public Task Invoke(HttpContext context) { - IEnumerable dirContents; PathString subpath; if (Helpers.IsGetOrHeadMethod(context.Request.Method) - && Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath) - && _options.FileSystem.TryGetDirectoryContents(subpath.Value, out dirContents)) + && Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)) { - // Check if any of our default files exist. - for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++) + var dirContents = _options.FileSystem.GetDirectoryContents(subpath.Value); + if (dirContents.Exists) { - string defaultFile = _options.DefaultFileNames[matchIndex]; - IFileInfo file; - // TryMatchPath will make sure subpath always ends with a "/" by adding it if needed. - if (_options.FileSystem.TryGetFileInfo(subpath + defaultFile, out file)) + // Check if any of our default files exist. + for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++) { - // If the path matches a directory but does not end in a slash, redirect to add the slash. - // This prevents relative links from breaking. - if (!Helpers.PathEndsInSlash(context.Request.Path)) + string defaultFile = _options.DefaultFileNames[matchIndex]; + var file = _options.FileSystem.GetFileInfo(subpath + defaultFile); + // TryMatchPath will make sure subpath always ends with a "/" by adding it if needed. + if (file.Exists) { - context.Response.StatusCode = 301; - context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString; - return Constants.CompletedTask; - } + // If the path matches a directory but does not end in a slash, redirect to add the slash. + // This prevents relative links from breaking. + if (!Helpers.PathEndsInSlash(context.Request.Path)) + { + context.Response.StatusCode = 301; + context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString; + return Constants.CompletedTask; + } - // Match found, re-write the url. A later middleware will actually serve the file. - context.Request.Path = new PathString(context.Request.Path.Value + defaultFile); - break; + // Match found, re-write the url. A later middleware will actually serve the file. + context.Request.Path = new PathString(context.Request.Path.Value + defaultFile); + break; + } } } } diff --git a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs index ba61682631..8b444538ef 100644 --- a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs @@ -50,7 +50,7 @@ namespace Microsoft.AspNet.StaticFiles { // Check if the URL matches any expected paths PathString subpath; - IEnumerable contents; + IDirectoryContents contents; if (Helpers.IsGetOrHeadMethod(context.Request.Method) && Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath) && TryGetDirectoryInfo(subpath, out contents)) @@ -70,9 +70,10 @@ namespace Microsoft.AspNet.StaticFiles return _next(context); } - private bool TryGetDirectoryInfo(PathString subpath, out IEnumerable contents) + private bool TryGetDirectoryInfo(PathString subpath, out IDirectoryContents contents) { - return _options.FileSystem.TryGetDirectoryContents(subpath.Value, out contents); + contents = _options.FileSystem.GetDirectoryContents(subpath.Value); + return contents.Exists; } } } diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs index a03a6c6fd5..06b6311338 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs @@ -116,8 +116,8 @@ namespace Microsoft.AspNet.StaticFiles public bool LookupFileInfo() { - bool found = _options.FileSystem.TryGetFileInfo(_subPath.Value, out _fileInfo); - if (found) + _fileInfo = _options.FileSystem.GetFileInfo(_subPath.Value); + if (_fileInfo.Exists) { _length = _fileInfo.Length; @@ -130,7 +130,7 @@ namespace Microsoft.AspNet.StaticFiles _etag = Convert.ToString(etagHash, 16); _etagQuoted = '\"' + _etag + '\"'; } - return found; + return _fileInfo.Exists; } public void ComprehendRequestHeaders()