@@ -198,37 +198,13 @@ await clientStreamWriter.WriteResponseAsync(args.HttpClient.Response,
198
198
connection = null ;
199
199
}
200
200
201
- RetryResult result ;
202
- if ( request . UpgradeToWebSocket )
203
- {
204
- //a connection generator task with captured parameters via closure.
205
- Func < Task < TcpServerConnection > > generator = ( ) =>
206
- tcpConnectionFactory . GetServerConnection ( this , args , isConnect : false ,
207
- applicationProtocol : clientConnection . NegotiatedApplicationProtocol ,
208
- noCache : false , cancellationToken : cancellationToken ) ;
209
-
210
- //for connection pool, retry fails until cache is exhausted.
211
- result = await retryPolicy < ServerConnectionException > ( ) . ExecuteAsync ( async ( serverConnection ) =>
212
- {
213
- args . TimeLine [ "Connection Ready" ] = DateTime . Now ;
214
-
215
- // if upgrading to websocket then relay the request without reading the contents
216
- await handleWebSocketUpgrade ( httpCmd , args , request ,
217
- response , clientStream , clientStreamWriter ,
218
- serverConnection , cancellationTokenSource , cancellationToken ) ;
219
- closeServerConnection = true ;
220
- return false ;
221
-
222
- } , generator , connection ) ;
223
- }
224
- else
225
- {
226
- result = await handleHttpSessionRequest ( args , connection ,
227
- clientConnection . NegotiatedApplicationProtocol , cancellationToken ) ;
228
- }
201
+ var result = await handleHttpSessionRequest ( httpCmd , args , connection ,
202
+ clientConnection . NegotiatedApplicationProtocol ,
203
+ cancellationToken , cancellationTokenSource ) ;
229
204
230
205
//update connection to latest used
231
206
connection = result . LatestConnection ;
207
+ closeServerConnection = ! result . Continue ;
232
208
233
209
//throw if exception happened
234
210
if ( ! result . IsSuccess )
@@ -260,13 +236,13 @@ await handleWebSocketUpgrade(httpCmd, args, request,
260
236
throw new Exception ( "Session was terminated by user." ) ;
261
237
}
262
238
263
- //Get/release server connection for each HTTP session instead of per client connection.
239
+ //Release server connection for each HTTP session instead of per client connection.
264
240
//This will be more efficient especially when client is idly holding server connection
265
241
//between sessions without using it.
266
242
//Do not release authenticated connections for performance reasons.
267
243
//Otherwise it will keep authenticating per session.
268
244
if ( EnableConnectionPool && connection != null
269
- && ! connection . IsWinAuthenticated )
245
+ && ! connection . IsWinAuthenticated )
270
246
{
271
247
await tcpConnectionFactory . Release ( connection ) ;
272
248
connection = null ;
@@ -300,45 +276,40 @@ await tcpConnectionFactory.Release(connection,
300
276
}
301
277
}
302
278
303
- private async Task < RetryResult > handleHttpSessionRequest ( SessionEventArgs args ,
304
- TcpServerConnection connection ,
305
- SslApplicationProtocol protocol ,
306
- CancellationToken cancellationToken )
279
+ private async Task < RetryResult > handleHttpSessionRequest ( string httpCmd , SessionEventArgs args ,
280
+ TcpServerConnection serverConnection , SslApplicationProtocol sslApplicationProtocol ,
281
+ CancellationToken cancellationToken , CancellationTokenSource cancellationTokenSource )
307
282
{
308
- //host/scheme changed from ReRequest
309
- if ( args . ReRequest
310
- && ( args . HttpClient . Request . IsHttps != connection . IsHttps
311
- || args . HttpClient . Request . Host != connection . HostName ) )
312
- {
313
- connection = null ;
314
- }
315
-
316
-
317
283
//a connection generator task with captured parameters via closure.
318
284
Func < Task < TcpServerConnection > > generator = ( ) =>
319
- tcpConnectionFactory . GetServerConnection ( this , args , isConnect : false ,
320
- applicationProtocol : protocol ,
321
- noCache : false , cancellationToken : cancellationToken ) ;
285
+ tcpConnectionFactory . GetServerConnection ( this , args , isConnect : false ,
286
+ applicationProtocol : sslApplicationProtocol ,
287
+ noCache : false , cancellationToken : cancellationToken ) ;
322
288
323
289
//for connection pool, retry fails until cache is exhausted.
324
- return await retryPolicy < ServerConnectionException > ( ) . ExecuteAsync ( async ( serverConnection ) =>
290
+ return await retryPolicy < ServerConnectionException > ( ) . ExecuteAsync ( async ( connection ) =>
325
291
{
326
292
args . TimeLine [ "Connection Ready" ] = DateTime . Now ;
327
293
294
+ if ( args . HttpClient . Request . UpgradeToWebSocket )
295
+ {
296
+ // if upgrading to websocket then relay the request without reading the contents
297
+ await handleWebSocketUpgrade ( httpCmd , args , args . HttpClient . Request ,
298
+ args . HttpClient . Response , args . ProxyClient . ClientStream , args . ProxyClient . ClientStreamWriter ,
299
+ connection , cancellationTokenSource , cancellationToken ) ;
300
+ return false ;
301
+ }
302
+
303
+ args . TimeLine [ "Connection Ready" ] = DateTime . Now ;
304
+
328
305
// construct the web request that we are going to issue on behalf of the client.
329
- await handleHttpSessionRequest ( serverConnection , args ) ;
306
+ await handleHttpSessionRequest ( connection , args ) ;
330
307
return true ;
331
308
332
- } , generator , connection ) ;
309
+ } , generator , serverConnection ) ;
333
310
}
334
311
335
- /// <summary>
336
- /// Handle a specific session (request/response sequence)
337
- /// </summary>
338
- /// <param name="serverConnection">The tcp connection.</param>
339
- /// <param name="args">The session event arguments.</param>
340
- /// <returns></returns>
341
- private async Task handleHttpSessionRequest ( TcpServerConnection serverConnection , SessionEventArgs args )
312
+ private async Task handleHttpSessionRequest ( TcpServerConnection connection , SessionEventArgs args )
342
313
{
343
314
var cancellationToken = args . CancellationTokenSource . Token ;
344
315
var request = args . HttpClient . Request ;
@@ -350,7 +321,7 @@ private async Task handleHttpSessionRequest(TcpServerConnection serverConnection
350
321
// and see if server would return 100 conitinue
351
322
if ( request . ExpectContinue )
352
323
{
353
- args . HttpClient . SetConnection ( serverConnection ) ;
324
+ args . HttpClient . SetConnection ( connection ) ;
354
325
await args . HttpClient . SendRequest ( Enable100ContinueBehaviour , args . IsTransparent ,
355
326
cancellationToken ) ;
356
327
}
@@ -377,7 +348,7 @@ await clientStreamWriter.WriteResponseStatusAsync(args.HttpClient.Response.HttpV
377
348
// If expect continue is not enabled then set the connectio and send request headers
378
349
if ( ! request . ExpectContinue )
379
350
{
380
- args . HttpClient . SetConnection ( serverConnection ) ;
351
+ args . HttpClient . SetConnection ( connection ) ;
381
352
await args . HttpClient . SendRequest ( Enable100ContinueBehaviour , args . IsTransparent ,
382
353
cancellationToken ) ;
383
354
}
0 commit comments