Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes passing plugin data #506

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions dev-proxy/ProxyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class ProxyEngine

private bool _isRecording = false;
private List<RequestLog> _requestLogs = new List<RequestLog>();
// Dictionary for plugins to store data between requests
// the key is HashObject of the SessionEventArgs object
private Dictionary<int, Dictionary<string, object>> _pluginData = new();

public ProxyEngine(ProxyConfiguration config, ISet<UrlToWatch> urlsToWatch, PluginEvents pluginEvents, ILogger logger)
{
Expand Down Expand Up @@ -459,6 +462,8 @@ async Task OnRequest(object sender, SessionEventArgs e)
{
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host))
{
_pluginData.Add(e.GetHashCode(), new Dictionary<string, object>());

// we need to keep the request body for further processing
// by plugins
e.HttpClient.Request.KeepBody = true;
Expand All @@ -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)
Expand All @@ -497,23 +506,33 @@ 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)
{
await e.GetResponseBody();
}

await _pluginEvents.RaiseProxyBeforeResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState()));
await _pluginEvents.RaiseProxyBeforeResponse(proxyResponseArgs);
}
}
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());
}
}

Expand Down