| Module | CGI::QueryExtension |
| In: |
lib/cgi.rb
|
Mixin module. It provides the follow functionality groups:
| path | -> | local_path |
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.
# File lib/cgi.rb, line 1162
1162: def [](key)
1163: params = @params[key]
1164: value = params[0]
1165: if @multipart
1166: if value
1167: return value
1168: elsif defined? StringIO
1169: StringIO.new("")
1170: else
1171: Tempfile.new("CGI")
1172: end
1173: else
1174: str = if value then value.dup else "" end
1175: str.extend(Value)
1176: str.set_params(params)
1177: str
1178: end
1179: end
Returns true if a given parameter key exists in the query.
# File lib/cgi.rb, line 1187
1187: def has_key?(*args)
1188: @params.has_key?(*args)
1189: end
Get the raw cookies as a string.
# File lib/cgi.rb, line 945
945: def raw_cookie
946: env_table["HTTP_COOKIE"]
947: end
Get the raw RFC2965 cookies as a string.
# File lib/cgi.rb, line 950
950: def raw_cookie2
951: env_table["HTTP_COOKIE2"]
952: end
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.
# File lib/cgi.rb, line 1102
1102: def initialize_query()
1103: if ("POST" == env_table['REQUEST_METHOD']) and
1104: %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n.match(env_table['CONTENT_TYPE'])
1105: boundary = $1.dup
1106: @multipart = true
1107: @params = read_multipart(boundary, Integer(env_table['CONTENT_LENGTH']))
1108: else
1109: @multipart = false
1110: @params = CGI::parse(
1111: case env_table['REQUEST_METHOD']
1112: when "GET", "HEAD"
1113: if defined?(MOD_RUBY)
1114: Apache::request.args or ""
1115: else
1116: env_table['QUERY_STRING'] or ""
1117: end
1118: when "POST"
1119: stdinput.binmode if defined? stdinput.binmode
1120: stdinput.read(Integer(env_table['CONTENT_LENGTH'])) or ''
1121: else
1122: read_from_cmdline
1123: end
1124: )
1125: end
1126:
1127: @cookies = CGI::Cookie::parse((env_table['HTTP_COOKIE'] or env_table['COOKIE']))
1128: end
offline mode. read name=value pairs on standard input.
# File lib/cgi.rb, line 1074
1074: def read_from_cmdline
1075: require "shellwords"
1076:
1077: string = unless ARGV.empty?
1078: ARGV.join(' ')
1079: else
1080: if STDIN.tty?
1081: STDERR.print(
1082: %|(offline mode: enter name=value pairs on standard input)\n|
1083: )
1084: end
1085: readlines.join(' ').gsub(/\n/n, '')
1086: end.gsub(/\\=/n, '%3D').gsub(/\\&/n, '%26')
1087:
1088: words = Shellwords.shellwords(string)
1089:
1090: if words.find{|x| /=/n.match(x) }
1091: words.join('&')
1092: else
1093: words.join('+')
1094: end
1095: end
# File lib/cgi.rb, line 967
967: def read_multipart(boundary, content_length)
968: params = Hash.new([])
969: boundary = "--" + boundary
970: buf = ""
971: bufsize = 10 * 1024
972: boundary_end=""
973:
974: # start multipart/form-data
975: stdinput.binmode if defined? stdinput.binmode
976: boundary_size = boundary.size + EOL.size
977: content_length -= boundary_size
978: status = stdinput.read(boundary_size)
979: if nil == status
980: raise EOFError, "no content body"
981: elsif boundary + EOL != status
982: raise EOFError, "bad content body"
983: end
984:
985: loop do
986: head = nil
987: if 10240 < content_length
988: require "tempfile"
989: body = Tempfile.new("CGI")
990: else
991: begin
992: require "stringio"
993: body = StringIO.new
994: rescue LoadError
995: require "tempfile"
996: body = Tempfile.new("CGI")
997: end
998: end
999: body.binmode if defined? body.binmode
1000:
1001: until head and /#{boundary}(?:#{EOL}|--)/n.match(buf)
1002:
1003: if (not head) and /#{EOL}#{EOL}/n.match(buf)
1004: buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do
1005: head = $1.dup
1006: ""
1007: end
1008: next
1009: end
1010:
1011: if head and ( (EOL + boundary + EOL).size < buf.size )
1012: body.print buf[0 ... (buf.size - (EOL + boundary + EOL).size)]
1013: buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = ""
1014: end
1015:
1016: c = if bufsize < content_length
1017: stdinput.read(bufsize)
1018: else
1019: stdinput.read(content_length)
1020: end
1021: if c.nil? || c.empty?
1022: raise EOFError, "bad content body"
1023: end
1024: buf.concat(c)
1025: content_length -= c.size
1026: end
1027:
1028: buf = buf.sub(/\A((?:.|\n)*?)(?:[\r\n]{1,2})?#{boundary}([\r\n]{1,2}|--)/n) do
1029: body.print $1
1030: if "--" == $2
1031: content_length = -1
1032: end
1033: boundary_end = $2.dup
1034: ""
1035: end
1036:
1037: body.rewind
1038:
1039: /Content-Disposition:.* filename="?([^\";]*)"?/ni.match(head)
1040: filename = ($1 or "")
1041: if /Mac/ni.match(env_table['HTTP_USER_AGENT']) and
1042: /Mozilla/ni.match(env_table['HTTP_USER_AGENT']) and
1043: (not /MSIE/ni.match(env_table['HTTP_USER_AGENT']))
1044: filename = CGI::unescape(filename)
1045: end
1046:
1047: /Content-Type: (.*)/ni.match(head)
1048: content_type = ($1 or "")
1049:
1050: (class << body; self; end).class_eval do
1051: alias local_path path
1052: define_method(:original_filename) {filename.dup.taint}
1053: define_method(:content_type) {content_type.dup.taint}
1054: end
1055:
1056: /Content-Disposition:.* name="?([^\";]*)"?/ni.match(head)
1057: name = $1.dup
1058:
1059: if params.has_key?(name)
1060: params[name].push(body)
1061: else
1062: params[name] = [body]
1063: end
1064: break if buf.size == 0
1065: break if content_length === -1
1066: end
1067: raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
1068:
1069: params
1070: end