summaryrefslogtreecommitdiff
path: root/lib/net/http.rb
diff options
context:
space:
mode:
authorJeremy Evans <[email protected]>2022-04-11 08:17:19 -0700
committergit <[email protected]>2022-04-12 00:17:34 +0900
commitebb4378237e572ce2e888136a613c7c051439f95 (patch)
tree13f22663021500a3aa223fcd175db1a05caf246e /lib/net/http.rb
parent4bd38e8120f2fdfdd47a34211720e048502377f1 (diff)
[ruby/net-http] Add HTTP#response_body_encoding for setting response body encoding
This allows for the ability to opt-in to a method to set the encoding of response bodies. By setting the accessor to a String or Encoding instance, it will use the specified encoding. Setting the value of true will try to detect the encoding of the response body, either using the Content-Type header (assuming it specifies charset) or by scanning for a <meta> tag in the document that specifies the encoding. The default is false in which case no forcing of encoding will be done (same as before the patch). Implements [Feature #2567] Implements [Feature #15517] https://github.com/ruby/net-http/commit/6233e6b7c1 Co-authored-by: Yui Naruse <[email protected]>
Diffstat (limited to 'lib/net/http.rb')
-rw-r--r--lib/net/http.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/net/http.rb b/lib/net/http.rb
index 3fcf23b05c..5e64e38665 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -698,6 +698,7 @@ module Net #:nodoc:
@continue_timeout = nil
@max_retries = 1
@debug_output = nil
+ @response_body_encoding = false
@proxy_from_env = false
@proxy_uri = nil
@@ -745,6 +746,18 @@ module Net #:nodoc:
# The local port used to establish the connection.
attr_accessor :local_port
+ # The encoding to use for the response body. If Encoding, uses the
+ # specified encoding. If other true value, tries to detect the response
+ # body encoding.
+ attr_reader :response_body_encoding
+
+ # Set the encoding to use for the response body. If given a String, find
+ # the related Encoding.
+ def response_body_encoding=(value)
+ value = Encoding.find(value) if value.is_a?(String)
+ @response_body_encoding = value
+ end
+
attr_writer :proxy_from_env
attr_writer :proxy_address
attr_writer :proxy_port
@@ -1592,6 +1605,7 @@ module Net #:nodoc:
begin
res = HTTPResponse.read_new(@socket)
res.decode_content = req.decode_content
+ res.body_encoding = @response_body_encoding
end while res.kind_of?(HTTPInformation)
res.uri = req.uri