lib/rack/utils.rb in rack-1.4.1 vs lib/rack/utils.rb in rack-1.4.2
- old
+ new
@@ -3,15 +3,18 @@
require 'set'
require 'tempfile'
require 'rack/multipart'
major, minor, patch = RUBY_VERSION.split('.').map { |v| v.to_i }
+ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
if major == 1 && minor < 9
require 'rack/backports/uri/common_18'
-elsif major == 1 && minor == 9 && patch < 3
+elsif major == 1 && minor == 9 && patch == 2 && RUBY_PATCHLEVEL <= 320 && RUBY_ENGINE != 'jruby'
require 'rack/backports/uri/common_192'
+elsif major == 1 && minor == 9 && patch == 3 && RUBY_PATCHLEVEL < 125
+ require 'rack/backports/uri/common_193'
else
require 'uri/common'
end
module Rack
@@ -58,15 +61,19 @@
# Stolen from Mongrel, with some small modifications:
# Parses a query string by breaking it up at the '&'
# and ';' characters. You can also use this to parse
# cookies by changing the characters used in the second
# parameter (which defaults to '&;').
- def parse_query(qs, d = nil)
+ def parse_query(qs, d = nil, &unescaper)
+ unescaper ||= method(:unescape)
+
params = KeySpaceConstrainedParams.new
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
- k, v = p.split('=', 2).map { |x| unescape(x) }
+ next if p.empty?
+ k, v = p.split('=', 2).map(&unescaper)
+ next unless k || v
if cur = params[k]
if cur.class == Array
params[k] << v
else
@@ -307,20 +314,20 @@
# Returns nil if the header is missing or syntactically invalid.
# Returns an empty array if none of the ranges are satisfiable.
def byte_ranges(env, size)
# See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35>
http_range = env['HTTP_RANGE']
- return nil unless http_range
+ return nil unless http_range && http_range =~ /bytes=([^;]+)/
ranges = []
- http_range.split(/,\s*/).each do |range_spec|
- matches = range_spec.match(/bytes=(\d*)-(\d*)/)
- return nil unless matches
- r0,r1 = matches[1], matches[2]
+ $1.split(/,\s*/).each do |range_spec|
+ return nil unless range_spec =~ /(\d*)-(\d*)/
+ r0,r1 = $1, $2
if r0.empty?
return nil if r1.empty?
# suffix-byte-range-spec, represents trailing suffix of file
- r0 = [size - r1.to_i, 0].max
+ r0 = size - r1.to_i
+ r0 = 0 if r0 < 0
r1 = size - 1
else
r0 = r0.to_i
if r1.empty?
r1 = size - 1
@@ -440,10 +447,10 @@
def [](key)
@params[key]
end
def []=(key, value)
- @size += key.size unless @params.key?(key)
+ @size += key.size if key && !@params.key?(key)
raise RangeError, 'exceeded available parameter key space' if @size > @limit
@params[key] = value
end
def key?(key)