Got at least one data fetching method working; turns out, we can't use a patched LogicStack to get the data

This commit is contained in:
2026-01-14 22:11:11 +01:00
parent 40a8431464
commit 3f7122d30a
350 changed files with 41444 additions and 119 deletions

View File

@@ -0,0 +1,54 @@
using System;
using System.Threading.Tasks;
using EmbedIO.Utilities;
namespace EmbedIO.Actions
{
/// <summary>
/// A module that passes requests to a callback.
/// </summary>
/// <seealso cref="WebModuleBase" />
public class ActionModule : WebModuleBase
{
private readonly HttpVerbs _verb;
private readonly RequestHandlerCallback _handler;
/// <summary>
/// Initializes a new instance of the <see cref="ActionModule" /> class.
/// </summary>
/// <param name="baseRoute">The base route.</param>
/// <param name="verb">The HTTP verb that will be served by this module.</param>
/// <param name="handler">The callback used to handle requests.</param>
/// <exception cref="ArgumentNullException"><paramref name="handler"/> is <see langword="null"/>.</exception>
/// <seealso cref="WebModuleBase(string)"/>
public ActionModule(string baseRoute, HttpVerbs verb, RequestHandlerCallback handler)
: base(baseRoute)
{
_verb = verb;
_handler = Validate.NotNull(nameof(handler), handler);
}
/// <summary>
/// Initializes a new instance of the <see cref="ActionModule"/> class.
/// </summary>
/// <param name="handler">The handler.</param>
public ActionModule(RequestHandlerCallback handler)
: this("/", HttpVerbs.Any, handler)
{
}
/// <inheritdoc />
public override bool IsFinalHandler => false;
/// <inheritdoc />
protected override async Task OnRequestAsync(IHttpContext context)
{
if (_verb != HttpVerbs.Any && context.Request.HttpVerb != _verb)
return;
await _handler(context).ConfigureAwait(false);
context.SetHandled();
}
}
}

View File

@@ -0,0 +1,99 @@
using System;
using System.Net;
using System.Threading.Tasks;
using EmbedIO.Utilities;
namespace EmbedIO.Actions
{
/// <summary>
/// A module that redirects requests.
/// </summary>
/// <seealso cref="WebModuleBase" />
public class RedirectModule : WebModuleBase
{
private readonly Func<IHttpContext, bool>? _shouldRedirect;
/// <summary>
/// Initializes a new instance of the <see cref="RedirectModule"/> class
/// that will redirect all served requests.
/// </summary>
/// <param name="baseRoute">The base route.</param>
/// <param name="redirectUrl">The redirect URL.</param>
/// <param name="statusCode">The response status code; default is <c>302 - Found</c>.</param>
/// <exception cref="ArgumentNullException"><paramref name="redirectUrl"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">
/// <para><paramref name="redirectUrl"/> is not a valid URL.</para>
/// <para>- or -</para>
/// <para><paramref name="statusCode"/> is not a redirection (3xx) status code.</para>
/// </exception>
/// <seealso cref="WebModuleBase(string)"/>
public RedirectModule(string baseRoute, string redirectUrl, HttpStatusCode statusCode = HttpStatusCode.Found)
: this(baseRoute, redirectUrl, null, statusCode, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectModule"/> class
/// that will redirect all requests for which the <paramref name="shouldRedirect"/> callback
/// returns <see langword="true"/>.
/// </summary>
/// <param name="baseRoute">The base route.</param>
/// <param name="redirectUrl">The redirect URL.</param>
/// <param name="shouldRedirect">A callback function that returns <see langword="true"/>
/// if a request must be redirected.</param>
/// <param name="statusCode">The response status code; default is <c>302 - Found</c>.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="redirectUrl"/> is <see langword="null"/>.</para>
/// <para>- or -</para>
/// <para><paramref name="shouldRedirect"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="ArgumentException">
/// <para><paramref name="redirectUrl"/> is not a valid URL.</para>
/// <para>- or -</para>
/// <para><paramref name="statusCode"/> is not a redirection (3xx) status code.</para>
/// </exception>
/// <seealso cref="WebModuleBase(string)"/>
public RedirectModule(string baseRoute, string redirectUrl, Func<IHttpContext, bool>? shouldRedirect, HttpStatusCode statusCode = HttpStatusCode.Found)
: this(baseRoute, redirectUrl, shouldRedirect, statusCode, true)
{
}
private RedirectModule(string baseRoute, string redirectUrl, Func<IHttpContext, bool>? shouldRedirect, HttpStatusCode statusCode, bool useCallback)
: base(baseRoute)
{
RedirectUrl = Validate.Url(nameof(redirectUrl), redirectUrl);
var status = (int)statusCode;
if (status < 300 || status > 399)
throw new ArgumentException("Status code does not imply a redirection.", nameof(statusCode));
StatusCode = statusCode;
_shouldRedirect = useCallback ? Validate.NotNull(nameof(shouldRedirect), shouldRedirect) : null;
}
/// <inheritdoc />
public override bool IsFinalHandler => false;
/// <summary>
/// Gets the redirect URL.
/// </summary>
public string RedirectUrl { get; }
/// <summary>
/// Gets the response status code.
/// </summary>
public HttpStatusCode StatusCode { get; }
/// <inheritdoc />
protected override Task OnRequestAsync(IHttpContext context)
{
if (_shouldRedirect?.Invoke(context) ?? true)
{
context.Redirect(RedirectUrl, (int)StatusCode);
context.SetHandled();
}
return Task.CompletedTask;
}
}
}