Methods
Constants
HTTP_METHODS = %w(get head put post delete options)
HTTP_METHOD_LOOKUP = HTTP_METHODS.inject({}) { |h, m| h[m] = h[m.upcase] = m.to_sym;
TRUSTED_PROXIES = /^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
  Which IP addresses are "trusted proxies" that can be stripped from the right-hand-side of X-Forwarded-For
Public Instance methods
GET()

Override Rack‘s GET method to support indifferent access

This method is also aliased as query_parameters
     # File actionpack/lib/action_controller/request.rb, line 426
426:     def GET
427:       @env["action_controller.request.query_parameters"] ||= normalize_parameters(super)
428:     end
POST()

Override Rack‘s POST method to support indifferent access

This method is also aliased as request_parameters
     # File actionpack/lib/action_controller/request.rb, line 432
432:     def POST
433:       @env["action_controller.request.request_parameters"] ||= normalize_parameters(super)
434:     end
accepts()

Returns the accepted MIME type for the request.

     # File actionpack/lib/action_controller/request.rb, line 103
103:     def accepts
104:       @accepts ||= begin
105:         header = @env['HTTP_ACCEPT'].to_s.strip
106: 
107:         if header.empty?
108:           [content_type, Mime::ALL].compact
109:         else
110:           Mime::Type.parse(header)
111:         end
112:       end
113:     end
body()

The request body is an IO input stream. If the RAW_POST_DATA environment variable is already set, wrap it in a StringIO.

     # File actionpack/lib/action_controller/request.rb, line 412
412:     def body
413:       if raw_post = @env['RAW_POST_DATA']
414:         raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
415:         StringIO.new(raw_post)
416:       else
417:         @env['rack.input']
418:       end
419:     end
cache_format()
     # File actionpack/lib/action_controller/request.rb, line 199
199:     def cache_format
200:       parameters[:format]
201:     end
content_length()

Returns the content length of the request as an integer.

    # File actionpack/lib/action_controller/request.rb, line 80
80:     def content_length
81:       super.to_i
82:     end
content_type()

The MIME type of the HTTP request, such as Mime::XML.

For backward compatibility, the post \format is extracted from the X-Post-Data-Format HTTP header if present.

    # File actionpack/lib/action_controller/request.rb, line 88
88:     def content_type
89:       @content_type ||= begin
90:         if @env['CONTENT_TYPE'] =~ /^([^,\;]*)/
91:           Mime::Type.lookup($1.strip.downcase)
92:         else
93:           nil
94:         end
95:       end
96:     end
delete?()

Is this a DELETE request? Equivalent to request.method == :delete.

    # File actionpack/lib/action_controller/request.rb, line 62
62:     def delete?
63:       request_method == :delete
64:     end
domain(tld_length = 1)

Returns the \domain part of a \host, such as "rubyonrails.org" in "www.rubyonrails.org". You can specify a different tld_length, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk".

     # File actionpack/lib/action_controller/request.rb, line 320
320:     def domain(tld_length = 1)
321:       return nil unless named_host?(host)
322: 
323:       host.split('.').last(1 + tld_length).join('.')
324:     end
etag_matches?(etag)
     # File actionpack/lib/action_controller/request.rb, line 129
129:     def etag_matches?(etag)
130:       if_none_match && if_none_match == etag
131:     end
form_data?()
     # File actionpack/lib/action_controller/request.rb, line 421
421:     def form_data?
422:       FORM_DATA_MEDIA_TYPES.include?(content_type.to_s)
423:     end
format()

Returns the Mime type for the \format used in the request.

  GET /posts/5.xml   | request.format => Mime::XML
  GET /posts/5.xhtml | request.format => Mime::HTML
  GET /posts/5       | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of <tt>ActionController::Base.use_accept_header</tt>
     # File actionpack/lib/action_controller/request.rb, line 154
154:     def format
155:       @format ||=
156:         if parameters[:format]
157:           Mime::Type.lookup_by_extension(parameters[:format])
158:         elsif ActionController::Base.use_accept_header
159:           accepts.first
160:         elsif xhr?
161:           Mime::Type.lookup_by_extension("js")
162:         else
163:           Mime::Type.lookup_by_extension("html")
164:         end
165:     end
format=(extension)

Sets the \format by string extension, which can be used to force custom formats that are not controlled by the extension.

  class ApplicationController < ActionController::Base
    before_filter :adjust_format_for_iphone

    private
      def adjust_format_for_iphone
        request.format = :iphone if request.env["HTTP_USER_AGENT"][/iPhone/]
      end
  end
     # File actionpack/lib/action_controller/request.rb, line 179
179:     def format=(extension)
180:       parameters[:format] = extension.to_s
181:       @format = Mime::Type.lookup_by_extension(parameters[:format])
182:     end
fresh?(response)

Check response freshness (Last-Modified and ETag) against request If-Modified-Since and If-None-Match conditions. If both headers are supplied, both must match, or the request is not considered fresh.

     # File actionpack/lib/action_controller/request.rb, line 136
136:     def fresh?(response)
137:       case
138:       when if_modified_since && if_none_match
139:         not_modified?(response.last_modified) && etag_matches?(response.etag)
140:       when if_modified_since
141:         not_modified?(response.last_modified)
142:       when if_none_match
143:         etag_matches?(response.etag)
144:       else
145:         false
146:       end
147:     end
get?()

Is this a GET (or HEAD) request? Equivalent to request.method == :get.

    # File actionpack/lib/action_controller/request.rb, line 47
47:     def get?
48:       method == :get
49:     end
head?()

Is this a HEAD request? Since request.method sees HEAD as :get, this \method checks the actual HTTP \method directly.

    # File actionpack/lib/action_controller/request.rb, line 68
68:     def head?
69:       request_method == :head
70:     end
headers()

Provides access to the request‘s HTTP headers, for example:

  request.headers["Content-Type"] # => "text/plain"
    # File actionpack/lib/action_controller/request.rb, line 75
75:     def headers
76:       @headers ||= ActionController::Http::Headers.new(@env)
77:     end
host()

Returns the host for this request, such as example.com.

     # File actionpack/lib/action_controller/request.rb, line 285
285:     def host
286:       raw_host_with_port.sub(/:\d+$/, '')
287:     end
host_with_port()

Returns a \host:\port string for this request, such as "example.com" or "example.com:8080".

     # File actionpack/lib/action_controller/request.rb, line 291
291:     def host_with_port
292:       "#{host}#{port_string}"
293:     end
if_modified_since()
     # File actionpack/lib/action_controller/request.rb, line 115
115:     def if_modified_since
116:       if since = env['HTTP_IF_MODIFIED_SINCE']
117:         Time.rfc2822(since) rescue nil
118:       end
119:     end
if_none_match()
     # File actionpack/lib/action_controller/request.rb, line 121
121:     def if_none_match
122:       env['HTTP_IF_NONE_MATCH']
123:     end
key?(key)
    # File actionpack/lib/action_controller/request.rb, line 24
24:     def key?(key)
25:       @env.key?(key)
26:     end
media_type()
     # File actionpack/lib/action_controller/request.rb, line 98
 98:     def media_type
 99:       content_type.to_s
100:     end
method()

Returns the HTTP request \method used for action processing as a lowercase symbol, such as :post. (Unlike request_method, this method returns :get for a HEAD request because the two are functionally equivalent from the application‘s perspective.)

    # File actionpack/lib/action_controller/request.rb, line 42
42:     def method
43:       request_method == :head ? :get : request_method
44:     end
not_modified?(modified_at)
     # File actionpack/lib/action_controller/request.rb, line 125
125:     def not_modified?(modified_at)
126:       if_modified_since && modified_at && if_modified_since >= modified_at
127:     end
parameters()

Returns both GET and POST \parameters in a single hash.

This method is also aliased as params
     # File actionpack/lib/action_controller/request.rb, line 385
385:     def parameters
386:       @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access
387:     end
params()

Alias for parameters

path()

Returns the interpreted \path to requested resource after all the installation directory of this application was taken into account.

     # File actionpack/lib/action_controller/request.rb, line 368
368:     def path
369:       path = request_uri.to_s[/\A[^\?]*/]
370:       path.sub!(/\A#{ActionController::Base.relative_url_root}/, '')
371:       path
372:     end
path_parameters()

Returns a hash with the \parameters used to form the \path of the request. Returned hash keys are strings:

  {'action' => 'my_action', 'controller' => 'my_controller'}

See symbolized_path_parameters for symbolized keys.

     # File actionpack/lib/action_controller/request.rb, line 406
406:     def path_parameters
407:       @env["action_controller.request.path_parameters"] ||= {}
408:     end
port()

Returns the port number of this request as an integer.

     # File actionpack/lib/action_controller/request.rb, line 296
296:     def port
297:       if raw_host_with_port =~ /:(\d+)$/
298:         $1.to_i
299:       else
300:         standard_port
301:       end
302:     end
port_string()

Returns a \port suffix like ":8080" if the \port number of this request is not the default HTTP \port 80 or HTTPS \port 443.

     # File actionpack/lib/action_controller/request.rb, line 314
314:     def port_string
315:       port == standard_port ? '' : ":#{port}"
316:     end
post?()

Is this a POST request? Equivalent to request.method == :post.

    # File actionpack/lib/action_controller/request.rb, line 52
52:     def post?
53:       request_method == :post
54:     end
protocol()

Returns ‘https://’ if this is an SSL request and ‘http://’ otherwise.

     # File actionpack/lib/action_controller/request.rb, line 266
266:     def protocol
267:       ssl? ? 'https://' : 'http://'
268:     end
put?()

Is this a PUT request? Equivalent to request.method == :put.

    # File actionpack/lib/action_controller/request.rb, line 57
57:     def put?
58:       request_method == :put
59:     end
query_parameters()

Alias for GET

query_string()

Returns the query string, accounting for server idiosyncrasies.

     # File actionpack/lib/action_controller/request.rb, line 337
337:     def query_string
338:       @env['QUERY_STRING'].present? ? @env['QUERY_STRING'] : (@env['REQUEST_URI'].split('?', 2)[1] || '')
339:     end
raw_host_with_port()

Returns the \host for this request, such as "example.com".

     # File actionpack/lib/action_controller/request.rb, line 276
276:     def raw_host_with_port
277:       if forwarded = env["HTTP_X_FORWARDED_HOST"]
278:         forwarded.split(/,\s?/).last
279:       else
280:         env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
281:       end
282:     end
raw_post()

Read the request \body. This is useful for web services that need to work with raw requests directly.

     # File actionpack/lib/action_controller/request.rb, line 376
376:     def raw_post
377:       unless @env.include? 'RAW_POST_DATA'
378:         @env['RAW_POST_DATA'] = body.read(@env['CONTENT_LENGTH'].to_i)
379:         body.rewind if body.respond_to?(:rewind)
380:       end
381:       @env['RAW_POST_DATA']
382:     end
remote_ip()

Determines originating IP address. REMOTE_ADDR is the standard but will fail if the user is behind a proxy. HTTP_CLIENT_IP and/or HTTP_X_FORWARDED_FOR are set by proxies so check for these if REMOTE_ADDR is a proxy. HTTP_X_FORWARDED_FOR may be a comma- delimited list in the case of multiple chained proxies; the last address which is not trusted is the originating IP.

     # File actionpack/lib/action_controller/request.rb, line 221
221:     def remote_ip
222:       remote_addr_list = @env['REMOTE_ADDR'] && @env['REMOTE_ADDR'].scan(/[^,\s]+/)
223: 
224:       unless remote_addr_list.blank?
225:         not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES}
226:         return not_trusted_addrs.first unless not_trusted_addrs.empty?
227:       end
228:       remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',')
229: 
230:       if @env.include? 'HTTP_CLIENT_IP'
231:         if ActionController::Base.ip_spoofing_check && remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP'])
232:           # We don't know which came from the proxy, and which from the user
233:           raise ActionControllerError.new("IP spoofing attack?!\nHTTP_CLIENT_IP=\#{@env['HTTP_CLIENT_IP'].inspect}\nHTTP_X_FORWARDED_FOR=\#{@env['HTTP_X_FORWARDED_FOR'].inspect}\n")
234:         end
235: 
236:         return @env['HTTP_CLIENT_IP']
237:       end
238: 
239:       if remote_ips
240:         while remote_ips.size > 1 && TRUSTED_PROXIES =~ remote_ips.last.strip
241:           remote_ips.pop
242:         end
243: 
244:         return remote_ips.last.strip
245:       end
246: 
247:       @env['REMOTE_ADDR']
248:     end
request_method()

Returns the true HTTP request \method as a lowercase symbol, such as :get. If the request \method is not listed in the HTTP_METHODS constant above, an UnknownHttpMethod exception is raised.

    # File actionpack/lib/action_controller/request.rb, line 34
34:     def request_method
35:       @request_method ||= HTTP_METHOD_LOOKUP[super] || raise(UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence(:locale => :en)}")
36:     end
request_parameters()

Alias for POST

request_uri()

Returns the request URI, accounting for server idiosyncrasies. WEBrick includes the full URL. IIS leaves REQUEST_URI blank.

     # File actionpack/lib/action_controller/request.rb, line 343
343:     def request_uri
344:       if uri = @env['REQUEST_URI']
345:         # Remove domain, which webrick puts into the request_uri.
346:         (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
347:       else
348:         # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
349:         uri = @env['PATH_INFO'].to_s
350: 
351:         if script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
352:           uri = uri.sub(/#{script_filename}\//, '')
353:         end
354: 
355:         env_qs = @env['QUERY_STRING'].to_s
356:         uri += "?#{env_qs}" unless env_qs.empty?
357: 
358:         if uri.blank?
359:           @env.delete('REQUEST_URI')
360:         else
361:           @env['REQUEST_URI'] = uri
362:         end
363:       end
364:     end
reset_session()
     # File actionpack/lib/action_controller/request.rb, line 449
449:     def reset_session
450:       session.destroy if session
451:       self.session = {}
452:     end
server_port()
     # File actionpack/lib/action_controller/request.rb, line 462
462:     def server_port
463:       @env['SERVER_PORT'].to_i
464:     end
server_software()

Returns the lowercase name of the HTTP server software.

     # File actionpack/lib/action_controller/request.rb, line 256
256:     def server_software
257:       (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil
258:     end
session()
     # File actionpack/lib/action_controller/request.rb, line 441
441:     def session
442:       @env['rack.session'] ||= {}
443:     end
session_options()
     # File actionpack/lib/action_controller/request.rb, line 454
454:     def session_options
455:       @env['rack.session.options'] ||= {}
456:     end
session_options=(options)
     # File actionpack/lib/action_controller/request.rb, line 458
458:     def session_options=(options)
459:       @env['rack.session.options'] = options
460:     end
ssl?()

Is this an SSL request?

     # File actionpack/lib/action_controller/request.rb, line 271
271:     def ssl?
272:       @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https'
273:     end
standard_port()

Returns the standard \port number for this request‘s protocol.

     # File actionpack/lib/action_controller/request.rb, line 305
305:     def standard_port
306:       case protocol
307:         when 'https://' then 443
308:         else 80
309:       end
310:     end
subdomains(tld_length = 1)

Returns all the \subdomains as an array, so ["dev", "www"] would be returned for "dev.www.rubyonrails.org". You can specify a different tld_length, such as 2 to catch ["www"] instead of ["www", "rubyonrails"] in "www.rubyonrails.co.uk".

     # File actionpack/lib/action_controller/request.rb, line 330
330:     def subdomains(tld_length = 1)
331:       return [] unless named_host?(host)
332:       parts = host.split('.')
333:       parts[0..-(tld_length+2)]
334:     end
symbolized_path_parameters()

The same as path_parameters with explicitly symbolized keys.

     # File actionpack/lib/action_controller/request.rb, line 396
396:     def symbolized_path_parameters
397:       @symbolized_path_parameters ||= path_parameters.symbolize_keys
398:     end
template_format()

Returns a symbolized version of the :format parameter of the request. If no \format is given it returns :jsfor Ajax requests and :html otherwise.

     # File actionpack/lib/action_controller/request.rb, line 187
187:     def template_format
188:       parameter_format = parameters[:format]
189: 
190:       if parameter_format
191:         parameter_format
192:       elsif xhr?
193:         :js
194:       else
195:         :html
196:       end
197:     end
url()

Returns the complete URL used for this request.

     # File actionpack/lib/action_controller/request.rb, line 261
261:     def url
262:       protocol + host_with_port + request_uri
263:     end
xhr?()

Alias for xml_http_request?

xml_http_request?()

Returns true if the request‘s "X-Requested-With" header contains "XMLHttpRequest". (The Prototype Javascript library sends this header with every Ajax request.)

This method is also aliased as xhr?
     # File actionpack/lib/action_controller/request.rb, line 206
206:     def xml_http_request?
207:       !(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i)
208:     end