| Class | ActionController::AbstractRequest |
| In: |
vendor/rails/actionpack/lib/action_controller/request.rb
|
| Parent: | Object |
CgiRequest and TestRequest provide concrete implementations.
| MULTIPART_BOUNDARY | = | %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n |
| EOL | = | "\015\012" |
| env | [R] | The hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }. |
Returns the accepted MIME type for the request
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 81
81: def accepts
82: @accepts ||=
83: if @env['HTTP_ACCEPT'].to_s.strip.empty?
84: [ content_type, Mime::ALL ].compact # make sure content_type being nil is not included
85: else
86: Mime::Type.parse(@env['HTTP_ACCEPT'])
87: end
88: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 68
68: def content_length
69: @content_length ||= env['CONTENT_LENGTH'].to_i
70: 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 76
76: def content_type
77: @content_type ||= Mime::Type.lookup(content_type_without_parameters)
78: end
Is this a DELETE request? Equivalent to request.method == :delete
# 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".
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 202
202: def domain(tld_length = 1)
203: return nil unless named_host?(host)
204:
205: host.split('.').last(1 + tld_length).join('.')
206: 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)
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 96
96: def format
97: @format ||= parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first
98: 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
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 112
112: def format=(extension)
113: parameters[:format] = extension.to_s
114: format
115: end
Is this a GET (or HEAD) request? Equivalent to request.method == :get
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 39
39: def get?
40: method == :get
41: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 64
64: def headers
65: @env
66: 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.
# 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 286
286: def parameters
287: @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access
288: end
Returns the interpreted path to requested resource after all the installation directory of this application was taken into account
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 251
251: def path
252: path = (uri = request_uri) ? uri.split('?').first.to_s : ''
253:
254: # Cut off the path to the installation directory if given
255: path.sub!(%r/^#{relative_url_root}/, '')
256: path || ''
257: 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'}
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 306
306: def path_parameters
307: @path_parameters ||= {}
308: end
Is this a POST request? Equivalent to request.method == :post
# 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 162
162: def protocol
163: ssl? ? 'https://' : 'http://'
164: end
Is this a PUT request? Equivalent to request.method == :put
# 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 218
218: def query_string
219: if uri = @env['REQUEST_URI']
220: uri.split('?', 2)[1] || ''
221: else
222: @env['QUERY_STRING'] || ''
223: end
224: end
Read the request body. This is useful for web services that need to work with raw requests directly.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 277
277: def raw_post
278: unless env.include? 'RAW_POST_DATA'
279: env['RAW_POST_DATA'] = body.read(content_length)
280: body.rewind if body.respond_to?(:rewind)
281: end
282: env['RAW_POST_DATA']
283: 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 263
263: def relative_url_root
264: @@relative_url_root ||= case
265: when @env["RAILS_RELATIVE_URL_ROOT"]
266: @env["RAILS_RELATIVE_URL_ROOT"]
267: when server_software == 'apache'
268: @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '')
269: else
270: ''
271: end
272: 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 before falling back to REMOTE_ADDR. HTTP_X_FORWARDED_FOR may be a comma- delimited list in the case of multiple chained proxies; the first is the originating IP.
Security note: do not use if IP spoofing is a concern for your application. Since remote_ip checks HTTP headers for addresses forwarded by proxies, the client may send any IP. remote_addr can‘t be spoofed but also doesn‘t work behind a proxy, since it‘s always the proxy‘s IP.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 136
136: def remote_ip
137: return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP'
138:
139: if @env.include? 'HTTP_X_FORWARDED_FOR' then
140: remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip|
141: ip.strip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
142: end
143:
144: return remote_ips.first.strip unless remote_ips.empty?
145: end
146:
147: @env['REMOTE_ADDR']
148: 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.
# 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 228
228: def request_uri
229: if uri = @env['REQUEST_URI']
230: # Remove domain, which webrick puts into the request_uri.
231: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
232: else
233: # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
234: script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
235: uri = @env['PATH_INFO']
236: uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
237: unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
238: uri << '?' << env_qs
239: end
240:
241: if uri.nil?
242: @env.delete('REQUEST_URI')
243: uri
244: else
245: @env['REQUEST_URI'] = uri
246: end
247: end
248: end
Returns the lowercase name of the HTTP server software.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 151
151: def server_software
152: (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil
153: end
Is this an SSL request?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 167
167: def ssl?
168: @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https'
169: 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".
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 211
211: def subdomains(tld_length = 1)
212: return [] unless named_host?(host)
213: parts = host.split('.')
214: parts[0..-(tld_length+2)]
215: end
The same as path_parameters with explicitly symbolized keys
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 296
296: def symbolized_path_parameters
297: @symbolized_path_parameters ||= path_parameters.symbolize_keys
298: end
Returns the complete URL used for this request
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 157
157: def url
158: protocol + host_with_port + request_uri
159: end
Returns true if the request‘s "X-Requested-With" header contains "XMLHttpRequest". (The Prototype Javascript library sends this header with every Ajax request.)
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 120
120: def xml_http_request?
121: !(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i)
122: end
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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 342
342: def content_type_with_parameters
343: content_type_from_legacy_post_data_format_header ||
344: env['CONTENT_TYPE'].to_s
345: end
The raw content type string with its parameters stripped off.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 348
348: def content_type_without_parameters
349: @content_type_without_parameters ||= self.class.extract_content_type_without_parameters(content_type_with_parameters)
350: end