diff --git a/CHANGELOG.md b/CHANGELOG.md index d4723138..897174a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `keyboard.type` and `node.type` type `\n` in a string as Enter, it was dropped - A failed `Browser.new` interrupted while it cleans up, e.g. by `Timeout.timeout`, closes the socket, stops its threads and the browser process instead of leaking them [#738] +- A message from Chrome over `:ws_max_receive_size` raises `DeadBrowserError` that names the limit [#739] ### Removed - `Target#page=`, a private API Cuprite no longer needs, build pages with `create_page` and set them up with diff --git a/lib/ferrum/client/web_socket.rb b/lib/ferrum/client/web_socket.rb index b29e397b..004a87d0 100644 --- a/lib/ferrum/client/web_socket.rb +++ b/lib/ferrum/client/web_socket.rb @@ -26,6 +26,7 @@ class WebSocket DEFAULT_PORTS = { "ws" => 80, "wss" => 443 }.freeze WRITE_CHUNK_SIZE = 64 * 1024 WRITE_TIMEOUT = 30 + MESSAGE_TOO_BIG = 1009 ESCAPE_OR_LONE_SURROGATE = /\\u[dD][89abAB]\h{2}\\u[dD][c-fC-F]\h{2}|\\u([dD][89a-fA-F]\h{2})|\\./ attr_reader :url, :messages @@ -51,8 +52,8 @@ def initialize(url, max_receive_size, log, write_timeout = WRITE_TIMEOUT, connec @sock = tcp end - max_receive_size ||= ::WebSocket::Driver::MAX_LENGTH - @driver = ::WebSocket::Driver.client(self, max_length: max_receive_size) + @max_receive_size = max_receive_size || ::WebSocket::Driver::MAX_LENGTH + @driver = ::WebSocket::Driver.client(self, max_length: @max_receive_size) # websocket-driver holds no locks and is called from many threads: commands from # callers, pong/close replies from the reader. One lock keeps frames from interleaving. @mutex = Mutex.new @@ -101,11 +102,15 @@ def on_message(event) # # Handles the driver's `:close` event: closes the message queue, lets the # writer send what's queued and close the socket, then kills the reader - # thread. + # thread. A message over the receive limit is named as the reason. # # @return [void] # - def on_close(_event) + def on_close(event) + if event.code == MESSAGE_TOO_BIG + @close_reason ||= "Browser sent a message larger than ws_max_receive_size (#{@max_receive_size} bytes), " \ + "connection closed" + end @messages.close queue_encoded @outgoing.close diff --git a/sig/ferrum/client/web_socket.rbs b/sig/ferrum/client/web_socket.rbs index 276d8b2b..51a047b9 100644 --- a/sig/ferrum/client/web_socket.rbs +++ b/sig/ferrum/client/web_socket.rbs @@ -5,6 +5,7 @@ module Ferrum DEFAULT_PORTS: Hash[String, Integer] WRITE_CHUNK_SIZE: Integer WRITE_TIMEOUT: Integer + MESSAGE_TOO_BIG: Integer attr_reader url: (String | ::Addressable::URI) attr_reader messages: ::Queue @@ -22,6 +23,7 @@ module Ferrum @write_timeout: ::Numeric? @encoded: Array[String] @close_reason: String? + @max_receive_size: Integer def initialize: ((String | ::Addressable::URI) url, Integer? max_receive_size, Log? log, ?::Numeric write_timeout, ?connect_timeout: ::Numeric?) -> void diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index d91c7443..42f8f271 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -570,6 +570,16 @@ def proxy_tunnel(socket, request, browser_url) expect(process.pid).to be(nil) end + it "raises DeadBrowserError naming :ws_max_receive_size when a message exceeds it", :launch do + browser = Ferrum::Browser.new(ws_max_receive_size: 1024 * 1024) + + expect { browser.evaluate("'x'.repeat(2 * 1024 * 1024)") } + .to raise_error(Ferrum::DeadBrowserError, /ws_max_receive_size \(1048576 bytes\)/) + expect { browser.evaluate("1") }.to raise_error(Ferrum::DeadBrowserError, /ws_max_receive_size/) + ensure + browser&.quit + end + it "closes the connection when :ws_url doesn't answer in time" do with_unresponsive_browser do |ws_url, connections| expect { Ferrum::Browser.new(ws_url: ws_url, timeout: 0.5) }.to raise_error(Ferrum::TimeoutError)