-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
251 additions
and
163 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
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,76 @@ | ||
using System; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
|
||
namespace Lib.Web.Mvc.Html | ||
{ | ||
/// <summary> | ||
/// Provides support for HTTP/2 Server Push during rendering. | ||
/// </summary> | ||
public static class PushPromiseExtensions | ||
{ | ||
#region Constants | ||
private const string _linkElement = "link"; | ||
private const string _scriptElement = "script"; | ||
|
||
private const string _hrefAttribute = "href"; | ||
private const string _relationAttribute = "rel"; | ||
private const string _sourceAttribute = "src"; | ||
private const string _typeAttribute = "type"; | ||
#endregion | ||
|
||
#region Methods | ||
/// <summary> | ||
/// Provides HTTP/2 Server Push support for link elements. | ||
/// </summary> | ||
/// <param name="htmlHelper">The HTML helper.</param> | ||
/// <param name="contentPath">The virtual path of resource.</param> | ||
/// <param name="relation">The relation (default is stylesheet).</param> | ||
/// <returns></returns> | ||
public static IHtmlString PushPromiseLink(this HtmlHelper htmlHelper, string contentPath, string relation = "stylesheet") | ||
{ | ||
PushPromise(htmlHelper.ViewContext.HttpContext, contentPath); | ||
|
||
UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); | ||
|
||
TagBuilder linkTagBuilder = new TagBuilder(_linkElement); | ||
linkTagBuilder.Attributes.Add(_relationAttribute, relation); | ||
linkTagBuilder.Attributes.Add(_hrefAttribute, urlHelper.Content(contentPath)); | ||
|
||
return new HtmlString(linkTagBuilder.ToString()); | ||
} | ||
|
||
/// <summary> | ||
/// Provides HTTP/2 Server Push support for script elements. | ||
/// </summary> | ||
/// <param name="htmlHelper">The HTML helper.</param> | ||
/// <param name="contentPath">The virtual path of resource.</param> | ||
/// <param name="type">The type.</param> | ||
/// <returns></returns> | ||
public static IHtmlString PushPromiseScript(this HtmlHelper htmlHelper, string contentPath, string type = "") | ||
{ | ||
PushPromise(htmlHelper.ViewContext.HttpContext, contentPath); | ||
|
||
UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); | ||
|
||
TagBuilder scriptTagBuilder = new TagBuilder(_scriptElement); | ||
scriptTagBuilder.Attributes.Add(_sourceAttribute, urlHelper.Content(contentPath)); | ||
|
||
if (!String.IsNullOrWhiteSpace(type)) | ||
{ | ||
scriptTagBuilder.Attributes.Add(_typeAttribute, type); | ||
} | ||
|
||
return new HtmlString(scriptTagBuilder.ToString()); | ||
} | ||
|
||
private static void PushPromise(HttpContextBase httpContext, string contentPath) | ||
{ | ||
if (httpContext.Request.IsSecureConnection) | ||
{ | ||
httpContext.Response.PushPromise(contentPath); | ||
} | ||
} | ||
#endregion | ||
} | ||
} |
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,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Web.Mvc; | ||
|
||
namespace Lib.Web.Mvc | ||
{ | ||
/// <summary> | ||
/// Action filter providing server push promise support | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] | ||
public class PushPromiseAttribute : FilterAttribute, IActionFilter | ||
{ | ||
#region Fields | ||
private PushPromiseTable _pushPromiseTable; | ||
#endregion | ||
|
||
#region Constructor | ||
/// <summary> | ||
/// Initializes new instance of PushPromiseAttribute | ||
/// </summary> | ||
/// <param name="pushPromiseTable">The push promises table.</param> | ||
public PushPromiseAttribute(PushPromiseTable pushPromiseTable) | ||
{ | ||
if (pushPromiseTable == null) | ||
{ | ||
throw new ArgumentNullException(nameof(pushPromiseTable)); | ||
} | ||
|
||
_pushPromiseTable = pushPromiseTable; | ||
} | ||
#endregion | ||
|
||
#region IActionFilter Members | ||
/// <summary> | ||
/// Called after the action method executes. | ||
/// </summary> | ||
/// <param name="filterContext"></param> | ||
public void OnActionExecuted(ActionExecutedContext filterContext) | ||
{ } | ||
|
||
/// <summary> | ||
/// Called before an action method executes. | ||
/// </summary> | ||
/// <param name="filterContext"></param> | ||
public void OnActionExecuting(ActionExecutingContext filterContext) | ||
{ | ||
if (filterContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(filterContext)); | ||
} | ||
|
||
// HTTP/2 is supported only over HTTPS | ||
if (filterContext.HttpContext.Request.IsSecureConnection) | ||
{ | ||
IEnumerable<string> pushPromiseContentPaths = _pushPromiseTable.GetPushPromiseContentPaths(filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName); | ||
foreach (string pushPromiseContentPath in pushPromiseContentPaths) | ||
{ | ||
filterContext.HttpContext.Response.PushPromise(pushPromiseContentPath); | ||
} | ||
} | ||
} | ||
#endregion | ||
} | ||
} |
Oops, something went wrong.