From 8f5f4fcfaa661d1738379c32ba9ce4ad00cf1534 Mon Sep 17 00:00:00 2001 From: Ben Ubois Date: Thu, 4 Aug 2022 02:16:01 +0200 Subject: [PATCH 1/3] Fix AutoInflate with encoding other than Encoding::BINARY. --- lib/http/features/auto_inflate.rb | 6 +++--- lib/http/response/body.rb | 5 +++++ spec/lib/http/features/auto_inflate_spec.rb | 8 ++++++++ spec/lib/http_spec.rb | 9 +++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/http/features/auto_inflate.rb b/lib/http/features/auto_inflate.rb index e1d383ef..6e58ceae 100644 --- a/lib/http/features/auto_inflate.rb +++ b/lib/http/features/auto_inflate.rb @@ -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, response.body.encoding), :request => response.request } Response.new(options) end - def stream_for(connection) - Response::Body.new(Response::Inflater.new(connection)) + def stream_for(connection, encoding) + Response::Body.new(Response::Inflater.new(connection), :encoding => encoding) end private diff --git a/lib/http/response/body.rb b/lib/http/response/body.rb index d4406a80..9fcf871b 100644 --- a/lib/http/response/body.rb +++ b/lib/http/response/body.rb @@ -15,6 +15,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 diff --git a/spec/lib/http/features/auto_inflate_spec.rb b/spec/lib/http/features/auto_inflate_spec.rb index f6c345d0..1a46c1e0 100644 --- a/spec/lib/http/features/auto_inflate_spec.rb +++ b/spec/lib/http/features/auto_inflate_spec.rb @@ -64,6 +64,14 @@ expect(result.body).to be_instance_of HTTP::Response::Body 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). diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index f7a1bb21..685ba42d 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -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) + + expect(response.to_s).to eq("#{body}-gzipped") + end end context "with :normalize_uri" do From 465e7f988815e8930f7f58de8418abc1357ec10b Mon Sep 17 00:00:00 2001 From: Ben Ubois Date: Thu, 4 Aug 2022 02:21:11 +0200 Subject: [PATCH 2/3] Rubocop fixes. --- lib/http/response/body.rb | 2 +- spec/lib/http/features/auto_inflate_spec.rb | 2 +- spec/lib/http_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/http/response/body.rb b/lib/http/response/body.rb index 9fcf871b..f41a6550 100644 --- a/lib/http/response/body.rb +++ b/lib/http/response/body.rb @@ -15,7 +15,7 @@ class Body # # @return [HTTP::Connection] attr_reader :connection - + # The charset encoding determined by the Response # # @return [Encoding] diff --git a/spec/lib/http/features/auto_inflate_spec.rb b/spec/lib/http/features/auto_inflate_spec.rb index 1a46c1e0..b87430d2 100644 --- a/spec/lib/http/features/auto_inflate_spec.rb +++ b/spec/lib/http/features/auto_inflate_spec.rb @@ -64,7 +64,7 @@ expect(result.body).to be_instance_of HTTP::Response::Body end end - + context "for gzip Content-Encoding header with charset" do let(:headers) { {:content_encoding => "gzip", :content_type => "text/html; charset=Shift_JIS"} } diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index 685ba42d..3b97c577 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -456,7 +456,7 @@ 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) From 45d7cb058e77b9b28d7efedc5f1c1b114584d960 Mon Sep 17 00:00:00 2001 From: Ben Ubois Date: Thu, 4 Aug 2022 02:55:17 +0200 Subject: [PATCH 3/3] Add keyword with default argument for encoding. --- lib/http/features/auto_inflate.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/http/features/auto_inflate.rb b/lib/http/features/auto_inflate.rb index 6e58ceae..3f253341 100644 --- a/lib/http/features/auto_inflate.rb +++ b/lib/http/features/auto_inflate.rb @@ -17,14 +17,14 @@ def wrap_response(response) :headers => response.headers, :proxy_headers => response.proxy_headers, :connection => response.connection, - :body => stream_for(response.connection, response.body.encoding), + :body => stream_for(response.connection, :encoding => response.body.encoding), :request => response.request } Response.new(options) end - def stream_for(connection, encoding) + def stream_for(connection, encoding: Encoding::BINARY) Response::Body.new(Response::Inflater.new(connection), :encoding => encoding) end