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 received in the query string.

     # File lib/rack/request.rb, line 181
181:     def GET
182:       if @env["rack.request.query_string"] == query_string
183:         @env["rack.request.query_hash"]
184:       else
185:         @env["rack.request.query_string"] = query_string
186:         @env["rack.request.query_hash"]   = parse_query(query_string)
187:       end
188:     end
POST() click to toggle source

Returns the data received in the request body.

This method support both application/x-www-form-urlencoded and multipart/form-data.

     # File lib/rack/request.rb, line 194
194:     def POST
195:       if @env["rack.input"].nil?
196:         raise "Missing rack.input"
197:       elsif @env["rack.request.form_input"].eql? @env["rack.input"]
198:         @env["rack.request.form_hash"]
199:       elsif form_data? || parseable_data?
200:         @env["rack.request.form_input"] = @env["rack.input"]
201:         unless @env["rack.request.form_hash"] = parse_multipart(env)
202:           form_vars = @env["rack.input"].read
203: 
204:           # Fix for Safari Ajax postings that always append \0
205:           # form_vars.sub!(/\0\z/, '') # performance replacement:
206:           form_vars.slice!(1) if form_vars[1] == \00
207: \
208: 
209:           @env["rack.request.form_vars"] = form_vars
210:           @env["rack.request.form_hash"] = parse_query(form_vars)
211: 
212:           @env["rack.input"].rewind
213:         end
214:         @env["rack.request.form_hash"]
215:       else
216:         {}
217:       end
218:     end
[](key) click to toggle source

shortcut for request.params[key]

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

shortcut for request.params[key] = value

     # File lib/rack/request.rb, line 232
232:     def []=(key, value)
233:       params[key.to_s] = value
234:     end
accept_encoding() click to toggle source
     # File lib/rack/request.rb, line 300
300:     def accept_encoding
301:       @env["HTTP_ACCEPT_ENCODING"].to_s.split(/\s*,\s*/).map do |part|
302:         encoding, parameters = part.split(/\s*;\s*/, 2)
303:         quality = 1.0
304:         if parameters and /\Aq=([\d.]+)/ =~ parameters
305:           quality = $1.to_f
306:         end
307:         [encoding, quality]
308:       end
309:     end
base_url() click to toggle source
     # File lib/rack/request.rb, line 275
275:     def base_url
276:       url = scheme + "://"
277:       url << host
278: 
279:       if scheme == "https" && port != 443 ||
280:           scheme == "http" && port != 80
281:         url << ":#{port}"
282:       end
283: 
284:       url
285:     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 66
66:     def content_charset
67:       media_type_params['charset']
68:     end
content_length() click to toggle source
    # File lib/rack/request.rb, line 29
29:     def content_length;  @env['CONTENT_LENGTH']                   end
content_type() click to toggle source
    # File lib/rack/request.rb, line 31
31:     def content_type
32:       content_type = @env['CONTENT_TYPE']
33:       content_type.nil? || content_type.empty? ? nil : content_type
34:     end
cookies() click to toggle source
     # File lib/rack/request.rb, line 251
251:     def cookies
252:       hash   = @env["rack.request.cookie_hash"] ||= {}
253:       string = @env["HTTP_COOKIE"]
254: 
255:       return hash if string == @env["rack.request.cookie_string"]
256:       hash.clear
257: 
258:       # According to RFC 2109:
259:       #   If multiple cookies satisfy the criteria above, they are ordered in
260:       #   the Cookie header such that those with more specific Path attributes
261:       #   precede those with less specific.  Ordering with respect to other
262:       #   attributes (e.g., Domain) is unspecified.
263:       Utils.parse_query(string, ';,').each { |k,v| hash[k] = Array === v ? v.first : v }
264:       @env["rack.request.cookie_string"] = string
265:       hash
266:     rescue => error
267:       error.message.replace "cannot parse Cookie header: #{error.message}"
268:       raise
269:     end
delete?() click to toggle source

Checks the HTTP request method (or verb) to see if it was of type DELETE

     # File lib/rack/request.rb, line 120
120:     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 168
168:     def form_data?
169:       type = media_type
170:       meth = env["rack.methodoverride.original_method"] || env['REQUEST_METHOD']
171:       (meth == 'POST' && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
172:     end
fullpath() click to toggle source
     # File lib/rack/request.rb, line 296
296:     def fullpath
297:       query_string.empty? ? path : "#{path}?#{query_string}"
298:     end
get?() click to toggle source

Checks the HTTP request method (or verb) to see if it was of type GET

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

Checks the HTTP request method (or verb) to see if it was of type HEAD

     # File lib/rack/request.rb, line 126
126:     def head?;    request_method == "HEAD"    end
host() click to toggle source
     # File lib/rack/request.rb, line 110
110:     def host
111:       # Remove port number.
112:       host_with_port.to_s.gsub(/:\d+\z/, '')
113:     end
host_with_port() click to toggle source
    # File lib/rack/request.rb, line 88
88:     def host_with_port
89:       if forwarded = @env["HTTP_X_FORWARDED_HOST"]
90:         forwarded.split(/,\s?/).last
91:       else
92:         @env['HTTP_HOST'] || "#{@env['SERVER_NAME'] || @env['SERVER_ADDR']}:#{@env['SERVER_PORT']}"
93:       end
94:     end
ip() click to toggle source
     # File lib/rack/request.rb, line 315
315:     def ip
316:       remote_addrs = @env['REMOTE_ADDR'] ? @env['REMOTE_ADDR'].split(/[,\s]+/) : []
317:       remote_addrs.reject! { |addr| trusted_proxy?(addr) }
318:       
319:       return remote_addrs.first if remote_addrs.any?
320: 
321:       forwarded_ips = @env['HTTP_X_FORWARDED_FOR'] ? @env['HTTP_X_FORWARDED_FOR'].strip.split(/[,\s]+/) : []
322: 
323:       if client_ip = @env['HTTP_CLIENT_IP']
324:         # If forwarded_ips doesn't include the client_ip, it might be an
325:         # ip spoofing attempt, so we ignore HTTP_CLIENT_IP
326:         return client_ip if forwarded_ips.include?(client_ip)
327:       end
328: 
329:       return forwarded_ips.reject { |ip| trusted_proxy?(ip) }.last || @env["REMOTE_ADDR"]
330:     end
logger() click to toggle source
    # File lib/rack/request.rb, line 38
38:     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 46
46:     def media_type
47:       content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
48:     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 55
55:     def media_type_params
56:       return {} if content_type.nil?
57:       Hash[*content_type.split(/\s*[;,]\s*/)[1..1].
58:         collect { |s| s.split('=', 2) }.
59:         map { |k,v| [k.downcase, v] }.flatten]
60:     end
options?() click to toggle source

Checks the HTTP request method (or verb) to see if it was of type OPTIONS

     # File lib/rack/request.rb, line 129
129:     def options?; request_method == "OPTIONS" end
params() click to toggle source

The union of GET and POST data.

     # File lib/rack/request.rb, line 220
220:     def params
221:       @params ||= self.GET.merge(self.POST)
222:     rescue EOFError
223:       self.GET
224:     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 176
176:     def parseable_data?
177:       PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
178:     end
patch?() click to toggle source

Checks the HTTP request method (or verb) to see if it was of type PATCH

     # File lib/rack/request.rb, line 132
132:     def patch?;   request_method == "PATCH"   end
path() click to toggle source
     # File lib/rack/request.rb, line 292
292:     def path
293:       script_name + path_info
294:     end
path_info() click to toggle source
    # File lib/rack/request.rb, line 26
26:     def path_info;       @env["PATH_INFO"].to_s                   end
path_info=(s) click to toggle source
     # File lib/rack/request.rb, line 116
116:     def path_info=(s);   @env["PATH_INFO"] = s.to_s               end
port() click to toggle source
     # File lib/rack/request.rb, line 96
 96:     def port
 97:       if port = host_with_port.split(/:/)[1]
 98:         port.to_i
 99:       elsif port = @env['HTTP_X_FORWARDED_PORT']
100:         port.to_i
101:       elsif ssl?
102:         443
103:       elsif @env.has_key?("HTTP_X_FORWARDED_HOST")
104:         80
105:       else
106:         @env["SERVER_PORT"].to_i
107:       end
108:     end
post?() click to toggle source

Checks the HTTP request method (or verb) to see if it was of type POST

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

Checks the HTTP request method (or verb) to see if it was of type PUT

     # File lib/rack/request.rb, line 138
138:     def put?;     request_method == "PUT"     end
query_string() click to toggle source
    # File lib/rack/request.rb, line 28
28:     def query_string;    @env["QUERY_STRING"].to_s                end
referer() click to toggle source

the referer of the client

     # File lib/rack/request.rb, line 242
242:     def referer
243:       @env['HTTP_REFERER']
244:     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 27
27:     def request_method;  @env["REQUEST_METHOD"]                   end
scheme() click to toggle source
    # File lib/rack/request.rb, line 70
70:     def scheme
71:       if @env['HTTPS'] == 'on'
72:         'https'
73:       elsif @env['HTTP_X_FORWARDED_SSL'] == 'on'
74:         'https'
75:       elsif @env['HTTP_X_FORWARDED_SCHEME']
76:         @env['HTTP_X_FORWARDED_SCHEME']
77:       elsif @env['HTTP_X_FORWARDED_PROTO']
78:         @env['HTTP_X_FORWARDED_PROTO'].split(',')[0]
79:       else
80:         @env["rack.url_scheme"]
81:       end
82:     end
script_name() click to toggle source
    # File lib/rack/request.rb, line 25
25:     def script_name;     @env["SCRIPT_NAME"].to_s                 end
script_name=(s) click to toggle source
     # File lib/rack/request.rb, line 115
115:     def script_name=(s); @env["SCRIPT_NAME"] = s.to_s             end
session() click to toggle source
    # File lib/rack/request.rb, line 36
36:     def session;         @env['rack.session'] ||= {}              end
session_options() click to toggle source
    # File lib/rack/request.rb, line 37
37:     def session_options; @env['rack.session.options'] ||= {}      end
ssl?() click to toggle source
    # File lib/rack/request.rb, line 84
84:     def ssl?
85:       scheme == 'https'
86:     end
trace?() click to toggle source

Checks the HTTP request method (or verb) to see if it was of type TRACE

     # File lib/rack/request.rb, line 141
141:     def trace?;   request_method == "TRACE"   end
trusted_proxy?(ip) click to toggle source
     # File lib/rack/request.rb, line 311
311:     def trusted_proxy?(ip)
312:       ip =~ /^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|^::1$|^fd[0-9a-f]{2}:.+|^localhost$/
313:     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 288
288:     def url
289:       base_url + fullpath
290:     end
user_agent() click to toggle source
     # File lib/rack/request.rb, line 247
247:     def user_agent
248:       @env['HTTP_USER_AGENT']
249:     end
values_at(*keys) click to toggle source

like Hash#values_at

     # File lib/rack/request.rb, line 237
237:     def values_at(*keys)
238:       keys.map{|key| params[key] }
239:     end
xhr?() click to toggle source
     # File lib/rack/request.rb, line 271
271:     def xhr?
272:       @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
273:     end

Protected Instance Methods

parse_multipart(env) click to toggle source
     # File lib/rack/request.rb, line 337
337:       def parse_multipart(env)
338:         Rack::Multipart.parse_multipart(env)
339:       end
parse_query(qs) click to toggle source
     # File lib/rack/request.rb, line 333
333:       def parse_query(qs)
334:         Utils.parse_nested_query(qs)
335:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.