From 8aa741c7fbebf0453eb7bf1e0be652459b87c1d5 Mon Sep 17 00:00:00 2001 From: Grant Hollingworth Date: Wed, 28 Jun 2023 15:15:07 -0700 Subject: [PATCH 1/2] handle empty attributes --- lib/net/http/response.rb | 9 +++------ test/net/http/test_httpresponse.rb | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 43a4a698..212d170c 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -519,19 +519,16 @@ def get_attribute(ss) case ss.peek(1) when '"' ss.getch - value = ss.scan(/[^"]+/) - value.downcase! + value = ss.scan(/[^"]+/)&.downcase ss.getch when "'" ss.getch - value = ss.scan(/[^']+/) - value.downcase! + value = ss.scan(/[^']+/)&.downcase ss.getch when '>' value = '' else - value = ss.scan(/[^\t\n\f\r >]+/) - value.downcase! + value = ss.scan(/[^\t\n\f\r >]+/)&.downcase end [name, value] end diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 99a94674..7e754099 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -249,6 +249,30 @@ def test_read_body_body_encoding_true_with_utf8_meta_content_charset Content-Length: #{res_body.bytesize} Content-Type: text/html +#{res_body} +EOS + + res = Net::HTTPResponse.read_new(io) + res.body_encoding = true + + body = nil + + res.reading_body io, true do + body = res.read_body + end + + assert_equal res_body, body + assert_equal Encoding::UTF_8, body.encoding + end + + def test_read_body_body_encoding_true_with_empty_meta_content + res_body = "hello\u1234" + io = dummy_io(< Date: Wed, 28 Jun 2023 15:26:25 -0700 Subject: [PATCH 2/2] handle multiple meta elements --- lib/net/http/response.rb | 8 ++++---- test/net/http/test_httpresponse.rb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 212d170c..e7102026 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -463,7 +463,7 @@ def check_bom(str) def scanning_meta(str) require 'strscan' ss = StringScanner.new(str) - if ss.scan_until(/