diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-04-12 10:20:50 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-04-12 10:20:50 +0000 |
commit | 8553b70a39bf4707cee6d667fc5903f09bf0a525 (patch) | |
tree | 1e25dcd6fc49e68ef38943d0eb7cca4800892708 /lib | |
parent | dd11a58be3d6eba910a8bcfdb888562fb64fde01 (diff) |
* lib/uri/common.rb: avoid race condition. fixes #4572
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31266 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/uri/common.rb | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/lib/uri/common.rb b/lib/uri/common.rb index e2ccbdc846..3bfe33ac64 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -743,11 +743,16 @@ module URI # See URI.decode_www_form_component, URI.encode_www_form def self.encode_www_form_component(str) if TBLENCWWWCOMP_.empty? + tbl = {} 256.times do |i| - TBLENCWWWCOMP_[i.chr] = '%%%02X' % i + tbl[i.chr] = '%%%02X' % i + end + tbl[' '] = '+' + begin + TBLENCWWWCOMP_.replace(tbl) + TBLENCWWWCOMP_.freeze + rescue end - TBLENCWWWCOMP_[' '] = '+' - TBLENCWWWCOMP_.freeze end str = str.to_s if HTML5ASCIIINCOMPAT.include?(str.encoding) @@ -767,15 +772,20 @@ module URI # See URI.encode_www_form_component, URI.decode_www_form def self.decode_www_form_component(str, enc=Encoding::UTF_8) if TBLDECWWWCOMP_.empty? + tbl = {} 256.times do |i| h, l = i>>4, i&15 - TBLDECWWWCOMP_['%%%X%X' % [h, l]] = i.chr - TBLDECWWWCOMP_['%%%x%X' % [h, l]] = i.chr - TBLDECWWWCOMP_['%%%X%x' % [h, l]] = i.chr - TBLDECWWWCOMP_['%%%x%x' % [h, l]] = i.chr + tbl['%%%X%X' % [h, l]] = i.chr + tbl['%%%x%X' % [h, l]] = i.chr + tbl['%%%X%x' % [h, l]] = i.chr + tbl['%%%x%x' % [h, l]] = i.chr + end + tbl['+'] = ' ' + begin + TBLDECWWWCOMP_.replace(tbl) + TBLDECWWWCOMP_.freeze + rescue end - TBLDECWWWCOMP_['+'] = ' ' - TBLDECWWWCOMP_.freeze end raise ArgumentError, "invalid %-encoding (#{str})" unless /\A(?:%\h\h|[^%]+)*\z/ =~ str str.gsub(/\+|%\h\h/, TBLDECWWWCOMP_).force_encoding(enc) |