Skip to content
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 7 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace OrchardCore.Liquid;
/// <summary>
/// This is a placeholder class that allows modules to extend the `HttpContext` property in the current Liquid scope.
/// </summary>
[Obsolete("This class is obsolete and will be removed in a future version.", error: true)]
public class LiquidHttpContextAccessor
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace OrchardCore.Liquid;
/// <summary>
/// This is a placeholder class that allows modules to extend the `HttpRequest` property in the current Liquid scope.
/// </summary>
[Obsolete("This class is obsolete and will be removed in a future version.", error: true)]
public class LiquidRequestAccessor
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,58 +44,9 @@ public void Configure(TemplateOptions options)

options.Scope.SetValue("Environment", new HostingEnvironmentValue(_hostEnvironment));

options.Scope.SetValue("Request", new ObjectValue(new LiquidRequestAccessor()));
options.MemberAccessStrategy.Register<LiquidRequestAccessor, FluidValue>((obj, name, ctx) =>
{
var request = ((LiquidTemplateContext)ctx).Services.GetRequiredService<IHttpContextAccessor>().HttpContext?.Request;
if (request != null)
{
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),

_ => NilValue.Instance
};
}

return NilValue.Instance;
});

options.Scope.SetValue("HttpContext", new ObjectValue(new LiquidHttpContextAccessor()));
options.MemberAccessStrategy.Register<LiquidHttpContextAccessor, FluidValue>((obj, name, ctx) =>
{
var httpContext = ((LiquidTemplateContext)ctx).Services.GetRequiredService<IHttpContextAccessor>().HttpContext;
if (httpContext != null)
{
return name switch
{
nameof(HttpContext.Items) => new ObjectValue(new HttpContextItemsWrapper(httpContext.Items)),
_ => NilValue.Instance
};
}
options.Scope.SetValue("Request", new HttpRequestValue());

return NilValue.Instance;
});
options.Scope.SetValue("HttpContext", new HttpContextValue());

options.MemberAccessStrategy.Register<FormCollection, FluidValue>((forms, name) =>
{
Expand All @@ -113,44 +64,4 @@ public void Configure(TemplateOptions options)
options.MemberAccessStrategy.Register<HeaderDictionaryWrapper, string[]>((headers, name) => headers.HeaderDictionary[name].ToArray());
options.MemberAccessStrategy.Register<RouteValueDictionaryWrapper, object>((headers, name) => headers.RouteValueDictionary[name]);
}

private sealed class CookieCollectionWrapper
{
public readonly IRequestCookieCollection RequestCookieCollection;

public CookieCollectionWrapper(IRequestCookieCollection requestCookieCollection)
{
RequestCookieCollection = requestCookieCollection;
}
}

private sealed class HeaderDictionaryWrapper
{
public readonly IHeaderDictionary HeaderDictionary;

public HeaderDictionaryWrapper(IHeaderDictionary headerDictionary)
{
HeaderDictionary = headerDictionary;
}
}

private sealed class HttpContextItemsWrapper
{
public readonly IDictionary<object, object> Items;

public HttpContextItemsWrapper(IDictionary<object, object> items)
{
Items = items;
}
}

private sealed class RouteValueDictionaryWrapper
{
public readonly IReadOnlyDictionary<string, object> RouteValueDictionary;

public RouteValueDictionaryWrapper(IReadOnlyDictionary<string, object> routeValueDictionary)
{
RouteValueDictionary = routeValueDictionary;
}
}
}
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;
}
}
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;
}
}
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;
}
}
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)),
Copy link
Member Author

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 the Request object in a major release

Copy link
Member Author

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?

_ => 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;
}
}
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;
}
}
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;
}
}