-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
ToolOptionsFileMiddleware.cs
163 lines (138 loc) · 5.35 KB
/
ToolOptionsFileMiddleware.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
using System.Collections.Generic;
using System.Threading.Tasks;
using HotChocolate.AspNetCore.Utilities;
using HotChocolate.Execution;
using Microsoft.AspNetCore.Http;
using HttpRequestDelegate = Microsoft.AspNetCore.Http.RequestDelegate;
using Microsoft.AspNetCore.StaticFiles;
namespace HotChocolate.AspNetCore
{
/// <summary>
/// This middleware handles the Banana Cake Pop configuration file request.
/// </summary>
public class ToolOptionsFileMiddleware
: MiddlewareBase
{
private const string _configFile = "/bcp-config.json";
private readonly IContentTypeProvider _contentTypeProvider;
private readonly PathString _matchUrl;
public ToolOptionsFileMiddleware(
HttpRequestDelegate next,
IRequestExecutorResolver executorResolver,
IHttpResultSerializer resultSerializer,
NameString schemaName,
PathString matchUrl)
: base(next, executorResolver, resultSerializer, schemaName)
{
_contentTypeProvider = new FileExtensionContentTypeProvider();
_matchUrl = matchUrl;
}
/// <summary>
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task Invoke(HttpContext context)
{
if (context.Request.IsGetOrHeadMethod() &&
context.Request.TryMatchPath(_matchUrl, false, out PathString subPath) &&
subPath.Value == _configFile)
{
ToolOptions? options = context.GetEndpoint()?.Metadata.GetMetadata<ToolOptions>();
string endpointPath = context.Request.Path.Value!.Replace(_configFile, "");
string schemaEndpoint = CreateEndpointUri(
context.Request.Host.Value,
endpointPath,
context.Request.IsHttps,
false);
var config = new BananaCakePopConfiguration(schemaEndpoint)
{
EndpointEditable = true,
};
ISchema schema = await ExecutorProxy.GetSchemaAsync(context.RequestAborted);
if (options is { })
{
config.Document = options.Document;
config.Credentials = ConvertCredentialsToString(options.Credentials);
config.HttpHeaders = ConvertHttpHeadersToDictionary(options.HttpHeaders);
config.HttpMethod = ConvertHttpMethodToString(options.HttpMethod);
}
if (schema.SubscriptionType is { })
{
config.SubscriptionEndpoint = CreateEndpointUri(
context.Request.Host.Value,
endpointPath,
context.Request.IsHttps,
true);
}
await context.Response.WriteAsJsonAsync(config, context.RequestAborted);
}
else
{
await NextAsync(context);
}
}
private string? ConvertCredentialsToString(DefaultCredentials? credentials)
{
if (credentials is { })
{
switch (credentials)
{
case DefaultCredentials.Include:
return "include";
case DefaultCredentials.Omit:
return "omit";
case DefaultCredentials.SameOrigin:
return "same-origin";
}
}
return null;
}
private IDictionary<string, string>? ConvertHttpHeadersToDictionary(IHeaderDictionary? httpHeaders)
{
if (httpHeaders is { })
{
var result = new Dictionary<string, string>();
foreach (var (key, value) in httpHeaders)
{
result.Add(key, value.ToString());
}
return result;
}
return null;
}
private string? ConvertHttpMethodToString(DefaultHttpMethod? httpMethod)
{
if (httpMethod is { })
{
switch (httpMethod)
{
case DefaultHttpMethod.Get:
return "GET";
case DefaultHttpMethod.Post:
return "POST";
}
}
return null;
}
private string CreateEndpointUri(string host, string path, bool isSecure, bool isWebSocket)
{
string scheme = isWebSocket ? "ws" : "http";
scheme = isSecure ? $"{scheme}s" : scheme;
return $"{scheme}://{host}{path}";
}
private class BananaCakePopConfiguration
{
public BananaCakePopConfiguration(string schemaEndpoint)
{
SchemaEndpoint = schemaEndpoint;
}
public string SchemaEndpoint { get; }
public string? SubscriptionEndpoint { get; set; }
public bool? EndpointEditable { get; set; }
public string? Document { get; set; }
public string? Credentials { get; set; }
public IDictionary<string, string>? HttpHeaders { get; set; }
public string? HttpMethod { get; set; }
}
}
}