| | | 1 | | using Kestrun.Hosting; |
| | | 2 | | using Kestrun.Models; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Forms; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides endpoint mapping helpers for form routes. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class KrFormEndpoints |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a delegate that handles a parsed form context. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="context">The form context.</param> |
| | | 15 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | | 16 | | public delegate Task KrFormHandler(KrFormContext context); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Maps a form route that parses multipart and urlencoded payloads. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="endpoints">The endpoint route builder.</param> |
| | | 22 | | /// <param name="pattern">The route pattern.</param> |
| | | 23 | | /// <param name="options">The form parsing options.</param> |
| | | 24 | | /// <param name="handler">The handler to execute after parsing.</param> |
| | | 25 | | /// <returns>The endpoint convention builder.</returns> |
| | | 26 | | public static IEndpointConventionBuilder MapKestrunFormRoute(this IEndpointRouteBuilder endpoints, string pattern, K |
| | | 27 | | { |
| | 4 | 28 | | ArgumentNullException.ThrowIfNull(endpoints); |
| | 4 | 29 | | ArgumentNullException.ThrowIfNull(pattern); |
| | 4 | 30 | | ArgumentNullException.ThrowIfNull(options); |
| | 4 | 31 | | ArgumentNullException.ThrowIfNull(handler); |
| | | 32 | | |
| | 4 | 33 | | var map = endpoints.MapPost(pattern, async httpContext => |
| | 4 | 34 | | { |
| | 4 | 35 | | var host = httpContext.RequestServices.GetRequiredService<KestrunHost>(); |
| | 4 | 36 | | var kestrunContext = new KestrunContext(host, httpContext); |
| | 4 | 37 | | options.Logger ??= host.Logger; |
| | 4 | 38 | | |
| | 4 | 39 | | try |
| | 4 | 40 | | { |
| | 4 | 41 | | var payload = await KrFormParser.ParseAsync(httpContext, options, httpContext.RequestAborted).ConfigureA |
| | 3 | 42 | | var formContext = new KrFormContext(kestrunContext, options, payload); |
| | 4 | 43 | | |
| | 3 | 44 | | httpContext.Items["KrFormContext"] = formContext; |
| | 3 | 45 | | httpContext.Items["KrFormPayload"] = payload; |
| | 4 | 46 | | |
| | 3 | 47 | | if (options.OnCompleted != null) |
| | 4 | 48 | | { |
| | 1 | 49 | | var result = await options.OnCompleted(formContext).ConfigureAwait(false); |
| | 1 | 50 | | if (result != null) |
| | 4 | 51 | | { |
| | 1 | 52 | | await Results.Ok(result).ExecuteAsync(httpContext).ConfigureAwait(false); |
| | 1 | 53 | | return; |
| | 4 | 54 | | } |
| | 4 | 55 | | } |
| | 4 | 56 | | |
| | 2 | 57 | | await handler(formContext).ConfigureAwait(false); |
| | 1 | 58 | | await kestrunContext.Response.ApplyTo(httpContext.Response).ConfigureAwait(false); |
| | 1 | 59 | | } |
| | 1 | 60 | | catch (KrFormException ex) |
| | 4 | 61 | | { |
| | 1 | 62 | | host.Logger.Error(ex, "Form parsing failed for {Pattern}", pattern); |
| | 1 | 63 | | httpContext.Response.StatusCode = ex.StatusCode; |
| | 1 | 64 | | await httpContext.Response.WriteAsync("Invalid form data.", httpContext.RequestAborted).ConfigureAwait(f |
| | 1 | 65 | | } |
| | 1 | 66 | | catch (Exception ex) |
| | 4 | 67 | | { |
| | 1 | 68 | | host.Logger.Error(ex, "Unhandled error in form route for {Pattern}", pattern); |
| | 1 | 69 | | httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; |
| | 1 | 70 | | await httpContext.Response.WriteAsync("Internal server error.", httpContext.RequestAborted).ConfigureAwa |
| | 4 | 71 | | } |
| | 4 | 72 | | }); |
| | | 73 | | |
| | 4 | 74 | | _ = map.DisableAntiforgery(); |
| | 4 | 75 | | return map; |
| | 4 | 76 | | } |
| | | 77 | | } |