Module: CGI::QueryExtension

Defined in:
lib/action_controller/cgi_ext/raw_post_data_fix.rb

Instance Method Summary collapse

Instance Method Details

#initialize_queryObject

Initialize the data from the query.

Handles multipart forms (in particular, forms that involve file uploads). Reads query parameters in the @params field, and cookies into @cookies.



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
# File 'lib/action_controller/cgi_ext/raw_post_data_fix.rb', line 7

def initialize_query
  @cookies = CGI::Cookie::parse(env_table['HTTP_COOKIE'] || env_table['COOKIE'])

  # Fix some strange request environments.
  if method = env_table['REQUEST_METHOD']
    method = method.to_s.downcase.intern
  else
    method = :get
  end

  # POST assumes missing Content-Type is application/x-www-form-urlencoded.
  content_type = env_table['CONTENT_TYPE']
  if content_type.blank? && method == :post
    content_type = 'application/x-www-form-urlencoded'
  end

  # Force content length to zero if missing.
  content_length = env_table['CONTENT_LENGTH'].to_i

  # Set multipart to false by default.
  @multipart = false

  # POST and PUT may have params in entity body. If content type is
  # missing for POST, assume urlencoded. If content type is missing
  # for PUT, don't assume anything and don't parse the parameters:
  # it's likely binary data.
  #
  # The other HTTP methods have their params in the query string.
  if method == :post || method == :put
    if boundary = extract_multipart_form_boundary(content_type)
      @multipart = true
      @params = read_multipart(boundary, content_length)
    elsif content_type.blank? || content_type !~ %r{application/x-www-form-urlencoded}i
      read_params(method, content_length)
      @params = {}
    end
  end

  @params ||= CGI.parse(read_params(method, content_length))
end