-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNancyMiddleware.cs
executable file
·166 lines (135 loc) · 6.88 KB
/
NancyMiddleware.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
using Microsoft.AspNetCore.Http;
using Nancy.Helpers;
using Nancy.IO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace Nancy.AspNetCore {
public class NancyMiddleware {
public const string AspNetCoreEnvironmentKey = "ASPNETCORE_ENVIRONMENT";
public const string NancyEnvironmentKey = "NANCY_ENVIRONMENT";
private readonly RequestDelegate _next;
private NancyOptions _options;
private INancyEngine _engine;
public NancyMiddleware(RequestDelegate next, NancyOptions options) {
_next = next;
_options = options;
_options.Bootstrapper.Initialise();
_engine = _options.Bootstrapper.GetEngine();
}
public async Task Invoke(HttpContext environment) {
var aspnetCoreRequestMethod = environment.Request.Method;
var aspnetCoreRequestScheme = environment.Request.Scheme;
var aspnetCoreRequestHeaders = environment.Request.Headers;
var aspnetCoreRequestPathBase = environment.Request.PathBase;
var aspnetCoreRequestPath = environment.Request.Path;
var aspnetCoreRequestQueryString = environment.Request.QueryString.Value ?? string.Empty;
var aspnetCoreRequestBody = environment.Request.Body;
var aspnetCoreRequestProtocol = environment.Request.Protocol;
var aspnetCoreCallCancelled = environment.RequestAborted;
var aspnetCoreRequestHost = environment.Request.Host.Value ?? Dns.GetHostName();
var aspnetCoreUser = environment.User;
X509Certificate2 certificate = null;
if (_options.EnableClientCertificates) {
var clientCertificate = new X509Certificate2(environment.Connection.ClientCertificate.Export(X509ContentType.Cert));
certificate = clientCertificate ?? null;
}
var serverClientIp = environment.Connection.RemoteIpAddress.ToString();
var url = CreateUrl(aspnetCoreRequestHost, aspnetCoreRequestScheme, aspnetCoreRequestPathBase, aspnetCoreRequestPath, aspnetCoreRequestQueryString);
var nancyRequestStream = new RequestStream(aspnetCoreRequestBody, ExpectedLength(aspnetCoreRequestHeaders), StaticConfiguration.DisableRequestStreamSwitching ?? false);
var nancyRequest = new Request(
aspnetCoreRequestMethod,
url,
nancyRequestStream,
aspnetCoreRequestHeaders.ToDictionary(kv => kv.Key, kv => (IEnumerable<string>)kv.Value, StringComparer.OrdinalIgnoreCase),
serverClientIp,
certificate,
aspnetCoreRequestProtocol);
var nancyContext = await _engine.HandleRequest(
nancyRequest,
StoreEnvironment(environment, aspnetCoreUser),
aspnetCoreCallCancelled).ConfigureAwait(false);
await RequestComplete(nancyContext, environment, _options.PerformPassThrough, _next).ConfigureAwait(false);
}
private static Task RequestComplete(
NancyContext context,
HttpContext environment,
Func<NancyContext, bool> performPassThrough,
RequestDelegate next) {
var aspnetCoreResponseHeaders = environment.Response.Headers;
var aspnetCoreResponseBody = environment.Response.Body;
var nancyResponse = context.Response;
if (!performPassThrough(context)) {
environment.Response.StatusCode = (int)nancyResponse.StatusCode;
if (nancyResponse.ReasonPhrase != null) {
environment.Response.Headers.Add("ReasonPhrase", nancyResponse.ReasonPhrase);
}
foreach (var responseHeader in nancyResponse.Headers) {
aspnetCoreResponseHeaders[responseHeader.Key] = new[] { responseHeader.Value };
}
if (!string.IsNullOrWhiteSpace(nancyResponse.ContentType)) {
aspnetCoreResponseHeaders["Content-Type"] = new[] { nancyResponse.ContentType };
}
if (nancyResponse.Cookies != null && nancyResponse.Cookies.Count != 0) {
const string setCookieHeaderKey = "Set-Cookie";
string[] setCookieHeader = aspnetCoreResponseHeaders.ContainsKey(setCookieHeaderKey)
? aspnetCoreResponseHeaders[setCookieHeaderKey].ToArray()
: ArrayCache.Empty<string>();
aspnetCoreResponseHeaders[setCookieHeaderKey] = setCookieHeader
.Concat(nancyResponse.Cookies.Select(cookie => cookie.ToString()))
.ToArray();
}
nancyResponse.Contents(aspnetCoreResponseBody);
} else {
return next(environment);
}
context.Dispose();
return TaskHelpers.CompletedTask;
}
private static long ExpectedLength(IHeaderDictionary headers) {
var header = headers["Content-Length"];
if (string.IsNullOrWhiteSpace(header))
return 0;
int contentLength;
return int.TryParse(header, NumberStyles.Any, CultureInfo.InvariantCulture, out contentLength) ? contentLength : 0;
}
private static Url CreateUrl(
string aspnetCoreRequestHost,
string aspnetCoreRequestScheme,
string aspnetCoreRequestPathBase,
string aspnetCoreRequestPath,
string aspnetCoreRequestQueryString) {
int? port = null;
var hostnameParts = aspnetCoreRequestHost.Split(':');
if (hostnameParts.Length == 2) {
aspnetCoreRequestHost = hostnameParts[0];
int tempPort;
if (int.TryParse(hostnameParts[1], out tempPort)) {
port = tempPort;
}
}
var url = new Url {
Scheme = aspnetCoreRequestScheme,
HostName = aspnetCoreRequestHost,
Port = port,
BasePath = aspnetCoreRequestPathBase,
Path = aspnetCoreRequestPath,
Query = aspnetCoreRequestQueryString,
};
return url;
}
private static Func<NancyContext, NancyContext> StoreEnvironment(HttpContext environment, ClaimsPrincipal user) {
return context => {
context.CurrentUser = user;
environment.Items[NancyEnvironmentKey] = context;
context.Items[AspNetCoreEnvironmentKey] = environment;
return context;
};
}
}
}