-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathMinimalParseStructures.cs
343 lines (266 loc) · 11.4 KB
/
MinimalParseStructures.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
namespace Imageflow.Server.Configuration.Parsing;
using Tomlyn.Model;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Mvc;
using Tomlyn.Syntax;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using Imageflow.Fluent;
#nullable enable
internal interface IValidationCapable{
public void Validate(ValidationContext c);
}
internal abstract class ConfigSectionBase : ITomlMetadataProvider, IValidationCapable{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public abstract void Validate(ValidationContext c);
}
internal class MissingConfigPropertyException : Exception
{
public string? PropertyName { get; init; }
// within table
public string? ParentFullName { get; init; }
public ConfigTextContext? TextContext { get; init; }
public string? FailureMessage { get; init; }
internal string FailureSegment => FailureMessage != null ? $"{FailureMessage} ({ParentFullName}.{PropertyName})" : $"Missing required property {PropertyName} = ... in [{ParentFullName}]";
public override string Message => $"{FailureSegment} {TextContext}";
}
internal class InvalidConfigPropertyException : Exception
{
public string? PropertyName { get; init; }
public string? PropertyValue { get; init; }
// within table
public string? ParentFullName { get; init; }
public string? FailedExpectation { get; init; }
public ConfigTextContext? TextContext { get; init; }
public string? FailureMessage { get; init; }
internal string FailureSegment => FailureMessage ?? $"failed {FailedExpectation}";
public override string Message => $"Invalid [{ParentFullName}] {PropertyName} = '{PropertyValue}' in ({FailureSegment}) {TextContext}";
}
internal class ImageflowServer : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public string? ConfigSchema { get; set; }
public void Validate(ValidationContext c)
{
c.Require(this, ConfigSchema);
if (ConfigSchema != null) {
// Parse the config schema version into MAJOR.MINOR or MAJOR
var versionSegments = ConfigSchema.Split('.');
var major = int.Parse(versionSegments[0]);
var minor = versionSegments.Length > 1 ? int.Parse(versionSegments[1]) : 0;
c.ValidateRecursive(this,ConfigSchema, major == c.ConfigSchemaMajorVersion);
c.ValidateRecursive(this,ConfigSchema, minor <= c.ConfigSchemaMinorVersion);
}
}
}
internal class LicenseSection : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public string? Enforcement { get; set; }
public string? Key { get; set; }
public void Validate(ValidationContext c)
{
var enforcement = Enforcement?.ToLowerInvariant();
// enforcement must be one of the following: "watermark", "http_402_error" or "http_422_error"
c.ValidateRecursive(this, Enforcement, enforcement == "watermark" || enforcement == "http_402_error" || enforcement == "http_422_error");
// We're leaving the key validation to the license validator
}
}
internal class RouteBase : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
// public string? Prefix { get; set; }
public bool? PrefixCaseSensitive { get; set; }
public bool? LowercasePathRemainder { get; set; }
// public RouteSource? Source { get; set; }
public string? CacheControl { get; set; }
public string? ApplyDefaultCommands { get; set; }
public bool? ApplyDefaultCommandsToQuerylessUrls { get; set; }
// public string? ApplyOverrideCommands { get; set; }
// public string? RequireSignature { get; set; }
// public List<HeaderPair>? ResponseHeaders { get; set; }
public virtual void Validate(ValidationContext c)
{
// Add your validations here
//c.Require(this, Prefix);
//c.Require(this, Source);
//c.ValidateRecursive(this, Source, true);
}
}
internal class RouteSection : RouteBase
{
public string? Prefix { get; set; }
//public RouteSource? Source { get; set; }
public string? MapToPhysicalFolder { get; set; }
public bool? AllowExtensionlessUrls { get; set; }
public override void Validate(ValidationContext c)
{
// Add your validations here
c.Require(this, Prefix);
//c.Require(this, Source);
//c.ValidateRecursive(this, Source, true);
base.Validate(c);
}
}
internal class HybridCacheSection
{
public bool? Enabled { get; set; }
public string? Folder { get; set; }
public int? CacheSizeMb { get; set; }
public int? WriteQueueRamMb { get; set; }
public int? SecondsUntilEvictable { get; set; } // Not available in public plugin options yet
public int? EvictionSweepSizeMb { get; set; }
public int? DatabaseShards { get; set; }
public void Validate(ValidationContext c)
{
// Add your validations here
}
}
/*
# ====== Diagnostics access ======
# Prefix keys with 'production.', 'staging.', or 'development.'
# to merge with the appropriate section based on the current environment
# Defaults to 'production' if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT are not set
[diagnostics]
# Access anywhere with /imageflow.debug?password=[[redacted]]
allow_with_password = "[[redacted]]"
# Allow localhost without a password
allow_localhost = true
allow_anyhost = false
*/
internal class Diagnostics : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public string? AllowWithPassword { get; set; }
public bool? AllowLocalhost { get; set; }
public bool? AllowAnyhost { get; set; }
public void Validate(ValidationContext c)
{
if (AllowWithPassword != null){
c.ValidateRecursive(this, AllowWithPassword, AllowWithPassword.Length > 12, "[diagnostics].allow_with_password must be at least 12 characters long if set");
}
// confusing with development., etc. We're safe with false defaults.
// c.Require(this, AllowLocalhost);
// c.Require(this, AllowAnyhost);
}
}
internal class AspNetServer : ITomlMetadataProvider, IValidationCapable {
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public bool? UseDeveloperExceptionPage { get; set; }
public string? UseExceptionHandler { get; set; }
public bool? UseHsts { get; set; }
public bool? UseHttpsRedirection { get; set; }
public bool? RestartWhenThisFileChanges { get; set; }
public AspNetRewriteRuleSection? ApacheModRewrite { get; set; }
//public AspNetRewriteRuleSection? IisUrlRewrite { get; set; }
public void Validate(ValidationContext c) {
c.ValidateRecursive(this,UseDeveloperExceptionPage, UseDeveloperExceptionPage == true || !string.IsNullOrWhiteSpace(UseExceptionHandler));
}
}
internal class AspNetRewriteRuleSection : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
// public string? Text { get; set; } // Or name it content for consistency?
public string? File { get; set; }
public void Validate(ValidationContext c)
{
// if (Text == null && File == null){
// throw c.InvalidProperty(Text, "either text or path must be specified");
// }
// if (Path != null){
// c.ValidateRecursive(this, Path, File.Exists(Path)); // TODO, expand string variables
// }
}
}
internal class StaticResponse : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public required string For { get; set; }
public string? ContentType { get; set; }
public string? Content { get; set; }
public string? File { get; set; }
public string? CacheControl { get; set; }
public int? StatusCode { get; set; }
// public List<HeaderPair>? ResponseHeaders { get; set; }
public void Validate(ValidationContext c)
{
c.Require(this, For);
c.Require(this, ContentType);
c.Require(this, Content);
c.Require(this, StatusCode);
//c.ValidateRecursive(this, ResponseHeaders, true);
}
}
internal class Security : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
// public bool? OnlyPermitPresets { get; set; }
public MaxResolution? MaxDecodeResolution { get; set; }
public MaxResolution? MaxEncodeResolution { get; set; }
public MaxResolution? MaxFrameResolution { get; set; }
public void Validate(ValidationContext c)
{
// Add your validations here
c.ValidateRecursive(this, MaxDecodeResolution, true);
c.ValidateRecursive(this, MaxEncodeResolution, true);
c.ValidateRecursive(this, MaxFrameResolution, true);
}
}
internal class MaxResolution : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public uint? Width { get; set; }
public uint? Height { get; set; }
public int? Megapixels { get; set; }
public void Validate(ValidationContext c)
{
c.ValidateRecursive(this, Width, Width > 0);
c.ValidateRecursive(this, Height, Height > 0);
c.ValidateRecursive(this, Megapixels, Megapixels > 0);
}
// to new FrameSizeLimit
public FrameSizeLimit ToFrameSizeLimit(){
// TODO: unsure if 0 means unlimited or not specified
return new FrameSizeLimit(Width ?? 0, Height ?? 0, Megapixels ?? 0);
}
}
internal class ImageflowConfig : ITomlMetadataProvider, IValidationCapable
{
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
public ImageflowServer? ImageflowServer { get; set; }
public LicenseSection? License { get; set; }
public HybridCacheSection? DiskCache { get; set; }
public Diagnostics? Diagnostics { get; set; }
public AspNetServer? AspnetServer { get; set; }
// public TomlTable? Vars { get; set; }
// public Dictionary<string,PresetSection>? Presets { get; set; }
public RouteBase? RouteDefaults { get; set; }
//public List<SourceSection>? Sources { get; set; }
public List<RouteSection>? Routes { get; set; }
//public List<Watermark>? Watermarks { get; set; }
public Security? Security { get; set; }
//static response
public List<StaticResponse>? StaticResponse { get; set; }
public void Validate(ValidationContext c)
{
c.Require(this, ImageflowServer);
c.ValidateRecursive(this, ImageflowServer, true);
c.Require(this, License);
c.ValidateRecursive(this, License, true);
//c.ValidateRecursive(this, HybridCache, true);
c.ValidateRecursive(this, Diagnostics, true);
c.ValidateRecursive(this, AspnetServer, true);
//c.ValidateRecursive(this, Vars, true);
//c.ValidateRecursive(this, Presets, true);
// c.ValidateRecursive(this, Sources, true);
// c.ValidateRecursive(this, Routes, true);
// c.ValidateRecursive(this, Watermarks, true);
c.ValidateRecursive(this, Security, true);
c.ValidateRecursive(this, StaticResponse, true);
}
}
// List each class and properties above this line
// class MaxResolution { uint? Width, Height, Megapixels }
// class Security { MaxResolution? MaxDecodeResolution, MaxEncodeResolution, MaxFrameResolution }
//