Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AutoInflate with encoding other than Encoding::BINARY. #721

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions lib/http/features/auto_inflate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def wrap_response(response)
:headers => response.headers,
:proxy_headers => response.proxy_headers,
:connection => response.connection,
:body => stream_for(response.connection),
:body => stream_for(response.connection, :encoding => response.body.encoding),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move logic to stream_for:

Suggested change
:body => stream_for(response.connection, :encoding => response.body.encoding),
:body => wrap_body(response),

Then move and rename stream_for to be private wrap_body:

private

def wrap_body(response)
  stream = Response::Inflater.new(response.connection)

  Response::Body.new(stream, :encoding => response.body.encoding)
end

:request => response.request
}

Response.new(options)
end

def stream_for(connection)
Response::Body.new(Response::Inflater.new(connection))
def stream_for(connection, encoding: Encoding::BINARY)
Response::Body.new(Response::Inflater.new(connection), :encoding => encoding)
Comment on lines +27 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stream_for can and should be private, and encoding: seems like don't need default value.

end

private
Expand Down
5 changes: 5 additions & 0 deletions lib/http/response/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Body
# @return [HTTP::Connection]
attr_reader :connection

# The charset encoding determined by the Response
#
# @return [Encoding]
attr_reader :encoding

def initialize(stream, encoding: Encoding::BINARY)
@stream = stream
@connection = stream.is_a?(Inflater) ? stream.connection : stream
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/http/features/auto_inflate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
end
end

context "for gzip Content-Encoding header with charset" do
let(:headers) { {:content_encoding => "gzip", :content_type => "text/html; charset=Shift_JIS"} }

it "returns a HTTP::Response with the encoding from the response charset" do
expect(result.body.encoding).to be Encoding::Shift_JIS
end
end

# TODO(ixti): We should refactor API to either make uri non-optional,
# or add reference to request into response object (better).
context "when response has uri" do
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,15 @@ def setsockopt(*args)

expect(response.to_s).to eq("")
end

it "returns decoded body with the correct charset" do
encoding = Encoding::Shift_JIS
client = HTTP.use(:auto_inflate).headers("Accept-Encoding" => "gzip").encoding(encoding)
body = "もしもし!".encode(encoding, Encoding::UTF_8)
response = client.post("#{dummy.endpoint}/encoded-body", :body => body)
Comment on lines +460 to +464
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling to understand this test.

client   = HTTP.use(:auto_inflate).headers("Accept-Encoding" => "gzip").encoding(encoding)

^^^ encoding(...) enforces encoding of the response.


expect(response.to_s).to eq("#{body}-gzipped")
end
end

context "with :normalize_uri" do
Expand Down