From b883968eefa73f7b546c681e5135f515ff666a13 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Thu, 4 Dec 2014 14:44:16 -0800 Subject: [PATCH] #86 - Implement IWebRootFileSystemProvider --- .../HostingEnvironment.cs | 5 +- .../HostingServicesCollectionExtensions.cs | 2 + .../IHostingEnvironment.cs | 2 - .../IWebRootFileSystemProvider.cs | 18 +++++++ .../WebRootFileSystemProvider.cs | 50 +++++++++++++++++++ src/Microsoft.AspNet.Hosting/project.json | 1 + .../HostingEngineTests.cs | 10 ++++ .../testroot/TextFile.txt | 1 + .../TestServerTests.cs | 6 +-- .../project.json | 3 +- 10 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/Microsoft.AspNet.Hosting/IWebRootFileSystemProvider.cs create mode 100644 src/Microsoft.AspNet.Hosting/WebRootFileSystemProvider.cs create mode 100644 test/Microsoft.AspNet.Hosting.Tests/testroot/TextFile.txt diff --git a/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs b/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs index f137f2ae5b..acafebc789 100644 --- a/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs +++ b/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs @@ -10,9 +10,8 @@ namespace Microsoft.AspNet.Hosting { private const string DefaultEnvironmentName = "Development"; - public HostingEnvironment(IApplicationEnvironment appEnv, IEnumerable configures) + public HostingEnvironment(IEnumerable configures) { - WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath); EnvironmentName = DefaultEnvironmentName; foreach (var configure in configures) { @@ -21,7 +20,5 @@ namespace Microsoft.AspNet.Hosting } public string EnvironmentName { get; set; } - - public string WebRoot { get; set; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs b/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs index ac661999b2..ee9fea15aa 100644 --- a/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs @@ -35,6 +35,8 @@ namespace Microsoft.Framework.DependencyInjection services.TryAdd(describer.Instance(new ApplicationLifetime())); + services.TryAdd(describer.Singleton()); + services.AddTypeActivator(configuration); // TODO: Do we expect this to be provide by the runtime eventually? services.AddLogging(configuration); diff --git a/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs b/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs index 3760c17799..380c72550e 100644 --- a/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs +++ b/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs @@ -9,7 +9,5 @@ namespace Microsoft.AspNet.Hosting public interface IHostingEnvironment { string EnvironmentName { get; set; } - - string WebRoot { get; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/IWebRootFileSystemProvider.cs b/src/Microsoft.AspNet.Hosting/IWebRootFileSystemProvider.cs new file mode 100644 index 0000000000..8f65cff6e4 --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/IWebRootFileSystemProvider.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.FileSystems; +using Microsoft.Framework.Runtime; + +namespace Microsoft.AspNet.Hosting +{ + [AssemblyNeutral] + public interface IWebRootFileSystemProvider + { + string WebRoot { get; } + + IFileSystem GetFileSystem(); + + string MapPath(string path); + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/WebRootFileSystemProvider.cs b/src/Microsoft.AspNet.Hosting/WebRootFileSystemProvider.cs new file mode 100644 index 0000000000..79fefaab5e --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/WebRootFileSystemProvider.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.IO; +using Microsoft.AspNet.FileSystems; +using Microsoft.Framework.Runtime; + +namespace Microsoft.AspNet.Hosting +{ + public class WebRootFileSystemProvider : IWebRootFileSystemProvider + { + private readonly IFileSystem _fileSystem; + + public WebRootFileSystemProvider(IApplicationEnvironment appEnvironment) + { + var root = HostingUtilities.GetWebRoot(appEnvironment.ApplicationBasePath); + + if (!string.IsNullOrEmpty(root) && + root[root.Length - 1] != Path.DirectorySeparatorChar) + { + root += Path.DirectorySeparatorChar; + } + + WebRoot = root; + + _fileSystem = new PhysicalFileSystem(WebRoot); + } + + public string WebRoot { get; private set; } + + public IFileSystem GetFileSystem() + { + return _fileSystem; + } + + public string MapPath(string path) + { + var fullPath = Path.GetFullPath(Path.Combine(WebRoot, path)); + + // Don't allow MapPath to escape the base root directory + if (!fullPath.StartsWith(WebRoot, StringComparison.OrdinalIgnoreCase)) + { + throw new ArgumentException("Invalid path: " + path, nameof(path)); + } + + return fullPath; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/project.json b/src/Microsoft.AspNet.Hosting/project.json index e39c50689a..63dd7c6c7d 100644 --- a/src/Microsoft.AspNet.Hosting/project.json +++ b/src/Microsoft.AspNet.Hosting/project.json @@ -2,6 +2,7 @@ "version": "1.0.0-*", "description": "ASP.NET 5 core hosting infrastructure and startup logic for web applications.", "dependencies": { + "Microsoft.AspNet.FileSystems": "1.0.0-*", "Microsoft.AspNet.PipelineCore": "1.0.0-*", "Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.OptionsModel": "1.0.0-*", diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index 7fa4a9a289..3677b8041d 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting.Server; @@ -52,6 +53,15 @@ namespace Microsoft.AspNet.Hosting Assert.Equal(1, _startInstances[0].DisposeCalls); } + [Fact] + public void WebRootCanBeResolvedFromTheProjectJson() + { + var services = HostingServices.Create().BuildServiceProvider(); + var provider = services.GetRequiredService(); + Assert.Equal(Path.GetFullPath("testroot") + Path.DirectorySeparatorChar, provider.WebRoot); + Assert.True(provider.GetFileSystem().GetFileInfo("TextFile.txt").Exists); + } + public void Initialize(IApplicationBuilder builder) { diff --git a/test/Microsoft.AspNet.Hosting.Tests/testroot/TextFile.txt b/test/Microsoft.AspNet.Hosting.Tests/testroot/TextFile.txt new file mode 100644 index 0000000000..d5669ad838 --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/testroot/TextFile.txt @@ -0,0 +1 @@ +A text file. \ No newline at end of file diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs index 4f8b619dd7..c64c0af3fc 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs @@ -53,12 +53,12 @@ namespace Microsoft.AspNet.TestHost } [Fact] - public void WebRootCanBeResolvedFromProjectJson() + public void WebRootCanBeResolvedWhenNotInTheProjectJson() { TestServer server = TestServer.Create(app => { - var env = app.ApplicationServices.GetRequiredService(); - Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot); + var provider = app.ApplicationServices.GetRequiredService(); + Assert.Equal(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, provider.WebRoot); }); } diff --git a/test/Microsoft.AspNet.TestHost.Tests/project.json b/test/Microsoft.AspNet.TestHost.Tests/project.json index ef0573cbe7..996af770f3 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/project.json +++ b/test/Microsoft.AspNet.TestHost.Tests/project.json @@ -8,6 +8,5 @@ }, "frameworks": { "aspnet50": { } - }, - "webroot": "testroot" + } }