Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit ea3ea90

Browse files
committed
#8 - Use IHostingEnvironment.WebRoot as the default static files root.
1 parent acc5c31 commit ea3ea90

17 files changed

+108
-148
lines changed

src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using Microsoft.AspNet.Http;
65
using Microsoft.AspNet.StaticFiles;
76

@@ -17,7 +16,7 @@ public static class DefaultFilesExtensions
1716
/// </summary>
1817
/// <param name="builder"></param>
1918
/// <returns></returns>
20-
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder)
19+
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder)
2120
{
2221
return builder.UseDefaultFiles(new DefaultFilesOptions());
2322
}
@@ -28,9 +27,9 @@ public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder build
2827
/// <param name="builder"></param>
2928
/// <param name="requestPath">The relative request path and physical path.</param>
3029
/// <returns></returns>
31-
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder, string requestPath)
30+
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
3231
{
33-
return UseDefaultFiles(builder, new DefaultFilesOptions() { RequestPath = new PathString(requestPath) });
32+
return builder.UseDefaultFiles(new DefaultFilesOptions() { RequestPath = new PathString(requestPath) });
3433
}
3534

3635
/// <summary>
@@ -39,14 +38,9 @@ public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder build
3938
/// <param name="builder"></param>
4039
/// <param name="options"></param>
4140
/// <returns></returns>
42-
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder, DefaultFilesOptions options)
41+
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] DefaultFilesOptions options)
4342
{
44-
if (builder == null)
45-
{
46-
throw new ArgumentNullException("builder");
47-
}
48-
49-
return builder.Use(next => new DefaultFilesMiddleware(next, options).Invoke);
43+
return builder.UseMiddleware<DefaultFilesMiddleware>(options);
5044
}
5145
}
5246
}

src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.AspNet.Builder;
88
using Microsoft.AspNet.FileSystems;
9+
using Microsoft.AspNet.Hosting;
910
using Microsoft.AspNet.Http;
1011

1112
namespace Microsoft.AspNet.StaticFiles
@@ -26,19 +27,11 @@ public class DefaultFilesMiddleware
2627
/// </summary>
2728
/// <param name="next">The next middleware in the pipeline.</param>
2829
/// <param name="options">The configuration options for this middleware.</param>
29-
public DefaultFilesMiddleware(RequestDelegate next, DefaultFilesOptions options)
30+
public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DefaultFilesOptions options)
3031
{
31-
if (next == null)
32-
{
33-
throw new ArgumentNullException("next");
34-
}
35-
if (options == null)
36-
{
37-
throw new ArgumentNullException("options");
38-
}
3932
if (options.FileSystem == null)
4033
{
41-
options.FileSystem = new PhysicalFileSystem("." + options.RequestPath.Value);
34+
options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath));
4235
}
4336

4437
_next = next;

src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using Microsoft.AspNet.Http;
65
using Microsoft.AspNet.StaticFiles;
76

@@ -17,7 +16,7 @@ public static class DirectoryBrowserExtensions
1716
/// </summary>
1817
/// <param name="builder"></param>
1918
/// <returns></returns>
20-
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder)
19+
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder)
2120
{
2221
return builder.UseDirectoryBrowser(new DirectoryBrowserOptions());
2322
}
@@ -28,9 +27,9 @@ public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder b
2827
/// <param name="builder"></param>
2928
/// <param name="requestPath">The relative request path and physical path.</param>
3029
/// <returns></returns>
31-
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder, string requestPath)
30+
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
3231
{
33-
return UseDirectoryBrowser(builder, new DirectoryBrowserOptions() { RequestPath = new PathString(requestPath) });
32+
return builder.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(requestPath) });
3433
}
3534

3635
/// <summary>
@@ -39,14 +38,9 @@ public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder b
3938
/// <param name="builder"></param>
4039
/// <param name="options"></param>
4140
/// <returns></returns>
42-
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder, DirectoryBrowserOptions options)
41+
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] DirectoryBrowserOptions options)
4342
{
44-
if (builder == null)
45-
{
46-
throw new ArgumentNullException("builder");
47-
}
48-
49-
return builder.Use(next => new DirectoryBrowserMiddleware(next, options).Invoke);
43+
return builder.UseMiddleware<DirectoryBrowserMiddleware>(options);
5044
}
5145
}
5246
}

src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.AspNet.Builder;
88
using Microsoft.AspNet.FileSystems;
9+
using Microsoft.AspNet.Hosting;
910
using Microsoft.AspNet.Http;
1011

1112
namespace Microsoft.AspNet.StaticFiles
@@ -24,23 +25,15 @@ public class DirectoryBrowserMiddleware
2425
/// </summary>
2526
/// <param name="next">The next middleware in the pipeline.</param>
2627
/// <param name="options">The configuration for this middleware.</param>
27-
public DirectoryBrowserMiddleware(RequestDelegate next, DirectoryBrowserOptions options)
28+
public DirectoryBrowserMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DirectoryBrowserOptions options)
2829
{
29-
if (next == null)
30-
{
31-
throw new ArgumentNullException("next");
32-
}
33-
if (options == null)
34-
{
35-
throw new ArgumentNullException("options");
36-
}
3730
if (options.Formatter == null)
3831
{
3932
throw new ArgumentException(Resources.Args_NoFormatter);
4033
}
4134
if (options.FileSystem == null)
4235
{
43-
options.FileSystem = new PhysicalFileSystem("." + options.RequestPath.Value);
36+
options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath));
4437
}
4538

4639
_next = next;

src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public static class FileServerExtensions
1818
/// </summary>
1919
/// <param name="builder"></param>
2020
/// <returns></returns>
21-
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder)
21+
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder)
2222
{
23-
return UseFileServer(builder, new FileServerOptions());
23+
return builder.UseFileServer(new FileServerOptions());
2424
}
2525

2626
/// <summary>
@@ -29,9 +29,9 @@ public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder
2929
/// <param name="builder"></param>
3030
/// <param name="enableDirectoryBrowsing">Should directory browsing be enabled?</param>
3131
/// <returns></returns>
32-
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, bool enableDirectoryBrowsing)
32+
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, bool enableDirectoryBrowsing)
3333
{
34-
return UseFileServer(builder, new FileServerOptions() { EnableDirectoryBrowsing = enableDirectoryBrowsing });
34+
return builder.UseFileServer(new FileServerOptions() { EnableDirectoryBrowsing = enableDirectoryBrowsing });
3535
}
3636

3737
/// <summary>
@@ -40,9 +40,9 @@ public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder
4040
/// <param name="builder"></param>
4141
/// <param name="requestPath">The relative request path and physical path.</param>
4242
/// <returns></returns>
43-
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, string requestPath)
43+
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
4444
{
45-
return UseFileServer(builder, new FileServerOptions() { RequestPath = new PathString(requestPath) });
45+
return builder.UseFileServer(new FileServerOptions() { RequestPath = new PathString(requestPath) });
4646
}
4747

4848
/// <summary>
@@ -51,7 +51,7 @@ public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder
5151
/// <param name="builder"></param>
5252
/// <param name="options"></param>
5353
/// <returns></returns>
54-
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, FileServerOptions options)
54+
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] FileServerOptions options)
5555
{
5656
if (options == null)
5757
{

src/Microsoft.AspNet.StaticFiles/Helpers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Globalization;
6+
using System.IO;
67
using Microsoft.AspNet.Http;
78

89
namespace Microsoft.AspNet.StaticFiles
@@ -49,5 +50,10 @@ internal static bool TryParseHttpDate(string dateString, out DateTime parsedDate
4950
{
5051
return DateTime.TryParseExact(dateString, Constants.HttpDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate);
5152
}
53+
54+
internal static string ResolveRootPath(string webRoot, PathString path)
55+
{
56+
return Path.GetFullPath(Path.Combine(webRoot, path.Value ?? string.Empty));
57+
}
5258
}
5359
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.Framework.Runtime;
5+
6+
namespace Microsoft.AspNet.Hosting
7+
{
8+
[AssemblyNeutral]
9+
public interface IHostingEnvironment
10+
{
11+
string EnvironmentName { get; }
12+
13+
string WebRoot { get; }
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.AspNet.StaticFiles
7+
{
8+
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
9+
internal sealed class NotNullAttribute : Attribute
10+
{
11+
}
12+
}

src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ public static class SendFileExtensions
1717
/// </summary>
1818
/// <param name="builder"></param>
1919
/// <returns></returns>
20-
public static IApplicationBuilder UseSendFileFallback(this IApplicationBuilder builder)
20+
public static IApplicationBuilder UseSendFileFallback([NotNull] this IApplicationBuilder builder)
2121
{
22-
if (builder == null)
23-
{
24-
throw new ArgumentNullException("builder");
25-
}
26-
2722
/* TODO builder.GetItem(typeof(ISendFile))
2823
2924
// Check for advertised support
@@ -35,7 +30,7 @@ public static IApplicationBuilder UseSendFileFallback(this IApplicationBuilder b
3530
// Otherwise, insert a fallback SendFile middleware and advertise support
3631
SetSendFileCapability(builder.Properties);
3732
*/
38-
return builder.Use(next => new SendFileMiddleware(next).Invoke);
33+
return builder.UseMiddleware<SendFileMiddleware>();
3934
}
4035

4136
private static bool IsSendFileSupported(IDictionary<string, object> properties)

src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@ public class SendFileMiddleware
2525
/// Creates a new instance of the SendFileMiddleware.
2626
/// </summary>
2727
/// <param name="next">The next middleware in the pipeline.</param>
28-
public SendFileMiddleware(RequestDelegate next)
28+
public SendFileMiddleware([NotNull] RequestDelegate next)
2929
{
30-
if (next == null)
31-
{
32-
throw new ArgumentNullException("next");
33-
}
34-
3530
_next = next;
3631
}
3732

0 commit comments

Comments
 (0)