Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions lib/ferrum/client/web_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions sig/ferrum/client/web_socket.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading