11
11
using System . Web . Http ;
12
12
using System . Web . Http . Controllers ;
13
13
using System . Web . Http . Dependencies ;
14
+ using Microsoft . Azure . AppService . Proxy . Client . Contract ;
14
15
using Microsoft . Azure . WebJobs . Extensions . Http ;
15
16
using Microsoft . Azure . WebJobs . Script . Description ;
17
+ using Microsoft . Azure . WebJobs . Script . Host ;
16
18
using Microsoft . Azure . WebJobs . Script . WebHost . Filters ;
17
19
using Microsoft . Azure . WebJobs . Script . WebHost . Properties ;
18
20
using Microsoft . Azure . WebJobs . Script . WebHost . WebHooks ;
@@ -33,110 +35,32 @@ public FunctionsController(WebScriptHostManager scriptHostManager, WebHookReceiv
33
35
_webHookReceiverManager = webHookReceiverManager ;
34
36
}
35
37
36
- private static bool IsHomepageDisabled
37
- {
38
- get
39
- {
40
- return string . Equals ( Environment . GetEnvironmentVariable ( EnvironmentSettingNames . AzureWebJobsDisableHomepage ) ,
41
- bool . TrueString , StringComparison . OrdinalIgnoreCase ) ;
42
- }
43
- }
44
-
45
38
public override async Task < HttpResponseMessage > ExecuteAsync ( HttpControllerContext controllerContext , CancellationToken cancellationToken )
46
39
{
47
40
var request = controllerContext . Request ;
48
41
var function = _scriptHostManager . GetHttpFunctionOrNull ( request ) ;
49
- if ( function == null )
50
- {
51
- if ( request . RequestUri . AbsolutePath == "/" )
52
- {
53
- // if the request is to the root and we can't find any matching FunctionDescriptors which might have been setup by proxies
54
- // then homepage logic will be applied.
55
- return ( IsHomepageDisabled || request . IsAntaresInternalRequest ( ) )
56
- ? new HttpResponseMessage ( HttpStatusCode . NoContent )
57
- : new HttpResponseMessage ( HttpStatusCode . OK )
58
- {
59
- Content = new StringContent ( Resources . Homepage , Encoding . UTF8 , "text/html" )
60
- } ;
61
- }
62
42
63
- // request does not map to an HTTP function
64
- return new HttpResponseMessage ( HttpStatusCode . NotFound ) ;
65
- }
66
-
67
- request . SetProperty ( ScriptConstants . AzureFunctionsHttpFunctionKey , function ) ;
43
+ var secretManager = controllerContext . Configuration . DependencyResolver . GetService < ISecretManager > ( ) ;
44
+ var functionRequestInvoker = new FunctionRequestInvoker ( function , secretManager ) ;
45
+ var response = await functionRequestInvoker . PreprocessRequestAsync ( request ) ;
68
46
69
- var authorizationLevel = await DetermineAuthorizationLevelAsync ( request , function , controllerContext . Configuration . DependencyResolver ) ;
70
- if ( function . Metadata . IsExcluded ||
71
- ( function . Metadata . IsDisabled && ! ( request . IsAuthDisabled ( ) || authorizationLevel == AuthorizationLevel . Admin ) ) )
47
+ if ( response != null )
72
48
{
73
- // disabled functions are not publicly addressable w/o Admin level auth,
74
- // and excluded functions are also ignored here (though the check above will
75
- // already exclude them)
76
- return new HttpResponseMessage ( HttpStatusCode . NotFound ) ;
49
+ return response ;
77
50
}
78
51
79
52
Func < HttpRequestMessage , CancellationToken , Task < HttpResponseMessage > > processRequestHandler = async ( req , ct ) =>
80
53
{
81
- return await ProcessRequestAsync ( req , function , ct ) ;
54
+ return await functionRequestInvoker . ProcessRequestAsync ( req , ct , _scriptHostManager , _webHookReceiverManager ) ;
82
55
} ;
83
- return await _scriptHostManager . HttpRequestManager . ProcessRequestAsync ( request , processRequestHandler , cancellationToken ) ;
84
- }
85
56
86
- public static async Task < AuthorizationLevel > DetermineAuthorizationLevelAsync ( HttpRequestMessage request , FunctionDescriptor function , IDependencyResolver resolver )
87
- {
88
- var secretManager = resolver . GetService < ISecretManager > ( ) ;
89
- var authorizationResult = await AuthorizationLevelAttribute . GetAuthorizationResultAsync ( request , secretManager , functionName : function . Name ) ;
90
- var authorizationLevel = authorizationResult . AuthorizationLevel ;
91
- request . SetAuthorizationLevel ( authorizationLevel ) ;
92
- request . SetProperty ( ScriptConstants . AzureFunctionsHttpRequestKeyNameKey , authorizationResult . KeyName ) ;
93
-
94
- return authorizationLevel ;
95
- }
96
-
97
- private async Task < HttpResponseMessage > ProcessRequestAsync ( HttpRequestMessage request , FunctionDescriptor function , CancellationToken cancellationToken )
98
- {
99
- var httpTrigger = function . GetTriggerAttributeOrNull < HttpTriggerAttribute > ( ) ;
100
- bool isWebHook = ! string . IsNullOrEmpty ( httpTrigger . WebHookType ) ;
101
- var authorizationLevel = request . GetAuthorizationLevel ( ) ;
102
- HttpResponseMessage response = null ;
103
-
104
- if ( isWebHook )
57
+ if ( function . Metadata . IsProxy )
105
58
{
106
- if ( request . HasAuthorizationLevel ( AuthorizationLevel . Admin ) )
107
- {
108
- // Admin level requests bypass the WebHook auth pipeline
109
- response = await _scriptHostManager . HandleRequestAsync ( function , request , cancellationToken ) ;
110
- }
111
- else
112
- {
113
- // This is a WebHook request so define a delegate for the user function.
114
- // The WebHook Receiver pipeline will first validate the request fully
115
- // then invoke this callback.
116
- Func < HttpRequestMessage , Task < HttpResponseMessage > > invokeFunction = async ( req ) =>
117
- {
118
- // Reset the content stream before passing the request down to the function
119
- Stream stream = await req . Content . ReadAsStreamAsync ( ) ;
120
- stream . Seek ( 0 , SeekOrigin . Begin ) ;
121
-
122
- return await _scriptHostManager . HandleRequestAsync ( function , req , cancellationToken ) ;
123
- } ;
124
- response = await _webHookReceiverManager . HandleRequestAsync ( function , request , invokeFunction ) ;
125
- }
59
+ IFuncExecutor proxyFunctionExecutor = new ProxyFunctionExecutor ( this . _scriptHostManager , _webHookReceiverManager , secretManager ) ;
60
+ request . Properties . Add ( ScriptConstants . AzureProxyFunctionExecutorKey , proxyFunctionExecutor ) ;
126
61
}
127
- else
128
- {
129
- // Authorize
130
- if ( ! request . HasAuthorizationLevel ( httpTrigger . AuthLevel ) )
131
- {
132
- return new HttpResponseMessage ( HttpStatusCode . Unauthorized ) ;
133
- }
134
62
135
- // Not a WebHook request so dispatch directly
136
- response = await _scriptHostManager . HandleRequestAsync ( function , request , cancellationToken ) ;
137
- }
138
-
139
- return response ;
63
+ return await _scriptHostManager . HttpRequestManager . ProcessRequestAsync ( request , processRequestHandler , cancellationToken ) ;
140
64
}
141
65
}
142
66
}
0 commit comments