Object
Rack::Request provides a convenient interface to a Rack environment. It is stateless, the environment env passed to the constructor will be directly modified.
req = Rack::Request.new(env) req.post? req.params["data"]
The environment hash passed will store a reference to the Request object instantiated so that it will only instantiate if an instance of the Request object doesn’t already exist.
The set of form-data media-types. Requests that do not indicate one of the media types presents in this list will not be eligible for form-data / param parsing.
The set of media-types. Requests that do not indicate one of the media types presents in this list will not be eligible for param parsing like soap attachments or generic multiparts
Returns the data recieved in the query string.
# File lib/rack/request.rb, line 128 128: def GET 129: if @env["rack.request.query_string"] == query_string 130: @env["rack.request.query_hash"] 131: else 132: @env["rack.request.query_string"] = query_string 133: @env["rack.request.query_hash"] = parse_query(query_string) 134: end 135: end
Returns the data recieved in the request body.
This method support both application/x-www-form-urlencoded and multipart/form-data.
# File lib/rack/request.rb, line 141 141: def POST 142: if @env["rack.input"].nil? 143: raise "Missing rack.input" 144: elsif @env["rack.request.form_input"].eql? @env["rack.input"] 145: @env["rack.request.form_hash"] 146: elsif form_data? || parseable_data? 147: @env["rack.request.form_input"] = @env["rack.input"] 148: unless @env["rack.request.form_hash"] = parse_multipart(env) 149: form_vars = @env["rack.input"].read 150: 151: # Fix for Safari Ajax postings that always append \0 152: form_vars.sub!(/\00\\z/, '') 153: 154: @env["rack.request.form_vars"] = form_vars 155: @env["rack.request.form_hash"] = parse_query(form_vars) 156: 157: @env["rack.input"].rewind 158: end 159: @env["rack.request.form_hash"] 160: else 161: {} 162: end 163: end
shortcut for request.params[key]
# File lib/rack/request.rb, line 173 173: def [](key) 174: params[key.to_s] 175: end
shortcut for request.params[key] = value
# File lib/rack/request.rb, line 178 178: def []=(key, value) 179: params[key.to_s] = value 180: end
# File lib/rack/request.rb, line 244 244: def accept_encoding 245: @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part| 246: m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick 247: 248: if m 249: [m[1], (m[2] || 1.0).to_f] 250: else 251: raise "Invalid value for Accept-Encoding: #{part.inspect}" 252: end 253: end 254: end
# File lib/rack/request.rb, line 24 24: def body; @env["rack.input"] end
The character set of the request body if a “charset” media type parameter was given, or nil if no “charset” was specified. Note that, per RFC2616, text/* media types that specify no explicit charset are to be considered ISO-8859-1.
# File lib/rack/request.rb, line 63 63: def content_charset 64: media_type_params['charset'] 65: end
# File lib/rack/request.rb, line 31 31: def content_length; @env['CONTENT_LENGTH'] end
# File lib/rack/request.rb, line 32 32: def content_type; @env['CONTENT_TYPE'] end
# File lib/rack/request.rb, line 83 83: def delete?; request_method == "DELETE" end
Determine whether the request body contains form-data by checking the request Content-Type for one of the media-types: “application/x-www-form-urlencoded“ or “multipart/form-data“. The list of form-data media types can be modified through the FORM_DATA_MEDIA_TYPES array.
A request body is also assumed to contain form-data when no Content-Type header is provided and the request_method is POST.
# File lib/rack/request.rb, line 115 115: def form_data? 116: type = media_type 117: meth = env["rack.methodoverride.original_method"] || env['REQUEST_METHOD'] 118: (meth == 'POST' && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type) 119: end
# File lib/rack/request.rb, line 240 240: def fullpath 241: query_string.empty? ? path : "#{path}?#{query_string}" 242: end
# File lib/rack/request.rb, line 84 84: def get?; request_method == "GET" end
# File lib/rack/request.rb, line 85 85: def head?; request_method == "HEAD" end
# File lib/rack/request.rb, line 75 75: def host 76: # Remove port number. 77: host_with_port.to_s.gsub(/:\d+\z/, '') 78: end
# File lib/rack/request.rb, line 67 67: def host_with_port 68: if forwarded = @env["HTTP_X_FORWARDED_HOST"] 69: forwarded.split(/,\s?/).last 70: else 71: @env['HTTP_HOST'] || "#{@env['SERVER_NAME'] || @env['SERVER_ADDR']}:#{@env['SERVER_PORT']}" 72: end 73: end
# File lib/rack/request.rb, line 256 256: def ip 257: if addr = @env['HTTP_X_FORWARDED_FOR'] 258: (addr.split(',').grep(/\d\./).first || @env['REMOTE_ADDR']).to_s.strip 259: else 260: @env['REMOTE_ADDR'] 261: end 262: end
# File lib/rack/request.rb, line 35 35: def logger; @env['rack.logger'] end
The media type (type/subtype) portion of the CONTENT_TYPE header without any media type parameters. e.g., when CONTENT_TYPE is “text/plain;charset=utf-8”, the media-type is “text/plain“.
For more information on the use of media types in HTTP, see: www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
# File lib/rack/request.rb, line 43 43: def media_type 44: content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase 45: end
The media type parameters provided in CONTENT_TYPE as a Hash, or an empty Hash if no CONTENT_TYPE or media-type parameters were provided. e.g., when the CONTENT_TYPE is “text/plain;charset=utf-8”, this method responds with the following Hash:
{ 'charset' => 'utf-8' }
# File lib/rack/request.rb, line 52 52: def media_type_params 53: return {} if content_type.nil? 54: content_type.split(/\s*[;,]\s*/)[1..1]. 55: collect { |s| s.split('=', 2) }. 56: inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash } 57: end
# File lib/rack/request.rb, line 86 86: def options?; request_method == "OPTIONS" end
Determine whether the request body contains data by checking the request media_type against registered parse-data media-types
# File lib/rack/request.rb, line 123 123: def parseable_data? 124: PARSEABLE_DATA_MEDIA_TYPES.include?(media_type) 125: end
# File lib/rack/request.rb, line 236 236: def path 237: script_name + path_info 238: end
# File lib/rack/request.rb, line 27 27: def path_info; @env["PATH_INFO"].to_s end
# File lib/rack/request.rb, line 81 81: def path_info=(s); @env["PATH_INFO"] = s.to_s end
# File lib/rack/request.rb, line 28 28: def port; @env["SERVER_PORT"].to_i end
# File lib/rack/request.rb, line 87 87: def post?; request_method == "POST" end
# File lib/rack/request.rb, line 88 88: def put?; request_method == "PUT" end
# File lib/rack/request.rb, line 30 30: def query_string; @env["QUERY_STRING"].to_s end
the referer of the client or ’/’
# File lib/rack/request.rb, line 188 188: def referer 189: @env['HTTP_REFERER'] || '/' 190: end
# File lib/rack/request.rb, line 29 29: def request_method; @env["REQUEST_METHOD"] end
# File lib/rack/request.rb, line 25 25: def scheme; @env["rack.url_scheme"] end
# File lib/rack/request.rb, line 26 26: def script_name; @env["SCRIPT_NAME"].to_s end
# File lib/rack/request.rb, line 80 80: def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
# File lib/rack/request.rb, line 33 33: def session; @env['rack.session'] ||= {} end
# File lib/rack/request.rb, line 34 34: def session_options; @env['rack.session.options'] ||= {} end
# File lib/rack/request.rb, line 89 89: def trace?; request_method == "TRACE" end
Tries to return a remake of the original request URL as a string.
# File lib/rack/request.rb, line 222 222: def url 223: url = scheme + "://" 224: url << host 225: 226: if scheme == "https" && port != 443 || 227: scheme == "http" && port != 80 228: url << ":#{port}" 229: end 230: 231: url << fullpath 232: 233: url 234: end
# File lib/rack/request.rb, line 193 193: def user_agent 194: @env['HTTP_USER_AGENT'] 195: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.