-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMiddleware.cs
More file actions
94 lines (85 loc) · 3.45 KB
/
Middleware.cs
File metadata and controls
94 lines (85 loc) · 3.45 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
using Fnproject.Fn.Fdk.Context;
using Fnproject.Fn.Fdk.Coercion;
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Threading.Tasks;
namespace Fnproject.Fn.Fdk
{
internal class Middleware
{
private readonly RequestDelegate _next;
public IHeaderDictionary ResponseHeaders { get; } = new HeaderDictionary();
public Middleware(RequestDelegate next)
{
_next = next;
}
private object prepareFunctionContext(IHTTPContext ctx)
{
if (Function.ContextType == typeof(IRuntimeContext))
{
return ctx.RuntimeContext();
}
return ctx;
}
private object[] prepareArgs(IHTTPContext ctx, string requestBody)
{
var parameters = Function.Method.GetParameters();
object[] args;
switch (parameters.Length)
{
case 0:
args = new object[0];
break;
case 1:
args = new object[1];
if (Function.ContextParameterIndex != -1)
{
args[0] = prepareFunctionContext(ctx);
}
else
{
args[0] = InputCoercion.Coerce(requestBody, parameters[0].ParameterType);
}
break;
default:
args = new object[2];
args[Function.ContextParameterIndex] = prepareFunctionContext(ctx);
args[Function.DataParameterIndex] = InputCoercion.Coerce(requestBody,
parameters[Function.DataParameterIndex].ParameterType);
break;
}
return args;
}
public async Task InvokeAsync(HttpContext context)
{
try
{
RuntimeContext runtimeContext = new RuntimeContext(context.Request.Headers);
HTTPContext httpContext = new HTTPContext(runtimeContext,
context.Request.Headers, context.Request.Query);
StreamReader reader = new StreamReader(context.Request.Body, encoding: System.Text.Encoding.UTF8);
var rawBodyString = await reader.ReadToEndAsync();
context.Request.Body.Close();
object[] args = prepareArgs(httpContext, rawBodyString);
object result = Function.Invoke(args);
string responseBodyString = OutputCoercion.Coerce(result,
result.GetType());
foreach (var entry in httpContext.ResponseHeaders())
context.Response.Headers[entry.Key] = entry.Value;
await context.Response.WriteAsync(responseBodyString);
}
catch (Exception e)
{
context.Response.Headers[Constants.FN_FDK_RUNTIME_HEADER] =
String.Format("dotnet/{0}", System.Environment.Version.ToString());
context.Response.Headers[Constants.FN_FDK_RUNTIME_HEADER] =
String.Format("fdk-dotnet/{0}", Version.Value);
context.Response.Headers[Constants.FN_HTTP_STATUS_HEADER] = 502.ToString();
context.Response.StatusCode = StatusCodes.Status502BadGateway;
await context.Response.WriteAsync(string.Empty);
throw e;
}
}
}
}