Subclassing AbstractRequest makes these methods available to the request objects used in production and testing, CgiRequest and TestRequest
- accepts
- content_type
- delete?
- domain
- formatted_post?
- get?
- head?
- host
- host_with_port
- method
- parameters
- path
- path_parameters
- port
- port_string
- post?
- post_format
- protocol
- put?
- raw_post
- relative_url_root
- remote_ip
- request_uri
- server_software
- ssl?
- standard_port
- subdomains
- symbolized_path_parameters
- xhr?
- xml_http_request?
- xml_post?
- yaml_post?
| [R] | env | Returns the hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }. |
Returns the accepted MIME type for the request
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 70
70: def accepts
71: @accepts ||=
72: if @env['HTTP_ACCEPT'].to_s.strip.empty?
73: [ content_type, Mime::ALL ]
74: else
75: Mime::Type.parse(@env['HTTP_ACCEPT'])
76: end
77: end
Determine whether the body of a HTTP call is URL-encoded (default) or matches one of the registered param_parsers.
For backward compatibility, the post format is extracted from the X-Post-Data-Format HTTP header if present.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 51
51: def content_type
52: @content_type ||=
53: begin
54: content_type = @env['CONTENT_TYPE'].to_s.downcase
55:
56: if x_post_format = @env['HTTP_X_POST_DATA_FORMAT']
57: case x_post_format.to_s.downcase
58: when 'yaml'
59: content_type = 'application/x-yaml'
60: when 'xml'
61: content_type = 'application/xml'
62: end
63: end
64:
65: Mime::Type.lookup(content_type)
66: end
67: end
Is this a DELETE request? Equivalent to request.method == :delete
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 37
37: def delete?
38: method == :delete
39: 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".
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 109
109: def domain(tld_length = 1)
110: return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil?
111:
112: host.split('.').last(1 + tld_length).join('.')
113: end
Is this a POST request formatted as XML or YAML?
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.rb, line 20
20: def formatted_post?
21: post? && (post_format == :yaml || post_format == :xml)
22: end
Is this a GET request? Equivalent to request.method == :get
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 22
22: def get?
23: method == :get
24: end
Is this a HEAD request? Equivalent to request.method == :head
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 42
42: def head?
43: method == :head
44: end
Returns the host for this request, such as example.com.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 241
241: def host
242: end
Returns a host:port string for this request, such as example.com or example.com:8080.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 203
203: def host_with_port
204: host + port_string
205: end
Returns the HTTP request method as a lowercase symbol (:get, for example)
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 17
17: def method
18: @request_method ||= @env['REQUEST_METHOD'].downcase.to_sym
19: end
Returns both GET and POST parameters in a single hash.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 12
12: def parameters
13: @parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access
14: end
Returns the interpreted path to requested resource after all the installation directory of this application was taken into account
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 158
158: def path
159: path = (uri = request_uri) ? uri.split('?').first : ''
160:
161: # Cut off the path to the installation directory if given
162: root = relative_url_root
163: path[0, root.length] = '' if root
164: path || ''
165: end
Returns a hash with the parameters used to form the path of the request
Example:
{:action => 'my_action', :controller => 'my_controller'}
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 222
222: def path_parameters
223: @path_parameters ||= {}
224: end
Returns the port number of this request as an integer.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 183
183: def port
184: @port_as_int ||= @env['SERVER_PORT'].to_i
185: 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 197
197: def port_string
198: (port == standard_port) ? '' : ":#{port}"
199: end
Is this a POST request? Equivalent to request.method == :post
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 27
27: def post?
28: method == :post
29: end
Determine whether the body of a HTTP call is URL-encoded (default) or matches one of the registered param_parsers.
For backward compatibility, the post format is extracted from the X-Post-Data-Format HTTP header if present.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.rb, line 8
8: def post_format
9: case content_type.to_s
10: when 'application/xml'
11: :xml
12: when 'application/x-yaml'
13: :yaml
14: else
15: :url_encoded
16: end
17: end
Return ‘https://’ if this is an SSL request and ‘http://’ otherwise.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 148
148: def protocol
149: ssl? ? 'https://' : 'http://'
150: end
Is this a PUT request? Equivalent to request.method == :put
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 32
32: def put?
33: method == :put
34: end
Receive the raw post data. This is useful for services such as REST, XMLRPC and SOAP which communicate over HTTP POST but don’t use the traditional parameter format.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 127
127: def raw_post
128: @env['RAW_POST_DATA']
129: 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 171
171: def relative_url_root
172: @@relative_url_root ||= case
173: when @env["RAILS_RELATIVE_URL_ROOT"]
174: @env["RAILS_RELATIVE_URL_ROOT"]
175: when server_software == 'apache'
176: @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '')
177: else
178: ''
179: end
180: 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 93
93: def remote_ip
94: return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP'
95:
96: if @env.include? 'HTTP_X_FORWARDED_FOR' then
97: remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip|
98: ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
99: end
100:
101: return remote_ips.first.strip unless remote_ips.empty?
102: end
103:
104: @env['REMOTE_ADDR']
105: end
Returns the request URI correctly, taking into account the idiosyncracies of the various servers.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 133
133: def request_uri
134: if uri = @env['REQUEST_URI']
135: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri.
136: else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME
137: script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
138: uri = @env['PATH_INFO']
139: uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
140: unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
141: uri << '?' << env_qs
142: end
143: uri
144: end
145: end
Returns the lowercase name of the HTTP server software.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 227
227: def server_software
228: (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil
229: end
Is this an SSL request?
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 153
153: def ssl?
154: @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https'
155: end
Returns the standard port number for this request’s protocol
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 188
188: def standard_port
189: case protocol
190: when 'https://' then 443
191: else 80
192: end
193: 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".
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 118
118: def subdomains(tld_length = 1)
119: return [] unless host
120: parts = host.split('.')
121: parts[0..-(tld_length+2)]
122: end
The same as path_parameters with explicitly symbolized keys
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 213
213: def symbolized_path_parameters
214: @symbolized_path_parameters ||= path_parameters.symbolize_keys
215: end
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.)
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 82
82: def xml_http_request?
83: not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil?
84: end
Is this a POST request formatted as XML?
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.rb, line 25
25: def xml_post?
26: post? && post_format == :xml
27: end
Is this a POST request formatted as YAML?
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.rb, line 30
30: def yaml_post?
31: post? && post_format == :yaml
32: end