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 113 113: def GET 114: if @env["rack.request.query_string"] == query_string 115: @env["rack.request.query_hash"] 116: else 117: @env["rack.request.query_string"] = query_string 118: @env["rack.request.query_hash"] = 119: Utils.parse_nested_query(query_string) 120: end 121: 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 127 127: def POST 128: if @env["rack.request.form_input"].eql? @env["rack.input"] 129: @env["rack.request.form_hash"] 130: elsif form_data? || parseable_data? 131: @env["rack.request.form_input"] = @env["rack.input"] 132: unless @env["rack.request.form_hash"] = 133: Utils::Multipart.parse_multipart(env) 134: form_vars = @env["rack.input"].read 135: 136: # Fix for Safari Ajax postings that always append \0 137: form_vars.sub!(/\0\z/, '') 138: 139: @env["rack.request.form_vars"] = form_vars 140: @env["rack.request.form_hash"] = Utils.parse_nested_query(form_vars) 141: 142: @env["rack.input"].rewind 143: end 144: @env["rack.request.form_hash"] 145: else 146: {} 147: end 148: end
shortcut for request.params[key]
# File lib/rack/request.rb, line 158 158: def [](key) 159: params[key.to_s] 160: end
shortcut for request.params[key] = value
# File lib/rack/request.rb, line 163 163: def []=(key, value) 164: params[key.to_s] = value 165: end
(Not documented)
# File lib/rack/request.rb, line 226 226: def accept_encoding 227: @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part| 228: m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick 229: 230: if m 231: [m[1], (m[2] || 1.0).to_f] 232: else 233: raise "Invalid value for Accept-Encoding: #{part.inspect}" 234: end 235: end 236: end
(Not documented)
# 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 62 62: def content_charset 63: media_type_params['charset'] 64: end
(Not documented)
# File lib/rack/request.rb, line 31 31: def content_length; @env['CONTENT_LENGTH'] end
(Not documented)
# File lib/rack/request.rb, line 32 32: def content_type; @env['CONTENT_TYPE'] end
(Not documented)
# File lib/rack/request.rb, line 77 77: def delete?; request_method == "DELETE" 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 102 102: def form_data? 103: FORM_DATA_MEDIA_TYPES.include?(media_type) 104: end
(Not documented)
# File lib/rack/request.rb, line 222 222: def fullpath 223: query_string.empty? ? path : "#{path}?#{query_string}" 224: end
(Not documented)
# File lib/rack/request.rb, line 74 74: def get?; request_method == "GET" end
(Not documented)
# File lib/rack/request.rb, line 78 78: def head?; request_method == "HEAD" end
(Not documented)
# File lib/rack/request.rb, line 66 66: def host 67: # Remove port number. 68: (@env["HTTP_HOST"] || @env["SERVER_NAME"]).to_s.gsub(/:\d+\z/, '') 69: end
(Not documented)
# File lib/rack/request.rb, line 238 238: def ip 239: if addr = @env['HTTP_X_FORWARDED_FOR'] 240: addr.split(',').last.strip 241: else 242: @env['REMOTE_ADDR'] 243: end 244: 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 42 42: def media_type 43: content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase 44: 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 51 51: def media_type_params 52: return {} if content_type.nil? 53: content_type.split(/\s*[;,]\s*/)[1..-1]. 54: collect { |s| s.split('=', 2) }. 55: inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash } 56: end
(Not documented)
# File lib/rack/auth/openid.rb, line 14 14: def openid_request 15: @env['rack.auth.openid.request'] 16: end
(Not documented)
# File lib/rack/auth/openid.rb, line 18 18: def openid_response 19: @env['rack.auth.openid.response'] 20: 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 108 108: def parseable_data? 109: PARSEABLE_DATA_MEDIA_TYPES.include?(media_type) 110: end
(Not documented)
# File lib/rack/request.rb, line 218 218: def path 219: script_name + path_info 220: end
(Not documented)
# File lib/rack/request.rb, line 27 27: def path_info; @env["PATH_INFO"].to_s end
(Not documented)
# File lib/rack/request.rb, line 72 72: def path_info=(s); @env["PATH_INFO"] = s.to_s end
(Not documented)
# File lib/rack/request.rb, line 28 28: def port; @env["SERVER_PORT"].to_i end
(Not documented)
# File lib/rack/request.rb, line 75 75: def post?; request_method == "POST" end
(Not documented)
# File lib/rack/request.rb, line 76 76: def put?; request_method == "PUT" end
(Not documented)
# 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 173 173: def referer 174: @env['HTTP_REFERER'] || '/' 175: end
(Not documented)
# File lib/rack/request.rb, line 29 29: def request_method; @env["REQUEST_METHOD"] end
(Not documented)
# File lib/rack/request.rb, line 25 25: def scheme; @env["rack.url_scheme"] end
(Not documented)
# File lib/rack/request.rb, line 26 26: def script_name; @env["SCRIPT_NAME"].to_s end
(Not documented)
# File lib/rack/request.rb, line 71 71: def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
(Not documented)
# File lib/rack/request.rb, line 33 33: def session; @env['rack.session'] ||= {} end
(Not documented)
# File lib/rack/request.rb, line 34 34: def session_options; @env['rack.session.options'] ||= {} end
Tries to return a remake of the original request URL as a string.
# File lib/rack/request.rb, line 204 204: def url 205: url = scheme + "://" 206: url << host 207: 208: if scheme == "https" && port != 443 || 209: scheme == "http" && port != 80 210: url << ":#{port}" 211: end 212: 213: url << fullpath 214: 215: url 216: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.