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
    # 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 128
128:     def GET
129:       if @env["rack.request.query_string"] == query_string
130:         @env["rack.request.query_hash"]
131:       else
132:         @env["rack.request.query_string"] = query_string
133:         @env["rack.request.query_hash"]   = parse_query(query_string)
134:       end
135:     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 141
141:     def POST
142:       if @env["rack.input"].nil?
143:         raise "Missing rack.input"
144:       elsif @env["rack.request.form_input"].eql? @env["rack.input"]
145:         @env["rack.request.form_hash"]
146:       elsif form_data? || parseable_data?
147:         @env["rack.request.form_input"] = @env["rack.input"]
148:         unless @env["rack.request.form_hash"] = parse_multipart(env)
149:           form_vars = @env["rack.input"].read
150: 
151:           # Fix for Safari Ajax postings that always append \0
152:           form_vars.sub!(/\00\\z/, '')
153: 
154:           @env["rack.request.form_vars"] = form_vars
155:           @env["rack.request.form_hash"] = parse_query(form_vars)
156: 
157:           @env["rack.input"].rewind
158:         end
159:         @env["rack.request.form_hash"]
160:       else
161:         {}
162:       end
163:     end
[](key) click to toggle source

shortcut for request.params[key]

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

shortcut for request.params[key] = value

     # File lib/rack/request.rb, line 178
178:     def []=(key, value)
179:       params[key.to_s] = value
180:     end
accept_encoding() click to toggle source
     # File lib/rack/request.rb, line 244
244:     def accept_encoding
245:       @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part|
246:         m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick
247: 
248:         if m
249:           [m[1], (m[2] || 1.0).to_f]
250:         else
251:           raise "Invalid value for Accept-Encoding: #{part.inspect}"
252:         end
253:       end
254:     end
body() click to toggle source
    # 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
    # File lib/rack/request.rb, line 31
31:     def content_length;  @env['CONTENT_LENGTH']                   end
content_type() click to toggle source
    # File lib/rack/request.rb, line 32
32:     def content_type;    @env['CONTENT_TYPE']                     end
cookies() click to toggle source
     # File lib/rack/request.rb, line 197
197:     def cookies
198:       return {}  unless @env["HTTP_COOKIE"]
199: 
200:       if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
201:         @env["rack.request.cookie_hash"]
202:       else
203:         @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
204:         # According to RFC 2109:
205:         #   If multiple cookies satisfy the criteria above, they are ordered in
206:         #   the Cookie header such that those with more specific Path attributes
207:         #   precede those with less specific.  Ordering with respect to other
208:         #   attributes (e.g., Domain) is unspecified.
209:         @env["rack.request.cookie_hash"] =
210:           Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
211:             h[k] = Array === v ? v.first : v
212:             h
213:           }
214:       end
215:     end
delete?() click to toggle source
    # File lib/rack/request.rb, line 83
83:     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 115
115:     def form_data?
116:       type = media_type
117:       meth = env["rack.methodoverride.original_method"] || env['REQUEST_METHOD']
118:       (meth == 'POST' && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
119:     end
fullpath() click to toggle source
     # File lib/rack/request.rb, line 240
240:     def fullpath
241:       query_string.empty? ? path : "#{path}?#{query_string}"
242:     end
get?() click to toggle source
    # File lib/rack/request.rb, line 84
84:     def get?;     request_method == "GET"     end
head?() click to toggle source
    # File lib/rack/request.rb, line 85
85:     def head?;    request_method == "HEAD"    end
host() click to toggle source
    # 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
    # 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
     # File lib/rack/request.rb, line 256
256:     def ip
257:       if addr = @env['HTTP_X_FORWARDED_FOR']
258:         (addr.split(',').grep(/\d\./).first || @env['REMOTE_ADDR']).to_s.strip
259:       else
260:         @env['REMOTE_ADDR']
261:       end
262:     end
logger() click to toggle source
    # 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
options?() click to toggle source
    # File lib/rack/request.rb, line 86
86:     def options?; request_method == "OPTIONS" end
params() click to toggle source

The union of GET and POST data.

     # File lib/rack/request.rb, line 166
166:     def params
167:       self.GET.update(self.POST)
168:     rescue EOFError => e
169:       self.GET
170:     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 123
123:     def parseable_data?
124:       PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
125:     end
path() click to toggle source
     # File lib/rack/request.rb, line 236
236:     def path
237:       script_name + path_info
238:     end
path_info() click to toggle source
    # File lib/rack/request.rb, line 27
27:     def path_info;       @env["PATH_INFO"].to_s                   end
path_info=(s) click to toggle source
    # File lib/rack/request.rb, line 81
81:     def path_info=(s);   @env["PATH_INFO"] = s.to_s               end
port() click to toggle source
    # File lib/rack/request.rb, line 28
28:     def port;            @env["SERVER_PORT"].to_i                 end
post?() click to toggle source
    # File lib/rack/request.rb, line 87
87:     def post?;    request_method == "POST"    end
put?() click to toggle source
    # File lib/rack/request.rb, line 88
88:     def put?;     request_method == "PUT"     end
query_string() click to toggle source
    # 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 188
188:     def referer
189:       @env['HTTP_REFERER'] || '/'
190:     end
Also aliased as: referrer
referrer() click to toggle source
Alias for: referer
request_method() click to toggle source
    # File lib/rack/request.rb, line 29
29:     def request_method;  @env["REQUEST_METHOD"]                   end
scheme() click to toggle source
    # File lib/rack/request.rb, line 25
25:     def scheme;          @env["rack.url_scheme"]                  end
script_name() click to toggle source
    # File lib/rack/request.rb, line 26
26:     def script_name;     @env["SCRIPT_NAME"].to_s                 end
script_name=(s) click to toggle source
    # File lib/rack/request.rb, line 80
80:     def script_name=(s); @env["SCRIPT_NAME"] = s.to_s             end
session() click to toggle source
    # File lib/rack/request.rb, line 33
33:     def session;         @env['rack.session'] ||= {}              end
session_options() click to toggle source
    # File lib/rack/request.rb, line 34
34:     def session_options; @env['rack.session.options'] ||= {}      end
trace?() click to toggle source
    # File lib/rack/request.rb, line 89
89:     def trace?;   request_method == "TRACE"   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 222
222:     def url
223:       url = scheme + "://"
224:       url << host
225: 
226:       if scheme == "https" && port != 443 ||
227:           scheme == "http" && port != 80
228:         url << ":#{port}"
229:       end
230: 
231:       url << fullpath
232: 
233:       url
234:     end
user_agent() click to toggle source
     # File lib/rack/request.rb, line 193
193:     def user_agent
194:       @env['HTTP_USER_AGENT']
195:     end
values_at(*keys) click to toggle source

like Hash#values_at

     # File lib/rack/request.rb, line 183
183:     def values_at(*keys)
184:       keys.map{|key| params[key] }
185:     end
xhr?() click to toggle source
     # File lib/rack/request.rb, line 217
217:     def xhr?
218:       @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
219:     end

Protected Instance Methods

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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.