From 81b223b3fda224b46f1be1b50dff346619c2d45e Mon Sep 17 00:00:00 2001 From: Aditya <152765789+adityawrk@users.noreply.github.com> Date: Sun, 29 Nov 2026 07:50:17 +0530 Subject: [PATCH 1/2] Test a Content-Type parameter that has no value type_params and body encoding both read that parameter. A header such as "text/html; charset" used to raise NoMethodError. --- test/net/http/test_httpheader.rb | 6 ++++++ test/net/http/test_httpresponse.rb | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/test/net/http/test_httpheader.rb b/test/net/http/test_httpheader.rb index b86d4868..bcda1c90 100644 --- a/test/net/http/test_httpheader.rb +++ b/test/net/http/test_httpheader.rb @@ -512,6 +512,12 @@ def test_type_params assert_equal({'charset' => 'iso-2022-jp'}, @c.type_params) @c.content_type = 'text' assert_equal({}, @c.type_params) + @c.content_type = 'text/html; charset' + assert_equal({}, @c.type_params) + @c.content_type = 'text/html; charset; boundary=foo' + assert_equal({'boundary' => 'foo'}, @c.type_params) + @c.content_type = 'text/html; charset=' + assert_equal({'charset' => ''}, @c.type_params) end def test_set_content_type diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 7c213f30..d0813707 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -382,6 +382,30 @@ def test_read_body_body_encoding_true_without_content_type_header assert_equal Encoding::ASCII_8BIT, body.encoding end + def test_read_body_body_encoding_true_with_valueless_content_type_parameter + body = "hello" + io = dummy_io(< Date: Sun, 29 Nov 2026 07:50:17 +0530 Subject: [PATCH 2/2] Ignore a Content-Type parameter that has no value type_params called strip on the missing half of a parameter, so a header such as "text/html; charset" raised NoMethodError. An empty value is still kept. --- lib/net/http/header.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index d6f12956..c6902f50 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -795,12 +795,16 @@ def sub_type # res['content-type'] # => "application/json; charset=utf-8" # res.type_params # => {"charset"=>"utf-8"} # + # A parameter with no =, such as text/html; charset, + # is ignored. An empty value, such as charset=, is kept. + # def type_params result = {} list = self['Content-Type'].to_s.split(';') list.shift list.each do |param| - k, v = *param.split('=', 2) + k, v = param.split('=', 2) + next unless v result[k.strip] = v.strip end result