Class | Rack::Request |
In: |
lib/rack/request.rb
|
Parent: | 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"]
FORM_DATA_MEDIA_TYPES | = | [ nil, 'application/x-www-form-urlencoded', 'multipart/form-data' | 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. |
env | [R] | The environment of the request. |
Returns the data recieved in the query string.
# File lib/rack/request.rb, line 93 93: def GET 94: if @env["rack.request.query_string"] == query_string 95: @env["rack.request.query_hash"] 96: else 97: @env["rack.request.query_string"] = query_string 98: @env["rack.request.query_hash"] = 99: Utils.parse_query(query_string) 100: end 101: 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 107 107: def POST 108: if @env["rack.request.form_input"].eql? @env["rack.input"] 109: @env["rack.request.form_hash"] 110: elsif form_data? 111: @env["rack.request.form_input"] = @env["rack.input"] 112: unless @env["rack.request.form_hash"] = 113: Utils::Multipart.parse_multipart(env) 114: @env["rack.request.form_vars"] = @env["rack.input"].read 115: @env["rack.request.form_hash"] = Utils.parse_query(@env["rack.request.form_vars"]) 116: @env["rack.input"].rewind if @env["rack.input"].respond_to?(:rewind) 117: end 118: @env["rack.request.form_hash"] 119: else 120: {} 121: end 122: end
shortcut for request.params[key]
# File lib/rack/request.rb, line 132 132: def [](key) 133: params[key.to_s] 134: end
shortcut for request.params[key] = value
# File lib/rack/request.rb, line 137 137: def []=(key, value) 138: params[key.to_s] = value 139: end
# File lib/rack/request.rb, line 198 198: def accept_encoding 199: @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part| 200: m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick 201: 202: if m 203: [m[1], (m[2] || 1.0).to_f] 204: else 205: raise "Invalid value for Accept-Encoding: #{part.inspect}" 206: end 207: end 208: 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 56 56: def content_charset 57: media_type_params['charset'] 58: end
# File lib/rack/request.rb, line 153 153: def cookies 154: return {} unless @env["HTTP_COOKIE"] 155: 156: if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"] 157: @env["rack.request.cookie_hash"] 158: else 159: @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"] 160: # According to RFC 2109: 161: # If multiple cookies satisfy the criteria above, they are ordered in 162: # the Cookie header such that those with more specific Path attributes 163: # precede those with less specific. Ordering with respect to other 164: # attributes (e.g., Domain) is unspecified. 165: @env["rack.request.cookie_hash"] = 166: Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)| 167: h[k] = Array === v ? v.first : v 168: h 169: } 170: end 171: end
Determine whether the request body contains form-data by checking the request media_type against registered form-data media-types: "application/x-www-form-urlencoded" and "multipart/form-data". The list of form-data media types can be modified through the FORM_DATA_MEDIA_TYPES array.
# File lib/rack/request.rb, line 88 88: def form_data? 89: FORM_DATA_MEDIA_TYPES.include?(media_type) 90: end
# File lib/rack/request.rb, line 192 192: def fullpath 193: path = script_name + path_info 194: path << "?" << query_string unless query_string.empty? 195: path 196: end
# File lib/rack/request.rb, line 60 60: def host 61: # Remove port number. 62: (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '') 63: end
# File lib/rack/request.rb, line 210 210: def ip 211: if addr = @env['HTTP_X_FORWARDED_FOR'] 212: addr.split(',').last.strip 213: else 214: @env['REMOTE_ADDR'] 215: end 216: 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 36 36: def media_type 37: content_type && content_type.split(/\s*[;,]\s*/, 2)[0].downcase 38: 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 45 45: def media_type_params 46: return {} if content_type.nil? 47: content_type.split(/\s*[;,]\s*/)[1..-1]. 48: collect { |s| s.split('=', 2) }. 49: inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash } 50: end
Tries to return a remake of the original request URL as a string.
# File lib/rack/request.rb, line 178 178: def url 179: url = scheme + "://" 180: url << host 181: 182: if scheme == "https" && port != 443 || 183: scheme == "http" && port != 80 184: url << ":#{port}" 185: end 186: 187: url << fullpath 188: 189: url 190: end