-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignTimeQueryMiddleware.cs
More file actions
60 lines (51 loc) · 2.1 KB
/
DesignTimeQueryMiddleware.cs
File metadata and controls
60 lines (51 loc) · 2.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
using Microsoft.AspNetCore.Http.Extensions;
namespace XmlDataSourceProxy;
/// <summary>
/// Snoops for the special query-string parameter <c>_query</c> that, when
/// present, switches the proxy into "design-time" mode: the response will be
/// a generated SSRS <Query> document with type-inferred fields,
/// instead of the actual data. The parameter is stripped from the request
/// before YARP forwards it upstream, so it never reaches the API.
///
/// Truthy values: "1", "true", "yes", "on" (case-insensitive). Bare
/// presence (e.g. "?_query") also counts.
/// </summary>
public class DesignTimeQueryMiddleware
{
public const string FlagItemKey = "XmlDataSourceProxy.GenerateQuery";
private const string TriggerName = "_query";
private readonly RequestDelegate _next;
public DesignTimeQueryMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Query.TryGetValue(TriggerName, out var rawValue) &&
IsTruthy(rawValue.ToString()))
{
context.Items[FlagItemKey] = true;
// Rebuild the query string without the trigger param so YARP
// (and the upstream) never see it.
var qb = new QueryBuilder();
foreach (var kvp in context.Request.Query)
{
if (string.Equals(kvp.Key, TriggerName, StringComparison.OrdinalIgnoreCase)) continue;
foreach (var value in kvp.Value)
{
qb.Add(kvp.Key, value ?? string.Empty);
}
}
context.Request.QueryString = qb.ToQueryString();
}
await _next(context);
}
private static bool IsTruthy(string? value)
{
if (string.IsNullOrEmpty(value)) return true; // bare ?_query
return value.Equals("1", StringComparison.OrdinalIgnoreCase)
|| value.Equals("true", StringComparison.OrdinalIgnoreCase)
|| value.Equals("yes", StringComparison.OrdinalIgnoreCase)
|| value.Equals("on", StringComparison.OrdinalIgnoreCase);
}
}