Module: CGI::QueryExtension

Defined in:
lib/cgi/core.rb

Overview

Mixin module that provides the following:

  1. Access to the CGI environment variables as methods. See documentation to the CGI class for a list of these variables. The methods are exposed by removing the leading HTTP_ (if it exists) and downcasing the name. For example, auth_type will return the environment variable AUTH_TYPE, and accept will return the value for HTTP_ACCEPT.

  2. Access to cookies, including the cookies attribute.

  3. Access to parameters, including the params attribute, and overloading #[] to perform parameter value lookup by key.

  4. The initialize_query method, for initializing the above mechanisms, handling multipart forms, and allowing the class to be used in “offline” mode.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookiesObject

Get the cookies as a hash of cookie-name=>Cookie pairs.



448
449
450
# File 'lib/cgi/core.rb', line 448

def cookies
  @cookies
end

#filesObject (readonly)

Get the uploaded files as a hash of name=>values pairs



455
456
457
# File 'lib/cgi/core.rb', line 455

def files
  @files
end

#paramsObject

Get the parameters as a hash of name=>values pairs, where values is an Array.



452
453
454
# File 'lib/cgi/core.rb', line 452

def params
  @params
end

Instance Method Details

#[](key) ⇒ Object

Get the value for the parameter with a given key.

If the parameter has multiple values, only the first will be retrieved; use #params to get the array of values.



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/cgi/core.rb', line 697

def [](key)
  params = @params[key]
  return '' unless params
  value = params[0]
  if @multipart
    if value
      return value
    elsif defined? StringIO
      StringIO.new("".force_encoding(Encoding::ASCII_8BIT))
    else
      Tempfile.new("CGI",encoding: Encoding::ASCII_8BIT)
    end
  else
    str = if value then value.dup else "" end
    str
  end
end

#create_body(is_large) ⇒ Object

:nodoc:



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/cgi/core.rb', line 588

def create_body(is_large)  #:nodoc:
  if is_large
    require 'tempfile'
    body = Tempfile.new('CGI', encoding: Encoding::ASCII_8BIT)
  else
    begin
      require 'stringio'
      body = StringIO.new("".force_encoding(Encoding::ASCII_8BIT))
    rescue LoadError
      require 'tempfile'
      body = Tempfile.new('CGI', encoding: Encoding::ASCII_8BIT)
    end
  end
  body.binmode if defined? body.binmode
  return body
end

#has_key?(*args) ⇒ Boolean Also known as: key?, include?

Returns true if a given query string parameter exists.

Returns:

  • (Boolean)


721
722
723
# File 'lib/cgi/core.rb', line 721

def has_key?(*args)
  @params.has_key?(*args)
end

#keys(*args) ⇒ Object

Return all query parameter names as an array of String.



716
717
718
# File 'lib/cgi/core.rb', line 716

def keys(*args)
  @params.keys(*args)
end

#multipart?Boolean

Returns whether the form contained multipart/form-data

Returns:

  • (Boolean)


689
690
691
# File 'lib/cgi/core.rb', line 689

def multipart?
  @multipart
end

Get the raw cookies as a string.



438
439
440
# File 'lib/cgi/core.rb', line 438

def raw_cookie
  env_table["HTTP_COOKIE"]
end

#raw_cookie2Object

Get the raw RFC2965 cookies as a string.



443
444
445
# File 'lib/cgi/core.rb', line 443

def raw_cookie2
  env_table["HTTP_COOKIE2"]
end

#unescape_filename?Boolean

:nodoc:

Returns:

  • (Boolean)


604
605
606
607
# File 'lib/cgi/core.rb', line 604

def unescape_filename?  #:nodoc:
  user_agent = $CGI_ENV['HTTP_USER_AGENT']
  return /Mac/i.match(user_agent) && /Mozilla/i.match(user_agent) && !/MSIE/i.match(user_agent)
end