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

Commit

Permalink
#8 - Use IHostingEnvironment.WebRoot as the default static files root.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Sep 11, 2014
1 parent acc5c31 commit c62209b
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 136 deletions.
16 changes: 5 additions & 11 deletions src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;

Expand All @@ -17,7 +16,7 @@ public static class DefaultFilesExtensions
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder)
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder)
{
return builder.UseDefaultFiles(new DefaultFilesOptions());
}
Expand All @@ -28,9 +27,9 @@ public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder build
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <returns></returns>
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder, string requestPath)
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{
return UseDefaultFiles(builder, new DefaultFilesOptions() { RequestPath = new PathString(requestPath) });
return builder.UseDefaultFiles(new DefaultFilesOptions() { RequestPath = new PathString(requestPath) });
}

/// <summary>
Expand All @@ -39,14 +38,9 @@ public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder build
/// <param name="builder"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder, DefaultFilesOptions options)
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] DefaultFilesOptions options)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}

return builder.Use(next => new DefaultFilesMiddleware(next, options).Invoke);
return builder.UseMiddleware<DefaultFilesMiddleware>(options);
}
}
}
13 changes: 3 additions & 10 deletions src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileSystems;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;

namespace Microsoft.AspNet.StaticFiles
Expand All @@ -26,19 +27,11 @@ public class DefaultFilesMiddleware
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="options">The configuration options for this middleware.</param>
public DefaultFilesMiddleware(RequestDelegate next, DefaultFilesOptions options)
public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DefaultFilesOptions options)
{
if (next == null)
{
throw new ArgumentNullException("next");
}
if (options == null)
{
throw new ArgumentNullException("options");
}
if (options.FileSystem == null)
{
options.FileSystem = new PhysicalFileSystem("." + options.RequestPath.Value);
options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath));
}

_next = next;
Expand Down
16 changes: 5 additions & 11 deletions src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;

Expand All @@ -17,7 +16,7 @@ public static class DirectoryBrowserExtensions
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder)
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder)
{
return builder.UseDirectoryBrowser(new DirectoryBrowserOptions());
}
Expand All @@ -28,9 +27,9 @@ public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder b
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder, string requestPath)
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{
return UseDirectoryBrowser(builder, new DirectoryBrowserOptions() { RequestPath = new PathString(requestPath) });
return builder.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(requestPath) });
}

/// <summary>
Expand All @@ -39,14 +38,9 @@ public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder b
/// <param name="builder"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder, DirectoryBrowserOptions options)
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] DirectoryBrowserOptions options)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}

return builder.Use(next => new DirectoryBrowserMiddleware(next, options).Invoke);
return builder.UseMiddleware<DirectoryBrowserMiddleware>(options);
}
}
}
13 changes: 3 additions & 10 deletions src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileSystems;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;

namespace Microsoft.AspNet.StaticFiles
Expand All @@ -24,23 +25,15 @@ public class DirectoryBrowserMiddleware
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="options">The configuration for this middleware.</param>
public DirectoryBrowserMiddleware(RequestDelegate next, DirectoryBrowserOptions options)
public DirectoryBrowserMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DirectoryBrowserOptions options)
{
if (next == null)
{
throw new ArgumentNullException("next");
}
if (options == null)
{
throw new ArgumentNullException("options");
}
if (options.Formatter == null)
{
throw new ArgumentException(Resources.Args_NoFormatter);
}
if (options.FileSystem == null)
{
options.FileSystem = new PhysicalFileSystem("." + options.RequestPath.Value);
options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath));
}

_next = next;
Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static class FileServerExtensions
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder)
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder)
{
return UseFileServer(builder, new FileServerOptions());
return builder.UseFileServer(new FileServerOptions());
}

/// <summary>
Expand All @@ -29,9 +29,9 @@ public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder
/// <param name="builder"></param>
/// <param name="enableDirectoryBrowsing">Should directory browsing be enabled?</param>
/// <returns></returns>
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, bool enableDirectoryBrowsing)
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, bool enableDirectoryBrowsing)
{
return UseFileServer(builder, new FileServerOptions() { EnableDirectoryBrowsing = enableDirectoryBrowsing });
return builder.UseFileServer(new FileServerOptions() { EnableDirectoryBrowsing = enableDirectoryBrowsing });
}

/// <summary>
Expand All @@ -40,9 +40,9 @@ public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <returns></returns>
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, string requestPath)
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{
return UseFileServer(builder, new FileServerOptions() { RequestPath = new PathString(requestPath) });
return builder.UseFileServer(new FileServerOptions() { RequestPath = new PathString(requestPath) });
}

/// <summary>
Expand All @@ -51,7 +51,7 @@ public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder
/// <param name="builder"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, FileServerOptions options)
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] FileServerOptions options)
{
if (options == null)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.AspNet.StaticFiles/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Globalization;
using System.IO;
using Microsoft.AspNet.Http;

namespace Microsoft.AspNet.StaticFiles
Expand Down Expand Up @@ -49,5 +50,10 @@ internal static bool TryParseHttpDate(string dateString, out DateTime parsedDate
{
return DateTime.TryParseExact(dateString, Constants.HttpDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate);
}

internal static string ResolveRootPath(string webRoot, PathString path)
{
return Path.GetFullPath(Path.Combine(webRoot, "." + path.Value));
}
}
}
15 changes: 15 additions & 0 deletions src/Microsoft.AspNet.StaticFiles/IHostingEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.Framework.Runtime;

namespace Microsoft.AspNet.Hosting
{
[AssemblyNeutral]
public interface IHostingEnvironment
{
string EnvironmentName { get; }

string WebRoot { get; }
}
}
12 changes: 12 additions & 0 deletions src/Microsoft.AspNet.StaticFiles/NotNullAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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;

namespace Microsoft.AspNet.StaticFiles
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
internal sealed class NotNullAttribute : Attribute
{
}
}
9 changes: 2 additions & 7 deletions src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ public static class SendFileExtensions
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseSendFileFallback(this IApplicationBuilder builder)
public static IApplicationBuilder UseSendFileFallback([NotNull] this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}

/* TODO builder.GetItem(typeof(ISendFile))
// Check for advertised support
Expand All @@ -35,7 +30,7 @@ public static IApplicationBuilder UseSendFileFallback(this IApplicationBuilder b
// Otherwise, insert a fallback SendFile middleware and advertise support
SetSendFileCapability(builder.Properties);
*/
return builder.Use(next => new SendFileMiddleware(next).Invoke);
return builder.UseMiddleware<SendFileMiddleware>();
}

private static bool IsSendFileSupported(IDictionary<string, object> properties)
Expand Down
7 changes: 1 addition & 6 deletions src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ public class SendFileMiddleware
/// Creates a new instance of the SendFileMiddleware.
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
public SendFileMiddleware(RequestDelegate next)
public SendFileMiddleware([NotNull] RequestDelegate next)
{
if (next == null)
{
throw new ArgumentNullException("next");
}

_next = next;
}

Expand Down
18 changes: 3 additions & 15 deletions src/Microsoft.AspNet.StaticFiles/SendFileResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ public static class SendFileResponseExtensions
/// </summary>
/// <param name="response"></param>
/// <returns>True if sendfile.SendAsync is defined in the environment.</returns>
public static bool SupportsSendFile(this HttpResponse response)
public static bool SupportsSendFile([NotNull] this HttpResponse response)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
return response.HttpContext.GetFeature<IHttpSendFileFeature>() != null;
}

Expand All @@ -34,12 +30,8 @@ public static bool SupportsSendFile(this HttpResponse response)
/// <param name="response"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static Task SendFileAsync(this HttpResponse response, string fileName)
public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
return response.SendFileAsync(fileName, 0, null, CancellationToken.None);
}

Expand All @@ -52,12 +44,8 @@ public static Task SendFileAsync(this HttpResponse response, string fileName)
/// <param name="count">The number of types to send, or null to send the remainder of the file.</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task SendFileAsync(this HttpResponse response, string fileName, long offset, long? count, CancellationToken cancellationToken)
public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName, long offset, long? count, CancellationToken cancellationToken)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
var sendFile = response.HttpContext.GetFeature<IHttpSendFileFeature>();
if (sendFile == null)
{
Expand Down
17 changes: 6 additions & 11 deletions src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;

Expand All @@ -17,9 +16,9 @@ public static class StaticFileExtensions
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builder)
public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder)
{
return UseStaticFiles(builder, new StaticFileOptions());
return builder.UseStaticFiles(new StaticFileOptions());
}

/// <summary>
Expand All @@ -28,9 +27,9 @@ public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builde
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <returns></returns>
public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builder, string requestPath)
public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{
return UseStaticFiles(builder, new StaticFileOptions() { RequestPath = new PathString(requestPath) });
return builder.UseStaticFiles(new StaticFileOptions() { RequestPath = new PathString(requestPath) });
}

/// <summary>
Expand All @@ -39,13 +38,9 @@ public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builde
/// <param name="builder"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builder, StaticFileOptions options)
public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] StaticFileOptions options)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
return builder.Use(next => new StaticFileMiddleware(next, options).Invoke);
return builder.UseMiddleware<StaticFileMiddleware>(options);
}
}
}
Loading

0 comments on commit c62209b

Please sign in to comment.