0
0
Fork 0

Remove 1.8 fixes

The fixes for Ruby 1.8 will take place in a seperate gem. So I remove
all the changes here.
This commit is contained in:
Stormwind 2012-12-23 12:23:14 +01:00
parent ca2273e700
commit 65b1d3a1da
3 changed files with 0 additions and 239 deletions

View File

@ -1,5 +1,3 @@
require 'zero/patches/uri'
module Zero
require 'zero/controller'
require 'zero/router'

View File

@ -1,90 +0,0 @@
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 it will run an onw implementation, without any
# encoding functionality.
#
# @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 self.decode_www_form_18 query
end
# Own implementation of decode_www_form.
# Shall behave almost like the original method, but without any encoding
# stuff.
#
# @param [String] query The query string
# @return [Array] Parsed query
#
def self.decode_www_form_18(query)
return [] if query.empty?
unless query.match /^[^#=;&]*=[^#=;&]*([;&][^#=;&]*=[^#=;&]*)*$/
raise ArgumentError,
"invalid data of application/x-www-form-urlencoded (#{query})"
end
parsed = []
# breakes the string at & and ;
query.split(/[&;]/).each do |query_part|
# breakes the string parts at =
key, value = query_part.split('=')
# make an empty string out of value, if it's nil
value = '' if value.nil?
# append the key value pair on the result array
parsed << [
decode_www_form_component_18(key),
decode_www_form_component_18(value)
]
end
# return result array
return parsed
end
TBLDECWWWCOMP18_ = {}
# Own implementation of decode_www_form_component.
# Shall behave almost like the original method, but without any encoding
# stuff.
#
# @param [String] string
# @return [String]
#
def self.decode_www_form_component_18(string)
# Fill table for URI special chars
if TBLDECWWWCOMP18_.empty?
tbl = {}
256.times do |i|
h, l = i>>4, i&15
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
TBLDECWWWCOMP18_.replace(tbl)
TBLDECWWWCOMP18_.freeze
rescue
end
end
# unless /\A[^%]*(?:%\h\h[^%]*)*\z/ =~ str
# raise ArgumentError, "invalid %-encoding (#{str})"
# end
# Replace URI special chars
string.gsub(/\+|%[a-zA-Z0-9]{2}/) do |sub_string|
TBLDECWWWCOMP18_[sub_string]
end
end
end

View File

@ -1,147 +0,0 @@
# encoding: UTF-8
require 'spec_helper'
describe URI, '#decode_www_form_18' do
it 'seperates parameter into an array' do
result = URI::decode_www_form_18("foo=bar&bar=foo")
result.should eq([['foo', 'bar'], ['bar', 'foo']])
end
it 'can handle more than two equal parameter names' do
result = URI::decode_www_form_18("foo=bar1&foo=bar2")
result.should eq([['foo', 'bar1'], ['foo', 'bar2']])
end
it 'can handle whitespaces in query string' do
result = URI::decode_www_form_18("foo=bar&bar=bar%20foo")
result.should eq([['foo', 'bar'], ['bar', 'bar foo']])
end
it 'accepts semi-colons as seperators' do
result = URI::decode_www_form_18("foo=bar;bar=foo")
result.should eq([['foo', 'bar'], ['bar', 'foo']])
end
it 'seperates & and ; mixed queries properly' do
result = URI::decode_www_form_18("foo=bar&bar=foo;baz=foo")
result.should eq([['foo', 'bar'], ['bar', 'foo'], ['baz', 'foo']])
end
it 'does not accept only a normal string as query string' do
expect{
result = URI::decode_www_form_18("foo")
# does not work, probably should?
#result.should eq([['foo', '']])
}.to raise_error(
ArgumentError,
"invalid data of application/x-www-form-urlencoded (foo)"
)
end
it 'accepts empty values' do
result = URI::decode_www_form_18("foo=bar&bar=&baz=foo")
result.should eq([['foo', 'bar'], ['bar', ''], ['baz', 'foo']])
end
it 'understands plus as whitespace' do
result = URI::decode_www_form_18("foo=bar&bar=bar+foo")
result.should eq([['foo', 'bar'], ['bar', 'bar foo']])
end
it 'does not accept whitespaces in query string' do
result = URI::decode_www_form_18("foo=bar&bar=bar foo&baz=foo")
# Works, it probably shouldn't?
result.should eq([['foo', 'bar'], ['bar', 'bar foo'], ['baz', 'foo']])
end
it 'can handle non ascii letters in query string' do
result = URI::decode_www_form_18("foo=bär&bar=föö")
# Works, but it maybe shouldn't?
result.should eq([['foo', 'bär'], ['bar', 'föö']])
end
it 'can handle escaped non ascii letters in query string' do
result = URI::decode_www_form_18("foo=b%C3%A4r&bar=f%C3%B6%C3%B6")
result.should eq([['foo', 'bär'], ['bar', 'föö']])
end
it 'accepts - in query string' do
result = URI::decode_www_form_18("foo-bar=bar&bar=foo-bar")
puts (URI::decode_www_form_18("foo-bar=bar&bar=foo-bar") == [['foo-bar', 'bar'], ['bar', 'foo-bar']]).inspect
result.should eq([['foo-bar', 'bar'], ['bar', 'foo-bar']])
end
it 'accepts . in query string' do
result = URI::decode_www_form_18("foo.bar=bar&bar=foo.bar")
result.should eq([['foo.bar', 'bar'], ['bar', 'foo.bar']])
end
it 'accepts ~ in query string' do
result = URI::decode_www_form_18("foo~bar=bar&bar=foo~bar")
result.should eq([['foo~bar', 'bar'], ['bar', 'foo~bar']])
end
it 'accepts _ in query string' do
result = URI::decode_www_form_18("foo_bar=bar&bar=foo_bar")
result.should eq([['foo_bar', 'bar'], ['bar', 'foo_bar']])
end
it 'handles [ ] in query string' do
result = URI::decode_www_form_18("foo[]=foo&foo[]=bar")
result.should eq([['foo[]', 'foo'], ['foo[]', 'bar']])
end
it 'returns an empty array, if query string is empty' do
result = URI::decode_www_form_18("")
result.should eq([])
end
it 'throws an error, if more than one = without an & or ; in between' do
expect {
result = URI::decode_www_form_18("foo=bar=foo&bar=foo=bar")
}.to raise_error(
ArgumentError,
"invalid data of application/x-www-form-urlencoded "+
"(foo=bar=foo&bar=foo=bar)"
)
end
it 'throws an error, if more than one & without an = in between' do
expect {
result = URI::decode_www_form_18("foo&bar=foo&bar")
}.to raise_error(
ArgumentError,
"invalid data of application/x-www-form-urlencoded (foo&bar=foo&bar)"
)
end
it 'throws an error, if more than one ; without an = in between' do
expect {
result = URI::decode_www_form_18("foo;bar=foo;bar")
}.to raise_error(
ArgumentError,
"invalid data of application/x-www-form-urlencoded (foo;bar=foo;bar)"
)
end
end