-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] (GH-300) Port Wyam.Hosting to netstandard2.0
- Loading branch information
Showing
25 changed files
with
689 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Wyam.Hosting | ||
{ | ||
/// <summary> | ||
/// Extenions to use when no extension is provided in the URL. | ||
/// </summary> | ||
public class DefaultExtensionsOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the extensions. | ||
/// </summary> | ||
/// <value> | ||
/// The extensions. | ||
/// </value> | ||
public IList<string> Extensions { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultExtensionsOptions"/> class. | ||
/// </summary> | ||
public DefaultExtensionsOptions() => Extensions = new List<string> | ||
{ | ||
".htm", | ||
".html" | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Wyam.Hosting | ||
{ | ||
public static class DefaultExtensionsExtensions | ||
{ | ||
public static IApplicationBuilder UseDefaultExtensions(this IApplicationBuilder app, DefaultExtensionsOptions options) | ||
{ | ||
if (app == null) | ||
{ | ||
throw new ArgumentNullException(nameof(app)); | ||
} | ||
|
||
return app.UseMiddleware<DefaultExtensionsMiddleware>(Options.Create(options)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Wyam.Hosting | ||
{ | ||
public class DefaultExtensionsMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly IFileProvider _fileProvider; | ||
private readonly DefaultExtensionsOptions _options; | ||
private readonly ILogger _logger; | ||
|
||
public DefaultExtensionsMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IOptions<DefaultExtensionsOptions> options, ILoggerFactory loggerFactory) | ||
{ | ||
if (next == null) | ||
{ | ||
throw new ArgumentNullException(nameof(next)); | ||
} | ||
|
||
if (hostingEnv == null) | ||
{ | ||
throw new ArgumentNullException(nameof(hostingEnv)); | ||
} | ||
|
||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
if (loggerFactory == null) | ||
{ | ||
throw new ArgumentNullException(nameof(loggerFactory)); | ||
} | ||
|
||
_next = next; | ||
_fileProvider = hostingEnv.WebRootFileProvider; | ||
_options = options.Value; | ||
_logger = loggerFactory.CreateLogger<DefaultExtensionsMiddleware>(); | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (IsGetOrHeadMethod(context.Request.Method) | ||
&& !PathEndsInSlash(context.Request.Path)) | ||
{ | ||
// Check if there's a file with a matched extension, and rewrite the request if found | ||
foreach (string extension in _options.Extensions) | ||
{ | ||
string filePath = context.Request.Path.ToString() + extension; | ||
IFileInfo fileInfo = _fileProvider.GetFileInfo(filePath); | ||
if (fileInfo != null && fileInfo.Exists) | ||
{ | ||
_logger.LogInformation($"Rewriting extensionless path to {filePath}"); | ||
context.Request.Path = new PathString(filePath); | ||
break; | ||
} | ||
} | ||
} | ||
await _next(context); | ||
} | ||
|
||
private static bool IsGetOrHeadMethod(string method) => | ||
HttpMethods.IsGet(method) || HttpMethods.IsHead(method); | ||
|
||
private static bool PathEndsInSlash(PathString path) => | ||
path.Value.EndsWith("/", StringComparison.Ordinal); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/core/Wyam.Hosting/Middleware/DisableCacheExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Builder; | ||
using Wyam.Hosting.Middleware; | ||
|
||
namespace Wyam.Hosting.Middleware | ||
{ | ||
public static class DisableCacheExtensions | ||
{ | ||
public static IApplicationBuilder UseDisableCache(this IApplicationBuilder builder) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
return builder.UseMiddleware<DisableCacheMiddleware>(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/core/Wyam.Hosting/Middleware/DisableCacheMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Wyam.Hosting.Middleware | ||
{ | ||
public class DisableCacheMiddleware | ||
{ | ||
private readonly IHostingEnvironment _hostingEnv; | ||
private readonly ILoggerFactory _loggerFactory; | ||
private readonly RequestDelegate _next; | ||
|
||
public DisableCacheMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, ILoggerFactory loggerFactory) | ||
{ | ||
_next = next; | ||
_hostingEnv = hostingEnv; | ||
_loggerFactory = loggerFactory; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
await _next(context); | ||
|
||
context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate"); | ||
context.Response.Headers.Append("Pragma", "no-cache"); | ||
context.Response.Headers.Append("Expires", "0"); | ||
} | ||
} | ||
} |
Oops, something went wrong.