From 22b9dd4f58012ada29c7988f48b72c782ee4f353 Mon Sep 17 00:00:00 2001 From: maurycy <5383+maurycy@users.noreply.github.com> Date: Sun, 8 Aug 2021 11:32:06 +0200 Subject: [PATCH 1/3] Support Net::HTTP::Persistent.write_timeout --- lib/mechanize.rb | 15 +++++++++++++++ lib/mechanize/http/agent.rb | 7 +++++++ test/test_mechanize.rb | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/lib/mechanize.rb b/lib/mechanize.rb index 4e8d0341..76d83af9 100644 --- a/lib/mechanize.rb +++ b/lib/mechanize.rb @@ -951,6 +951,21 @@ def read_timeout= read_timeout @agent.read_timeout = read_timeout end + ## + # Length of time to wait for data to be sent to the server + + def write_timeout + @agent.write_timeout + end + + ## + # Sets the timeout for each chunk of data to be sent to the server to + # +write_timeout+. A single request may write many chunks of data. + + def write_timeout= write_timeout + @agent.write_timeout = write_timeout + end + ## # Controls how mechanize deals with redirects. The following values are # allowed: diff --git a/lib/mechanize/http/agent.rb b/lib/mechanize/http/agent.rb index d3f318a6..5c38b83a 100644 --- a/lib/mechanize/http/agent.rb +++ b/lib/mechanize/http/agent.rb @@ -106,6 +106,9 @@ class Mechanize::HTTP::Agent # Length of time to attempt to read data from the server attr_accessor :read_timeout + # Length of time to attempt to write data to the server + attr_accessor :write_timeout + # :section: # The cookies for this agent @@ -161,6 +164,7 @@ def initialize(connection_name = 'mechanize') @robots_mutex = Mutex.new @user_agent = nil @webrobots = nil + @write_timeout = nil # HTTP Authentication @auth_store = Mechanize::HTTP::AuthStore.new @@ -274,6 +278,9 @@ def fetch uri, method = :get, headers = {}, params = [], if @read_timeout && connection.respond_to?(:read_timeout=) connection.read_timeout = @read_timeout end + if @write_timeout && connection.respond_to?(:write_timeout=) + connection.write_timeout = @write_timeout + end request_log request diff --git a/test/test_mechanize.rb b/test/test_mechanize.rb index 1932e5d3..56094df9 100644 --- a/test/test_mechanize.rb +++ b/test/test_mechanize.rb @@ -1020,6 +1020,12 @@ def test_read_timeout_equals assert_equal 5, @mech.read_timeout end + def test_write_timeout_equals + @mech.write_timeout = 7 + + assert_equal 7, @mech.write_timeout + end + def test_timeouts_for_file_connection uri = URI.parse "file://#{File.expand_path __FILE__}" @mech.read_timeout = 5 From 588eee97e2ecdfe0b73532b4363c44ccbbdc19d2 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 5 Jan 2025 12:58:33 -0500 Subject: [PATCH 2/3] doc: update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe029856..f9477539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Mechanize CHANGELOG +## next / unreleased + +* `Mechanize` exposes a `write_timeout` attribute, which is set on the connection if it's supported (e.g., Net::HTTP::Persistent.write_timeout). (#586) @maurycy + + ## 2.13.0 / 2025-01-02 * Quash frozen string warnings in Ruby 3.4. (#661) @simpl1g From 805451f1d306be4701b2a7a2cea647faac45b5ac Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 5 Jan 2025 13:11:49 -0500 Subject: [PATCH 3/3] test: additional coverage for read_timeout= and write_timeout= --- test/test_mechanize.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_mechanize.rb b/test/test_mechanize.rb index 56094df9..6abad40f 100644 --- a/test/test_mechanize.rb +++ b/test/test_mechanize.rb @@ -1016,14 +1016,16 @@ def test_put_redirect def test_read_timeout_equals @mech.read_timeout = 5 - assert_equal 5, @mech.read_timeout + assert @mech.get('http://localhost/response_code?code=200') + assert_equal 5, @mech.agent.http.read_timeout end def test_write_timeout_equals @mech.write_timeout = 7 - assert_equal 7, @mech.write_timeout + assert @mech.get('http://localhost/response_code?code=200') + assert_equal 7, @mech.agent.http.write_timeout end def test_timeouts_for_file_connection