| 1 |
require 'cgi' |
|---|
| 2 |
require 'action_controller/vendor/xml_node' |
|---|
| 3 |
require 'strscan' |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
class CGIMethods |
|---|
| 8 |
class << self |
|---|
| 9 |
|
|---|
| 10 |
def parse_query_parameters(query_string) |
|---|
| 11 |
pairs = query_string.split('&').collect do |chunk| |
|---|
| 12 |
next if chunk.empty? |
|---|
| 13 |
key, value = chunk.split('=', 2) |
|---|
| 14 |
next if key.empty? |
|---|
| 15 |
value = (value.nil? || value.empty?) ? nil : CGI.unescape(value) |
|---|
| 16 |
[ CGI.unescape(key), value ] |
|---|
| 17 |
end.compact |
|---|
| 18 |
|
|---|
| 19 |
FormEncodedPairParser.new(pairs).result |
|---|
| 20 |
end |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
def parse_request_parameters(params) |
|---|
| 24 |
parser = FormEncodedPairParser.new |
|---|
| 25 |
|
|---|
| 26 |
params = params.dup |
|---|
| 27 |
until params.empty? |
|---|
| 28 |
for key, value in params |
|---|
| 29 |
if key.blank? |
|---|
| 30 |
params.delete key |
|---|
| 31 |
elsif !key.include?('[') |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
parser.result[key] = get_typed_value(value[0]) |
|---|
| 35 |
params.delete key |
|---|
| 36 |
elsif value.is_a?(Array) |
|---|
| 37 |
parser.parse(key, get_typed_value(value.shift)) |
|---|
| 38 |
params.delete key if value.empty? |
|---|
| 39 |
else |
|---|
| 40 |
raise TypeError, "Expected array, found #{value.inspect}" |
|---|
| 41 |
end |
|---|
| 42 |
end |
|---|
| 43 |
end |
|---|
| 44 |
|
|---|
| 45 |
parser.result |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
def parse_formatted_request_parameters(mime_type, raw_post_data) |
|---|
| 49 |
case strategy = ActionController::Base.param_parsers[mime_type] |
|---|
| 50 |
when Proc |
|---|
| 51 |
strategy.call(raw_post_data) |
|---|
| 52 |
when :xml_simple |
|---|
| 53 |
raw_post_data.blank? ? {} : Hash.from_xml(raw_post_data) |
|---|
| 54 |
when :yaml |
|---|
| 55 |
YAML.load(raw_post_data) |
|---|
| 56 |
when :xml_node |
|---|
| 57 |
node = XmlNode.from_xml(raw_post_data) |
|---|
| 58 |
{ node.node_name => node } |
|---|
| 59 |
end |
|---|
| 60 |
rescue Exception => e |
|---|
| 61 |
{ "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, |
|---|
| 62 |
"raw_post_data" => raw_post_data, "format" => mime_type } |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 65 |
private |
|---|
| 66 |
def get_typed_value(value) |
|---|
| 67 |
case value |
|---|
| 68 |
when String |
|---|
| 69 |
value |
|---|
| 70 |
when NilClass |
|---|
| 71 |
'' |
|---|
| 72 |
when Array |
|---|
| 73 |
value.map { |v| get_typed_value(v) } |
|---|
| 74 |
else |
|---|
| 75 |
|
|---|
| 76 |
if value.respond_to?(:content_type) && |
|---|
| 77 |
!value.content_type.blank? && |
|---|
| 78 |
!value.original_filename.blank? |
|---|
| 79 |
unless value.respond_to?(:full_original_filename) |
|---|
| 80 |
class << value |
|---|
| 81 |
alias_method :full_original_filename, :original_filename |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
def original_filename |
|---|
| 89 |
if md = /^(?:.*[:\\\/])?(.*)/m.match(full_original_filename) |
|---|
| 90 |
md.captures.first |
|---|
| 91 |
else |
|---|
| 92 |
File.basename full_original_filename |
|---|
| 93 |
end |
|---|
| 94 |
end |
|---|
| 95 |
end |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
value |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
elsif value.respond_to?(:read) |
|---|
| 103 |
result = value.read |
|---|
| 104 |
value.rewind |
|---|
| 105 |
result |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
else |
|---|
| 109 |
raise "Unknown form value: #{value.inspect}" |
|---|
| 110 |
end |
|---|
| 111 |
end |
|---|
| 112 |
end |
|---|
| 113 |
end |
|---|
| 114 |
|
|---|
| 115 |
class FormEncodedPairParser < StringScanner |
|---|
| 116 |
attr_reader :top, :parent, :result |
|---|
| 117 |
|
|---|
| 118 |
def initialize(pairs = []) |
|---|
| 119 |
super('') |
|---|
| 120 |
@result = {} |
|---|
| 121 |
pairs.each { |key, value| parse(key, value) } |
|---|
| 122 |
end |
|---|
| 123 |
|
|---|
| 124 |
KEY_REGEXP = %r{([^\[\]=&]+)} |
|---|
| 125 |
BRACKETED_KEY_REGEXP = %r{\[([^\[\]=&]+)\]} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
def parse(key, value) |
|---|
| 129 |
self.string = key |
|---|
| 130 |
@top, @parent = result, nil |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
key = scan(KEY_REGEXP) or return |
|---|
| 134 |
key = post_key_check(key) |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
until eos? |
|---|
| 138 |
r = scan(BRACKETED_KEY_REGEXP) or return |
|---|
| 139 |
key = self[1] |
|---|
| 140 |
key = post_key_check(key) |
|---|
| 141 |
end |
|---|
| 142 |
|
|---|
| 143 |
bind(key, value) |
|---|
| 144 |
end |
|---|
| 145 |
|
|---|
| 146 |
private |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
def post_key_check(key) |
|---|
| 154 |
if scan(/\[\]/) |
|---|
| 155 |
container(key, Array) |
|---|
| 156 |
nil |
|---|
| 157 |
elsif check(/\[[^\]]/) |
|---|
| 158 |
container(key, Hash) |
|---|
| 159 |
nil |
|---|
| 160 |
else |
|---|
| 161 |
key |
|---|
| 162 |
end |
|---|
| 163 |
end |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
def container(key, klass) |
|---|
| 168 |
type_conflict! klass, top[key] if top.is_a?(Hash) && top.key?(key) && ! top[key].is_a?(klass) |
|---|
| 169 |
value = bind(key, klass.new) |
|---|
| 170 |
type_conflict! klass, value unless value.is_a?(klass) |
|---|
| 171 |
push(value) |
|---|
| 172 |
end |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
def push(value) |
|---|
| 176 |
@parent, @top = @top, value |
|---|
| 177 |
end |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
def bind(key, value) |
|---|
| 181 |
if top.is_a? Array |
|---|
| 182 |
if key |
|---|
| 183 |
if top[-1].is_a?(Hash) && ! top[-1].key?(key) |
|---|
| 184 |
top[-1][key] = value |
|---|
| 185 |
else |
|---|
| 186 |
top << {key => value}.with_indifferent_access |
|---|
| 187 |
push top.last |
|---|
| 188 |
end |
|---|
| 189 |
else |
|---|
| 190 |
top << value |
|---|
| 191 |
end |
|---|
| 192 |
elsif top.is_a? Hash |
|---|
| 193 |
key = CGI.unescape(key) |
|---|
| 194 |
parent << (@top = {}) if top.key?(key) && parent.is_a?(Array) |
|---|
| 195 |
return top[key] ||= value |
|---|
| 196 |
else |
|---|
| 197 |
raise ArgumentError, "Don't know what to do: top is #{top.inspect}" |
|---|
| 198 |
end |
|---|
| 199 |
|
|---|
| 200 |
return value |
|---|
| 201 |
end |
|---|
| 202 |
|
|---|
| 203 |
def type_conflict!(klass, value) |
|---|
| 204 |
raise TypeError, |
|---|
| 205 |
"Conflicting types for parameter containers. " + |
|---|
| 206 |
"Expected an instance of #{klass}, but found an instance of #{value.class}. " + |
|---|
| 207 |
"This can be caused by passing Array and Hash based paramters qs[]=value&qs[key]=value. " |
|---|
| 208 |
end |
|---|
| 209 |
|
|---|
| 210 |
end |
|---|
| 211 |
end |
|---|