-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into sebros/net90
- Loading branch information
Showing
12 changed files
with
232 additions
and
93 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
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
13 changes: 13 additions & 0 deletions
13
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/CookieCollectionWrapper.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,13 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace OrchardCore.DisplayManagement.Liquid.Values; | ||
|
||
internal sealed class CookieCollectionWrapper | ||
{ | ||
public readonly IRequestCookieCollection RequestCookieCollection; | ||
|
||
public CookieCollectionWrapper(IRequestCookieCollection requestCookieCollection) | ||
{ | ||
RequestCookieCollection = requestCookieCollection; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HeaderDictionaryWrapper.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,13 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace OrchardCore.DisplayManagement.Liquid.Values; | ||
|
||
internal sealed class HeaderDictionaryWrapper | ||
{ | ||
public readonly IHeaderDictionary HeaderDictionary; | ||
|
||
public HeaderDictionaryWrapper(IHeaderDictionary headerDictionary) | ||
{ | ||
HeaderDictionary = headerDictionary; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpContextItemsWrapper.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,11 @@ | ||
namespace OrchardCore.DisplayManagement.Liquid.Values; | ||
|
||
internal sealed class HttpContextItemsWrapper | ||
{ | ||
public readonly IDictionary<object, object> Items; | ||
|
||
public HttpContextItemsWrapper(IDictionary<object, object> items) | ||
{ | ||
Items = items; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpContextValue.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,66 @@ | ||
using System.Globalization; | ||
using System.Text.Encodings.Web; | ||
using Fluid; | ||
using Fluid.Values; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Liquid; | ||
|
||
namespace OrchardCore.DisplayManagement.Liquid.Values; | ||
|
||
internal sealed class HttpContextValue : FluidValue | ||
{ | ||
public override FluidValues Type => FluidValues.Object; | ||
|
||
public override bool Equals(FluidValue other) | ||
{ | ||
if (other is null) | ||
{ | ||
return false; | ||
} | ||
|
||
return other is HttpContextValue; | ||
} | ||
|
||
public override bool ToBooleanValue() => true; | ||
|
||
public override decimal ToNumberValue() => 0; | ||
|
||
public override object ToObjectValue() => null; | ||
|
||
public override string ToStringValue() => "HttpContext"; | ||
|
||
#pragma warning disable CS0672 // Member overrides obsolete member | ||
public override void WriteTo(TextWriter writer, TextEncoder encoder, CultureInfo cultureInfo) | ||
#pragma warning restore CS0672 // Member overrides obsolete member | ||
=> writer.Write(ToStringValue()); | ||
|
||
public async override ValueTask WriteToAsync(TextWriter writer, TextEncoder encoder, CultureInfo cultureInfo) | ||
Check failure on line 38 in src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpContextValue.cs GitHub Actions / Build & Test (ubuntu-latest)
Check failure on line 38 in src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpContextValue.cs GitHub Actions / Build & Test (ubuntu-latest)
|
||
=> await writer.WriteAsync(ToStringValue()); | ||
|
||
public override ValueTask<FluidValue> GetValueAsync(string name, TemplateContext context) | ||
{ | ||
var httpContext = GetHttpContext(context); | ||
|
||
if (httpContext is null) | ||
{ | ||
return new ValueTask<FluidValue>(NilValue.Instance); | ||
} | ||
|
||
return name switch | ||
{ | ||
nameof(HttpContext.Items) => new ObjectValue(new HttpContextItemsWrapper(httpContext.Items)), | ||
_ => NilValue.Instance | ||
}; | ||
} | ||
|
||
private static HttpContext GetHttpContext(TemplateContext context) | ||
{ | ||
var ctx = context as LiquidTemplateContext | ||
?? throw new InvalidOperationException($"An implementation of '{nameof(LiquidTemplateContext)}' is required"); | ||
|
||
var httpContextAccessor = ctx.Services.GetRequiredService<IHttpContextAccessor>(); | ||
|
||
return httpContextAccessor.HttpContext; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpRequestValue.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,89 @@ | ||
using System.Globalization; | ||
using System.Text.Encodings.Web; | ||
using Fluid; | ||
using Fluid.Values; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Liquid; | ||
|
||
namespace OrchardCore.DisplayManagement.Liquid.Values; | ||
|
||
internal sealed class HttpRequestValue : FluidValue | ||
{ | ||
public override FluidValues Type => FluidValues.Object; | ||
|
||
/// <summary> | ||
/// Creates a new instance of a <see cref="HttpRequestValue"/> for the specified HTTP request. | ||
/// </summary> | ||
public override bool Equals(FluidValue other) | ||
{ | ||
if (other is null) | ||
{ | ||
return false; | ||
} | ||
|
||
return other is HttpRequestValue; | ||
} | ||
|
||
public override bool ToBooleanValue() => true; | ||
|
||
public override decimal ToNumberValue() => 0; | ||
|
||
public override object ToObjectValue() => null; | ||
|
||
public override string ToStringValue() => "Request"; | ||
|
||
#pragma warning disable CS0672 // Member overrides obsolete member | ||
public override void WriteTo(TextWriter writer, TextEncoder encoder, CultureInfo cultureInfo) | ||
#pragma warning restore CS0672 // Member overrides obsolete member | ||
=> writer.Write(ToStringValue()); | ||
|
||
public async override ValueTask WriteToAsync(TextWriter writer, TextEncoder encoder, CultureInfo cultureInfo) | ||
Check failure on line 41 in src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpRequestValue.cs GitHub Actions / Build & Test (ubuntu-latest)
Check failure on line 41 in src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpRequestValue.cs GitHub Actions / Build & Test (ubuntu-latest)
|
||
=> await writer.WriteAsync(ToStringValue()); | ||
|
||
public override ValueTask<FluidValue> GetValueAsync(string name, TemplateContext context) | ||
{ | ||
var request = GetHttpRequest(context); | ||
|
||
if (request is null) | ||
{ | ||
return new ValueTask<FluidValue>(NilValue.Instance); | ||
} | ||
|
||
return name switch | ||
{ | ||
nameof(HttpRequest.QueryString) => new StringValue(request.QueryString.Value), | ||
nameof(HttpRequest.ContentType) => new StringValue(request.ContentType), | ||
nameof(HttpRequest.ContentLength) => NumberValue.Create(request.ContentLength ?? 0), | ||
nameof(HttpRequest.Cookies) => new ObjectValue(new CookieCollectionWrapper(request.Cookies)), | ||
nameof(HttpRequest.Headers) => new ObjectValue(new HeaderDictionaryWrapper(request.Headers)), | ||
nameof(HttpRequest.Query) => new ObjectValue(new QueryCollection(request.Query.ToDictionary(kv => kv.Key, kv => kv.Value))), | ||
nameof(HttpRequest.Form) => request.HasFormContentType ? (FluidValue)new ObjectValue(request.Form) : NilValue.Instance, | ||
nameof(HttpRequest.Protocol) => new StringValue(request.Protocol), | ||
nameof(HttpRequest.Path) => new StringValue(request.Path.Value), | ||
nameof(HttpRequest.PathBase) => new StringValue(request.PathBase.Value), | ||
nameof(HttpRequest.Host) => new StringValue(request.Host.Value), | ||
nameof(HttpRequest.IsHttps) => BooleanValue.Create(request.IsHttps), | ||
nameof(HttpRequest.Scheme) => new StringValue(request.Scheme), | ||
nameof(HttpRequest.Method) => new StringValue(request.Method), | ||
nameof(HttpRequest.RouteValues) => new ObjectValue(new RouteValueDictionaryWrapper(request.RouteValues)), | ||
|
||
// Provides correct escaping to reconstruct a request or redirect URI. | ||
"UriHost" => new StringValue(request.Host.ToUriComponent(), encode: false), | ||
"UriPath" => new StringValue(request.Path.ToUriComponent(), encode: false), | ||
"UriPathBase" => new StringValue(request.PathBase.ToUriComponent(), encode: false), | ||
"UriQueryString" => new StringValue(request.QueryString.ToUriComponent(), encode: false), | ||
_ => ValueTask.FromResult<FluidValue>(NilValue.Instance) | ||
}; | ||
} | ||
|
||
private static HttpRequest GetHttpRequest(TemplateContext context) | ||
{ | ||
var ctx = context as LiquidTemplateContext | ||
?? throw new InvalidOperationException($"An implementation of '{nameof(LiquidTemplateContext)}' is required"); | ||
|
||
var httpContext = ctx.Services.GetRequiredService<IHttpContextAccessor>().HttpContext; | ||
|
||
return httpContext.Request; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/RouteValueDictionaryWrapper.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,11 @@ | ||
namespace OrchardCore.DisplayManagement.Liquid.Values; | ||
|
||
internal sealed class RouteValueDictionaryWrapper | ||
{ | ||
public readonly IReadOnlyDictionary<string, object> RouteValueDictionary; | ||
|
||
public RouteValueDictionaryWrapper(IReadOnlyDictionary<string, object> routeValueDictionary) | ||
{ | ||
RouteValueDictionary = routeValueDictionary; | ||
} | ||
} |
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