| 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: end
117: @env["rack.request.form_hash"]
118: else
119: {}
120: end
121: end
shortcut for request.params[key]
# File lib/rack/request.rb, line 131
131: def [](key)
132: params[key.to_s]
133: end
shortcut for request.params[key] = value
# File lib/rack/request.rb, line 136
136: def []=(key, value)
137: params[key.to_s] = value
138: end
# File lib/rack/request.rb, line 197
197: def accept_encoding
198: @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part|
199: m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick
200:
201: if m
202: [m[1], (m[2] || 1.0).to_f]
203: else
204: raise "Invalid value for Accept-Encoding: #{part.inspect}"
205: end
206: end
207: 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 152
152: def cookies
153: return {} unless @env["HTTP_COOKIE"]
154:
155: if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
156: @env["rack.request.cookie_hash"]
157: else
158: @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
159: # According to RFC 2109:
160: # If multiple cookies satisfy the criteria above, they are ordered in
161: # the Cookie header such that those with more specific Path attributes
162: # precede those with less specific. Ordering with respect to other
163: # attributes (e.g., Domain) is unspecified.
164: @env["rack.request.cookie_hash"] =
165: Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
166: h[k] = Array === v ? v.first : v
167: h
168: }
169: end
170: 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 191
191: def fullpath
192: path = script_name + path_info
193: path << "?" << query_string unless query_string.empty?
194: path
195: 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
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 177
177: def url
178: url = scheme + "://"
179: url << host
180:
181: if scheme == "https" && port != 443 ||
182: scheme == "http" && port != 80
183: url << ":#{port}"
184: end
185:
186: url << fullpath
187:
188: url
189: end