Skip to content

Commit 02bd825

Browse files
authored
Merge pull request #414 from janko-m/return-response-body-chunks-in-specified-encoding
Return response body chunks in specified encoding
2 parents 4445835 + e3efe86 commit 02bd825

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

lib/http/response/body.rb

+14-11
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ def initialize(stream, length: nil, encoding: Encoding::BINARY)
2020
@connection = stream.is_a?(Inflater) ? stream.connection : stream
2121
@streaming = nil
2222
@contents = nil
23-
@encoding = encoding
23+
@encoding = find_encoding(encoding)
2424
@length = length || Float::INFINITY
2525
end
2626

2727
# (see HTTP::Client#readpartial)
2828
def readpartial(*args)
2929
stream!
30-
@stream.readpartial(*args)
30+
chunk = @stream.readpartial(*args)
31+
chunk.force_encoding(@encoding) if chunk
3132
end
3233

3334
# Iterate over the body, allowing it to be enumerable
@@ -43,22 +44,15 @@ def to_s
4344

4445
raise StateError, "body is being streamed" unless @streaming.nil?
4546

46-
# see issue 312
47-
begin
48-
encoding = Encoding.find @encoding
49-
rescue ArgumentError
50-
encoding = Encoding::BINARY
51-
end
52-
5347
begin
5448
@streaming = false
55-
@contents = String.new("").force_encoding(encoding)
49+
@contents = String.new("").force_encoding(@encoding)
5650

5751
length = @length
5852

5953
while length > 0 && (chunk = @stream.readpartial)
6054
length -= chunk.bytesize
61-
@contents << chunk.force_encoding(encoding)
55+
@contents << chunk.force_encoding(@encoding)
6256
end
6357
rescue
6458
@contents = nil
@@ -79,6 +73,15 @@ def stream!
7973
def inspect
8074
"#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>"
8175
end
76+
77+
private
78+
79+
# Retrieve encoding by name. If encoding cannot be found, default to binary.
80+
def find_encoding(encoding)
81+
Encoding.find encoding
82+
rescue ArgumentError
83+
Encoding::BINARY
84+
end
8285
end
8386
end
8487
end

spec/lib/http/response/body_spec.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@
3737
body.readpartial
3838
end
3939
end
40+
41+
it "returns content in specified encoding" do
42+
body = described_class.new(connection)
43+
expect(connection).to receive(:readpartial).
44+
and_return(String.new("content").force_encoding(Encoding::UTF_8))
45+
expect(body.readpartial.encoding).to eq Encoding::BINARY
46+
47+
body = described_class.new(connection, :encoding => Encoding::UTF_8)
48+
expect(connection).to receive(:readpartial).
49+
and_return(String.new("content").force_encoding(Encoding::BINARY))
50+
expect(body.readpartial.encoding).to eq Encoding::UTF_8
51+
end
4052
end
4153

4254
context "when body is gzipped" do
@@ -58,7 +70,7 @@
5870
it "streams decoded body" do
5971
[
6072
"Hi, HTTP ",
61-
String.new("here ☺").force_encoding("ASCII-8BIT"),
73+
"here ☺",
6274
nil
6375
].each do |part|
6476
expect(subject.readpartial).to eq(part)

0 commit comments

Comments
 (0)