diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-14 04:24:27 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-14 04:24:27 +0000 |
commit | a20b957bafba111f95d238b66013037fe30baadb (patch) | |
tree | 532eefae940d070287ba6bc82706904dd6aea36b /lib | |
parent | a540b10c96c187005c5037bcc942262234c43b45 (diff) |
merge revision(s) 40848: [Backport #8425]
* lib/webrick/htmlutils.rb (WEBrick::HTMLUtils#escape): replace HTML
meta chars even in non-ascii string. [Bug #8425] [ruby-core:55052]
* lib/webrick/httputils.rb (WEBrick::HTTPUtils#{_escape,_unescape}):
fix %-escape encodings. [Bug #8425] [ruby-core:55052]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@44934 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/webrick/htmlutils.rb | 5 | ||||
-rw-r--r-- | lib/webrick/httputils.rb | 14 |
2 files changed, 15 insertions, 4 deletions
diff --git a/lib/webrick/htmlutils.rb b/lib/webrick/htmlutils.rb index ed901f1ce2..90994f18b8 100644 --- a/lib/webrick/htmlutils.rb +++ b/lib/webrick/htmlutils.rb @@ -15,12 +15,13 @@ module WEBrick # Escapes &, ", > and < in +string+ def escape(string) - str = string ? string.dup : "" + return "" unless string + str = string.dup.force_encoding('binary') str.gsub!(/&/n, '&') str.gsub!(/\"/n, '"') str.gsub!(/>/n, '>') str.gsub!(/</n, '<') - str + str.force_encoding(string.encoding) end module_function :escape diff --git a/lib/webrick/httputils.rb b/lib/webrick/httputils.rb index f029dacb56..d99573762c 100644 --- a/lib/webrick/httputils.rb +++ b/lib/webrick/httputils.rb @@ -350,8 +350,18 @@ module WEBrick def _make_regex(str) /([#{Regexp.escape(str)}])/n end def _make_regex!(str) /([^#{Regexp.escape(str)}])/n end - def _escape(str, regex) str.gsub(regex){ "%%%02X" % $1.ord } end - def _unescape(str, regex) str.gsub(regex){ $1.hex.chr } end + def _escape(str, regex) + str = str.dup.force_encoding('binary') + 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.dup.force_encoding('binary') + str.gsub!(regex) {$1.hex.chr} + # encoding of %-unescaped string is unknown + str + end UNESCAPED = _make_regex(control+space+delims+unwise+nonascii) UNESCAPED_FORM = _make_regex(reserved+control+delims+unwise+nonascii) |