5
5
using System . Collections . Generic ;
6
6
using System . Linq ;
7
7
using System . Net . Http ;
8
- using System . Net . Http . Formatting ;
9
8
using System . Net . Sockets ;
10
9
using System . Threading ;
11
10
using System . Threading . Tasks ;
12
11
using Microsoft . AspNetCore . Http ;
13
- using Microsoft . AspNetCore . WebUtilities ;
14
12
using Microsoft . Azure . WebJobs . Script . Description ;
15
13
using Microsoft . Azure . WebJobs . Script . Extensions ;
16
14
using Microsoft . Extensions . Logging ;
@@ -31,9 +29,9 @@ public DefaultHttpWorkerService(IOptions<HttpWorkerOptions> httpWorkerOptions, I
31
29
32
30
internal DefaultHttpWorkerService ( HttpClient httpClient , IOptions < HttpWorkerOptions > httpWorkerOptions , ILogger logger )
33
31
{
34
- _httpClient = httpClient ;
35
- _httpWorkerOptions = httpWorkerOptions . Value ;
36
- _logger = logger ;
32
+ _httpClient = httpClient ?? throw new ArgumentNullException ( nameof ( httpClient ) ) ;
33
+ _httpWorkerOptions = httpWorkerOptions . Value ?? throw new ArgumentNullException ( nameof ( httpWorkerOptions . Value ) ) ;
34
+ _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
37
35
}
38
36
39
37
public Task InvokeAsync ( ScriptInvocationContext scriptInvocationContext )
@@ -69,11 +67,17 @@ internal async Task ProcessHttpInAndOutInvocationRequest(ScriptInvocationContext
69
67
70
68
try
71
69
{
72
- using ( HttpRequestMessage httpRequestMessage = CreateAndGetHttpRequestMessage ( scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId . ToString ( ) , new HttpMethod ( httpRequest . Method ) , httpRequest . GetQueryCollectionAsDictionary ( ) ) )
70
+ string uriPathValue = GetPathValue ( _httpWorkerOptions , scriptInvocationContext . FunctionMetadata . Name , httpRequest ) ;
71
+ string uri = BuildAndGetUri ( uriPathValue ) ;
72
+
73
+ using ( HttpRequestMessage httpRequestMessage = httpRequest . ToHttpRequestMessage ( uri ) )
73
74
{
74
- _logger . LogDebug ( "Sending http request message for simple httpTrigger function: '{functionName}' invocationId: '{invocationId}'" , scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId ) ;
75
+ AddHeaders ( httpRequestMessage , scriptInvocationContext . ExecutionContext . InvocationId . ToString ( ) ) ;
76
+
77
+ _logger . LogDebug ( "Forwarding http request for httpTrigger function: '{functionName}' invocationId: '{invocationId}'" , scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId ) ;
75
78
HttpResponseMessage invocationResponse = await _httpClient . SendAsync ( httpRequestMessage ) ;
76
- _logger . LogDebug ( "Received http response for simple httpTrigger function: '{functionName}' invocationId: '{invocationId}'" , scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId ) ;
79
+ _logger . LogDebug ( "Received http response for httpTrigger function: '{functionName}' invocationId: '{invocationId}'" , scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId ) ;
80
+
77
81
BindingMetadata httpOutputBinding = scriptInvocationContext . FunctionMetadata . OutputBindings . FirstOrDefault ( ) ;
78
82
if ( httpOutputBinding != null )
79
83
{
@@ -91,18 +95,39 @@ internal async Task ProcessHttpInAndOutInvocationRequest(ScriptInvocationContext
91
95
}
92
96
}
93
97
98
+ internal void AddHeaders ( HttpRequestMessage httpRequest , string invocationId )
99
+ {
100
+ httpRequest . Headers . Add ( HttpWorkerConstants . HostVersionHeaderName , ScriptHost . Version ) ;
101
+ httpRequest . Headers . Add ( HttpWorkerConstants . InvocationIdHeaderName , invocationId ) ;
102
+ httpRequest . Headers . UserAgent . ParseAdd ( $ "{ HttpWorkerConstants . UserAgentHeaderValue } /{ ScriptHost . Version } ") ;
103
+ }
104
+
105
+ internal string GetPathValue ( HttpWorkerOptions httpWorkerOptions , string functionName , HttpRequest httpRequest )
106
+ {
107
+ string pathValue = functionName ;
108
+ if ( httpWorkerOptions . EnableForwardingHttpRequest && httpWorkerOptions . Type == CustomHandlerType . Http )
109
+ {
110
+ pathValue = httpRequest . GetRequestUri ( ) . AbsolutePath ;
111
+ }
112
+
113
+ return pathValue ;
114
+ }
115
+
94
116
internal async Task ProcessDefaultInvocationRequest ( ScriptInvocationContext scriptInvocationContext )
95
117
{
96
118
try
97
119
{
98
120
HttpScriptInvocationContext httpScriptInvocationContext = await scriptInvocationContext . ToHttpScriptInvocationContext ( ) ;
121
+ string uri = BuildAndGetUri ( scriptInvocationContext . FunctionMetadata . Name ) ;
122
+
99
123
// Build httpRequestMessage from scriptInvocationContext
100
- using ( HttpRequestMessage httpRequestMessage = CreateAndGetHttpRequestMessage ( scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId . ToString ( ) , HttpMethod . Post ) )
124
+ using ( HttpRequestMessage httpRequestMessage = httpScriptInvocationContext . ToHttpRequestMessage ( uri ) )
101
125
{
102
- httpRequestMessage . Content = new ObjectContent < HttpScriptInvocationContext > ( httpScriptInvocationContext , new JsonMediaTypeFormatter ( ) ) ;
126
+ AddHeaders ( httpRequestMessage , scriptInvocationContext . ExecutionContext . InvocationId . ToString ( ) ) ;
127
+
103
128
_logger . LogDebug ( "Sending http request for function:{functionName} invocationId:{invocationId}" , scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId ) ;
104
129
HttpResponseMessage response = await _httpClient . SendAsync ( httpRequestMessage ) ;
105
- _logger . LogDebug ( "Received http request for function:{functionName} invocationId:{invocationId}" , scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId ) ;
130
+ _logger . LogDebug ( "Received http response for function:{functionName} invocationId:{invocationId}" , scriptInvocationContext . FunctionMetadata . Name , scriptInvocationContext . ExecutionContext . InvocationId ) ;
106
131
107
132
// Only process output bindings if response is succeess code
108
133
response . EnsureSuccessStatusCode ( ) ;
@@ -113,11 +138,11 @@ internal async Task ProcessDefaultInvocationRequest(ScriptInvocationContext scri
113
138
{
114
139
if ( httpScriptInvocationResult . Outputs == null || ! httpScriptInvocationResult . Outputs . Any ( ) )
115
140
{
116
- _logger . LogWarning ( "Outputs not set on http response for invocationId:{invocationId}" , scriptInvocationContext . ExecutionContext . InvocationId ) ;
141
+ _logger . LogWarning ( "Outputs not set on http response for invocationId:{invocationId}" , scriptInvocationContext . ExecutionContext . InvocationId ) ;
117
142
}
118
143
if ( httpScriptInvocationResult . ReturnValue == null )
119
144
{
120
- _logger . LogWarning ( "ReturnValue not set on http response for invocationId:{invocationId}" , scriptInvocationContext . ExecutionContext . InvocationId ) ;
145
+ _logger . LogWarning ( "ReturnValue not set on http response for invocationId:{invocationId}" , scriptInvocationContext . ExecutionContext . InvocationId ) ;
121
146
}
122
147
123
148
ProcessLogsFromHttpResponse ( scriptInvocationContext , httpScriptInvocationResult ) ;
@@ -156,32 +181,6 @@ internal void ProcessLogsFromHttpResponse(ScriptInvocationContext scriptInvocati
156
181
}
157
182
}
158
183
159
- private HttpRequestMessage CreateAndGetHttpRequestMessage ( string functionName , string invocationId , HttpMethod requestMethod , IDictionary < string , string > queryCollectionAsDictionary = null )
160
- {
161
- var requestMessage = new HttpRequestMessage ( ) ;
162
- AddRequestHeadersAndSetRequestUri ( requestMessage , functionName , invocationId ) ;
163
- if ( queryCollectionAsDictionary != null )
164
- {
165
- requestMessage . RequestUri = new Uri ( QueryHelpers . AddQueryString ( requestMessage . RequestUri . ToString ( ) , queryCollectionAsDictionary ) ) ;
166
- }
167
- requestMessage . Method = requestMethod ;
168
- return requestMessage ;
169
- }
170
-
171
- private void AddRequestHeadersAndSetRequestUri ( HttpRequestMessage httpRequestMessage , string functionName , string invocationId )
172
- {
173
- string pathValue = functionName ;
174
- // _httpWorkerOptions.Type is set to None only in HttpWorker section
175
- if ( httpRequestMessage . RequestUri != null && _httpWorkerOptions . Type != CustomHandlerType . None )
176
- {
177
- pathValue = httpRequestMessage . RequestUri . AbsolutePath ;
178
- }
179
- httpRequestMessage . RequestUri = new Uri ( BuildAndGetUri ( pathValue ) ) ;
180
- httpRequestMessage . Headers . Add ( HttpWorkerConstants . InvocationIdHeaderName , invocationId ) ;
181
- httpRequestMessage . Headers . Add ( HttpWorkerConstants . HostVersionHeaderName , ScriptHost . Version ) ;
182
- httpRequestMessage . Headers . UserAgent . ParseAdd ( $ "{ HttpWorkerConstants . UserAgentHeaderValue } /{ ScriptHost . Version } ") ;
183
- }
184
-
185
184
public async Task < bool > IsWorkerReady ( CancellationToken cancellationToken )
186
185
{
187
186
bool continueWaitingForWorker = await Utility . DelayAsync ( WorkerConstants . WorkerInitTimeoutSeconds , WorkerConstants . WorkerReadyCheckPollingIntervalMilliseconds , async ( ) =>
@@ -213,13 +212,13 @@ private async Task<bool> IsWorkerReadyForRequest()
213
212
}
214
213
}
215
214
216
- private string BuildAndGetUri ( string pathValue = null )
215
+ internal string BuildAndGetUri ( string pathValue = null )
217
216
{
218
- if ( ! string . IsNullOrEmpty ( pathValue ) )
217
+ if ( string . IsNullOrEmpty ( pathValue ) )
219
218
{
220
- return new UriBuilder ( WorkerConstants . HttpScheme , WorkerConstants . HostName , _httpWorkerOptions . Port , pathValue ) . ToString ( ) ;
219
+ return new UriBuilder ( WorkerConstants . HttpScheme , WorkerConstants . HostName , _httpWorkerOptions . Port ) . ToString ( ) ;
221
220
}
222
- return new UriBuilder ( WorkerConstants . HttpScheme , WorkerConstants . HostName , _httpWorkerOptions . Port ) . ToString ( ) ;
221
+ return new UriBuilder ( WorkerConstants . HttpScheme , WorkerConstants . HostName , _httpWorkerOptions . Port , pathValue ) . ToString ( ) ;
223
222
}
224
223
225
224
private async Task < HttpResponseMessage > SendRequest ( string requestUri , HttpMethod method = null )
0 commit comments