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
7 changes: 3 additions & 4 deletions lib/ethon/easy/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 5 additions & 3 deletions spec/ethon/easy/callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/ethon/easy/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Loading