3
3
using System ;
4
4
using System . Collections . Generic ;
5
5
using System . Diagnostics ;
6
- using System . IO ;
7
6
using System . Threading ;
8
7
using System . Threading . Tasks ;
9
8
using Microsoft . AspNetCore . Authorization ;
@@ -36,26 +35,23 @@ public async Task Invoke(HttpContext context, WebScriptHostManager manager)
36
35
{
37
36
// flow required context through the request pipeline
38
37
// downstream middleware and filters rely on this
39
- context . Items . Add ( ScriptConstants . AzureFunctionsHostManagerKey , manager ) ;
38
+ context . Items [ ScriptConstants . AzureFunctionsHostManagerKey ] = manager ;
40
39
SetRequestId ( context . Request ) ;
40
+
41
41
if ( _next != null )
42
42
{
43
43
await _next ( context ) ;
44
44
}
45
45
46
46
IFunctionExecutionFeature functionExecution = context . Features . Get < IFunctionExecutionFeature > ( ) ;
47
-
48
- int nestedProxiesCount = GetNestedProxiesCount ( context , functionExecution ) ;
49
-
50
- IActionResult result = null ;
51
-
52
47
if ( functionExecution != null && ! context . Response . HasStarted )
53
48
{
54
- result = await GetResultAsync ( context , functionExecution ) ;
55
-
49
+ int nestedProxiesCount = GetNestedProxiesCount ( context , functionExecution ) ;
50
+ IActionResult result = await GetResultAsync ( context , functionExecution ) ;
56
51
if ( nestedProxiesCount > 0 )
57
52
{
58
- // if Proxy, the rest of the pipleline will be processed bt Proxies in case there are response overrides and what not.
53
+ // if Proxy, the rest of the pipleline will be processed by Proxies in
54
+ // case there are response overrides and what not.
59
55
SetProxyResult ( context , nestedProxiesCount , result ) ;
60
56
return ;
61
57
}
@@ -71,18 +67,17 @@ public async Task Invoke(HttpContext context, WebScriptHostManager manager)
71
67
72
68
private static void SetProxyResult ( HttpContext context , int nestedProxiesCount , IActionResult result )
73
69
{
74
- context . Items . Add ( ScriptConstants . AzureFunctionsProxyResult , result ) ;
75
-
70
+ context . Items [ ScriptConstants . AzureFunctionsProxyResult ] = result ;
76
71
context . Items [ ScriptConstants . AzureFunctionsNestedProxyCount ] = nestedProxiesCount - 1 ;
77
72
}
78
73
79
74
private static int GetNestedProxiesCount ( HttpContext context , IFunctionExecutionFeature functionExecution )
80
75
{
81
76
context . Items . TryGetValue ( ScriptConstants . AzureFunctionsNestedProxyCount , out object nestedProxiesCount ) ;
82
77
83
- //HttpBufferingService is disabled for non - proxy functions.
84
78
if ( functionExecution != null && ! functionExecution . Descriptor . Metadata . IsProxy && nestedProxiesCount == null )
85
79
{
80
+ // HttpBufferingService is disabled for non-proxy functions.
86
81
var bufferingFeature = context . Features . Get < IHttpBufferingFeature > ( ) ;
87
82
bufferingFeature ? . DisableRequestBuffering ( ) ;
88
83
bufferingFeature ? . DisableResponseBuffering ( ) ;
@@ -103,21 +98,21 @@ private async Task<IActionResult> GetResultAsync(HttpContext context, IFunctionE
103
98
return new NotFoundResult ( ) ;
104
99
}
105
100
106
- if ( context . Request . IsColdStart ( ) )
101
+ if ( context . Request . IsColdStart ( ) && ! context . Items . ContainsKey ( ScriptConstants . AzureFunctionsColdStartKey ) )
107
102
{
108
103
// for cold start requests we want to measure the request
109
104
// pipeline dispatch time
110
105
// important that this stopwatch is started as early as possible
111
106
// in the pipeline (in this case, in our first middleware)
112
107
var sw = new Stopwatch ( ) ;
113
108
sw . Start ( ) ;
114
- context . Request . HttpContext . Items . Add ( ScriptConstants . AzureFunctionsColdStartKey , sw ) ;
109
+ context . Items [ ScriptConstants . AzureFunctionsColdStartKey ] = sw ;
115
110
}
116
111
117
112
// Add route data to request info
118
113
// TODO: Keeping this here for now as other code depend on this property, but this can be done in the HTTP binding.
119
114
var routingFeature = context . Features . Get < IRoutingFeature > ( ) ;
120
- context . Items . Add ( HttpExtensionConstants . AzureWebJobsHttpRouteDataKey , new Dictionary < string , object > ( routingFeature . RouteData . Values ) ) ;
115
+ context . Items [ HttpExtensionConstants . AzureWebJobsHttpRouteDataKey ] = new Dictionary < string , object > ( routingFeature . RouteData . Values ) ;
121
116
122
117
bool authorized = await AuthenticateAndAuthorizeAsync ( context , functionExecution . Descriptor ) ;
123
118
if ( ! authorized )
0 commit comments