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

shortcut for request.params[key]

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

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

(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
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 63
63:     def content_charset
64:       media_type_params['charset']
65:     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 195
195:     def cookies
196:       return {}  unless @env["HTTP_COOKIE"]
197: 
198:       if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
199:         @env["rack.request.cookie_hash"]
200:       else
201:         @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
202:         # According to RFC 2109:
203:         #   If multiple cookies satisfy the criteria above, they are ordered in
204:         #   the Cookie header such that those with more specific Path attributes
205:         #   precede those with less specific.  Ordering with respect to other
206:         #   attributes (e.g., Domain) is unspecified.
207:         @env["rack.request.cookie_hash"] =
208:           Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
209:             h[k] = Array === v ? v.first : v
210:             h
211:           }
212:       end
213:     end
delete?() click to toggle source

(Not documented)

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

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

(Not documented)

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

(Not documented)

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

(Not documented)

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

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

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

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

(Not documented)

    # File lib/rack/request.rb, line 35
35:     def logger;          @env['rack.logger']                      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 43
43:     def media_type
44:       content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
45:     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 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
params() click to toggle source

The union of GET and POST data.

     # File lib/rack/request.rb, line 164
164:     def params
165:       self.GET.update(self.POST)
166:     rescue EOFError => e
167:       self.GET
168:     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 121
121:     def parseable_data?
122:       PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
123:     end
path() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 234
234:     def path
235:       script_name + path_info
236:     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 81
81:     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 84
84:     def post?;           request_method == "POST"                 end
put?() click to toggle source

(Not documented)

    # File lib/rack/request.rb, line 85
85:     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 186
186:     def referer
187:       @env['HTTP_REFERER'] || '/'
188:     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 80
80:     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 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
user_agent() click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 191
191:     def user_agent
192:       @env['HTTP_USER_AGENT']
193:     end
values_at(*keys) click to toggle source

like Hash#values_at

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

(Not documented)

     # File lib/rack/request.rb, line 215
215:     def xhr?
216:       @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
217:     end

Protected Instance Methods

parse_multipart(env) click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 267
267:       def parse_multipart(env)
268:         Utils::Multipart.parse_multipart(env)
269:       end
parse_query(qs) click to toggle source

(Not documented)

     # File lib/rack/request.rb, line 263
263:       def parse_query(qs)
264:         Utils.parse_nested_query(qs)
265:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.