Parent

Class Index [+]

Quicksearch

Rack::Request

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.

Constants

FORM_DATA_MEDIA_TYPES

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.

PARSEABLE_DATA_MEDIA_TYPES

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

Attributes

env[R]

The environment of the request.

Public Class Methods

new(env, *args) click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 20
20:     def self.new(env, *args)
21:       if self == Rack::Request
22:         env["rack.request"] ||= super
23:       else
24:         super
25:       end
26:     end
new(env) click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 28
28:     def initialize(env)
29:       @env = env
30:     end

Public Instance Methods

GET() click to toggle source

Returns the data recieved in the query string.

     # File lib/rack/request.rb, line 121
121:     def GET
122:       if @env["rack.request.query_string"] == query_string
123:         @env["rack.request.query_hash"]
124:       else
125:         @env["rack.request.query_string"] = query_string
126:         @env["rack.request.query_hash"]   =
127:           Utils.parse_nested_query(query_string)
128:       end
129:     end
POST() click to toggle source

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 135
135:     def POST
136:       if @env["rack.request.form_input"].eql? @env["rack.input"]
137:         @env["rack.request.form_hash"]
138:       elsif form_data? || parseable_data?
139:         @env["rack.request.form_input"] = @env["rack.input"]
140:         unless @env["rack.request.form_hash"] =
141:             Utils::Multipart.parse_multipart(env)
142:           form_vars = @env["rack.input"].read
143: 
144:           # Fix for Safari Ajax postings that always append \0
145:           form_vars.sub!(/\0\z/, '')
146: 
147:           @env["rack.request.form_vars"] = form_vars
148:           @env["rack.request.form_hash"] = Utils.parse_nested_query(form_vars)
149: 
150:           @env["rack.input"].rewind
151:         end
152:         @env["rack.request.form_hash"]
153:       else
154:         {}
155:       end
156:     end
[](key) click to toggle source

shortcut for request.params[key]

     # File lib/rack/request.rb, line 166
166:     def [](key)
167:       params[key.to_s]
168:     end
[]=(key, value) click to toggle source

shortcut for request.params[key] = value

     # File lib/rack/request.rb, line 171
171:     def []=(key, value)
172:       params[key.to_s] = value
173:     end
accept_encoding() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 234
234:     def accept_encoding
235:       @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part|
236:         m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick
237: 
238:         if m
239:           [m[1], (m[2] || 1.0).to_f]
240:         else
241:           raise "Invalid value for Accept-Encoding: #{part.inspect}"
242:         end
243:       end
244:     end
body() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 32
32:     def body;            @env["rack.input"]                       end
content_charset() click to toggle source

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 70
70:     def content_charset
71:       media_type_params['charset']
72:     end
content_length() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 39
39:     def content_length;  @env['CONTENT_LENGTH']                   end
content_type() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 40
40:     def content_type;    @env['CONTENT_TYPE']                     end
cookies() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 187
187:     def cookies
188:       return {}  unless @env["HTTP_COOKIE"]
189: 
190:       if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
191:         @env["rack.request.cookie_hash"]
192:       else
193:         @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
194:         # According to RFC 2109:
195:         #   If multiple cookies satisfy the criteria above, they are ordered in
196:         #   the Cookie header such that those with more specific Path attributes
197:         #   precede those with less specific.  Ordering with respect to other
198:         #   attributes (e.g., Domain) is unspecified.
199:         @env["rack.request.cookie_hash"] =
200:           Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
201:             h[k] = Array === v ? v.first : v
202:             h
203:           }
204:       end
205:     end
delete?() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 85
85:     def delete?;         request_method == "DELETE"               end
form_data?() click to toggle source

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 110
110:     def form_data?
111:       FORM_DATA_MEDIA_TYPES.include?(media_type)
112:     end
fullpath() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 230
230:     def fullpath
231:       query_string.empty? ? path : "#{path}?#{query_string}"
232:     end
get?() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 82
82:     def get?;            request_method == "GET"                  end
head?() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 86
86:     def head?;           request_method == "HEAD"                 end
host() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 74
74:     def host
75:       # Remove port number.
76:       (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
77:     end
ip() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 246
246:     def ip
247:       if addr = @env['HTTP_X_FORWARDED_FOR']
248:         addr.split(',').last.strip
249:       else
250:         @env['REMOTE_ADDR']
251:       end
252:     end
media_type() click to toggle source

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 50
50:     def media_type
51:       content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
52:     end
media_type_params() click to toggle source

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 59
59:     def media_type_params
60:       return {} if content_type.nil?
61:       content_type.split(/\s*[;,]\s*/)[1..-1].
62:         collect { |s| s.split('=', 2) }.
63:         inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
64:     end
openid_request() click to toggle source

(Not documented)

    # File lib/rack/auth/openid.rb, line 14
14:     def openid_request
15:       @env['rack.auth.openid.request']
16:     end
openid_response() click to toggle source

(Not documented)

    # File lib/rack/auth/openid.rb, line 18
18:     def openid_response
19:       @env['rack.auth.openid.response']
20:     end
params() click to toggle source

The union of GET and POST data.

     # File lib/rack/request.rb, line 159
159:     def params
160:       self.put? ? self.GET : self.GET.update(self.POST)
161:     rescue EOFError => e
162:       self.GET
163:     end
parseable_data?() click to toggle source

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 116
116:     def parseable_data?
117:       PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
118:     end
path() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 226
226:     def path
227:       script_name + path_info
228:     end
path_info() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 35
35:     def path_info;       @env["PATH_INFO"].to_s                   end
path_info=(s) click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 80
80:     def path_info=(s);   @env["PATH_INFO"] = s.to_s               end
port() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 36
36:     def port;            @env["SERVER_PORT"].to_i                 end
post?() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 83
83:     def post?;           request_method == "POST"                 end
put?() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 84
84:     def put?;            request_method == "PUT"                  end
query_string() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 38
38:     def query_string;    @env["QUERY_STRING"].to_s                end
referer() click to toggle source

the referer of the client or ’/’

     # File lib/rack/request.rb, line 181
181:     def referer
182:       @env['HTTP_REFERER'] || '/'
183:     end
Also aliased as: referrer
referrer() click to toggle source

Alias for referer

request_method() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 37
37:     def request_method;  @env["REQUEST_METHOD"]                   end
scheme() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 33
33:     def scheme;          @env["rack.url_scheme"]                  end
script_name() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 34
34:     def script_name;     @env["SCRIPT_NAME"].to_s                 end
script_name=(s) click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 79
79:     def script_name=(s); @env["SCRIPT_NAME"] = s.to_s             end
session() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 41
41:     def session;         @env['rack.session'] ||= {}              end
session_options() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 42
42:     def session_options; @env['rack.session.options'] ||= {}      end
url() click to toggle source

Tries to return a remake of the original request URL as a string.

     # File lib/rack/request.rb, line 212
212:     def url
213:       url = scheme + "://"
214:       url << host
215: 
216:       if scheme == "https" && port != 443 ||
217:           scheme == "http" && port != 80
218:         url << ":#{port}"
219:       end
220: 
221:       url << fullpath
222: 
223:       url
224:     end
values_at(*keys) click to toggle source

like Hash#values_at

     # File lib/rack/request.rb, line 176
176:     def values_at(*keys)
177:       keys.map{|key| params[key] }
178:     end
xhr?() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 207
207:     def xhr?
208:       @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
209:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.