summaryrefslogtreecommitdiff
path: root/net/ruby-addressable/patches/patch-lib_addressable_uri.rb
blob: 9d1a47d61570a0f43c6450f61562bac2e566ae13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$NetBSD: patch-lib_addressable_uri.rb,v 1.1 2011/11/06 07:44:28 obache Exp $

* some fixes and improvements (from upstream repo).

--- lib/addressable/uri.rb.orig	2011-11-06 07:18:52.000000000 +0000
+++ lib/addressable/uri.rb
@@ -325,7 +325,11 @@ module Addressable
       return nil if component.nil?
 
       begin
-        component = component.to_str
+        if component.kind_of?(Symbol) || component.kind_of?(Numeric)
+          component = component.to_s
+        else
+          component = component.to_str
+        end
       rescue TypeError, NoMethodError
         raise TypeError, "Can't convert #{component.class} into String."
       end if !component.is_a? String
@@ -715,6 +719,7 @@ module Addressable
         self.authority = options[:authority] if options[:authority]
         self.path = options[:path] if options[:path]
         self.query = options[:query] if options[:query]
+        self.query_values = options[:query_values] if options[:query_values]
         self.fragment = options[:fragment] if options[:fragment]
       end
     end
@@ -1497,39 +1502,55 @@ module Addressable
         # Only to be used for non-Array inputs. Arrays should preserve order.
         new_query_values.sort!
       end
-      # new_query_values have form [['key1', 'value1'], ['key2', 'value2']]
 
-      # Algorithm shamelessly stolen from Julien Genestoux, slightly modified
-      buffer = ""
-      stack = []
-      e = lambda do |component|
-        component = component.to_s if component.kind_of?(Symbol)
-        URI.encode_component(component, CharacterClasses::UNRESERVED)
-      end
-      new_query_values.each do |key, value|
-        if value.kind_of?(Hash)
-          stack << [key, value]
-        elsif value.kind_of?(Array)
-          stack << [
-            key,
-            value.inject({}) { |accu, x| accu[accu.size.to_s] = x; accu }
-          ]
+      ##
+      # Joins and converts parent and value into a properly encoded and
+      # ordered URL query.
+      #
+      # @private
+      # @param [String] parent an URI encoded component.
+      # @param [Array, Hash, Symbol, #to_str] value
+      #
+      # @return [String] a properly escaped and ordered URL query.
+      to_query = lambda do |parent, value|
+        if value.is_a?(Hash)
+          value = value.map do |key, val|
+            [
+              URI.encode_component(key, CharacterClasses::UNRESERVED),
+              val
+            ]
+          end
+          value.sort!
+          buffer = ""
+          value.each do |key, val|
+            new_parent = "#{parent}[#{key}]"
+            buffer << "#{to_query.call(new_parent, val)}&"
+          end
+          return buffer.chop
+        elsif value.is_a?(Array)
+          buffer = ""
+          value.each_with_index do |val, i|
+            new_parent = "#{parent}[#{i}]"
+            buffer << "#{to_query.call(new_parent, val)}&"
+          end
+          return buffer.chop
         elsif value == true
-          buffer << "#{e.call(key)}&"
+          return parent
         else
-          buffer << "#{e.call(key)}=#{e.call(value)}&"
+          encoded_value = URI.encode_component(
+            value, CharacterClasses::UNRESERVED
+          )
+          return "#{parent}=#{encoded_value}"
         end
       end
-      stack.each do |(parent, hash)|
-        (hash.sort_by { |key| key.to_s }).each do |(key, value)|
-          if value.kind_of?(Hash)
-            stack << ["#{parent}[#{key}]", value]
-          elsif value == true
-            buffer << "#{parent}[#{e.call(key)}]&"
-          else
-            buffer << "#{parent}[#{e.call(key)}]=#{e.call(value)}&"
-          end
-        end
+
+      # new_query_values have form [['key1', 'value1'], ['key2', 'value2']]
+      buffer = ""
+      new_query_values.each do |parent, value|
+        encoded_parent = URI.encode_component(
+          parent, CharacterClasses::UNRESERVED
+        )
+        buffer << "#{to_query.call(encoded_parent, value)}&"
       end
       self.query = buffer.chop
     end
@@ -2182,7 +2203,7 @@ module Addressable
         parent = normalized_path.match(NPATH2)
         if parent && ((parent[1] != PARENT1 && parent[1] != PARENT2) \
                       || (parent[2] != PARENT1 && parent[2] != PARENT2))
-          mod ||= normalized_path.gsub!(/\/#{parent[1]}\/\.\.\/|(\/#{parent[2]}\/\.\.$)/, SLASH)
+          mod ||= normalized_path.gsub!(/\/#{Regexp.escape(parent[1].to_s)}\/\.\.\/|(\/#{Regexp.escape(parent[2].to_s)}\/\.\.$)/, SLASH)
         end
 
         mod ||= normalized_path.gsub!(NPATH3, EMPTYSTR)