forked from NancyFx/Nancy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
NancyEngine.cs
315 lines (258 loc) · 11.6 KB
/
NancyEngine.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
namespace Nancy
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Nancy.Bootstrapper;
using Cookies;
using Diagnostics;
using ErrorHandling;
using Routing;
using Helpers;
using Nancy.Configuration;
using Responses.Negotiation;
/// <summary>
/// Default engine for handling Nancy <see cref="Request"/>s.
/// </summary>
public class NancyEngine : INancyEngine
{
public const string ERROR_KEY = "ERROR_TRACE";
public const string ERROR_EXCEPTION = "ERROR_EXCEPTION";
private readonly IRequestDispatcher dispatcher;
private readonly INancyContextFactory contextFactory;
private readonly IRequestTracing requestTracing;
private readonly IReadOnlyCollection<IStatusCodeHandler> statusCodeHandlers;
private readonly IStaticContentProvider staticContentProvider;
private readonly IResponseNegotiator negotiator;
private readonly CancellationTokenSource engineDisposedCts;
private readonly TraceConfiguration traceConfiguration;
/// <summary>
/// Initializes a new instance of the <see cref="NancyEngine"/> class.
/// </summary>
/// <param name="dispatcher">An <see cref="IRouteResolver"/> instance that will be used to resolve a route, from the modules, that matches the incoming <see cref="Request"/>.</param>
/// <param name="contextFactory">A factory for creating contexts</param>
/// <param name="statusCodeHandlers">Error handlers</param>
/// <param name="requestTracing">The request tracing instance.</param>
/// <param name="staticContentProvider">The provider to use for serving static content</param>
/// <param name="negotiator">The response negotiator.</param>
/// <param name="environment">An <see cref="INancyEnvironment"/> instance.</param>
public NancyEngine(IRequestDispatcher dispatcher,
INancyContextFactory contextFactory,
IEnumerable<IStatusCodeHandler> statusCodeHandlers,
IRequestTracing requestTracing,
IStaticContentProvider staticContentProvider,
IResponseNegotiator negotiator,
INancyEnvironment environment)
{
if (dispatcher == null)
{
throw new ArgumentNullException("dispatcher", "The resolver parameter cannot be null.");
}
if (contextFactory == null)
{
throw new ArgumentNullException("contextFactory");
}
if (statusCodeHandlers == null)
{
throw new ArgumentNullException("statusCodeHandlers");
}
if (requestTracing == null)
{
throw new ArgumentNullException("requestTracing");
}
if (staticContentProvider == null)
{
throw new ArgumentNullException("staticContentProvider");
}
if (negotiator == null)
{
throw new ArgumentNullException("negotiator");
}
this.dispatcher = dispatcher;
this.contextFactory = contextFactory;
this.statusCodeHandlers = statusCodeHandlers.ToArray();
this.requestTracing = requestTracing;
this.staticContentProvider = staticContentProvider;
this.negotiator = negotiator;
this.engineDisposedCts = new CancellationTokenSource();
this.traceConfiguration = environment.GetValue<TraceConfiguration>();
}
/// <summary>
/// Factory for creating an <see cref="IPipelines"/> instance for a incoming request.
/// </summary>
/// <value>An <see cref="IPipelines"/> instance.</value>
public Func<NancyContext, IPipelines> RequestPipelinesFactory { get; set; }
/// <summary>
/// Handles an incoming <see cref="Request"/> async.
/// </summary>
/// <param name="request">An <see cref="Request"/> instance, containing the information about the current request.</param>
/// <param name="preRequest">Delegate to call before the request is processed</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public async Task<NancyContext> HandleRequest(Request request, Func<NancyContext, NancyContext> preRequest, CancellationToken cancellationToken)
{
using (var cts = CancellationTokenSource.CreateLinkedTokenSource(this.engineDisposedCts.Token, cancellationToken))
{
cts.Token.ThrowIfCancellationRequested();
if (request == null)
{
throw new ArgumentNullException("request", "The request parameter cannot be null.");
}
var context = this.contextFactory.Create(request);
if (preRequest != null)
{
context = preRequest(context);
}
var staticContentResponse = this.staticContentProvider.GetContent(context);
if (staticContentResponse != null)
{
context.Response = staticContentResponse;
return context;
}
var pipelines = this.RequestPipelinesFactory.Invoke(context);
var nancyContext = await this.InvokeRequestLifeCycle(context, cts.Token, pipelines)
.ConfigureAwait(false);
this.CheckStatusCodeHandler(nancyContext);
this.SaveTraceInformation(nancyContext);
return nancyContext;
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
this.engineDisposedCts.Cancel();
}
private void SaveTraceInformation(NancyContext ctx)
{
if (!this.EnableTracing(ctx))
{
return;
}
if (ctx.Request == null || ctx.Response == null)
{
return;
}
var sessionGuid = this.GetDiagnosticsSessionGuid(ctx);
ctx.Trace.RequestData = ctx.Request;
ctx.Trace.ResponseData = ctx.Response;
this.requestTracing.AddRequestDiagnosticToSession(sessionGuid, ctx);
this.UpdateTraceCookie(ctx, sessionGuid);
}
private bool EnableTracing(NancyContext ctx)
{
return this.traceConfiguration.Enabled && !ctx.Items.ContainsKey(DiagnosticsHook.ItemsKey);
}
private Guid GetDiagnosticsSessionGuid(NancyContext ctx)
{
string sessionId;
if (!ctx.Request.Cookies.TryGetValue("__NCTRACE", out sessionId))
{
return this.requestTracing.CreateSession();
}
Guid sessionGuid;
if (!Guid.TryParse(sessionId, out sessionGuid))
{
return this.requestTracing.CreateSession();
}
if (!this.requestTracing.IsValidSessionId(sessionGuid))
{
return this.requestTracing.CreateSession();
}
return sessionGuid;
}
private void UpdateTraceCookie(NancyContext ctx, Guid sessionGuid)
{
var cookie = new NancyCookie("__NCTRACE", sessionGuid.ToString(), true)
{
Expires = DateTime.Now.AddMinutes(30)
};
ctx.Response = ctx.Response.WithCookie(cookie);
}
private void CheckStatusCodeHandler(NancyContext context)
{
if (context.Response == null)
{
return;
}
IStatusCodeHandler defaultHandler = null;
IStatusCodeHandler customHandler = null;
foreach (var statusCodeHandler in this.statusCodeHandlers)
{
if (!statusCodeHandler.HandlesStatusCode(context.Response.StatusCode, context))
{
continue;
}
if (defaultHandler == null && (statusCodeHandler is DefaultStatusCodeHandler))
{
defaultHandler = statusCodeHandler;
continue;
}
if (customHandler == null && !(statusCodeHandler is DefaultStatusCodeHandler))
{
customHandler = statusCodeHandler;
continue;
}
if ((defaultHandler != null) && (customHandler != null))
{
break;
}
}
var handler = customHandler ?? defaultHandler;
if (handler == null)
{
return;
}
handler.Handle(context.Response.StatusCode, context);
}
private async Task<NancyContext> InvokeRequestLifeCycle(NancyContext context, CancellationToken cancellationToken, IPipelines pipelines)
{
try
{
var response = await InvokePreRequestHook(context, cancellationToken, pipelines.BeforeRequest).ConfigureAwait(false) ??
await this.dispatcher.Dispatch(context, cancellationToken).ConfigureAwait(false);
context.Response = response;
await this.InvokePostRequestHook(context, cancellationToken, pipelines.AfterRequest).ConfigureAwait(false);
await response.PreExecute(context).ConfigureAwait(false);
}
catch (Exception ex)
{
this.InvokeOnErrorHook(context, pipelines.OnError, ex);
}
return context;
}
private static Task<Response> InvokePreRequestHook(NancyContext context, CancellationToken cancellationToken, BeforePipeline pipeline)
{
return pipeline == null ? Task.FromResult<Response>(null) : pipeline.Invoke(context, cancellationToken);
}
private Task InvokePostRequestHook(NancyContext context, CancellationToken cancellationToken, AfterPipeline pipeline)
{
return pipeline == null ? TaskHelpers.CompletedTask : pipeline.Invoke(context, cancellationToken);
}
private void InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)
{
try
{
if (pipeline == null)
{
throw new RequestExecutionException(ex);
}
var onErrorResult = pipeline.Invoke(context, ex);
if (onErrorResult == null)
{
throw new RequestExecutionException(ex);
}
context.Response = this.negotiator.NegotiateResponse(onErrorResult, context);
}
catch (Exception e)
{
context.Response = new Response { StatusCode = HttpStatusCode.InternalServerError };
context.Items[ERROR_KEY] = e.ToString();
context.Items[ERROR_EXCEPTION] = e;
}
}
}
}