From 443958f8859a47751f7e57deff6b3eda0c13e5ad Mon Sep 17 00:00:00 2001 From: Stormwind Date: Sat, 22 Dec 2012 11:44:26 +0100 Subject: Add patch for URI decode_www_form Plus some specs. This should it make later possible to make Zero work with Ruby 1.8. But at the moment it only routes to the original decode_www_form method, if it exists. Otherwise it returns an empty Array. So this will still fail on the 1.8 versions at the moment. --- lib/zero.rb | 1 + lib/zero/patches/uri.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 lib/zero/patches/uri.rb (limited to 'lib') diff --git a/lib/zero.rb b/lib/zero.rb index 03c77b8..1b3d32c 100644 --- a/lib/zero.rb +++ b/lib/zero.rb @@ -1,6 +1,7 @@ if RUBY_VERSION <= '1.9' require 'backports' end +require 'zero/patches/uri' module Zero require 'zero/controller' diff --git a/lib/zero/patches/uri.rb b/lib/zero/patches/uri.rb new file mode 100644 index 0000000..fb821b2 --- /dev/null +++ b/lib/zero/patches/uri.rb @@ -0,0 +1,45 @@ +module URI + + # Gets a query string and splits it into its key value pairs. + # If URI already supports this functionality (decode_www_form on Ruby 1.9+), + # it will use this. Else ... nothing at the moment. + # + # @param [String] query The query string + # @return [Array] Parsed query + # + def self.parse_query_string(query) + # Call the original decode_www_form method on ruby 1.9+ + if URI::respond_to? 'decode_www_form' + return self.decode_www_form query + end + + # else split the query self + return [] + end + +=begin + WFKV_ = '(?:[^%#=;&]*(?:%\h\h[^%#=;&]*)*)' + + def self.decode_www_form(str, enc= nil) + return [] if str.empty? + unless /\A#{WFKV_}=#{WFKV_}(?:[;&]#{WFKV_}=#{WFKV_})*\z/ =~ str + raise ArgumentError, + "invalid data of application/x-www-form-urlencoded (#{str})" + end + ary = [] + $&.scan(/([^=;&]+)=([^;&]*)/) do + ary << [ + decode_www_form_component($1, enc), + decode_www_form_component($2, enc) + ] + end + ary + end + + def self.decode_www_form_component(str, enc= nil) + raise ArgumentError, "invalid %-encoding (#{str})" unless /\A[^%]*(?:%\h\h[^%]*)*\z/ =~ str + str.gsub(/\+|%\h\h/, TBLDECWWWCOMP_).force_encoding(enc) + end +=end + +end -- cgit v1.2.3-70-g09d2