diff --git a/lib/ethon/easy/callbacks.rb b/lib/ethon/easy/callbacks.rb index 73f2f210..3abcb519 100644 --- a/lib/ethon/easy/callbacks.rb +++ b/lib/ethon/easy/callbacks.rb @@ -37,10 +37,10 @@ def set_callbacks # @return [ Proc ] The callback. def body_write_callback @body_write_callback ||= proc do |stream, size, num, object| - headers + headers_user_callback_result = headers result = body(chunk = stream.read_string(size * num)) @response_body << chunk if result == :unyielded - result != :abort ? size * num : -1 + (result != :abort && headers_user_callback_result != :abort) ? size * num : -1 end end @@ -52,9 +52,8 @@ def body_write_callback # @return [ Proc ] The callback. def header_write_callback @header_write_callback ||= proc {|stream, size, num, object| - result = headers @response_headers << stream.read_string(size * num) - result != :abort ? size * num : -1 + size * num } end diff --git a/spec/ethon/easy/callbacks_spec.rb b/spec/ethon/easy/callbacks_spec.rb index 2fa75da9..9d00273c 100644 --- a/spec/ethon/easy/callbacks_spec.rb +++ b/spec/ethon/easy/callbacks_spec.rb @@ -71,10 +71,12 @@ easy.on_headers.clear easy.on_headers { :abort } end - let(:header_write_callback) { easy.instance_variable_get(:@header_write_callback) } - + let(:body_write_callback) { easy.instance_variable_get(:@body_write_callback) } + # on_headers callbacks are expected to be called exactly one time, once all headers are in. + # We can't abort exactly on receiving the headers, as we might need to follow redirects, + # so we do the next best thing, which is aborting on the first body chunk. it "returns -1 to indicate abort to libcurl" do - expect(header_write_callback.call(stream, 1, 1, nil)).to eq(-1) + expect(body_write_callback.call(stream, 1, 1, nil)).to eq(-1) end end end diff --git a/spec/ethon/easy/http_spec.rb b/spec/ethon/easy/http_spec.rb index b74a2df7..a7d985fe 100644 --- a/spec/ethon/easy/http_spec.rb +++ b/spec/ethon/easy/http_spec.rb @@ -45,7 +45,7 @@ it "notifies when headers are ready" do headers = [] - easy.on_headers { |r| headers << r.response_headers } + easy.on_headers { |r| headers << r.response_headers.dup } easy.http_request(url, action, options) easy.perform expect(headers).to eq([easy.response_headers])