@@ -36,6 +36,9 @@ public class ProxyEngine
36
36
37
37
private bool _isRecording = false ;
38
38
private List < RequestLog > _requestLogs = new List < RequestLog > ( ) ;
39
+ // Dictionary for plugins to store data between requests
40
+ // the key is HashObject of the SessionEventArgs object
41
+ private Dictionary < int , Dictionary < string , object > > _pluginData = new ( ) ;
39
42
40
43
public ProxyEngine ( ProxyConfiguration config , ISet < UrlToWatch > urlsToWatch , PluginEvents pluginEvents , ILogger logger )
41
44
{
@@ -459,6 +462,8 @@ async Task OnRequest(object sender, SessionEventArgs e)
459
462
{
460
463
if ( IsProxiedHost ( e . HttpClient . Request . RequestUri . Host ) )
461
464
{
465
+ _pluginData . Add ( e . GetHashCode ( ) , new Dictionary < string , object > ( ) ) ;
466
+
462
467
// we need to keep the request body for further processing
463
468
// by plugins
464
469
e . HttpClient . Request . KeepBody = true ;
@@ -476,7 +481,11 @@ async Task OnRequest(object sender, SessionEventArgs e)
476
481
private async Task HandleRequest ( SessionEventArgs e )
477
482
{
478
483
ResponseState responseState = new ResponseState ( ) ;
479
- await _pluginEvents . RaiseProxyBeforeRequest ( new ProxyRequestArgs ( e , _throttledRequests , responseState ) ) ;
484
+ var proxyRequestArgs = new ProxyRequestArgs ( e , _throttledRequests , responseState )
485
+ {
486
+ PluginData = _pluginData [ e . GetHashCode ( ) ]
487
+ } ;
488
+ await _pluginEvents . RaiseProxyBeforeRequest ( proxyRequestArgs ) ;
480
489
481
490
// We only need to set the proxy header if the proxy has not set a response and the request is going to be sent to the target.
482
491
if ( ! responseState . HasBeenSet )
@@ -497,23 +506,33 @@ async Task OnBeforeResponse(object sender, SessionEventArgs e)
497
506
// read response headers
498
507
if ( IsProxiedHost ( e . HttpClient . Request . RequestUri . Host ) )
499
508
{
509
+ var proxyResponseArgs = new ProxyResponseArgs ( e , _throttledRequests , new ResponseState ( ) )
510
+ {
511
+ PluginData = _pluginData [ e . GetHashCode ( ) ]
512
+ } ;
500
513
// necessary to make the response body available to plugins
501
514
e . HttpClient . Response . KeepBody = true ;
502
515
if ( e . HttpClient . Response . HasBody )
503
516
{
504
517
await e . GetResponseBody ( ) ;
505
518
}
506
519
507
- await _pluginEvents . RaiseProxyBeforeResponse ( new ProxyResponseArgs ( e , _throttledRequests , new ResponseState ( ) ) ) ;
520
+ await _pluginEvents . RaiseProxyBeforeResponse ( proxyResponseArgs ) ;
508
521
}
509
522
}
510
523
async Task OnAfterResponse ( object sender , SessionEventArgs e )
511
524
{
512
525
// read response headers
513
526
if ( IsProxiedHost ( e . HttpClient . Request . RequestUri . Host ) )
514
527
{
528
+ var proxyResponseArgs = new ProxyResponseArgs ( e , _throttledRequests , new ResponseState ( ) )
529
+ {
530
+ PluginData = _pluginData [ e . GetHashCode ( ) ]
531
+ } ;
515
532
_logger . LogRequest ( new [ ] { $ "{ e . HttpClient . Request . Method } { e . HttpClient . Request . Url } " } , MessageType . InterceptedResponse , new LoggingContext ( e ) ) ;
516
- await _pluginEvents . RaiseProxyAfterResponse ( new ProxyResponseArgs ( e , _throttledRequests , new ResponseState ( ) ) ) ;
533
+ await _pluginEvents . RaiseProxyAfterResponse ( proxyResponseArgs ) ;
534
+ // clean up
535
+ _pluginData . Remove ( e . GetHashCode ( ) ) ;
517
536
}
518
537
}
519
538
0 commit comments