diff --git a/dev-proxy/ProxyEngine.cs b/dev-proxy/ProxyEngine.cs index 991151b5..fa8eb044 100644 --- a/dev-proxy/ProxyEngine.cs +++ b/dev-proxy/ProxyEngine.cs @@ -36,6 +36,9 @@ public class ProxyEngine private bool _isRecording = false; private List _requestLogs = new List(); + // Dictionary for plugins to store data between requests + // the key is HashObject of the SessionEventArgs object + private Dictionary> _pluginData = new(); public ProxyEngine(ProxyConfiguration config, ISet urlsToWatch, PluginEvents pluginEvents, ILogger logger) { @@ -459,6 +462,8 @@ async Task OnRequest(object sender, SessionEventArgs e) { if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) { + _pluginData.Add(e.GetHashCode(), new Dictionary()); + // we need to keep the request body for further processing // by plugins e.HttpClient.Request.KeepBody = true; @@ -476,7 +481,11 @@ async Task OnRequest(object sender, SessionEventArgs e) private async Task HandleRequest(SessionEventArgs e) { ResponseState responseState = new ResponseState(); - await _pluginEvents.RaiseProxyBeforeRequest(new ProxyRequestArgs(e, _throttledRequests, responseState)); + var proxyRequestArgs = new ProxyRequestArgs(e, _throttledRequests, responseState) + { + PluginData = _pluginData[e.GetHashCode()] + }; + await _pluginEvents.RaiseProxyBeforeRequest(proxyRequestArgs); // 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. if (!responseState.HasBeenSet) @@ -497,6 +506,10 @@ async Task OnBeforeResponse(object sender, SessionEventArgs e) // read response headers if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) { + var proxyResponseArgs = new ProxyResponseArgs(e, _throttledRequests, new ResponseState()) + { + PluginData = _pluginData[e.GetHashCode()] + }; // necessary to make the response body available to plugins e.HttpClient.Response.KeepBody = true; if (e.HttpClient.Response.HasBody) @@ -504,7 +517,7 @@ async Task OnBeforeResponse(object sender, SessionEventArgs e) await e.GetResponseBody(); } - await _pluginEvents.RaiseProxyBeforeResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState())); + await _pluginEvents.RaiseProxyBeforeResponse(proxyResponseArgs); } } async Task OnAfterResponse(object sender, SessionEventArgs e) @@ -512,8 +525,14 @@ async Task OnAfterResponse(object sender, SessionEventArgs e) // read response headers if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) { + var proxyResponseArgs = new ProxyResponseArgs(e, _throttledRequests, new ResponseState()) + { + PluginData = _pluginData[e.GetHashCode()] + }; _logger.LogRequest(new[] { $"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}" }, MessageType.InterceptedResponse, new LoggingContext(e)); - await _pluginEvents.RaiseProxyAfterResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState())); + await _pluginEvents.RaiseProxyAfterResponse(proxyResponseArgs); + // clean up + _pluginData.Remove(e.GetHashCode()); } }