| | | 1 | | namespace Kestrun.Forms; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Configures form parsing behavior for Kestrun. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class KrFormOptions |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Gets or sets the name of the form parser. |
| | | 10 | | /// </summary> |
| | 20 | 11 | | public string? Name { get; set; } |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets or sets the description of the form parser. |
| | | 15 | | /// </summary> |
| | 0 | 16 | | public string? Description { get; set; } |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the allowed request content types. |
| | | 19 | | /// </summary> |
| | 15 | 20 | | public List<string> AllowedRequestContentTypes { get; } = |
| | 15 | 21 | | [ |
| | 15 | 22 | | "multipart/form-data" |
| | 15 | 23 | | ]; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets a value indicating whether unknown request content types should be rejected. |
| | | 27 | | /// </summary> |
| | 22 | 28 | | public bool RejectUnknownRequestContentType { get; set; } = true; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the form parsing limits. |
| | | 32 | | /// </summary> |
| | 52 | 33 | | public KrFormLimits Limits { get; } = new(); |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the default upload path for stored parts. |
| | | 37 | | /// </summary> |
| | 18 | 38 | | public string DefaultUploadPath { get; set; } = KestrunHostManager.Default?.Options.DefaultUploadPath ?? Path.Combin |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the filename sanitizer. |
| | | 42 | | /// </summary> |
| | 15 | 43 | | public Func<string, string> SanitizeFileName { get; set; } = static name => |
| | 15 | 44 | | { |
| | 0 | 45 | | var sanitized = Path.GetFileName(name); |
| | 0 | 46 | | return string.IsNullOrWhiteSpace(sanitized) ? Path.GetRandomFileName() : sanitized; |
| | 15 | 47 | | }; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets or sets a value indicating whether SHA-256 hashes should be computed for file parts. |
| | | 51 | | /// </summary> |
| | 7 | 52 | | public bool ComputeSha256 { get; set; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets or sets a value indicating whether per-part decompression is enabled. |
| | | 56 | | /// </summary> |
| | 14 | 57 | | public bool EnablePartDecompression { get; set; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the allowed part content encodings for decompression. |
| | | 61 | | /// </summary> |
| | 4 | 62 | | public List<string> AllowedPartContentEncodings { get; } = |
| | 15 | 63 | | [ |
| | 15 | 64 | | "identity", |
| | 15 | 65 | | "gzip", |
| | 15 | 66 | | "deflate", |
| | 15 | 67 | | "br" |
| | 15 | 68 | | ]; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets or sets the maximum decompressed bytes per part. |
| | | 72 | | /// </summary> |
| | 17 | 73 | | public long MaxDecompressedBytesPerPart { get; set; } = 20 * 1024 * 1024; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets or sets a value indicating whether unknown content encodings should be rejected. |
| | | 77 | | /// </summary> |
| | 21 | 78 | | public bool RejectUnknownContentEncoding { get; set; } = true; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets the per-part rules. |
| | | 82 | | /// </summary> |
| | 67 | 83 | | public List<KrFormPartRule> Rules { get; } = []; |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets or sets the hook invoked for each part. |
| | | 87 | | /// </summary> |
| | 4 | 88 | | public Func<KrPartContext, ValueTask<KrPartAction>>? OnPart { get; set; } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets or sets the hook invoked after parsing completes. |
| | | 92 | | /// </summary> |
| | 5 | 93 | | public Func<KrFormContext, ValueTask<object?>>? OnCompleted { get; set; } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets or sets the logger used by the form parser. |
| | | 97 | | /// </summary> |
| | 20 | 98 | | public Serilog.ILogger? Logger { get; set; } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Initializes a new instance of the <see cref="KrFormOptions"/> class. |
| | | 102 | | /// </summary> |
| | 30 | 103 | | public KrFormOptions() { } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Initializes a new instance of the <see cref="KrFormOptions"/> class by copying settings from another instance. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="copyFrom">The instance to copy settings from.</param> |
| | 0 | 109 | | public KrFormOptions(KrFormOptions copyFrom) |
| | | 110 | | { |
| | 0 | 111 | | Name = copyFrom.Name; |
| | 0 | 112 | | Description = copyFrom.Description; |
| | 0 | 113 | | AllowedRequestContentTypes.AddRange(copyFrom.AllowedRequestContentTypes); |
| | 0 | 114 | | RejectUnknownRequestContentType = copyFrom.RejectUnknownRequestContentType; |
| | 0 | 115 | | DefaultUploadPath = copyFrom.DefaultUploadPath; |
| | 0 | 116 | | SanitizeFileName = copyFrom.SanitizeFileName; |
| | 0 | 117 | | ComputeSha256 = copyFrom.ComputeSha256; |
| | 0 | 118 | | EnablePartDecompression = copyFrom.EnablePartDecompression; |
| | 0 | 119 | | AllowedPartContentEncodings.AddRange(copyFrom.AllowedPartContentEncodings); |
| | 0 | 120 | | MaxDecompressedBytesPerPart = copyFrom.MaxDecompressedBytesPerPart; |
| | 0 | 121 | | RejectUnknownContentEncoding = copyFrom.RejectUnknownContentEncoding; |
| | 0 | 122 | | Rules.AddRange(copyFrom.Rules); |
| | 0 | 123 | | OnPart = copyFrom.OnPart; |
| | 0 | 124 | | OnCompleted = copyFrom.OnCompleted; |
| | 0 | 125 | | Logger = copyFrom.Logger; |
| | | 126 | | // Copy limits |
| | 0 | 127 | | Limits = new KrFormLimits(copyFrom.Limits); |
| | 0 | 128 | | } |
| | | 129 | | } |