Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Just use CGI.escape/unescape. #140

Merged
merged 1 commit into from

6 participants

John Firebaugh Matt Culpepper Joshua Peek Aaron Patterson eli Christian Neukirchen
John Firebaugh

The previous definitions were virtually identical to those provided
by CGI in 1.8.7/1.9.2, except that Rack::Utils.escape failed on non-
UTF8 encoded input.

Tested on 1.8.7, 1.9.2 and JRuby.

See comment thread here: d8a980f

John Firebaugh jfirebaugh Just use CGI.escape/unescape.
The previous definitions were virtually identical to those provided
by CGI in 1.8.7/1.9.2, except that Rack::Utils.escape failed on non-
UTF8 encoded input.

Tested on 1.8.7, 1.9.2 and JRuby.
0700cd0
Matt Culpepper

I just found this page trying to figure out an issue with Capybara:

  Encoding::CompatibilityError:
   incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)

I fixed the issue the same way locally, and will probably end up forking for now. +1

Aaron Patterson tenderlove merged commit e7c8454 into from
Joshua Peek

I think @chneukirchen has turned this down many a times

John Firebaugh

Yeah, we discussed it here: d8a980f

How about backporting URI.encode_www_form_component for 1.8.x?

Aaron Patterson
Owner

Oops. I guess I shouldn't have merged it then. :-(

I would like a rack release soon. Can we please get people who are less green than me to review and reject or accept pull requests. I don't want to make more mistakes. :-(

Joshua Peek

I'm indifferent, but I just remembered this being controversial.

Aaron Patterson
Owner

From the comments it seemed good. Also, the code looks as if it was taken from cgi.rb in the first place and just slapped a /u on the end (which is wrong). We should probably backport and conditionally include this for 1.8.6 users.

Christian Neukirchen

:(

eli

it seems the complaint was sourcing the huge CGI library itself (2k LOC?), just to get 4 lines or so.
Maybe just require 'cgi/util' instead of all of cgi (much smaller than all of cgi at 131 LOC without comments)?

Christian Neukirchen
Owner
John Firebaugh

Yeah, 1.8 has only cgi.rb.

Aaron Patterson
Owner

doh. I will fix

eli

:(

Aaron Patterson
Owner

@cactus DON'T BE SAD! :heart:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 10, 2011
  1. John Firebaugh

    Just use CGI.escape/unescape.

    jfirebaugh authored
    The previous definitions were virtually identical to those provided
    by CGI in 1.8.7/1.9.2, except that Rack::Utils.escape failed on non-
    UTF8 encoded input.
    
    Tested on 1.8.7, 1.9.2 and JRuby.
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 10 deletions.
  1. +5 −10 lib/rack/utils.rb
  2. +8 −0 test/spec_utils.rb
15 lib/rack/utils.rb
View
@@ -3,27 +3,22 @@
require 'fileutils'
require 'set'
require 'tempfile'
+require 'cgi'
module Rack
# Rack::Utils contains a grab-bag of useful methods for writing web
# applications adopted from all kinds of Ruby libraries.
module Utils
- # Performs URI escaping so that you can construct proper
- # query strings faster. Use this rather than the cgi.rb
- # version since it's faster. (Stolen from Camping).
+ # URI escapes a string.
def escape(s)
- s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/u) {
- '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
- }.tr(' ', '+')
+ CGI.escape(s.to_s)
end
module_function :escape
- # Unescapes a URI escaped string. (Stolen from Camping).
+ # Unescapes a URI escaped string.
def unescape(s)
- s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
- [$1.delete('%')].pack('H*')
- }
+ CGI.unescape(s)
end
module_function :unescape
8 test/spec_utils.rb
View
@@ -1,3 +1,5 @@
+# -*- encoding: utf-8 -*-
+
require 'rack/utils'
require 'rack/mock'
@@ -18,6 +20,12 @@
Rack::Utils.escape(matz_name_sep).should.equal '%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8'
end
+ if "".respond_to?(:encode)
+ should "escape non-UTF8 strings" do
+ Rack::Utils.escape("ø".encode("ISO-8859-1")).should.equal "%F8"
+ end
+ end
+
should "unescape correctly" do
Rack::Utils.unescape("fo%3Co%3Ebar").should.equal "fo<o>bar"
Rack::Utils.unescape("a+space").should.equal "a space"
Something went wrong with that request. Please try again.