-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHttpClientTypes.cs
More file actions
675 lines (583 loc) · 23.1 KB
/
Copy pathHttpClientTypes.cs
File metadata and controls
675 lines (583 loc) · 23.1 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
using System.Collections.Frozen;
using Npgsql;
namespace NpgsqlRest.HttpClientType;
public class HttpClientTypes
{
private const string TypeQuery = @"select
(quote_ident(n.nspname) || '.' || quote_ident(t.typname))::regtype::text as name,
des.description as comment,
array_agg(quote_ident(a.attname)) as att_names
from
pg_catalog.pg_type t
join pg_catalog.pg_namespace n on n.oid = t.typnamespace
join pg_catalog.pg_class c on t.typrelid = c.oid and c.relkind = 'c'
join pg_catalog.pg_attribute a on t.typrelid = a.attrelid and a.attisdropped is false
join pg_catalog.pg_description des on t.oid = des.objoid
where
nspname not like 'pg_%'
and nspname <> 'information_schema'
and des.description is not null
and a.attnum > 0
group by n.nspname, t.typname, des.description";
public static FrozenDictionary<string, HttpTypeDefinition> Definitions { get; private set; } = FrozenDictionary<string, HttpTypeDefinition>.Empty;
public static bool NeedsParsing { get; private set; }
public HttpClientTypes(IApplicationBuilder? builder, RetryStrategy? retryStrategy)
{
bool shouldDispose = true;
NpgsqlConnection? connection = null;
try
{
Options.CreateAndOpenSourceConnection(builder?.ApplicationServices, ref connection, ref shouldDispose, "HttpClientTypes");
if (connection is null)
{
return;
}
using var command = connection.CreateCommand();
command.CommandText = TypeQuery;
command.LogCommand(nameof(HttpClientTypes));
using NpgsqlDataReader reader = command.ExecuteReaderWithRetry(retryStrategy);
var definitions = new Dictionary<string, HttpTypeDefinition>();
bool needsParsing = false;
while (reader.Read())
{
string typeName = reader.GetString(0);
string? comment = reader.IsDBNull(1) ? null : reader.GetString(1);
string[] attNames = reader.IsDBNull(2) ? Array.Empty<string>() : reader.GetFieldValue<string[]>(2);
if (string.IsNullOrWhiteSpace(comment))
{
continue;
}
var typeDefinition = ParseHttpTypeDefinition(comment, typeName);
if (typeDefinition is not null)
{
definitions[typeName] = typeDefinition;
if (typeDefinition.NeedsParsing)
{
needsParsing = true;
}
}
}
Definitions = definitions.ToFrozenDictionary();
NeedsParsing = needsParsing;
}
finally
{
if (connection is not null && shouldDispose is true)
{
connection.Dispose();
}
}
}
public HttpTypeDefinition? ParseHttpTypeDefinition(string comment, string? typeName = null)
{
if (string.IsNullOrWhiteSpace(comment))
{
return null;
}
var span = comment.AsSpan();
int pos = 0;
TimeSpan? timeout = null;
TimeSpan[]? retryDelays = null;
HashSet<int>? retryOnStatusCodes = null;
bool cacheEnabled = false;
TimeSpan? cacheDuration = null;
// Parse directives before the request line
while (pos < span.Length)
{
int lineEnd = span[pos..].IndexOfAny('\r', '\n');
var line = lineEnd == -1 ? span[pos..] : span[pos..(pos + lineEnd)];
var trimmedLine = TrimSpan(line);
// Skip empty lines
if (trimmedLine.IsEmpty)
{
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
// Check for timeout directive (with or without # prefix)
if (TryParseTimeoutDirective(trimmedLine, typeName, out var parsedTimeout))
{
timeout = parsedTimeout;
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
// Check for retry_delay directive
if (TryParseRetryDirective(trimmedLine, typeName, out var parsedDelays, out var parsedRetryCodes))
{
retryDelays = parsedDelays;
retryOnStatusCodes = parsedRetryCodes;
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
// Check for cache directive
if (TryParseCacheDirective(trimmedLine, typeName, out var parsedCacheDuration))
{
cacheEnabled = true;
cacheDuration = parsedCacheDuration;
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
// Check for # comment (non-timeout directive)
if (trimmedLine[0] == '#')
{
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
// Not a directive, must be the request line
break;
}
// Find first line (request line)
int firstLineEnd = span[pos..].IndexOfAny('\r', '\n');
var firstLine = firstLineEnd == -1 ? span[pos..] : span[pos..(pos + firstLineEnd)];
firstLine = TrimSpan(firstLine);
if (firstLine.IsEmpty)
{
Logger?.LogWarning("Type '{TypeName}': HTTP type definition has no request line (expected 'METHOD URL')", typeName);
return null;
}
// Parse request line: METHOD URL [HTTP/version]
int firstSpace = firstLine.IndexOf(' ');
if (firstSpace <= 0)
{
Logger?.LogWarning("Type '{TypeName}': HTTP type definition request line is missing URL: '{RequestLine}'", typeName, new string(firstLine));
return null;
}
var methodSpan = firstLine[..firstSpace];
// Fast uppercase check and validation (most methods are already uppercase)
Span<char> methodUpper = stackalloc char[methodSpan.Length];
for (int i = 0; i < methodSpan.Length; i++)
{
methodUpper[i] = char.ToUpperInvariant(methodSpan[i]);
}
if (!IsValidHttpMethod(methodUpper))
{
Logger?.LogWarning("Type '{TypeName}': HTTP type definition has invalid HTTP method '{Method}'. Supported methods: GET, POST, PUT, PATCH, DELETE", typeName, new string(methodSpan));
return null;
}
// Find URL (skip spaces after method)
var afterMethod = firstLine[(firstSpace + 1)..];
afterMethod = TrimSpan(afterMethod);
if (afterMethod.IsEmpty)
{
Logger?.LogWarning("Type '{TypeName}': HTTP type definition is missing URL after method '{Method}'", typeName, new string(methodUpper));
return null;
}
// URL ends at space (before HTTP/version) or end of line
int urlEnd = afterMethod.IndexOf(' ');
var urlSpan = urlEnd == -1 ? afterMethod : afterMethod[..urlEnd];
// Check URL for placeholders
bool needsParsing = ContainsPlaceholder(urlSpan);
var result = new HttpTypeDefinition
{
Method = new string(methodUpper),
Url = new string(urlSpan),
Timeout = timeout,
RetryDelays = retryDelays,
RetryOnStatusCodes = retryOnStatusCodes,
CacheEnabled = cacheEnabled,
CacheDuration = cacheDuration
};
// Move past first line
if (firstLineEnd == -1)
{
NormalizeCacheDirective(result, typeName);
result.NeedsParsing = needsParsing;
return result;
}
pos += firstLineEnd;
// Skip \r\n or \n
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
Dictionary<string, string>? headers = null;
int bodyStart = -1;
// Parse headers
while (pos < span.Length)
{
// Find end of current line
int lineEnd = span[pos..].IndexOfAny('\r', '\n');
var line = lineEnd == -1 ? span[pos..] : span[pos..(pos + lineEnd)];
// Empty line indicates end of headers
if (line.IsEmpty || line.IsWhiteSpace())
{
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
bodyStart = pos;
break;
}
// Directives may also appear in the header section (after the request line), not only
// before it. Check for them before treating the line as an HTTP header. A directive's
// separator/value shape means real headers (which carry a 'Name-Word: value') don't match.
if (TryParseTimeoutDirective(line, typeName, out var hdrTimeout))
{
result.Timeout = hdrTimeout;
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
if (TryParseRetryDirective(line, typeName, out var hdrDelays, out var hdrCodes))
{
result.RetryDelays = hdrDelays;
result.RetryOnStatusCodes = hdrCodes;
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
if (TryParseCacheDirective(line, typeName, out var hdrCacheDuration))
{
result.CacheEnabled = true;
result.CacheDuration = hdrCacheDuration;
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
continue;
}
int colonIndex = line.IndexOf(':');
if (colonIndex > 0)
{
var headerName = TrimSpan(line[..colonIndex]);
var headerValue = TrimSpan(line[(colonIndex + 1)..]);
if (!headerName.IsEmpty)
{
// Check header value for placeholders
if (!needsParsing && ContainsPlaceholder(headerValue))
{
needsParsing = true;
}
if (headerName.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
{
result.ContentType = new string(headerValue);
}
else
{
headers ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
headers[new string(headerName)] = new string(headerValue);
}
}
}
pos += lineEnd == -1 ? line.Length : lineEnd;
if (pos < span.Length && span[pos] == '\r') pos++;
if (pos < span.Length && span[pos] == '\n') pos++;
}
if (headers is { Count: > 0 })
{
result.Headers = headers;
}
// Parse body
if (bodyStart >= 0 && bodyStart < span.Length)
{
var bodySpan = span[bodyStart..];
if (!bodySpan.IsEmpty && !bodySpan.IsWhiteSpace())
{
result.Body = new string(bodySpan);
// Check body for placeholders
if (!needsParsing && ContainsPlaceholder(bodySpan))
{
needsParsing = true;
}
}
}
NormalizeCacheDirective(result, typeName);
result.NeedsParsing = needsParsing;
return result;
}
// Caching is only safe for GET (idempotent reads). A @cache directive on any other method is
// almost always a mistake; warn and ignore it rather than caching a mutating call. Applied after
// the whole comment is parsed so it sees @cache whether it appeared before the request line or
// among the headers, and after the method is known.
private static void NormalizeCacheDirective(HttpTypeDefinition result, string? typeName)
{
if (!result.CacheEnabled)
{
return;
}
if (!string.Equals(result.Method, "GET", StringComparison.Ordinal))
{
Logger?.LogWarning("Type '{TypeName}': @cache is only supported for GET requests; ignoring it for '{Method}'", typeName, result.Method);
result.CacheEnabled = false;
result.CacheDuration = null;
}
else if (result.CacheDuration is null)
{
Logger?.LogWarning("Type '{TypeName}': @cache has no expiration interval; responses will be cached until the process restarts", typeName);
}
}
private static ReadOnlySpan<char> TrimSpan(ReadOnlySpan<char> span)
{
int start = 0;
int end = span.Length - 1;
while (start <= end && char.IsWhiteSpace(span[start])) start++;
while (end >= start && char.IsWhiteSpace(span[end])) end--;
return start > end ? ReadOnlySpan<char>.Empty : span[start..(end + 1)];
}
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
private static bool IsValidHttpMethod(ReadOnlySpan<char> method)
{
return method.Length switch
{
3 => method is "GET" || method is "PUT",
4 => method is "POST",
5 => method is "PATCH",
6 => method is "DELETE",
_ => false
};
}
private static bool ContainsPlaceholder(ReadOnlySpan<char> span)
{
// Find {key} where key is identifier chars (letters, digits, underscore)
// This avoids false positives from JSON like {"name": "value"}
int pos = 0;
while (pos < span.Length)
{
int openBrace = span[pos..].IndexOf('{');
if (openBrace < 0) return false;
int start = pos + openBrace + 1;
if (start >= span.Length) return false;
// Check if first char after { is a valid identifier start (letter or underscore)
char firstChar = span[start];
if (char.IsLetter(firstChar) || firstChar == '_')
{
// Find the closing brace and verify all chars between are identifier chars
int i = start;
while (i < span.Length && (char.IsLetterOrDigit(span[i]) || span[i] == '_'))
{
i++;
}
if (i < span.Length && span[i] == '}' && i > start)
{
return true;
}
}
pos = start;
}
return false;
}
private static TimeSpan ParseTimeoutValue(ReadOnlySpan<char> value, string? typeName)
{
if (value.IsEmpty)
{
return DefaultTimeout;
}
var valueStr = new string(value);
// If contains colon, try TimeSpan.Parse (e.g., "00:00:30")
if (value.Contains(':'))
{
if (TimeSpan.TryParse(valueStr, out var ts))
{
return ts;
}
Logger?.LogError("Type '{TypeName}': Failed to parse timeout value '{Value}' as TimeSpan, using default {Default}s", typeName, valueStr, DefaultTimeout.TotalSeconds);
return DefaultTimeout;
}
// If only digits, treat as seconds
bool allDigits = true;
foreach (var c in value)
{
if (!char.IsDigit(c))
{
allDigits = false;
break;
}
}
if (allDigits)
{
if (int.TryParse(valueStr, out var seconds))
{
return TimeSpan.FromSeconds(seconds);
}
Logger?.LogError("Type '{TypeName}': Failed to parse timeout value '{Value}' as seconds, using default {Default}s", typeName, valueStr, DefaultTimeout.TotalSeconds);
return DefaultTimeout;
}
// Otherwise use PostgresInterval parser (e.g., "30s", "5m", "1h")
var result = Parser.ParsePostgresInterval(valueStr);
if (result.HasValue)
{
return result.Value;
}
Logger?.LogError("Type '{TypeName}': Failed to parse timeout value '{Value}', using default {Default}s", typeName, valueStr, DefaultTimeout.TotalSeconds);
return DefaultTimeout;
}
private static bool TryParseTimeoutDirective(ReadOnlySpan<char> line, string? typeName, out TimeSpan timeout)
{
timeout = DefaultTimeout;
var trimmed = TrimSpan(line);
// Remove leading # if present
if (!trimmed.IsEmpty && trimmed[0] == '#')
{
trimmed = TrimSpan(trimmed[1..]);
}
// Remove leading @ if present
if (!trimmed.IsEmpty && trimmed[0] == '@')
{
trimmed = TrimSpan(trimmed[1..]);
}
// Check for "timeout" keyword (case-insensitive)
if (!trimmed.StartsWith("timeout", StringComparison.OrdinalIgnoreCase))
{
return false;
}
var afterKeyword = trimmed[7..]; // Skip "timeout"
// Handle separator: space, '=', or ':'
afterKeyword = TrimSpan(afterKeyword);
if (afterKeyword.IsEmpty)
{
return false;
}
// Skip separator character if present
if (afterKeyword[0] == '=' || afterKeyword[0] == ':')
{
afterKeyword = TrimSpan(afterKeyword[1..]);
}
if (afterKeyword.IsEmpty)
{
return false;
}
timeout = ParseTimeoutValue(afterKeyword, typeName);
return true;
}
private static bool TryParseRetryDirective(
ReadOnlySpan<char> line,
string? typeName,
out TimeSpan[]? delays,
out HashSet<int>? statusCodes)
{
delays = null;
statusCodes = null;
var trimmed = TrimSpan(line);
// Remove leading # if present
if (!trimmed.IsEmpty && trimmed[0] == '#')
{
trimmed = TrimSpan(trimmed[1..]);
}
// Remove leading @ if present
if (!trimmed.IsEmpty && trimmed[0] == '@')
{
trimmed = TrimSpan(trimmed[1..]);
}
// Check for "retry_delay" keyword (case-insensitive)
if (!trimmed.StartsWith("retry_delay", StringComparison.OrdinalIgnoreCase))
{
return false;
}
var afterKeyword = trimmed[11..]; // Skip "retry_delay"
// Handle separator: space, '=', or ':'
afterKeyword = TrimSpan(afterKeyword);
if (afterKeyword.IsEmpty)
{
return false;
}
if (afterKeyword[0] == '=' || afterKeyword[0] == ':')
{
afterKeyword = TrimSpan(afterKeyword[1..]);
}
if (afterKeyword.IsEmpty)
{
return false;
}
// Split by " on " to separate delays from status codes
var afterStr = new string(afterKeyword);
var onIndex = afterStr.IndexOf(" on ", StringComparison.OrdinalIgnoreCase);
string delaysPart;
string? codesPart = null;
if (onIndex >= 0)
{
delaysPart = afterStr[..onIndex];
codesPart = afterStr[(onIndex + 4)..]; // Skip " on "
}
else
{
delaysPart = afterStr;
}
// Parse delays: comma-separated time values
var delayParts = delaysPart.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (delayParts.Length == 0)
{
Logger?.LogError("Type '{TypeName}': retry_delay has no delay values", typeName);
return false;
}
var parsedDelays = new TimeSpan[delayParts.Length];
for (int i = 0; i < delayParts.Length; i++)
{
parsedDelays[i] = ParseTimeoutValue(delayParts[i].AsSpan(), typeName);
}
delays = parsedDelays;
// Parse status codes if present
if (codesPart is not null)
{
var codeParts = codesPart.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var codes = new HashSet<int>();
foreach (var code in codeParts)
{
if (int.TryParse(code, out var statusCode))
{
codes.Add(statusCode);
}
else
{
Logger?.LogError("Type '{TypeName}': Failed to parse retry status code '{Code}'", typeName, code);
}
}
if (codes.Count > 0)
{
statusCodes = codes;
}
}
return true;
}
private static bool TryParseCacheDirective(ReadOnlySpan<char> line, string? typeName, out TimeSpan? duration)
{
duration = null;
var trimmed = TrimSpan(line);
// Remove leading # if present
if (!trimmed.IsEmpty && trimmed[0] == '#')
{
trimmed = TrimSpan(trimmed[1..]);
}
// Remove leading @ if present
if (!trimmed.IsEmpty && trimmed[0] == '@')
{
trimmed = TrimSpan(trimmed[1..]);
}
// Check for "cache" keyword (case-insensitive). Must be the whole token, so "cache_profile"
// or other future "cache*" directives are not swallowed here.
if (!trimmed.StartsWith("cache", StringComparison.OrdinalIgnoreCase))
{
return false;
}
var afterKeyword = trimmed[5..]; // Skip "cache"
// Bare "@cache" (no value) → enabled with no expiration.
if (afterKeyword.IsEmpty)
{
return true;
}
// The next char must be a separator (space, '=', ':'); otherwise this is a different keyword.
char sep = afterKeyword[0];
if (sep != ' ' && sep != '\t' && sep != '=' && sep != ':')
{
return false;
}
if (sep == '=' || sep == ':')
{
afterKeyword = afterKeyword[1..];
}
afterKeyword = TrimSpan(afterKeyword);
// "@cache" followed only by separator → enabled with no expiration.
if (afterKeyword.IsEmpty)
{
return true;
}
duration = ParseTimeoutValue(afterKeyword, typeName);
return true;
}
}