-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNpgsqlRestEndpoint.Helpers.cs
More file actions
460 lines (418 loc) · 16.8 KB
/
Copy pathNpgsqlRestEndpoint.Helpers.cs
File metadata and controls
460 lines (418 loc) · 16.8 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
using System.Data;
using System.IO.Pipelines;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using Npgsql;
using NpgsqlRest.Proxy;
using static System.Net.Mime.MediaTypeNames;
namespace NpgsqlRest;
public partial class NpgsqlRestEndpoint
{
/// <summary>
/// Result of evaluating <see cref="RoutineEndpoint.CacheWhen"/> against the resolved parameter values.
/// Three possible outcomes:
/// - <c>Skip = true</c> → bypass cache for this request (no read, no write).
/// - <c>Skip = false</c>, <c>TtlOverride</c> set → use this TTL when writing.
/// - <c>Skip = false</c>, <c>TtlOverride</c> null → no rule matched; fall through to endpoint's default expiration.
/// </summary>
private readonly record struct CacheWhenResult(bool Skip, TimeSpan? TtlOverride);
/// <summary>
/// Walks <see cref="RoutineEndpoint.CacheWhen"/> in order, returning the first rule's resolution.
/// First match wins. Lenient: rules referencing unknown routine parameter names are skipped silently
/// (startup logs a Warning per misuse). Parameters absent from <paramref name="command"/> (request omitted them
/// and NpgsqlRest deferred to the PG-side default) are treated as null for matching purposes.
/// </summary>
private static CacheWhenResult EvaluateCacheWhenRules(RoutineEndpoint endpoint, NpgsqlCommand command)
{
if (endpoint.CacheWhen is null || endpoint.CacheWhen.Length == 0)
{
return default;
}
foreach (var rule in endpoint.CacheWhen)
{
// Verify the named param exists on the routine. Skip silently if not (startup already warned).
bool paramExistsInRoutine = false;
for (int i = 0; i < endpoint.Routine.Parameters.Length; i++)
{
var rp = endpoint.Routine.Parameters[i];
if (string.Equals(rp.ActualName, rule.Parameter, StringComparison.Ordinal) ||
string.Equals(rp.ConvertedName, rule.Parameter, StringComparison.Ordinal))
{
paramExistsInRoutine = true;
break;
}
}
if (!paramExistsInRoutine)
{
continue;
}
object? valueToCompare = null;
for (int i = 0; i < command.Parameters.Count; i++)
{
if (command.Parameters[i] is NpgsqlRestParameter p &&
(string.Equals(p.ActualName, rule.Parameter, StringComparison.Ordinal) ||
string.Equals(p.ConvertedName, rule.Parameter, StringComparison.Ordinal)))
{
valueToCompare = p.Value;
break;
}
}
if (CacheWhenRuleMatches(valueToCompare, rule.Value))
{
if (rule.Skip)
{
Logger?.CacheSkippedDueToWhenRule(string.Concat(endpoint.Method.ToString(), " ", endpoint.Path), rule.Parameter);
}
return new CacheWhenResult(rule.Skip, rule.ThenExpiration);
}
}
return default;
}
/// <summary>
/// Compares one parameter value against one When-rule condition. Semantics:
/// - JSON <c>null</c> matches .NET <c>null</c> and <see cref="DBNull.Value"/> (does NOT match empty string).
/// - JSON array: OR over entries.
/// - Any non-null scalar (string, number, boolean): both sides are stringified and compared
/// case-insensitive ordinal. This matches JSON <c>true</c> against .NET <c>"True"</c> and is lenient
/// for case-only differences in user-typed string values.
///
/// Note: when <see cref="CacheProfile.When"/> is loaded from JSON config (NpgsqlRestClient), all values
/// arrive as strings via <c>IConfiguration</c> regardless of their original JSON type. The case-insensitive
/// comparison preserves the intuitive matching behavior across that boundary.
/// </summary>
private static bool CacheWhenRuleMatches(object? value, JsonNode? condition)
{
if (condition is null)
{
return value is null || value == DBNull.Value;
}
if (condition is JsonArray arr)
{
foreach (var entry in arr)
{
if (CacheWhenRuleMatches(value, entry))
{
return true;
}
}
return false;
}
if (value is null || value == DBNull.Value)
{
return false;
}
var kind = condition.GetValueKind();
var condStr = kind == JsonValueKind.String ? condition.GetValue<string>() : condition.ToString();
return string.Equals(value.ToString(), condStr, StringComparison.OrdinalIgnoreCase);
}
private async ValueTask<bool> PrepareCommand(
NpgsqlConnection connection,
NpgsqlCommand command,
string commandText,
HttpContext context,
RoutineEndpoint endpoint,
bool unknownResults,
CancellationToken cancellationToken)
{
await OpenConnectionAsync(connection, context, endpoint, cancellationToken);
command.CommandText = commandText;
if (endpoint.CommandTimeout.HasValue)
{
command.CommandTimeout = endpoint.CommandTimeout.Value.Seconds;
}
if (Options.CommandCallbackAsync is not null)
{
await Options.CommandCallbackAsync(endpoint, command, context);
if (context.Response.HasStarted || context.Response.StatusCode != (int)HttpStatusCode.OK)
{
return false;
}
}
if (unknownResults)
{
if (endpoint.Routine.UnknownResultTypeList is not null)
{
command.UnknownResultTypeList = endpoint.Routine.UnknownResultTypeList;
}
else
{
command.AllResultTypesAreUnknown = true;
}
}
else
{
command.AllResultTypesAreUnknown = false;
}
return true;
}
private async ValueTask OpenConnectionAsync(NpgsqlConnection connection, HttpContext context, RoutineEndpoint endpoint, CancellationToken cancellationToken)
{
if (connection.State != ConnectionState.Open)
{
if (Options.BeforeConnectionOpen is not null)
{
Options.BeforeConnectionOpen(connection, endpoint, context);
}
await connection.OpenRetryAsync(Options.ConnectionRetryOptions, cancellationToken);
}
}
private static void SearializeHeader(NpgsqlRestOptions options, HttpContext context, ref string? headers)
{
if (Options.CustomRequestHeaders.Count > 0)
{
foreach (var header in Options.CustomRequestHeaders)
{
context.Request.Headers.Add(header);
}
}
var sb = new StringBuilder();
sb.Append('{');
var i = 0;
foreach (var header in context.Request.Headers)
{
if (i++ > 0)
{
sb.Append(',');
}
sb.Append(SerializeString(header.Key));
sb.Append(':');
sb.Append(SerializeString(header.Value.ToString()));
}
sb.Append('}');
headers = sb.ToString();
}
private static void MapProxyResponseToParameters(
ProxyResponse proxyResponse,
NpgsqlParameterCollection parameters,
RoutineEndpoint endpoint)
{
var proxyOptions = Options.ProxyOptions;
for (var i = 0; i < parameters.Count; i++)
{
var parameter = (NpgsqlRestParameter)parameters[i];
var paramName = parameter.ActualName ?? parameter.ParameterName;
if (endpoint.ProxyResponseParameterNames?.Contains(paramName) != true)
{
continue;
}
if (string.Equals(paramName, proxyOptions.ResponseStatusCodeParameter, StringComparison.OrdinalIgnoreCase))
{
// Use NpgsqlDbType (which reflects @param retype) rather than TypeDescriptor
// (which reflects the original Describe type and may be stale for SQL files)
var dbType = parameter.NpgsqlDbType;
if (dbType == NpgsqlTypes.NpgsqlDbType.Text || dbType == NpgsqlTypes.NpgsqlDbType.Varchar || dbType == NpgsqlTypes.NpgsqlDbType.Unknown)
{
parameter.Value = proxyResponse.StatusCode.ToString();
}
else
{
parameter.Value = proxyResponse.StatusCode;
}
}
else if (string.Equals(paramName, proxyOptions.ResponseBodyParameter, StringComparison.OrdinalIgnoreCase))
{
parameter.Value = (object?)proxyResponse.Body ?? DBNull.Value;
}
else if (string.Equals(paramName, proxyOptions.ResponseHeadersParameter, StringComparison.OrdinalIgnoreCase))
{
parameter.Value = (object?)proxyResponse.Headers ?? DBNull.Value;
}
else if (string.Equals(paramName, proxyOptions.ResponseContentTypeParameter, StringComparison.OrdinalIgnoreCase))
{
parameter.Value = (object?)proxyResponse.ContentType ?? DBNull.Value;
}
else if (string.Equals(paramName, proxyOptions.ResponseSuccessParameter, StringComparison.OrdinalIgnoreCase))
{
parameter.Value = proxyResponse.IsSuccess;
}
else if (string.Equals(paramName, proxyOptions.ResponseErrorMessageParameter, StringComparison.OrdinalIgnoreCase))
{
parameter.Value = (object?)proxyResponse.ErrorMessage ?? DBNull.Value;
}
}
}
/// <summary>
/// Check if a parameter is a proxy response parameter that will be filled in by the proxy response.
/// </summary>
private static bool IsProxyResponseParameter(RoutineEndpoint endpoint, NpgsqlRestParameter parameter)
{
if (!endpoint.IsProxy || !Options.ProxyOptions.Enabled || endpoint.ProxyResponseParameterNames is null)
{
return false;
}
var paramName = parameter.ActualName ?? parameter.ConvertedName;
return paramName is not null && endpoint.ProxyResponseParameterNames.Contains(paramName);
}
private static void WriteStringBuilderToWriter(StringBuilder row, PipeWriter writer)
{
foreach (ReadOnlyMemory<char> chunk in row.GetChunks())
{
var buffer = writer.GetSpan(Encoding.UTF8.GetMaxByteCount(chunk.Length));
int bytesWritten = Encoding.UTF8.GetBytes(chunk.Span, buffer);
writer.Advance(bytesWritten);
}
}
private async ValueTask ReturnErrorAsync(
string message,
bool log, HttpContext context,
CancellationToken cancellationToken,
int statusCode = (int)HttpStatusCode.InternalServerError)
{
if (log)
{
Logger?.LogError("{message}", message);
}
context.Response.StatusCode = statusCode;
context.Response.ContentType = Text.Plain;
await context.Response.WriteAsync(message, cancellationToken);
await context.Response.CompleteAsync();
}
private async ValueTask<bool> ValidateParametersAsync(
NpgsqlParameterCollection parameters,
RoutineEndpoint endpoint,
HttpContext context,
CancellationToken cancellationToken)
{
if (endpoint.ParameterValidations is null)
{
return true;
}
foreach (var kvp in endpoint.ParameterValidations)
{
var originalParamName = kvp.Key;
var rules = kvp.Value;
// Find the parameter by original name
NpgsqlRestParameter? parameter = null;
for (var i = 0; i < parameters.Count; i++)
{
var p = parameters[i] as NpgsqlRestParameter;
if (p is not null && string.Equals(p.ActualName, originalParamName, StringComparison.Ordinal))
{
parameter = p;
break;
}
}
if (parameter is null)
{
continue;
}
// Get the value to validate - use OriginalStringValue for consistency
var valueToValidate = parameter.OriginalStringValue;
var convertedParamName = parameter.ConvertedName;
// Find rule names for error messages
foreach (var rule in rules)
{
// Find the rule name by looking it up in ValidationOptions
var ruleName = FindRuleName(rule);
var (isValid, message) = ValidateParameterValue(
valueToValidate,
parameter.Value,
rule,
originalParamName,
convertedParamName,
ruleName);
if (!isValid)
{
var urlInfo = string.Concat(endpoint.Method.ToString(), " ", endpoint.Path);
Logger?.ValidationFailed(urlInfo, originalParamName, message);
context.Response.StatusCode = rule.StatusCode;
context.Response.ContentType = Text.Plain;
await context.Response.WriteAsync(message, cancellationToken);
await context.Response.CompleteAsync();
return false;
}
}
}
return true;
}
private static string FindRuleName(ValidationRule rule)
{
foreach (var kvp in Options.ValidationOptions.Rules)
{
if (ReferenceEquals(kvp.Value, rule))
{
return kvp.Key;
}
}
return string.Empty;
}
private static (bool IsValid, string Message) ValidateParameterValue(
string? originalStringValue,
object? value,
ValidationRule rule,
string originalParamName,
string convertedParamName,
string ruleName)
{
// Message format: {0} = original param name, {1} = converted param name, {2} = rule name
var message = string.Format(rule.Message, originalParamName, convertedParamName, ruleName);
switch (rule.Type)
{
case ValidationType.NotNull:
// Check if value is null or DBNull
if (value is null || value == DBNull.Value)
{
return (false, message);
}
break;
case ValidationType.NotEmpty:
// Check if value is an empty string (null values pass - use NotNull for null check)
if (originalStringValue is not null && originalStringValue.Length == 0)
{
return (false, message);
}
break;
case ValidationType.Required:
// Combines NotNull and NotEmpty - value cannot be null or empty string
if (value is null || value == DBNull.Value)
{
return (false, message);
}
if (originalStringValue is not null && originalStringValue.Length == 0)
{
return (false, message);
}
break;
case ValidationType.Regex:
if (rule.Pattern is null)
{
return (true, string.Empty);
}
// Null/empty values don't match regex (use NotEmpty rule first if required)
if (string.IsNullOrEmpty(originalStringValue))
{
return (false, message);
}
if (!System.Text.RegularExpressions.Regex.IsMatch(originalStringValue, rule.Pattern))
{
return (false, message);
}
break;
case ValidationType.MinLength:
if (rule.MinLength is null)
{
return (true, string.Empty);
}
var strValue = originalStringValue ?? string.Empty;
if (strValue.Length < rule.MinLength.Value)
{
return (false, message);
}
break;
case ValidationType.MaxLength:
if (rule.MaxLength is null)
{
return (true, string.Empty);
}
var strVal = originalStringValue ?? string.Empty;
if (strVal.Length > rule.MaxLength.Value)
{
return (false, message);
}
break;
}
return (true, string.Empty);
}
}