Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix Rack::Utils.escape in the case when $KCODE='U'

CGI.escape backport used in this case
  • Loading branch information...
commit 4eab6501a205021a40c4dfa394f5819f7248f8bb 1 parent 413155a
@be9 be9 authored rkh committed
Showing with 40 additions and 12 deletions.
  1. +18 −12 lib/rack/backports/uri/common.rb
  2. +22 −0 test/spec_utils.rb
View
30 lib/rack/backports/uri/common.rb
@@ -20,20 +20,26 @@ 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|
- tbl[i.chr] = '%%%02X' % i
- end
- tbl[' '] = '+'
- begin
- TBLENCWWWCOMP_.replace(tbl)
- TBLENCWWWCOMP_.freeze
- rescue
+ if RUBY_VERSION < "1.9" && $KCODE =~ /u/i
+ str.gsub(/([^ a-zA-Z0-9_.-]+)/) do
+ '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
+ end.tr(' ', '+')
+ else
+ if TBLENCWWWCOMP_.empty?
+ tbl = {}
+ 256.times do |i|
+ tbl[i.chr] = '%%%02X' % i
+ end
+ tbl[' '] = '+'
+ begin
+ TBLENCWWWCOMP_.replace(tbl)
+ TBLENCWWWCOMP_.freeze
+ rescue
+ end
end
+ str = str.to_s
+ str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]}
end
- str = str.to_s
- str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]}
end
# Decode given +str+ of URL-encoded form data.
View
22 test/spec_utils.rb
@@ -19,6 +19,28 @@
Rack::Utils.escape(matz_name_sep).should.equal '%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8'
end
+ if RUBY_VERSION[/^\d+\.\d+/] == '1.8'
+ should "escape correctly for multibyte characters if $KCODE is set to 'U'" do
+ default_kcode, $KCODE = $KCODE, 'U'
+
+ matz_name = "\xE3\x81\xBE\xE3\x81\xA4\xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0] # Matsumoto
+ matz_name.force_encoding("UTF-8") if matz_name.respond_to? :force_encoding
+ Rack::Utils.escape(matz_name).should.equal '%E3%81%BE%E3%81%A4%E3%82%82%E3%81%A8'
+ matz_name_sep = "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0] # Matsu moto
+ matz_name_sep.force_encoding("UTF-8") if matz_name_sep.respond_to? :force_encoding
+ Rack::Utils.escape(matz_name_sep).should.equal '%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8'
+
+ $KCODE = default_kcode
+ end
+
+ should "unescape multibyte characters correctly if $KCODE is set to 'U'" do
+ default_kcode, $KCODE = $KCODE, 'U'
+ Rack::Utils.unescape('%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8').should.equal(
+ "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0])
+ $KCODE = default_kcode
+ end
+ end
+
if "".respond_to?(:encode)
should "escape non-UTF8 strings" do
Rack::Utils.escape("ø".encode("ISO-8859-1")).should.equal "%F8"
Please sign in to comment.
Something went wrong with that request. Please try again.