ferrum icon indicating copy to clipboard operation
ferrum copied to clipboard

Proxied requests cannot be aborted

Open tbardiuk-ua opened this issue 2 years ago • 4 comments

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.

tbardiuk-ua avatar Jan 23 '23 16:01 tbardiuk-ua

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.

olegp avatar Apr 30 '23 05:04 olegp

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

route avatar May 20 '23 07:05 route

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

danest avatar Jul 27 '24 20:07 danest