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