Skip to content

Commit e8342e2

Browse files
Fixes passing plugin data (#506)
1 parent c74843f commit e8342e2

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

dev-proxy/ProxyEngine.cs

+22-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class ProxyEngine
3636

3737
private bool _isRecording = false;
3838
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();
3942

4043
public ProxyEngine(ProxyConfiguration config, ISet<UrlToWatch> urlsToWatch, PluginEvents pluginEvents, ILogger logger)
4144
{
@@ -459,6 +462,8 @@ async Task OnRequest(object sender, SessionEventArgs e)
459462
{
460463
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host))
461464
{
465+
_pluginData.Add(e.GetHashCode(), new Dictionary<string, object>());
466+
462467
// we need to keep the request body for further processing
463468
// by plugins
464469
e.HttpClient.Request.KeepBody = true;
@@ -476,7 +481,11 @@ async Task OnRequest(object sender, SessionEventArgs e)
476481
private async Task HandleRequest(SessionEventArgs e)
477482
{
478483
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);
480489

481490
// 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.
482491
if (!responseState.HasBeenSet)
@@ -497,23 +506,33 @@ async Task OnBeforeResponse(object sender, SessionEventArgs e)
497506
// read response headers
498507
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host))
499508
{
509+
var proxyResponseArgs = new ProxyResponseArgs(e, _throttledRequests, new ResponseState())
510+
{
511+
PluginData = _pluginData[e.GetHashCode()]
512+
};
500513
// necessary to make the response body available to plugins
501514
e.HttpClient.Response.KeepBody = true;
502515
if (e.HttpClient.Response.HasBody)
503516
{
504517
await e.GetResponseBody();
505518
}
506519

507-
await _pluginEvents.RaiseProxyBeforeResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState()));
520+
await _pluginEvents.RaiseProxyBeforeResponse(proxyResponseArgs);
508521
}
509522
}
510523
async Task OnAfterResponse(object sender, SessionEventArgs e)
511524
{
512525
// read response headers
513526
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host))
514527
{
528+
var proxyResponseArgs = new ProxyResponseArgs(e, _throttledRequests, new ResponseState())
529+
{
530+
PluginData = _pluginData[e.GetHashCode()]
531+
};
515532
_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());
517536
}
518537
}
519538

0 commit comments

Comments
 (0)