Parent

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) click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 20
20:     def initialize(env)
21:       @env = env
22:     end

Public Instance Methods

GET() click to toggle source

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
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 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
[](key) click to toggle source

shortcut for request.params[key]

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

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
accept_encoding() click to toggle source

(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
body() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 24
24:     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 62
62:     def content_charset
63:       media_type_params['charset']
64:     end
content_length() click to toggle source

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

    # File lib/rack/request.rb, line 77
77:     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 102
102:     def form_data?
103:       FORM_DATA_MEDIA_TYPES.include?(media_type)
104:     end
fullpath() click to toggle source

(Not documented)

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

(Not documented)

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

(Not documented)

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

(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
ip() click to toggle source

(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
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 42
42:     def media_type
43:       content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
44:     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 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
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 151
151:     def params
152:       self.put? ? self.GET : self.GET.update(self.POST)
153:     rescue EOFError => e
154:       self.GET
155:     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 108
108:     def parseable_data?
109:       PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
110:     end
path() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 218
218:     def path
219:       script_name + path_info
220:     end
path_info() click to toggle source

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

    # File lib/rack/request.rb, line 30
30:     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 173
173:     def referer
174:       @env['HTTP_REFERER'] || '/'
175:     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 29
29:     def request_method;  @env["REQUEST_METHOD"]                   end
scheme() click to toggle source

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

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

(Not documented)

    # File lib/rack/request.rb, line 34
34:     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 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
values_at(*keys) click to toggle source

like Hash#values_at

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

(Not documented)

     # File lib/rack/request.rb, line 199
199:     def xhr?
200:       @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
201:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.