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
12 changes: 9 additions & 3 deletions lib/webrick/httputils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,20 @@ def parse_form_data(io, boundary)

def _make_regex(str) /([#{Regexp.escape(str)}])/n end
def _make_regex!(str) /([^#{Regexp.escape(str)}])/n end
def _ensure_string(str)
unless str.is_a?(String)
raise TypeError, "no implicit conversion of #{str.class} into String"
end
str
end
def _escape(str, regex)
str = str.b
str = _ensure_string(str).b
str.gsub!(regex) {"%%%02X" % $1.ord}
# %-escaped string should contain US-ASCII only
str.force_encoding(Encoding::US_ASCII)
end
def _unescape(str, regex)
str = str.b
str = _ensure_string(str).b
str.gsub!(regex) {$1.hex.chr}
# encoding of %-unescaped string is unknown
str
Expand Down Expand Up @@ -519,7 +525,7 @@ def escape_form(str)
# Unescapes form reserved characters in +str+

def unescape_form(str)
_unescape(str.gsub(/\+/, " "), ESCAPED)
_unescape(_ensure_string(str).gsub(/\+/, " "), ESCAPED)
end

##
Expand Down
10 changes: 10 additions & 0 deletions test/webrick/test_httputils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@ def test_escape_path
assert_equal("/foo/bar/", escape_path("/foo/bar/"))
assert_equal("/%25foo/bar/", escape_path("/%foo/bar/"))
end

def test_escape_with_nil_raises_clear_type_error
# [Bug: nil raised a confusing NoMethodError ("undefined method 'b'
# for nil:NilClass") instead of a clear, documented error - see
# https://github.com/ruby/webrick/issues/96
[:escape, :unescape, :escape_form, :unescape_form].each do |meth|
err = assert_raise(TypeError) { send(meth, nil) }
assert_equal("no implicit conversion of NilClass into String", err.message)
end
end
end