-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathInvokeFeatures.cs
More file actions
396 lines (315 loc) · 12.4 KB
/
Copy pathInvokeFeatures.cs
File metadata and controls
396 lines (315 loc) · 12.4 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipelines;
using System.Runtime.CompilerServices;
using System.Net;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Features.Authentication;
#pragma warning disable 1591
namespace Amazon.Lambda.AspNetCoreServer.Internal
{
public class InvokeFeatures : IFeatureCollection,
IItemsFeature,
IHttpAuthenticationFeature,
IHttpRequestFeature,
IHttpResponseFeature,
IHttpConnectionFeature,
IServiceProvidersFeature,
ITlsConnectionFeature,
IHttpRequestIdentifierFeature,
IHttpResponseBodyFeature
,IHttpRequestBodyDetectionFeature
,IHttpActivityFeature
/*
,
IHttpUpgradeFeature,
IHttpRequestLifetimeFeature*/
{
private volatile int _containerRevision;
public InvokeFeatures()
{
this[typeof(IItemsFeature)] = this;
this[typeof(IHttpAuthenticationFeature)] = this;
this[typeof(IHttpRequestFeature)] = this;
this[typeof(IHttpResponseFeature)] = this;
this[typeof(IHttpConnectionFeature)] = this;
this[typeof(IServiceProvidersFeature)] = this;
this[typeof(ITlsConnectionFeature)] = this;
this[typeof(IHttpResponseBodyFeature)] = this;
this[typeof(IHttpRequestIdentifierFeature)] = this;
this[typeof(IHttpRequestBodyDetectionFeature)] = this;
this[typeof(IHttpActivityFeature)] = this;
}
#region IFeatureCollection
public bool IsReadOnly => false;
IDictionary<Type, object> _features = new Dictionary<Type, object>();
public int Revision => _containerRevision;
public object this[Type key]
{
get
{
object feature;
if (_features.TryGetValue(key, out feature))
{
return feature;
}
return null;
}
set
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
if (_features != null && _features.Remove(key))
{
_containerRevision++;
}
return;
}
if (_features == null)
{
_features = new Dictionary<Type, object>();
}
_features[key] = value;
_containerRevision++;
}
}
public TFeature Get<TFeature>()
{
object feature;
if (_features.TryGetValue(typeof(TFeature), out feature))
{
return (TFeature)feature;
}
return default(TFeature);
}
public IEnumerator<KeyValuePair<Type, object>> GetEnumerator()
{
return _features.GetEnumerator();
}
public void Set<TFeature>(TFeature instance)
{
// Delegate to the indexer so _containerRevision is bumped, otherwise
// ASP.NET Core's FeatureReferences cache will return stale feature
// references after middleware (e.g. OutputCache, ResponseCompression)
// wraps the response body via HttpContext.Response.Body = wrapper.
this[typeof(TFeature)] = instance;
}
IEnumerator IEnumerable.GetEnumerator()
{
return _features.GetEnumerator();
}
#endregion
#region IItemsFeature
IDictionary<object, object> IItemsFeature.Items { get; set; }
#endregion
#region IHttpAuthenticationFeature
ClaimsPrincipal IHttpAuthenticationFeature.User { get; set; }
#endregion
#region IHttpRequestFeature
string IHttpRequestFeature.Protocol { get; set; }
string IHttpRequestFeature.Scheme { get; set; }
string IHttpRequestFeature.Method { get; set; }
string IHttpRequestFeature.PathBase { get; set; }
string IHttpRequestFeature.Path { get; set; }
string IHttpRequestFeature.QueryString { get; set; }
string IHttpRequestFeature.RawTarget { get; set; }
IHeaderDictionary IHttpRequestFeature.Headers { get; set; } = new HeaderDictionary();
Stream IHttpRequestFeature.Body { get; set; } = new MemoryStream();
#endregion
#region IHttpResponseFeature
int IHttpResponseFeature.StatusCode
{
get;
set;
} = 200;
string IHttpResponseFeature.ReasonPhrase
{
get;
set;
}
bool _hasStarted;
bool IHttpResponseFeature.HasStarted => _hasStarted;
IHeaderDictionary IHttpResponseFeature.Headers
{
get;
set;
} = new HeaderDictionary();
Stream IHttpResponseFeature.Body
{
get;
set;
} = new MemoryStream();
internal EventCallbacks ResponseStartingEvents { get; private set; }
void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)
{
if (ResponseStartingEvents == null)
ResponseStartingEvents = new EventCallbacks();
ResponseStartingEvents.Add(callback, state);
}
internal EventCallbacks ResponseCompletedEvents { get; private set; }
void IHttpResponseFeature.OnCompleted(Func<object, Task> callback, object state)
{
if (ResponseCompletedEvents == null)
ResponseCompletedEvents = new EventCallbacks();
ResponseCompletedEvents.Add(callback, state);
}
internal class EventCallbacks
{
readonly List<EventCallback> _callbacks = new List<EventCallback>();
internal void Add(Func<object, Task> callback, object state)
{
_callbacks.Add(new EventCallback(callback, state));
}
internal async Task ExecuteAsync()
{
foreach(var callback in _callbacks)
{
await callback.ExecuteAsync();
}
}
internal class EventCallback
{
internal EventCallback(Func<object, Task> callback, object state)
{
Callback = callback;
State = state;
}
Func<object, Task> Callback { get; }
object State { get; }
internal Task ExecuteAsync()
{
var task = Callback(State);
return task;
}
}
}
#endregion
#region IHttpResponseBodyFeature
// Disabled in case the user's ASP.NET Core application is still using the older API that set the body on the response feature instead of the new API that sets the body on the HttpResponse object.
#pragma warning disable CS0618
Stream IHttpResponseBodyFeature.Stream => ((IHttpResponseFeature)this).Body;
#pragma warning restore CS0618
private PipeWriter _pipeWriter;
PipeWriter IHttpResponseBodyFeature.Writer
{
get
{
if (_pipeWriter == null)
{
_pipeWriter = PipeWriter.Create(((IHttpResponseBodyFeature) this).Stream);
}
return _pipeWriter;
}
}
Task IHttpResponseBodyFeature.CompleteAsync()
{
return Task.CompletedTask;
}
void IHttpResponseBodyFeature.DisableBuffering()
{
}
// This code is taken from the Apache 2.0 licensed ASP.NET Core repo.
// https://github.com/aspnet/AspNetCore/blob/ab02951b37ac0cb09f8f6c3ed0280b46d89b06e0/src/Http/Http/src/SendFileFallback.cs
async Task IHttpResponseBodyFeature.SendFileAsync(
string filePath,
long offset,
long? count,
CancellationToken cancellationToken)
{
var fileInfo = new FileInfo(filePath);
if (offset < 0 || offset > fileInfo.Length)
{
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
}
if (count.HasValue &&
(count.Value < 0 || count.Value > fileInfo.Length - offset))
{
throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty);
}
cancellationToken.ThrowIfCancellationRequested();
int bufferSize = 1024 * 16;
var fileStream = new FileStream(
filePath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite,
bufferSize: bufferSize,
options: FileOptions.Asynchronous | FileOptions.SequentialScan);
using (fileStream)
{
fileStream.Seek(offset, SeekOrigin.Begin);
await Utilities.CopyToAsync(fileStream, ((IHttpResponseBodyFeature)this).Stream, count, bufferSize, cancellationToken);
}
}
Task IHttpResponseBodyFeature.StartAsync(CancellationToken cancellationToken)
{
_hasStarted = true;
return Task.CompletedTask;
}
#endregion
#region IHttpConnectionFeature
string IHttpConnectionFeature.ConnectionId { get; set; }
IPAddress IHttpConnectionFeature.RemoteIpAddress { get; set; }
IPAddress IHttpConnectionFeature.LocalIpAddress { get; set; }
int IHttpConnectionFeature.RemotePort { get; set; }
int IHttpConnectionFeature.LocalPort { get; set; }
#endregion
#region IServiceProvidersFeature
IServiceProvider IServiceProvidersFeature.RequestServices
{
get;
set;
}
#endregion
#region ITlsConnectionFeatures
public Task<X509Certificate2> GetClientCertificateAsync(CancellationToken cancellationToken)
{
return Task.FromResult(ClientCertificate);
}
public X509Certificate2 ClientCertificate { get; set; }
#endregion
#region IHttpRequestIdentifierFeature
string _traceIdentifier;
string IHttpRequestIdentifierFeature.TraceIdentifier
{
get
{
if(_traceIdentifier != null)
{
return _traceIdentifier;
}
var lambdaTraceId = Environment.GetEnvironmentVariable("_X_AMZN_TRACE_ID");
if (!string.IsNullOrEmpty(lambdaTraceId))
{
return lambdaTraceId;
}
// If there is no Lambda trace id then fallback to the trace id that ASP.NET Core would have generated.
_traceIdentifier = (new Microsoft.AspNetCore.Http.Features.HttpRequestIdentifierFeature()).TraceIdentifier;
return _traceIdentifier;
}
set { _traceIdentifier = value; }
}
#endregion
bool IHttpRequestBodyDetectionFeature.CanHaveBody
{
get
{
var requestFeature = (IHttpRequestFeature)this;
return requestFeature.Body != null && requestFeature.Body.Length > 0;
}
}
Activity IHttpActivityFeature.Activity { get; set; }
}
}