Skip to content

Use Net::HTTPHeaderSyntaxError instead of ArgumentError #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
10 changes: 6 additions & 4 deletions lib/net/http/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ def initialize_http_header(initheader)
else
value = value.strip # raise error for invalid byte sequences
if value.count("\r\n") > 0
raise ArgumentError, "header #{key} has field value #{value.inspect}, this cannot include CR/LF"
raise Net::HTTPHeaderSyntaxError, "header #{key} has field value #{value.inspect}, this cannot include CR/LF"
end
@header[key.downcase.to_s] = [value]
end
rescue ArgumentError => e
raise Net::HTTPHeaderSyntaxError, e.message
end
end

Expand Down Expand Up @@ -82,7 +84,7 @@ def add_field(key, val)
else
val = val.to_s # for compatibility use to_s instead of to_str
if val.b.count("\r\n") > 0
raise ArgumentError, 'header field value cannot include CR/LF'
raise Net::HTTPHeaderSyntaxError, 'header field value cannot include CR/LF'
end
@header[key.downcase.to_s] = [val]
end
Expand All @@ -95,7 +97,7 @@ def add_field(key, val)
else
val = val.to_s
if /[\r\n]/n.match?(val.b)
raise ArgumentError, 'header field value cannot include CR/LF'
raise Net::HTTPHeaderSyntaxError, 'header field value cannot include CR/LF'
end
ary.push val
end
Expand Down Expand Up @@ -481,7 +483,7 @@ def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
/\Amultipart\/form-data\z/i
self.content_type = enctype
else
raise ArgumentError, "invalid enctype: #{enctype}"
raise Net::HTTPHeaderSyntaxError, "invalid enctype: #{enctype}"
end
end

Expand Down
12 changes: 6 additions & 6 deletions test/net/http/test_httpheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def test_initialize
@c.initialize_http_header([["foo", "abc"], ["bar","xyz"]])
assert_equal "xyz", @c["bar"]
assert_raise(NoMethodError){ @c.initialize_http_header("foo"=>[]) }
assert_raise(ArgumentError){ @c.initialize_http_header("foo"=>"a\nb") }
assert_raise(ArgumentError){ @c.initialize_http_header("foo"=>"a\rb") }
assert_raise(ArgumentError){ @c.initialize_http_header("foo"=>"a\xff") }
assert_raise(Net::HTTPHeaderSyntaxError){ @c.initialize_http_header("foo"=>"a\nb") }
assert_raise(Net::HTTPHeaderSyntaxError){ @c.initialize_http_header("foo"=>"a\rb") }
assert_raise(Net::HTTPHeaderSyntaxError){ @c.initialize_http_header("foo"=>"a\xff") }
end

def test_initialize_with_symbol
Expand Down Expand Up @@ -68,8 +68,8 @@ def test_ASET
@c['aaa'] = "aaa\xff"
assert_equal 2, @c.length

assert_raise(ArgumentError){ @c['foo'] = "a\nb" }
assert_raise(ArgumentError){ @c['foo'] = ["a\nb"] }
assert_raise(Net::HTTPHeaderSyntaxError){ @c['foo'] = "a\nb" }
assert_raise(Net::HTTPHeaderSyntaxError){ @c['foo'] = ["a\nb"] }
end

def test_AREF
Expand All @@ -95,7 +95,7 @@ def test_add_field
@c.add_field 'My-Header', 'd, d'
assert_equal 'a, b, c, d, d', @c['My-Header']
assert_equal ['a', 'b', 'c', 'd, d'], @c.get_fields('My-Header')
assert_raise(ArgumentError){ @c.add_field 'My-Header', "d\nd" }
assert_raise(Net::HTTPHeaderSyntaxError){ @c.add_field 'My-Header', "d\nd" }
@c.add_field 'My-Header', ['e', ["\xff", 7]]
assert_equal "a, b, c, d, d, e, \xff, 7", @c['My-Header']
assert_equal ['a', 'b', 'c', 'd, d', 'e', "\xff", '7'], @c.get_fields('My-Header')
Expand Down