-
Notifications
You must be signed in to change notification settings - Fork 445
/
RpcMessageConversionExtensions.cs
163 lines (151 loc) · 6.33 KB
/
RpcMessageConversionExtensions.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
// 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.IO;
using System.Net.Http.Headers;
using System.Text;
using Google.Protobuf;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RpcDataType = Microsoft.Azure.WebJobs.Script.Grpc.Messages.TypedData.DataOneofCase;
namespace Microsoft.Azure.WebJobs.Script.Rpc
{
internal static class RpcMessageConversionExtensions
{
private static readonly JsonSerializerSettings _datetimeSerializerSettings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };
public static object ToObject(this TypedData typedData)
{
switch (typedData.DataCase)
{
case RpcDataType.Bytes:
case RpcDataType.Stream:
return typedData.Bytes.ToByteArray();
case RpcDataType.String:
return typedData.String;
case RpcDataType.Json:
return JsonConvert.DeserializeObject(typedData.Json, _datetimeSerializerSettings);
case RpcDataType.Http:
return Utilities.ConvertFromHttpMessageToExpando(typedData.Http);
case RpcDataType.Int:
return typedData.Int;
case RpcDataType.Double:
return typedData.Double;
case RpcDataType.None:
return null;
default:
// TODO better exception
throw new InvalidOperationException("Unknown RpcDataType");
}
}
public static TypedData ToRpc(this object value)
{
TypedData typedData = new TypedData();
if (value == null)
{
return typedData;
}
if (value is byte[] arr)
{
typedData.Bytes = ByteString.CopyFrom(arr);
}
else if (value is JObject jobj)
{
typedData.Json = jobj.ToString();
}
else if (value is string str)
{
typedData.String = str;
}
else if (value is HttpRequest request)
{
var http = new RpcHttp()
{
Url = $"{(request.IsHttps ? "https" : "http")}://{request.Host.ToString()}{request.Path.ToString()}{request.QueryString.ToString()}", // [http|https]://{url}{path}{query}
Method = request.Method.ToString()
};
typedData.Http = http;
http.RawBody = null;
foreach (var pair in request.Query)
{
if (!string.IsNullOrEmpty(pair.Value.ToString()))
{
http.Query.Add(pair.Key, pair.Value.ToString());
}
}
foreach (var pair in request.Headers)
{
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)
{
http.Params.Add(pair.Key, pair.Value.ToString());
}
}
}
// parse request body as content-type
if (request.Body != null && request.ContentLength > 0)
{
object body = null;
string rawBody = null;
MediaTypeHeaderValue mediaType = null;
if (MediaTypeHeaderValue.TryParse(request.ContentType, out mediaType))
{
if (string.Equals(mediaType.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
{
var jsonReader = new StreamReader(request.Body, Encoding.UTF8);
rawBody = jsonReader.ReadToEnd();
try
{
body = JsonConvert.DeserializeObject(rawBody);
}
catch (JsonException)
{
body = rawBody;
}
}
else if (string.Equals(mediaType.MediaType, "application/octet-stream", StringComparison.OrdinalIgnoreCase) ||
mediaType.MediaType.IndexOf("multipart/", StringComparison.OrdinalIgnoreCase) >= 0)
{
var length = Convert.ToInt32(request.ContentLength);
var bytes = new byte[length];
request.Body.Read(bytes, 0, length);
body = bytes;
rawBody = Encoding.UTF8.GetString(bytes);
}
}
// default if content-tye not found or recognized
if (body == null && rawBody == null)
{
var reader = new StreamReader(request.Body, Encoding.UTF8);
body = rawBody = reader.ReadToEnd();
}
request.Body.Position = 0;
http.Body = body.ToRpc();
http.RawBody = rawBody.ToRpc();
}
}
else
{
// attempt POCO / array of pocos
try
{
typedData.Json = JsonConvert.SerializeObject(value);
}
catch
{
typedData.String = value.ToString();
}
}
return typedData;
}
}
}