2007-01-22 14:43:50 +01:00
|
|
|
class CGI #:nodoc:
|
|
|
|
module QueryExtension
|
|
|
|
# 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.
|
2007-02-09 09:04:31 +01:00
|
|
|
def initialize_query
|
2007-01-22 14:43:50 +01:00
|
|
|
@cookies = CGI::Cookie::parse(env_table['HTTP_COOKIE'] || env_table['COOKIE'])
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
# Fix some strange request environments.
|
2007-01-22 14:43:50 +01:00
|
|
|
if method = env_table['REQUEST_METHOD']
|
|
|
|
method = method.to_s.downcase.intern
|
|
|
|
else
|
|
|
|
method = :get
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
# 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'
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-02-09 09:04:31 +01:00
|
|
|
|
|
|
|
# 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))
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
unless defined?(MULTIPART_FORM_BOUNDARY_RE)
|
|
|
|
MULTIPART_FORM_BOUNDARY_RE = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n #"
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def extract_multipart_form_boundary(content_type)
|
|
|
|
MULTIPART_FORM_BOUNDARY_RE.match(content_type).to_a.pop
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if defined? MOD_RUBY
|
2007-02-09 09:04:31 +01:00
|
|
|
def read_query
|
2007-01-22 14:43:50 +01:00
|
|
|
Apache::request.args || ''
|
|
|
|
end
|
|
|
|
else
|
2007-02-09 09:04:31 +01:00
|
|
|
def read_query
|
2007-01-22 14:43:50 +01:00
|
|
|
# fixes CGI querystring parsing for lighttpd
|
|
|
|
env_qs = env_table['QUERY_STRING']
|
|
|
|
if env_qs.blank? && !(uri = env_table['REQUEST_URI']).blank?
|
|
|
|
uri.split('?', 2)[1] || ''
|
|
|
|
else
|
|
|
|
env_qs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def read_body(content_length)
|
2007-01-22 14:43:50 +01:00
|
|
|
stdinput.binmode if stdinput.respond_to?(:binmode)
|
2007-02-09 09:04:31 +01:00
|
|
|
content = stdinput.read(content_length) || ''
|
|
|
|
# Fix for Safari Ajax postings that always append \000
|
2007-01-22 14:43:50 +01:00
|
|
|
content.chop! if content[-1] == 0
|
2007-02-09 09:04:31 +01:00
|
|
|
content.gsub!(/&_=$/, '')
|
2007-01-22 14:43:50 +01:00
|
|
|
env_table['RAW_POST_DATA'] = content.freeze
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def read_params(method, content_length)
|
2007-01-22 14:43:50 +01:00
|
|
|
case method
|
|
|
|
when :get
|
2007-02-09 09:04:31 +01:00
|
|
|
read_query
|
2007-01-22 14:43:50 +01:00
|
|
|
when :post, :put
|
2007-02-09 09:04:31 +01:00
|
|
|
read_body(content_length)
|
2007-01-22 14:43:50 +01:00
|
|
|
when :cmd
|
|
|
|
read_from_cmdline
|
2007-02-09 09:04:31 +01:00
|
|
|
else # :head, :delete, :options, :trace, :connect
|
|
|
|
read_query
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end # module QueryExtension
|
|
|
|
end
|