-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathExtensions.cs
More file actions
562 lines (517 loc) · 20.2 KB
/
Copy pathExtensions.cs
File metadata and controls
562 lines (517 loc) · 20.2 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
using System.Security.Claims;
using System.Text;
using Npgsql;
using NpgsqlRest.Auth;
using NpgsqlTypes;
namespace NpgsqlRest;
public static class Ext
{
public static void CreateAndOpenSourceConnection(this NpgsqlRestOptions options,
IServiceProvider? serviceProvider,
ref NpgsqlConnection? connection,
ref bool shouldDispose,
string? sourceName = null)
{
// Try named connection first (check DataSources for multi-host, then ConnectionStrings)
if (options.MetadataQueryConnectionName is not null)
{
// First check if there's a data source for this connection name (for multi-host support)
if (options.DataSources?.TryGetValue(options.MetadataQueryConnectionName, out var namedDataSource) is true)
{
connection = namedDataSource.OpenConnection();
shouldDispose = true;
if (options.MetadataQuerySchema is not null && HasSearchPathInConnectionString(connection.ConnectionString, options.MetadataQuerySchema) is false)
{
SetSearchPath(connection, options.MetadataQuerySchema);
Logger?.LogTrace("{source}: Using named data source '{name}' with schema '{schema}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQueryConnectionName,
options.MetadataQuerySchema);
}
else
{
Logger?.LogTrace("{source}: Using named data source '{name}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQueryConnectionName);
}
return;
}
// Fall back to ConnectionStrings
if (options.ConnectionStrings is null)
{
throw new ArgumentException("ConnectionStrings or DataSources must be provided when using named connections.");
}
if (!options.ConnectionStrings.TryGetValue(options.MetadataQueryConnectionName, out var connectionString))
{
throw new ArgumentException($"Connection '{options.MetadataQueryConnectionName}' not found in DataSources or ConnectionStrings.");
}
if (options.MetadataQuerySchema is not null)
{
if (HasSearchPathInConnectionString(connectionString, options.MetadataQuerySchema) is false)
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString)
{
SearchPath = options.MetadataQuerySchema
};
connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString);
}
else
{
connection = new NpgsqlConnection(connectionString);
}
shouldDispose = true;
Logger?.LogTrace("{source}: Using named connection string '{name}' with schema '{schema}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQueryConnectionName,
options.MetadataQuerySchema);
}
else
{
connection = new NpgsqlConnection(connectionString);
shouldDispose = true;
Logger?.LogTrace("{source}: Using named connection string '{name}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQueryConnectionName);
}
connection.OpenRetry(Options.ConnectionRetryOptions);
return;
}
// Try service provider
if (options.ServiceProviderMode != ServiceProviderObject.None)
{
if (serviceProvider is null)
{
throw new ArgumentException($"ServiceProvider must be provided when ServiceProviderMode is set to {options.ServiceProviderMode}.");
}
if (options.ServiceProviderMode == ServiceProviderObject.NpgsqlDataSource)
{
var dataSource = serviceProvider.GetRequiredService<NpgsqlDataSource>();
connection = dataSource.OpenConnection();
shouldDispose = true;
if (options.MetadataQuerySchema is not null && HasSearchPathInConnectionString(connection.ConnectionString, options.MetadataQuerySchema) is false)
{
SetSearchPath(connection, options.MetadataQuerySchema);
Logger?.LogTrace("{source}: Using NpgsqlDataSource from service provider with schema '{schema}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQuerySchema);
}
else
{
Logger?.LogTrace("{source}: Using NpgsqlDataSource from service provider for metadata queries.",
sourceName ?? "Unknown");
}
}
else if (options.ServiceProviderMode == ServiceProviderObject.NpgsqlConnection)
{
shouldDispose = false;
connection = serviceProvider.GetRequiredService<NpgsqlConnection>();
if (connection.State != System.Data.ConnectionState.Open)
{
connection.OpenRetry(Options.ConnectionRetryOptions);
}
if (options.MetadataQuerySchema is not null && HasSearchPathInConnectionString(connection.ConnectionString, options.MetadataQuerySchema) is false)
{
SetSearchPath(connection, options.MetadataQuerySchema);
Logger?.LogTrace("{source}: Using NpgsqlConnection from service provider with schema '{schema}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQuerySchema);
}
else
{
Logger?.LogTrace("{source}: Using NpgsqlConnection from service provider for metadata queries.",
sourceName ?? "Unknown");
}
}
return;
}
// Try DataSource
if (options.DataSource is not null)
{
connection = options.DataSource.CreateConnection();
shouldDispose = true;
connection.OpenRetry(Options.ConnectionRetryOptions);
if (options.MetadataQuerySchema is not null && HasSearchPathInConnectionString(connection.ConnectionString, options.MetadataQuerySchema) is false)
{
SetSearchPath(connection, options.MetadataQuerySchema);
Logger?.LogTrace("{source}: Using DataSource with schema '{schema}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQuerySchema);
}
else
{
Logger?.LogTrace("{source}: Using DataSource for metadata queries.",
sourceName ?? "Unknown");
}
return;
}
// Fall back to connection string
if (string.IsNullOrEmpty(options.ConnectionString))
{
throw new ArgumentException("ConnectionString must be provided when no other connection source is configured.");
}
if (options.MetadataQuerySchema is not null)
{
if (HasSearchPathInConnectionString(options.ConnectionString, options.MetadataQuerySchema) is false)
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(options.ConnectionString)
{
SearchPath = options.MetadataQuerySchema
};
connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString);
}
else
{
connection = new NpgsqlConnection(options.ConnectionString);
}
shouldDispose = true;
connection.OpenRetry(Options.ConnectionRetryOptions);
Logger?.LogTrace("{source}: Using default connection string with schema '{schema}' for metadata queries.",
sourceName ?? "Unknown",
options.MetadataQuerySchema);
}
else
{
connection = new NpgsqlConnection(options.ConnectionString);
shouldDispose = true;
connection.OpenRetry(Options.ConnectionRetryOptions);
Logger?.LogTrace("{source}: Using default connection string for metadata queries.",
sourceName ?? "Unknown");
}
}
public static void AddParameter(this NpgsqlCommand command, object? value, bool isArray = false)
{
if (value is null)
{
value = DBNull.Value;
}
else if (isArray && value is string[] array)
{
if (array.Length == 0)
{
value = DBNull.Value;
}
}
else if (!isArray && value is string str)
{
if (string.IsNullOrWhiteSpace(str))
{
value = DBNull.Value;
}
}
command.Parameters.Add(new NpgsqlParameter
{
NpgsqlDbType = isArray ? NpgsqlDbType.Text | NpgsqlDbType.Array : NpgsqlDbType.Text,
Value = value
});
}
private static bool HasSearchPathInConnectionString(string connectionString, string schema)
{
// Fast string search for "Search Path=schema" pattern
// This avoids expensive NpgsqlConnectionStringBuilder parsing
int index = connectionString.IndexOf("Search Path", StringComparison.OrdinalIgnoreCase);
if (index == -1)
{
return false;
}
// Find the value after the key
int equalsIndex = connectionString.IndexOf('=', index);
if (equalsIndex == -1)
{
return false;
}
// Skip whitespace after '='
int valueStart = equalsIndex + 1;
while (valueStart < connectionString.Length && char.IsWhiteSpace(connectionString[valueStart]))
{
valueStart++;
}
// Find end of value (semicolon or end of string)
int valueEnd = connectionString.IndexOf(';', valueStart);
if (valueEnd == -1)
{
valueEnd = connectionString.Length;
}
// Extract and trim the value
int valueLength = valueEnd - valueStart;
if (valueLength == 0)
{
return false;
}
var value = connectionString.AsSpan(valueStart, valueLength).Trim();
return value.Equals(schema.AsSpan(), StringComparison.OrdinalIgnoreCase);
}
private static void SetSearchPath(NpgsqlConnection connection, string schema)
{
using var command = connection.CreateCommand();
command.CommandText = $"SET search_path TO \"{schema.Replace("\"", "\"\"")}\"";
command.ExecuteNonQuery();
}
public static T Get<T>(this NpgsqlDataReader reader, int ordinal)
{
object? value;
if (typeof(T) == typeof(short?[]))
{
value = reader.GetFieldValue<short?[]>(ordinal);
}
else
{
value = reader[ordinal];
}
if (value == DBNull.Value)
{
return default!;
}
// strange bug single char representing as string on older pg versions when using functions
if (typeof(T) == typeof(char) && value.GetType() == typeof(string))
{
if (value is null)
{
return default!;
}
object c = ((string)value)[0];
return (T)c;
}
return (T)value;
}
public static T GetEnum<T>(this string? value) where T : struct
{
Enum.TryParse<T>(value, true, out var result);
// return the first enum (Other) when no match
return result;
}
public static bool IsTypeOf(this Claim claim, string type)
{
return string.Equals(claim.Type, type, StringComparison.OrdinalIgnoreCase);
}
public static object GetClaimDbParam(this Dictionary<string, object> dict, string key)
{
if (dict.TryGetValue(key, out var value))
{
if (value is null)
{
return DBNull.Value;
}
return value;
}
return DBNull.Value;
}
public static object GetClaimDbContextParam(this Dictionary<string, object> dict, string key)
{
object value = dict.GetClaimDbParam(key);
if (value == DBNull.Value || value is string)
{
return value;
}
var list = value as List<string>;
StringBuilder sb = new(100);
sb.Append('{');
for (int i = 0; i < list?.Count; i++)
{
if (i > 0)
{
sb.Append(',');
}
sb.Append(PgConverters.SerializeString(list[i]));
}
sb.Append('}');
return sb.ToString();
}
public static Dictionary<string, object?> BuildClaimsDictionary(this ClaimsPrincipal? user, NpgsqlRestAuthenticationOptions options)
{
Dictionary<string, object?> claimValues = [];
if (user is null)
{
return claimValues;
}
foreach (var claim in user.Claims)
{
if (claimValues.TryGetValue(claim.Type, out var existing))
{
if (string.IsNullOrEmpty(claim.Value) is false)
{
if (existing is List<string> list)
{
list.Add(claim.Value);
}
else if (existing is not null)
{
var newList = new List<string>(4) { (string)existing, claim.Value };
claimValues[claim.Type] = newList;
}
}
}
else
{
if (claim.IsTypeOf(options.DefaultRoleClaimType))
{
if (string.IsNullOrEmpty(claim.Value) is false)
{
claimValues[claim.Type] = new List<string>(4) { claim.Value };
}
}
else
{
if (string.IsNullOrEmpty(claim.Value))
{
claimValues[claim.Type] = null;
}
else
{
claimValues[claim.Type] = claim.Value;
}
}
}
}
return claimValues;
}
public static object GetUserClaimsDbParam(this Dictionary<string, object?> claimValues)
{
if (claimValues.Count == 0)
{
return "{}";
}
int estimatedCapacity = 2 + (claimValues.Count * 10);
foreach (var entry in claimValues)
{
estimatedCapacity += entry.Key.Length * 2;
if (entry.Value is List<string> list)
{
estimatedCapacity += 2;
foreach (var value in list)
{
estimatedCapacity += value.Length * 2 + 3;
}
}
else
{
estimatedCapacity += entry.Value is null ? 4 : ((string)entry.Value).Length * 2 + 2;
}
}
StringBuilder sb = new(estimatedCapacity);
sb.Append('{');
int i = 0;
foreach (var entry in claimValues)
{
if (i > 0)
{
sb.Append(',');
}
sb.Append(PgConverters.SerializeString(entry.Key));
sb.Append(':');
if (entry.Value is List<string> values)
{
sb.Append('[');
for (int j = 0; j < values.Count; j++)
{
if (j > 0)
{
sb.Append(',');
}
sb.Append(PgConverters.SerializeString(values[j]));
}
sb.Append(']');
}
else
{
sb.Append(entry.Value is not null ? PgConverters.SerializeString((string)entry.Value) : "null");
}
i++;
}
sb.Append('}');
return sb.ToString();
}
public static string? GetClientIpAddress(this HttpRequest request)
{
// Check X-Forwarded-For header
var forwardedIp = request.Headers["X-Forwarded-For"].FirstOrDefault();
if (!string.IsNullOrEmpty(forwardedIp))
{
int commaIndex = forwardedIp.IndexOf(',');
return commaIndex > 0 ? forwardedIp[..commaIndex].Trim() : forwardedIp.Trim();
}
// Check other headers with null-coalescing operator
var ip = request.Headers["X-Real-IP"].FirstOrDefault()
?? request.Headers["HTTP_X_FORWARDED_FOR"].FirstOrDefault()
?? request.Headers["REMOTE_ADDR"].FirstOrDefault();
return !string.IsNullOrEmpty(ip) ? ip : request.HttpContext.Connection.RemoteIpAddress?.ToString();
}
public static object GetClientIpAddressDbParam(this HttpRequest request)
{
return request.GetClientIpAddress() as object ?? DBNull.Value;
}
private const string Info = nameof(PostgresNoticeLevels.INFO);
private const string Notice = nameof(PostgresNoticeLevels.NOTICE);
private const string Warning = nameof(PostgresNoticeLevels.WARNING);
public static bool IsInfo(this PostgresNotice notice)
{
return string.Equals(notice.Severity, Info, StringComparison.OrdinalIgnoreCase);
}
public static bool IsNotice(this PostgresNotice notice)
{
return string.Equals(notice.Severity, Notice, StringComparison.OrdinalIgnoreCase);
}
public static bool IsWarning(this PostgresNotice notice)
{
return string.Equals(notice.Severity, Warning, StringComparison.OrdinalIgnoreCase);
}
public static bool? ParameterEnabled(this Dictionary<string, string>? parameters, string key)
{
if (parameters is null || parameters.Count == 0)
{
return null;
}
if (parameters.TryGetValue(key, out var value))
{
// Check for "off" values
if (string.Equals(value, "false", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "off", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "disabled", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "disable", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "0", StringComparison.OrdinalIgnoreCase))
{
return false;
}
// Check for "on" values
if (string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "on", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "enabled", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "enable", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "1", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return null;
}
return null;
}
public static bool IsSsl(this HttpRequest request)
{
if (request.IsHttps)
{
return true;
}
if (string.Equals(request.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
public static bool TryGetErrorCodeMapping(this string policyName, string sqlState, out ErrorCodeMappingOptions mappingOptions)
{
mappingOptions = default!;
if (Options==null! || Options.ErrorHandlingOptions==null!)
{
return false;
}
if (!Options.ErrorHandlingOptions.ErrorCodePolicies.TryGetValue(policyName, out var policyMappings))
{
return false;
}
if (policyMappings.TryGetValue(sqlState, out var mapping))
{
mappingOptions = mapping;
return true;
}
return false;
}
}