Class ActionController::AbstractRequest
In: vendor/rails/actionpack/lib/action_controller/request.rb
Parent: Object

CgiRequest and TestRequest provide concrete implementations.

Methods

Constants

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
MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
EOL = "\015\012"

Attributes

env  [R]  The hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }.

Public Instance methods

Returns the accepted MIME type for the request

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 83
83:     def accepts
84:       @accepts ||=
85:         if @env['HTTP_ACCEPT'].to_s.strip.empty?
86:           [ content_type, Mime::ALL ].compact # make sure content_type being nil is not included
87:         else
88:           Mime::Type.parse(@env['HTTP_ACCEPT'])
89:         end
90:     end

The request body is an IO input stream.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 334
334:     def body
335:     end

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 70
70:     def content_length
71:       @content_length ||= env['CONTENT_LENGTH'].to_i
72:     end

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.

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 78
78:     def content_type
79:       @content_type ||= Mime::Type.lookup(content_type_without_parameters)
80:     end

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

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 54
54:     def delete?
55:       request_method == :delete
56:     end

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".

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 220
220:     def domain(tld_length = 1)
221:       return nil unless named_host?(host)
222: 
223:       host.split('.').last(1 + tld_length).join('.')
224:     end

Returns the Mime type for the format used in the request. If there is no format available, the first of the accept types will be used. Examples:

  GET /posts/5.xml   | request.format => Mime::XML
  GET /posts/5.xhtml | request.format => Mime::HTML
  GET /posts/5       | request.format => request.accepts.first (usually Mime::HTML for browsers)

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 98
 98:     def format
 99:       @format ||= parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first
100:     end

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

  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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 114
114:     def format=(extension)
115:       parameters[:format] = extension.to_s
116:       @format = Mime::Type.lookup_by_extension(parameters[:format])
117:     end

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

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 39
39:     def get?
40:       method == :get
41:     end

Is this a HEAD request? request.method sees HEAD as :get, so check the HTTP method directly.

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 60
60:     def head?
61:       request_method == :head
62:     end

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

 request.headers["Content-Type"] # => "text/plain"

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 66
66:     def headers
67:       @headers ||= ActionController::Http::Headers.new(@env)
68:     end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 190
190:     def host
191:     end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 195
195:     def host_with_port
196:       @host_with_port ||= host + port_string
197:     end

The HTTP request method as a lowercase symbol, such as :get. Note, HEAD is returned as :get since the two are functionally equivalent from the application‘s perspective.

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 34
34:     def method
35:       request_method == :head ? :get : request_method
36:     end

Returns both GET and POST parameters in a single hash.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 304
304:     def parameters
305:       @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access
306:     end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 269
269:     def path
270:       path = (uri = request_uri) ? uri.split('?').first.to_s : ''
271: 
272:       # Cut off the path to the installation directory if given
273:       path.sub!(%r/^#{relative_url_root}/, '')
274:       path || ''      
275:     end

Returns a hash with the parameters used to form the path of the request. Returned hash keys are strings. See symbolized_path_parameters for symbolized keys.

Example:

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 324
324:     def path_parameters
325:       @path_parameters ||= {}
326:     end

Returns the port number of this request as an integer.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 200
200:     def port
201:       @port_as_int ||= @env['SERVER_PORT'].to_i
202:     end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 214
214:     def port_string
215:       (port == standard_port) ? '' : ":#{port}"
216:     end

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

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 44
44:     def post?
45:       request_method == :post
46:     end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 180
180:     def protocol
181:       ssl? ? 'https://' : 'http://'
182:     end

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

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 49
49:     def put?
50:       request_method == :put
51:     end

Return the query string, accounting for server idiosyncracies.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 236
236:     def query_string
237:       if uri = @env['REQUEST_URI']
238:         uri.split('?', 2)[1] || ''
239:       else
240:         @env['QUERY_STRING'] || ''
241:       end
242:     end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 295
295:     def raw_post
296:       unless env.include? 'RAW_POST_DATA'
297:         env['RAW_POST_DATA'] = body.read(content_length)
298:         body.rewind if body.respond_to?(:rewind)
299:       end
300:       env['RAW_POST_DATA']
301:     end

Returns the path minus the web server relative installation directory. This can be set with the environment variable RAILS_RELATIVE_URL_ROOT. It can be automatically extracted for Apache setups. If the server is not Apache, this method returns an empty string.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 281
281:     def relative_url_root
282:       @@relative_url_root ||= case
283:         when @env["RAILS_RELATIVE_URL_ROOT"]
284:           @env["RAILS_RELATIVE_URL_ROOT"]
285:         when server_software == 'apache'
286:           @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '')
287:         else
288:           ''
289:       end
290:     end

Determine 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.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 138
138:     def remote_ip
139:       if TRUSTED_PROXIES !~ @env['REMOTE_ADDR']
140:         return @env['REMOTE_ADDR']
141:       end
142: 
143:       if @env.include? 'HTTP_CLIENT_IP'
144:         if @env.include? 'HTTP_X_FORWARDED_FOR'
145:           # We don't know which came from the proxy, and which from the user
146:           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")
147:         end
148:         return @env['HTTP_CLIENT_IP']
149:       end
150: 
151:       if @env.include? 'HTTP_X_FORWARDED_FOR' then
152:         remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',')
153:         while remote_ips.size > 1 && TRUSTED_PROXIES =~ remote_ips.last.strip
154:           remote_ips.pop
155:         end
156: 
157:         return remote_ips.last.strip
158:       end
159: 
160:       @env['REMOTE_ADDR']
161:     end

The true HTTP request method as a lowercase symbol, such as :get. UnknownHttpMethod is raised for invalid methods not listed in ACCEPTED_HTTP_METHODS.

[Source]

    # File vendor/rails/actionpack/lib/action_controller/request.rb, line 20
20:     def request_method
21:       @request_method ||= begin
22:         method = ((@env['REQUEST_METHOD'] == 'POST' && !parameters[:_method].blank?) ? parameters[:_method].to_s : @env['REQUEST_METHOD']).downcase
23:         if ACCEPTED_HTTP_METHODS.include?(method)
24:           method.to_sym
25:         else
26:           raise UnknownHttpMethod, "#{method}, accepted HTTP methods are #{ACCEPTED_HTTP_METHODS.to_a.to_sentence}"
27:         end
28:       end
29:     end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 246
246:     def request_uri
247:       if uri = @env['REQUEST_URI']
248:         # Remove domain, which webrick puts into the request_uri.
249:         (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
250:       else
251:         # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
252:         script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
253:         uri = @env['PATH_INFO']
254:         uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
255:         unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
256:           uri << '?' << env_qs
257:         end
258: 
259:         if uri.nil?
260:           @env.delete('REQUEST_URI')
261:           uri
262:         else
263:           @env['REQUEST_URI'] = uri
264:         end
265:       end
266:     end

Returns the lowercase name of the HTTP server software.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 169
169:     def server_software
170:       (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil
171:     end

Is this an SSL request?

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 185
185:     def ssl?
186:       @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https'
187:     end

Returns the standard port number for this request‘s protocol

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 205
205:     def standard_port
206:       case protocol
207:         when 'https://' then 443
208:         else 80
209:       end
210:     end

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".

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 229
229:     def subdomains(tld_length = 1)
230:       return [] unless named_host?(host)
231:       parts = host.split('.')
232:       parts[0..-(tld_length+2)]
233:     end

The same as path_parameters with explicitly symbolized keys

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 314
314:     def symbolized_path_parameters 
315:       @symbolized_path_parameters ||= path_parameters.symbolize_keys
316:     end

Returns the complete URL used for this request

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 175
175:     def url
176:       protocol + host_with_port + request_uri
177:     end
xhr?()

Alias for 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.)

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 122
122:     def xml_http_request?
123:       !(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i)
124:     end

Protected Instance methods

The raw content type string. Use when you need parameters such as charset or boundary which aren‘t included in the content_type MIME type. Overridden by the X-POST_DATA_FORMAT header for backward compatibility.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 360
360:       def content_type_with_parameters
361:         content_type_from_legacy_post_data_format_header ||
362:           env['CONTENT_TYPE'].to_s
363:       end

The raw content type string with its parameters stripped off.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 366
366:       def content_type_without_parameters
367:         @content_type_without_parameters ||= self.class.extract_content_type_without_parameters(content_type_with_parameters)
368:       end

[Validate]