| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using Kestrun.Hosting; |
| | | 3 | | using Kestrun.Models; |
| | | 4 | | |
| | | 5 | | namespace Kestrun.Forms; |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents the form parsing context for a request. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed record KrFormContext |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Initializes a new instance of the <see cref="KrFormContext"/> class. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="kestrunContext">The Kestrun context.</param> |
| | | 15 | | /// <param name="options">The form parsing options.</param> |
| | | 16 | | /// <param name="payload">The parsed payload.</param> |
| | | 17 | | public KrFormContext(KestrunContext kestrunContext, KrFormOptions options, IKrFormPayload payload) |
| | | 18 | | { |
| | | 19 | | KestrunContext = kestrunContext; |
| | | 20 | | Host = kestrunContext.Host; |
| | | 21 | | HttpContext = kestrunContext.HttpContext; |
| | | 22 | | Options = options; |
| | | 23 | | Payload = payload; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the associated Kestrun host. |
| | | 28 | | /// </summary> |
| | | 29 | | public KestrunHost Host { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the associated Kestrun context. |
| | | 33 | | /// </summary> |
| | | 34 | | public KestrunContext KestrunContext { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the underlying HTTP context. |
| | | 38 | | /// </summary> |
| | | 39 | | public HttpContext HttpContext { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the form parsing options. |
| | | 43 | | /// </summary> |
| | | 44 | | public KrFormOptions Options { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the parsed payload. |
| | | 48 | | /// </summary> |
| | | 49 | | public IKrFormPayload Payload { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the logger associated with the host. |
| | | 53 | | /// </summary> |
| | | 54 | | public Serilog.ILogger Logger => Host.Logger; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Represents the context for a part as it is being processed. |
| | | 59 | | /// </summary> |
| | | 60 | | public sealed record KrPartContext |
| | | 61 | | { |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the zero-based index of the part within the multipart body. |
| | | 64 | | /// </summary> |
| | | 65 | | public int Index { get; init; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the part name, if present. |
| | | 69 | | /// </summary> |
| | | 70 | | public string? Name { get; init; } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the original filename, if present. |
| | | 74 | | /// </summary> |
| | | 75 | | public string? FileName { get; init; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the content type of the part. |
| | | 79 | | /// </summary> |
| | | 80 | | public string ContentType { get; init; } = "application/octet-stream"; |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the content encoding of the part, if present. |
| | | 84 | | /// </summary> |
| | | 85 | | public string? ContentEncoding { get; init; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the declared length of the part, if present. |
| | | 89 | | /// </summary> |
| | | 90 | | public long? DeclaredLength { get; init; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the headers associated with the part. |
| | | 94 | | /// </summary> |
| | | 95 | | public IReadOnlyDictionary<string, string[]> Headers { get; init; } = new ReadOnlyDictionary<string, string[]>(new D |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets the matching rule for the part, if any. |
| | | 99 | | /// </summary> |
| | | 100 | | public KrFormPartRule? Rule { get; init; } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Defines the action taken for a part. |
| | | 105 | | /// </summary> |
| | | 106 | | public enum KrPartAction |
| | | 107 | | { |
| | | 108 | | /// <summary> |
| | | 109 | | /// Continue processing the part. |
| | | 110 | | /// </summary> |
| | | 111 | | Continue, |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Skip the part content without storing. |
| | | 115 | | /// </summary> |
| | | 116 | | Skip, |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Reject the request. |
| | | 120 | | /// </summary> |
| | | 121 | | Reject |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Represents form parsing errors with HTTP status codes. |
| | | 126 | | /// </summary> |
| | | 127 | | public class KrFormException(string message, int statusCode) : InvalidOperationException(message) |
| | | 128 | | { |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the HTTP status code to return. |
| | | 131 | | /// </summary> |
| | | 132 | | public int StatusCode { get; } = statusCode; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Represents a form parsing limit violation. |
| | | 137 | | /// </summary> |
| | | 138 | | public sealed class KrFormLimitExceededException(string message, int statusCode = StatusCodes.Status413PayloadTooLarge) |
| | 2 | 139 | | : KrFormException(message, statusCode) |
| | | 140 | | { } |