Skip to content

Ignore a Content-Type parameter that has no value - #380

Open
adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/content-type-valueless-param
Open

adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/content-type-valueless-param

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #375.

Net::HTTPHeader#type_params split each parameter on = and called strip on both halves. A parameter with no =, such as Content-Type: text/html; charset, left the value nil and raised NoMethodError. Net::HTTPResponse#detect_encoding uses the same method when body encoding is enabled, so reading the body raised it too. NoMethodError is not a Net::HTTP error, so the rescues documented for protocol failures do not catch it.

A parameter without = is now skipped. charset= still records an empty value, and a later parameter on the same header is still parsed.

Before this change, type_params on text/html; charset raised undefined method 'strip' for nil. After it, that header returns {}, text/html; charset; boundary=foo returns {"boundary"=>"foo"}, and text/html; charset= returns {"charset"=>""}.

ruby -Ilib:test/lib -rhelper test/net/http/test_httpheader.rb: 41 tests, 205 assertions, 0 failures.
ruby -Ilib:test/lib -rhelper test/net/http/test_httpresponse.rb: 50 tests, 110 assertions, 0 failures. The new example reads a body with body_encoding = true and Content-Type: text/plain; charset.

type_params and body encoding both read that parameter. A header such
as "text/html; charset" used to raise NoMethodError.
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Net::HTTPHeader#type_params raises NoMethodError on a valueless Content-Type parameter, crashing callers

1 participant