cgi_methods.rb 5.2 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
require 'cgi'
2
require 'action_controller/vendor/xml_simple'
3
require 'action_controller/vendor/xml_node'
D
Initial  
David Heinemeier Hansson 已提交
4 5 6 7 8 9 10 11 12 13 14

# Static methods for parsing the query and request parameters that can be used in
# a CGI extension class or testing in isolation.
class CGIMethods #:nodoc:
  public
    # Returns a hash with the pairs from the query string. The implicit hash construction that is done in
    # parse_request_params is not done here.
    def CGIMethods.parse_query_parameters(query_string)
      parsed_params = {}
  
      query_string.split(/[&;]/).each { |p| 
15 16 17
        # Ignore repeated delimiters.
        next if p.empty?

18
        k, v = p.split('=',2)
19
        v = nil if (v && v.empty?)
D
Initial  
David Heinemeier Hansson 已提交
20

21 22
        k = CGI.unescape(k) if k
        v = CGI.unescape(v) if v
D
Initial  
David Heinemeier Hansson 已提交
23

24 25 26 27 28 29 30 31 32 33 34
        unless k.include?(?[)
          parsed_params[k] = v
        else
          keys = split_key(k)
          last_key = keys.pop
          last_key = keys.pop if (use_array = last_key.empty?)
          parent = keys.inject(parsed_params) {|h, k| h[k] ||= {}}
          
          if use_array then (parent[last_key] ||= []) << v
          else parent[last_key] = v
          end
D
Initial  
David Heinemeier Hansson 已提交
35 36 37
        end
      }
  
38
      parsed_params
D
Initial  
David Heinemeier Hansson 已提交
39
    end
40

D
Initial  
David Heinemeier Hansson 已提交
41 42 43 44 45 46 47 48
    # Returns the request (POST/GET) parameters in a parsed form where pairs such as "customer[address][street]" / 
    # "Somewhere cool!" are translated into a full hash hierarchy, like
    # { "customer" => { "address" => { "street" => "Somewhere cool!" } } }
    def CGIMethods.parse_request_parameters(params)
      parsed_params = {}

      for key, value in params
        value = [value] if key =~ /.*\[\]$/
49 50 51 52 53 54 55
        unless key.include?('[')
          # much faster to test for the most common case first (GET)
          # and avoid the call to build_deep_hash
          parsed_params[key] = get_typed_value(value[0])
        else
          build_deep_hash(get_typed_value(value[0]), parsed_params, get_levels(key))
        end
D
Initial  
David Heinemeier Hansson 已提交
56 57
      end
    
58
      parsed_params
D
Initial  
David Heinemeier Hansson 已提交
59 60
    end

61 62
    def self.parse_formatted_request_parameters(mime_type, raw_post_data)
      params = case strategy = ActionController::Base.param_parsers[mime_type]
63 64 65
        when Proc
          strategy.call(raw_post_data)
        when :xml_simple
66
          XmlSimple.xml_in(raw_post_data, 'ForceArray' => false, 'keeproot' => true)
67 68 69 70 71
        when :yaml
          YAML.load(raw_post_data)
        when :xml_node
          node = XmlNode.from_xml(raw_post_data)
          { node.node_name => node }
72 73 74
      end
      
      params || {}
75 76
    rescue Object => e
      { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, 
J
Jamis Buck 已提交
77
        "raw_post_data" => raw_post_data, "format" => mime_type }
78 79
    end

D
Initial  
David Heinemeier Hansson 已提交
80
  private
81 82 83 84 85 86 87 88 89 90 91

    # Splits the given key into several pieces. Example keys are 'name', 'person[name]',
    # 'person[name][first]', and 'people[]'. In each instance, an Array instance is returned.
    # 'person[name][first]' produces ['person', 'name', 'first']; 'people[]' produces ['people', '']
    def CGIMethods.split_key(key)
      if /^([^\[]+)((?:\[[^\]]*\])+)$/ =~ key
        keys = [$1]
        
        keys.concat($2[1..-2].split(']['))
        keys << '' if key[-2..-1] == '[]' # Have to add it since split will drop empty strings
        
92
        keys
93
      else
94
        [key]
95 96 97
      end
    end
    
D
Initial  
David Heinemeier Hansson 已提交
98
    def CGIMethods.get_typed_value(value)
99 100 101
      # test most frequent case first
      if value.is_a?(String)
        value
102
      elsif value.respond_to?(:content_type) && ! value.content_type.blank?
D
Initial  
David Heinemeier Hansson 已提交
103
        # Uploaded file
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        unless value.respond_to?(:full_original_filename)
          class << value
            alias_method :full_original_filename, :original_filename

            # Take the basename of the upload's original filename.
            # This handles the full Windows paths given by Internet Explorer
            # (and perhaps other broken user agents) without affecting
            # those which give the lone filename.
            # The Windows regexp is adapted from Perl's File::Basename.
            def original_filename
              if md = /^(?:.*[:\\\/])?(.*)/m.match(full_original_filename)
                md.captures.first
              else
                File.basename full_original_filename
              end
            end
          end
        end

        # Return the same value after overriding original_filename.
D
Initial  
David Heinemeier Hansson 已提交
124
        value
125

D
Initial  
David Heinemeier Hansson 已提交
126 127 128 129
      elsif value.respond_to?(:read)
        # Value as part of a multipart request
        value.read
      elsif value.class == Array
130
        value.collect { |v| CGIMethods.get_typed_value(v) }
D
Initial  
David Heinemeier Hansson 已提交
131
      else
132
        # other value (neither string nor a multipart request)
D
Initial  
David Heinemeier Hansson 已提交
133 134 135 136
        value.to_s
      end
    end
  
137 138 139 140 141 142 143 144 145 146 147
    PARAMS_HASH_RE = /^([^\[]+)(\[.*\])?(.)?.*$/
    def CGIMethods.get_levels(key)
      all, main, bracketed, trailing = PARAMS_HASH_RE.match(key).to_a
      if main.nil?
        []
      elsif trailing
        [key]
      elsif bracketed
        [main] + bracketed.slice(1...-1).split('][')
      else
        [main]
D
Initial  
David Heinemeier Hansson 已提交
148 149
      end
    end
150

D
Initial  
David Heinemeier Hansson 已提交
151 152
    def CGIMethods.build_deep_hash(value, hash, levels)
      if levels.length == 0
153
        value
D
Initial  
David Heinemeier Hansson 已提交
154 155 156 157 158 159
      elsif hash.nil?
        { levels.first => CGIMethods.build_deep_hash(value, nil, levels[1..-1]) }
      else
        hash.update({ levels.first => CGIMethods.build_deep_hash(value, hash[levels.first], levels[1..-1]) })
      end
    end
160
end