Skip to content

Commit

Permalink
[.NET] Fix network interception using CDP
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Feb 17, 2021
1 parent e88c143 commit 3892be6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 80 deletions.
47 changes: 23 additions & 24 deletions dotnet/src/webdriver/DevTools/v86/V86Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public V86Network(NetworkAdapter network, FetchAdapter fetch)
this.fetch = fetch;
fetch.AuthRequired += OnFetchAuthRequired;
fetch.RequestPaused += OnFetchRequestPaused;
network.ResponseReceived += OnNetworkResponseReceived;
}

/// <summary>
Expand Down Expand Up @@ -85,7 +84,8 @@ await fetch.Enable(new OpenQA.Selenium.DevTools.V86.Fetch.EnableCommandSettings(
{
Patterns = new OpenQA.Selenium.DevTools.V86.Fetch.RequestPattern[]
{
new OpenQA.Selenium.DevTools.V86.Fetch.RequestPattern() { UrlPattern = "*" }
new OpenQA.Selenium.DevTools.V86.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Request },
new OpenQA.Selenium.DevTools.V86.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Response }
},
HandleAuthRequests = true
});
Expand Down Expand Up @@ -255,9 +255,9 @@ private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)

private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
{
RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
wrapped.RequestData = new HttpRequestData()
{
RequestId = e.RequestId,
Expand All @@ -266,32 +266,31 @@ private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};
}

this.OnRequestPaused(wrapped);
}

private void OnNetworkResponseReceived(object sender, Network.ResponseReceivedEventArgs e)
{
HttpResponseData responseData = new HttpResponseData()
this.OnRequestPaused(wrapped);
}
else
{
RequestId = e.RequestId,
StatusCode = e.Response.Status,
Url = e.Response.Url,
ResourceType = e.Type.ToString()
};
ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs();
wrappedResponse.ResponseData = new HttpResponseData()
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString()
};

foreach(var header in e.Response.Headers)
{
responseData.Headers.Add(header.Key, header.Value);
}
if (e.ResponseStatusCode.HasValue)
{
wrappedResponse.ResponseData.StatusCode = e.ResponseStatusCode.Value;
}

ResponsePausedEventArgs wrapped = new ResponsePausedEventArgs()
{
ResponseData = responseData
};
foreach (var header in e.ResponseHeaders)
{
wrappedResponse.ResponseData.Headers.Add(header.Name, header.Value);
}

this.OnResponsePaused(wrapped);
this.OnResponsePaused(wrappedResponse);
}
}
}
}
20 changes: 12 additions & 8 deletions dotnet/src/webdriver/DevTools/v87/V87Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ await fetch.Enable(new OpenQA.Selenium.DevTools.V87.Fetch.EnableCommandSettings(
{
Patterns = new OpenQA.Selenium.DevTools.V87.Fetch.RequestPattern[]
{
new OpenQA.Selenium.DevTools.V87.Fetch.RequestPattern() { UrlPattern = "*" },
new OpenQA.Selenium.DevTools.V87.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Request },
new OpenQA.Selenium.DevTools.V87.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Response }
},
HandleAuthRequests = true
Expand Down Expand Up @@ -266,26 +266,30 @@ private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};

this.OnRequestPaused(wrapped);
}
else
{
ResponsePausedEventArgs wrapped = new ResponsePausedEventArgs();
wrapped.ResponseData = new HttpResponseData()
ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs();
wrappedResponse.ResponseData = new HttpResponseData()
{
RequestId = e.RequestId,
StatusCode = e.ResponseStatusCode.Value,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString(),
ErrorReason = e.ResponseErrorReason.HasValue ? e.ResponseErrorReason.Value.ToString() : ""
ResourceType = e.ResourceType.ToString()
};

if (e.ResponseStatusCode.HasValue)
{
wrappedResponse.ResponseData.StatusCode = e.ResponseStatusCode.Value;
}

foreach (var header in e.ResponseHeaders)
{
wrapped.ResponseData.Headers.Add(header.Name, header.Value);
wrappedResponse.ResponseData.Headers.Add(header.Name, header.Value);
}

this.OnResponsePaused(wrapped);
this.OnResponsePaused(wrappedResponse);
}
}
}
Expand Down
48 changes: 24 additions & 24 deletions dotnet/src/webdriver/DevTools/v88/V88Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public V88Network(NetworkAdapter network, FetchAdapter fetch)
this.fetch = fetch;
fetch.AuthRequired += OnFetchAuthRequired;
fetch.RequestPaused += OnFetchRequestPaused;
network.ResponseReceived += OnNetworkResponseReceived;

}

/// <summary>
Expand Down Expand Up @@ -85,7 +85,8 @@ await fetch.Enable(new OpenQA.Selenium.DevTools.V88.Fetch.EnableCommandSettings(
{
Patterns = new OpenQA.Selenium.DevTools.V88.Fetch.RequestPattern[]
{
new OpenQA.Selenium.DevTools.V88.Fetch.RequestPattern() { UrlPattern = "*" }
new OpenQA.Selenium.DevTools.V88.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Request },
new OpenQA.Selenium.DevTools.V88.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Response }
},
HandleAuthRequests = true
});
Expand Down Expand Up @@ -255,9 +256,9 @@ private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)

private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
{
RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
wrapped.RequestData = new HttpRequestData()
{
RequestId = e.RequestId,
Expand All @@ -266,32 +267,31 @@ private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};
}

this.OnRequestPaused(wrapped);
}

private void OnNetworkResponseReceived(object sender, Network.ResponseReceivedEventArgs e)
{
HttpResponseData responseData = new HttpResponseData()
this.OnRequestPaused(wrapped);
}
else
{
RequestId = e.RequestId,
StatusCode = e.Response.Status,
Url = e.Response.Url,
ResourceType = e.Type.ToString()
};
ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs();
wrappedResponse.ResponseData = new HttpResponseData()
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString()
};

foreach(var header in e.Response.Headers)
{
responseData.Headers.Add(header.Key, header.Value);
}
if (e.ResponseStatusCode.HasValue)
{
wrappedResponse.ResponseData.StatusCode = e.ResponseStatusCode.Value;
}

ResponsePausedEventArgs wrapped = new ResponsePausedEventArgs()
{
ResponseData = responseData
};
foreach (var header in e.ResponseHeaders)
{
wrappedResponse.ResponseData.Headers.Add(header.Name, header.Value);
}

this.OnResponsePaused(wrapped);
this.OnResponsePaused(wrappedResponse);
}
}
}
}
47 changes: 23 additions & 24 deletions dotnet/src/webdriver/DevTools/v89/V89Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public V89Network(NetworkAdapter network, FetchAdapter fetch)
this.fetch = fetch;
fetch.AuthRequired += OnFetchAuthRequired;
fetch.RequestPaused += OnFetchRequestPaused;
network.ResponseReceived += OnNetworkResponseReceived;
}

/// <summary>
Expand Down Expand Up @@ -85,7 +84,8 @@ await fetch.Enable(new OpenQA.Selenium.DevTools.V89.Fetch.EnableCommandSettings(
{
Patterns = new OpenQA.Selenium.DevTools.V89.Fetch.RequestPattern[]
{
new OpenQA.Selenium.DevTools.V89.Fetch.RequestPattern() { UrlPattern = "*" }
new OpenQA.Selenium.DevTools.V89.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Request },
new OpenQA.Selenium.DevTools.V89.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Response }
},
HandleAuthRequests = true
});
Expand Down Expand Up @@ -255,9 +255,9 @@ private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)

private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
{
RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
wrapped.RequestData = new HttpRequestData()
{
RequestId = e.RequestId,
Expand All @@ -266,32 +266,31 @@ private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};
}

this.OnRequestPaused(wrapped);
}

private void OnNetworkResponseReceived(object sender, Network.ResponseReceivedEventArgs e)
{
HttpResponseData responseData = new HttpResponseData()
this.OnRequestPaused(wrapped);
}
else
{
RequestId = e.RequestId,
StatusCode = e.Response.Status,
Url = e.Response.Url,
ResourceType = e.Type.ToString()
};
ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs();
wrappedResponse.ResponseData = new HttpResponseData()
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString()
};

foreach(var header in e.Response.Headers)
{
responseData.Headers.Add(header.Key, header.Value);
}
if (e.ResponseStatusCode.HasValue)
{
wrappedResponse.ResponseData.StatusCode = e.ResponseStatusCode.Value;
}

ResponsePausedEventArgs wrapped = new ResponsePausedEventArgs()
{
ResponseData = responseData
};
foreach (var header in e.ResponseHeaders)
{
wrappedResponse.ResponseData.Headers.Add(header.Name, header.Value);
}

this.OnResponsePaused(wrapped);
this.OnResponsePaused(wrappedResponse);
}
}
}
}

0 comments on commit 3892be6

Please sign in to comment.