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 337
337:     def body
338:     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 223
223:     def domain(tld_length = 1)
224:       return nil unless named_host?(host)
225: 
226:       host.split('.').last(1 + tld_length).join('.')
227:     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 193
193:     def host
194:     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 198
198:     def host_with_port
199:       @host_with_port ||= host + port_string
200:     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 307
307:     def parameters
308:       @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access
309:     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 272
272:     def path
273:       path = (uri = request_uri) ? uri.split('?').first.to_s : ''
274: 
275:       # Cut off the path to the installation directory if given
276:       path.sub!(%r/^#{relative_url_root}/, '')
277:       path || ''      
278:     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 327
327:     def path_parameters
328:       @path_parameters ||= {}
329:     end

Returns the port number of this request as an integer.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 203
203:     def port
204:       @port_as_int ||= @env['SERVER_PORT'].to_i
205:     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 217
217:     def port_string
218:       (port == standard_port) ? '' : ":#{port}"
219:     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 183
183:     def protocol
184:       ssl? ? 'https://' : 'http://'
185:     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 239
239:     def query_string
240:       if uri = @env['REQUEST_URI']
241:         uri.split('?', 2)[1] || ''
242:       else
243:         @env['QUERY_STRING'] || ''
244:       end
245:     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 298
298:     def raw_post
299:       unless env.include? 'RAW_POST_DATA'
300:         env['RAW_POST_DATA'] = body.read(content_length)
301:         body.rewind if body.respond_to?(:rewind)
302:       end
303:       env['RAW_POST_DATA']
304:     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 284
284:     def relative_url_root
285:       @@relative_url_root ||= case
286:         when @env["RAILS_RELATIVE_URL_ROOT"]
287:           @env["RAILS_RELATIVE_URL_ROOT"]
288:         when server_software == 'apache'
289:           @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '')
290:         else
291:           ''
292:       end
293:     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 137
137:     def remote_ip
138:       remote_addr_list = @env['REMOTE_ADDR'] && @env['REMOTE_ADDR'].split(',').collect(&:strip)
139: 
140:       unless remote_addr_list.blank?
141:         not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES}
142:         return not_trusted_addrs.first unless not_trusted_addrs.empty?
143:       end
144:       remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',')
145: 
146:       if @env.include? 'HTTP_CLIENT_IP'
147:         if remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP'])
148:           # We don't know which came from the proxy, and which from the user
149:           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")
150:         end
151: 
152:         return @env['HTTP_CLIENT_IP']
153:       end
154: 
155:       if remote_ips
156:         while remote_ips.size > 1 && TRUSTED_PROXIES =~ remote_ips.last.strip
157:           remote_ips.pop
158:         end
159: 
160:         return remote_ips.last.strip
161:       end
162: 
163:       @env['REMOTE_ADDR']
164:     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 249
249:     def request_uri
250:       if uri = @env['REQUEST_URI']
251:         # Remove domain, which webrick puts into the request_uri.
252:         (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
253:       else
254:         # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
255:         script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
256:         uri = @env['PATH_INFO']
257:         uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
258:         unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
259:           uri << '?' << env_qs
260:         end
261: 
262:         if uri.nil?
263:           @env.delete('REQUEST_URI')
264:           uri
265:         else
266:           @env['REQUEST_URI'] = uri
267:         end
268:       end
269:     end

Returns the lowercase name of the HTTP server software.

[Source]

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

Is this an SSL request?

[Source]

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

Returns the standard port number for this request‘s protocol

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 208
208:     def standard_port
209:       case protocol
210:         when 'https://' then 443
211:         else 80
212:       end
213:     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 232
232:     def subdomains(tld_length = 1)
233:       return [] unless named_host?(host)
234:       parts = host.split('.')
235:       parts[0..-(tld_length+2)]
236:     end

The same as path_parameters with explicitly symbolized keys

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 317
317:     def symbolized_path_parameters 
318:       @symbolized_path_parameters ||= path_parameters.symbolize_keys
319:     end

Returns the complete URL used for this request

[Source]

     # File vendor/rails/actionpack/lib/action_controller/request.rb, line 178
178:     def url
179:       protocol + host_with_port + request_uri
180:     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 363
363:       def content_type_with_parameters
364:         content_type_from_legacy_post_data_format_header ||
365:           env['CONTENT_TYPE'].to_s
366:       end

The raw content type string with its parameters stripped off.

[Source]

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

[Validate]