-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce HttpRequest & HttpContext Fluid values #16979
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e8cad86
Introduce HttpRequest & HttpContext Fluid values
hishamco dcf9df2
Obsolete error -> true
hishamco a1f1efa
Merge branch 'main' into hishamco/liquid-accessor
hishamco e7bcaa7
Remove partial
hishamco 6c17332
Fix the build
hishamco 688c67c
Remove ArgumentNullException.ThrowIfNull
hishamco 6d6c37e
Fix lifetime
sebastienros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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) | ||
=> 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) | ||
=> 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add
Request
,User
.. etc and deprecate theRequest
object in a major releaseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this one @sebastienros?