-
Notifications
You must be signed in to change notification settings - Fork 447
/
GrpcMessageConversionExtensions.cs
406 lines (359 loc) · 15.9 KB
/
GrpcMessageConversionExtensions.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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Extensions;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RpcDataType = Microsoft.Azure.WebJobs.Script.Grpc.Messages.TypedData.DataOneofCase;
namespace Microsoft.Azure.WebJobs.Script.Grpc
{
internal static class GrpcMessageConversionExtensions
{
private static readonly JsonSerializerSettings _datetimeSerializerSettings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };
public static object ToObject(this TypedData typedData) =>
typedData.DataCase switch
{
RpcDataType.None => null,
RpcDataType.String => typedData.String,
RpcDataType.Json => JsonConvert.DeserializeObject(typedData.Json, _datetimeSerializerSettings),
RpcDataType.Bytes or RpcDataType.Stream => typedData.Bytes.ToByteArray(),
RpcDataType.Http => GrpcMessageExtensionUtilities.ConvertFromHttpMessageToExpando(typedData.Http),
RpcDataType.Int => typedData.Int,
RpcDataType.Double => typedData.Double,
// TODO better exception
_ => throw new InvalidOperationException($"Unknown RpcDataType: {typedData.DataCase}")
};
public static ValueTask<TypedData> ToRpc(this object value, ILogger logger, GrpcCapabilities capabilities)
{
if (value is HttpRequest request)
{
return new ValueTask<TypedData>(request.ToRpcHttp(logger, capabilities));
}
else
{
return ValueTask.FromResult(value switch
{
null => new TypedData(),
byte[] arr => new TypedData() { Bytes = ByteString.CopyFrom(arr) },
JObject jobj => new TypedData() { Json = jobj.ToString(Formatting.None) },
string str => new TypedData() { String = str },
double dbl => new TypedData() { Double = dbl },
ParameterBindingData bindingData => bindingData.ToModelBindingData(),
ParameterBindingData[] bindingDataArray => bindingDataArray.ToModelBindingDataArray(),
byte[][] arrBytes when IsTypedDataCollectionSupported(capabilities) => arrBytes.ToRpcByteArray(),
string[] arrStr when IsTypedDataCollectionSupported(capabilities) => arrStr.ToRpcStringArray(
ShouldIncludeEmptyEntriesInMessagePayload(capabilities)),
double[] arrDouble when IsTypedDataCollectionSupported(capabilities) => arrDouble.ToRpcDoubleArray(),
long[] arrLong when IsTypedDataCollectionSupported(capabilities) => arrLong.ToRpcLongArray(),
_ => value.ToRpcDefault(),
});
}
}
internal static TypedData ToModelBindingData(this ParameterBindingData data)
{
var modelBindingData = new ModelBindingData
{
Version = data.Version,
Source = data.Source,
ContentType = data.ContentType,
Content = ByteString.CopyFrom(data.Content)
};
var typedData = new TypedData
{
ModelBindingData = modelBindingData
};
return typedData;
}
internal static TypedData ToModelBindingDataArray(this ParameterBindingData[] dataArray)
{
var collectionModelBindingData = new CollectionModelBindingData();
foreach (ParameterBindingData element in dataArray)
{
if (element != null)
{
collectionModelBindingData.ModelBindingData.Add(element.ToModelBindingData().ModelBindingData);
}
}
return new TypedData() { CollectionModelBindingData = collectionModelBindingData };
}
internal static async Task<TypedData> ToRpcHttp(this HttpRequest request, ILogger logger, GrpcCapabilities capabilities)
{
var http = new RpcHttp()
{
Url = $"{(request.IsHttps ? "https" : "http")}://{request.Host}{request.Path}{request.QueryString}",
Method = request.Method.ToString(),
RawBody = null
};
var typedData = new TypedData
{
Http = http
};
foreach (var pair in request.Query)
{
var value = pair.Value.ToString();
if (ShouldUseNullableValueDictionary(capabilities))
{
http.NullableQuery.Add(pair.Key, new NullableString { Value = value });
}
else
{
if (!string.IsNullOrEmpty(value))
{
http.Query.Add(pair.Key, value);
}
}
}
foreach (var pair in request.Headers)
{
if (ShouldUseNullableValueDictionary(capabilities))
{
http.NullableHeaders.Add(pair.Key.ToLowerInvariant(), new NullableString { Value = pair.Value.ToString() });
}
else
{
if (ShouldIgnoreEmptyHeaderValues(capabilities) && string.IsNullOrEmpty(pair.Value.ToString()))
{
continue;
}
http.Headers.Add(pair.Key.ToLowerInvariant(), pair.Value.ToString());
}
}
if (request.HttpContext.Items.TryGetValue(HttpExtensionConstants.AzureWebJobsHttpRouteDataKey, out object routeData))
{
Dictionary<string, object> parameters = (Dictionary<string, object>)routeData;
foreach (var pair in parameters)
{
if (pair.Value != null)
{
if (ShouldUseNullableValueDictionary(capabilities))
{
http.NullableParams.Add(pair.Key, new NullableString { Value = pair.Value.ToString() });
}
else
{
http.Params.Add(pair.Key, pair.Value.ToString());
}
}
}
}
// parse ClaimsPrincipal if exists
if (request.HttpContext?.User?.Identities != null)
{
logger.LogTrace("HttpContext has ClaimsPrincipal; parsing to gRPC.");
foreach (var id in request.HttpContext.User.Identities)
{
var rpcClaimsIdentity = new RpcClaimsIdentity();
if (id.AuthenticationType != null)
{
rpcClaimsIdentity.AuthenticationType = new NullableString { Value = id.AuthenticationType };
}
if (id.NameClaimType != null)
{
rpcClaimsIdentity.NameClaimType = new NullableString { Value = id.NameClaimType };
}
if (id.RoleClaimType != null)
{
rpcClaimsIdentity.RoleClaimType = new NullableString { Value = id.RoleClaimType };
}
foreach (var claim in id.Claims)
{
if (claim.Type != null && claim.Value != null)
{
rpcClaimsIdentity.Claims.Add(new RpcClaim { Value = claim.Value, Type = claim.Type });
}
}
http.Identities.Add(rpcClaimsIdentity);
}
}
// parse request body as content-type
if (request.Body != null && request.ContentLength > 0)
{
if (IsBodyOnlySupported(capabilities))
{
await PopulateBody(request, http, capabilities, logger);
}
else
{
await PopulateBodyAndRawBody(request, http, capabilities, logger);
}
}
return typedData;
}
private static async Task PopulateBody(HttpRequest request, RpcHttp http, GrpcCapabilities capabilities, ILogger logger)
{
object body = null;
if (request.IsMediaTypeOctetOrMultipart() || IsRawBodyBytesRequested(capabilities))
{
body = await request.GetRequestBodyAsBytesAsync();
}
else
{
body = await request.ReadAsStringAsync();
}
http.Body = await body.ToRpc(logger, capabilities);
}
private static async Task PopulateBodyAndRawBody(HttpRequest request, RpcHttp http, GrpcCapabilities capabilities, ILogger logger)
{
object body = null;
string rawBodyString = null;
if (MediaTypeHeaderValue.TryParse(request.ContentType, out MediaTypeHeaderValue mediaType))
{
if (string.Equals(mediaType.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
{
rawBodyString = await request.ReadAsStringAsync();
try
{
// REVIEW: We are json deserializing this to a JObject only to serialize
// it back to string below. Why?
body = JsonConvert.DeserializeObject(rawBodyString);
}
catch (JsonException)
{
body = rawBodyString;
}
}
else if (request.IsMediaTypeOctetOrMultipart())
{
byte[] bytes = await request.GetRequestBodyAsBytesAsync();
body = bytes;
if (!IsRawBodyBytesRequested(capabilities))
{
rawBodyString = Encoding.UTF8.GetString(bytes);
}
}
}
// default if content-tye not found or recognized
if (body == null && rawBodyString == null)
{
body = rawBodyString = await request.ReadAsStringAsync();
}
http.Body = await body.ToRpc(logger, capabilities);
if (IsRawBodyBytesRequested(capabilities))
{
byte[] bytes = await request.GetRequestBodyAsBytesAsync();
http.RawBody = await bytes.ToRpc(logger, capabilities);
}
else
{
http.RawBody = await rawBodyString.ToRpc(logger, capabilities);
}
}
internal static TypedData ToRpcDefault(this object value)
{
// attempt POCO / array of pocos
TypedData typedData = new TypedData();
try
{
typedData.Json = JsonConvert.SerializeObject(value, Formatting.None);
}
catch
{
typedData.String = value.ToString();
}
return typedData;
}
internal static TypedData ToRpcByteArray(this byte[][] arrBytes)
{
TypedData typedData = new TypedData();
CollectionBytes collectionBytes = new CollectionBytes();
foreach (byte[] element in arrBytes)
{
if (element != null)
{
collectionBytes.Bytes.Add(ByteString.CopyFrom(element));
}
}
typedData.CollectionBytes = collectionBytes;
return typedData;
}
internal static TypedData ToRpcStringArray(this string[] arrString, bool includeEmptyEntries)
{
TypedData typedData = new TypedData();
CollectionString collectionString = new CollectionString();
foreach (string element in arrString)
{
// Empty string/null entries are okay to add based on includeEmptyEntries param value.
if (string.IsNullOrEmpty(element) && !includeEmptyEntries)
{
continue;
}
// Convert null entries to emptyEntry because "Add" method doesn't support null (will throw)
collectionString.String.Add(element ?? string.Empty);
}
typedData.CollectionString = collectionString;
return typedData;
}
internal static TypedData ToRpcDoubleArray(this double[] arrDouble)
{
TypedData typedData = new TypedData();
CollectionDouble collectionDouble = new CollectionDouble();
foreach (double element in arrDouble)
{
collectionDouble.Double.Add(element);
}
typedData.CollectionDouble = collectionDouble;
return typedData;
}
internal static TypedData ToRpcLongArray(this long[] arrLong)
{
TypedData typedData = new TypedData();
CollectionSInt64 collectionLong = new CollectionSInt64();
foreach (long element in arrLong)
{
collectionLong.Sint64.Add(element);
}
typedData.CollectionSint64 = collectionLong;
return typedData;
}
private static bool ShouldIncludeEmptyEntriesInMessagePayload(GrpcCapabilities capabilities)
{
return !string.IsNullOrWhiteSpace(capabilities.GetCapabilityState(RpcWorkerConstants.IncludeEmptyEntriesInMessagePayload));
}
private static bool IsRawBodyBytesRequested(GrpcCapabilities capabilities)
{
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.RawHttpBodyBytes));
}
private static bool IsBodyOnlySupported(GrpcCapabilities capabilities)
{
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.RpcHttpBodyOnly));
}
private static bool IsTypedDataCollectionSupported(GrpcCapabilities capabilities)
{
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.TypedDataCollection));
}
private static bool ShouldIgnoreEmptyHeaderValues(GrpcCapabilities capabilities)
{
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.IgnoreEmptyValuedRpcHttpHeaders));
}
private static bool ShouldUseNullableValueDictionary(GrpcCapabilities capabilities)
{
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.UseNullableValueDictionaryForHttp));
}
public static BindingInfo ToBindingInfo(this BindingMetadata bindingMetadata)
{
BindingInfo bindingInfo = new BindingInfo
{
Direction = (BindingInfo.Types.Direction)bindingMetadata.Direction,
Type = bindingMetadata.Type
};
if (bindingMetadata.DataType != null)
{
bindingInfo.DataType = (BindingInfo.Types.DataType)bindingMetadata.DataType;
}
return bindingInfo;
}
}
}