Skip to content
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: 5 additions & 1 deletion lib/net/http/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>=</tt>, such as <tt>text/html; charset</tt>,
# is ignored. An empty value, such as <tt>charset=</tt>, 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
Expand Down
6 changes: 6 additions & 0 deletions test/net/http/test_httpheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/net/http/test_httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<EOS)
HTTP/1.1 200 OK
Connection: close
Content-Length: #{body.bytesize}
Content-Type: text/plain; charset

#{body}
EOS

res = Net::HTTPResponse.read_new(io)
res.body_encoding = true

read = nil

res.reading_body io, true do
read = res.read_body
end

assert_equal "hello", read
assert_equal({}, res.type_params)
end

def test_read_body_body_encoding_true_with_utf8_content_type_header
body = "hello\u1234"
io = dummy_io(<<EOS)
Expand Down