rubycdp / ferrum

Headless Chrome Ruby API
https://ferrum.rubycdp.com
MIT License
1.7k stars 122 forks source link

Proxied requests cannot be aborted #332

Open tbardiuk-ua opened 1 year ago

tbardiuk-ua commented 1 year ago

Hi guys. I'm trying to reduce traffic usage by not loading images. However, it seems that intercepted requests cannot be aborted when a proxy is used.

Here's an example:

browser = Ferrum::Browser.new(headless: false)
browser.network.intercept
browser.on(:request) do |request|
  if request.resource_type == 'Image'
    request.abort
  else
    request.continue
  end
end

browser.go_to('https://bing.com')

The result is a page with no images loaded, so everything works as expected. But if I just change the browser options by adding a proxy, all the images will load:

browser = Ferrum::Browser.new(proxy: { host: 'host.com', port: '80', user: 'username', password: 'password' }, headless: false)

I also checked if the on(:request) block is executed at all, and yes, it is. But request.abort doesn't seem to do anything.

Does anyone have any idea why this is happening and how to fix the problem?

Thanks in advance.

olegp commented 1 year ago

I enabled logging and found out that the problem is with this line, which calls continue on an intercepted request before we call abort. As a result, when we call abort, we get back an "Invalid state for continueInterceptedRequest" error.

Until this is fixed, the workaround is to do the following:

browser = Ferrum::Browser.new(proxy: { host: 'host.com', port: '80' }, headless: false)
browser.network.authorize(user: 'username', password: 'password', type: :proxy) {}
browser.network.intercept
browser.on(:request) do |request|
  if request.resource_type == 'Image'
    request.abort
  else
    request.continue
  end
end

browser.go_to('https://bing.com')

This ensures that the line I linked to isn't called, while still enabling authorization, but passing through an empty block to authorize that doesn't call request.continue.

route commented 1 year ago

Thank you guys for reporting and for findings, I’ll get back to you after investigation

danest commented 4 weeks ago

I ran into this issue as well with proxies. Thank you for the tip on how to solve it