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 126
126: def GET
127: if @env["rack.request.query_string"] == query_string
128: @env["rack.request.query_hash"]
129: else
130: @env["rack.request.query_string"] = query_string
131: @env["rack.request.query_hash"] = parse_query(query_string)
132: end
133: 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 139
139: def POST
140: if @env["rack.input"].nil?
141: raise "Missing rack.input"
142: elsif @env["rack.request.form_input"].eql? @env["rack.input"]
143: @env["rack.request.form_hash"]
144: elsif form_data? || parseable_data?
145: @env["rack.request.form_input"] = @env["rack.input"]
146: unless @env["rack.request.form_hash"] = parse_multipart(env)
147: form_vars = @env["rack.input"].read
148:
149: # Fix for Safari Ajax postings that always append \0
150: form_vars.sub!(/\0\z/, '')
151:
152: @env["rack.request.form_vars"] = form_vars
153: @env["rack.request.form_hash"] = parse_query(form_vars)
154:
155: @env["rack.input"].rewind
156: end
157: @env["rack.request.form_hash"]
158: else
159: {}
160: end
161: end
shortcut for request.params[key]
# File lib/rack/request.rb, line 171
171: def [](key)
172: params[key.to_s]
173: end
shortcut for request.params[key] = value
# File lib/rack/request.rb, line 176
176: def []=(key, value)
177: params[key.to_s] = value
178: end
(Not documented)
# File lib/rack/request.rb, line 242
242: def accept_encoding
243: @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part|
244: m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick
245:
246: if m
247: [m[1], (m[2] || 1.0).to_f]
248: else
249: raise "Invalid value for Accept-Encoding: #{part.inspect}"
250: end
251: end
252: 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 63
63: def content_charset
64: media_type_params['charset']
65: 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 86
86: 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 113
113: def form_data?
114: type = media_type
115: meth = env["rack.methodoverride.original_method"] || env['REQUEST_METHOD']
116: (meth == 'POST' && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
117: end
(Not documented)
# File lib/rack/request.rb, line 238
238: def fullpath
239: query_string.empty? ? path : "#{path}?#{query_string}"
240: end
(Not documented)
# File lib/rack/request.rb, line 83
83: def get?; request_method == "GET" end
(Not documented)
# File lib/rack/request.rb, line 87
87: def head?; request_method == "HEAD" end
(Not documented)
# 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
(Not documented)
# 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
(Not documented)
# File lib/rack/request.rb, line 254
254: def ip
255: if addr = @env['HTTP_X_FORWARDED_FOR']
256: addr.split(',').last.strip
257: else
258: @env['REMOTE_ADDR']
259: end
260: end
(Not documented)
# 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
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 121
121: def parseable_data?
122: PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
123: end
(Not documented)
# File lib/rack/request.rb, line 234
234: def path
235: script_name + path_info
236: 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 81
81: 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 84
84: def post?; request_method == "POST" end
(Not documented)
# File lib/rack/request.rb, line 85
85: 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 186
186: def referer
187: @env['HTTP_REFERER'] || '/'
188: 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 80
80: 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 220
220: def url
221: url = scheme + "://"
222: url << host
223:
224: if scheme == "https" && port != 443 ||
225: scheme == "http" && port != 80
226: url << ":#{port}"
227: end
228:
229: url << fullpath
230:
231: url
232: end
(Not documented)
# File lib/rack/request.rb, line 191
191: def user_agent
192: @env['HTTP_USER_AGENT']
193: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.