| Class | ActionController::AbstractRequest |
| In: |
vendor/rails/actionpack/lib/action_controller/request.rb
|
| Parent: | Object |
These methods are available in both the production and test Request objects.
Is this a DELETE request? Equivalent to request.method == :delete
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 32
32: def delete?
33: method == :delete
34: 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 110
110: def domain(tld_length = 1)
111: host.split('.').last(1 + tld_length).join('.')
112: end
Is this a POST request formatted as XML or YAML?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 66
66: def formatted_post?
67: [ :xml, :yaml ].include?(post_format) && post?
68: end
Is this a GET request? Equivalent to request.method == :get
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 17
17: def get?
18: method == :get
19: end
Is this a HEAD request? Equivalent to request.method == :head
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 37
37: def head?
38: method == :head
39: end
Returns a host:port string for this request, such as example.com or example.com:8080.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 182
182: def host_with_port
183: env['HTTP_HOST'] || host + port_string
184: end
Returns the HTTP request method as a lowercase symbol (:get, for example)
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 12
12: def method
13: env['REQUEST_METHOD'].downcase.to_sym
14: end
Returns both GET and POST parameters in a single hash.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 7 7: def parameters 8: @parameters ||= request_parameters.merge(query_parameters).merge(path_parameters).with_indifferent_access 9: 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 152
152: def path
153: path = (uri = request_uri) ? uri.split('?').first : ''
154:
155: # Cut off the path to the installation directory if given
156: if root = relative_url_root
157: path[root.length..-1]
158: else
159: path
160: end
161: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 195
195: def path_parameters
196: @path_parameters ||= {}
197: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 186
186: def path_parameters=(parameters)
187: @path_parameters = parameters
188: @symbolized_path_parameters = @parameters = nil
189: end
Returns the port number of this request as an integer.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 170
170: def port
171: env['SERVER_PORT'].to_i
172: 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 176
176: def port_string
177: (protocol == 'http://' && port == 80) || (protocol == 'https://' && port == 443) ? '' : ":#{port}"
178: end
Is this a POST request? Equivalent to request.method == :post
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 22
22: def post?
23: method == :post
24: end
Determine whether the body of a POST request is URL-encoded (default), XML, or YAML by checking the Content-Type HTTP header:
Content-Type Post Format application/xml :xml text/xml :xml application/x-yaml :yaml text/x-yaml :yaml * :url_encoded
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 53
53: def post_format
54: if env['HTTP_X_POST_DATA_FORMAT']
55: env['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym
56: else
57: case env['CONTENT_TYPE'].to_s.downcase
58: when 'application/xml', 'text/xml' then :xml
59: when 'application/x-yaml', 'text/x-yaml' then :yaml
60: else :url_encoded
61: end
62: end
63: end
Return ‘https://’ if this is an SSL request and ‘http://’ otherwise.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 142
142: def protocol
143: env["HTTPS"] == "on" ? 'https://' : 'http://'
144: end
Is this a PUT request? Equivalent to request.method == :put
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 27
27: def put?
28: method == :put
29: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 207
207: def query_parameters
208: 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 125
125: def raw_post
126: env['RAW_POST_DATA']
127: end
Returns the path minus the web server relative installation directory. This method returns nil unless the web server is apache.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 165
165: def relative_url_root
166: @@relative_url_root ||= File.dirname(env["SCRIPT_NAME"].to_s).gsub(/(^\.$|^\/$)/, '') if server_software == 'apache'
167: 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 94
94: def remote_ip
95: return env['HTTP_CLIENT_IP'] if env.include? 'HTTP_CLIENT_IP'
96:
97: if env.include? 'HTTP_X_FORWARDED_FOR' then
98: remote_ips = env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip|
99: ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
100: end
101:
102: return remote_ips.first.strip unless remote_ips.empty?
103: end
104:
105: return env['REMOTE_ADDR']
106: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 210
210: def request_parameters
211: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 129
129: def request_uri
130: unless env['REQUEST_URI'].nil?
131: (%r{^\w+\://[^/]+(/.*|$)$} =~ env['REQUEST_URI']) ? $1 : env['REQUEST_URI'] # Remove domain, which webrick puts into the request_uri.
132: else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME
133: script_filename = env["SCRIPT_NAME"].to_s.match(%r{[^/]+$})
134: request_uri = env["PATH_INFO"]
135: request_uri.sub!(/#{script_filename}\//, '') unless script_filename.nil?
136: request_uri += '?' + env["QUERY_STRING"] unless env["QUERY_STRING"].nil? || env["QUERY_STRING"].empty?
137: return request_uri
138: end
139: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 225
225: def reset_session
226: end
Returns the lowercase name of the HTTP server software.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 200
200: def server_software
201: (env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ env['SERVER_SOFTWARE']) ? $1.downcase : nil
202: end
Is this an SSL request?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 147
147: def ssl?
148: protocol == 'https://'
149: 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 117
117: def subdomains(tld_length = 1)
118: parts = host.split('.')
119: parts - parts.last(1 + tld_length)
120: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 191
191: def symbolized_path_parameters
192: @symbolized_path_parameters ||= path_parameters.symbolize_keys
193: 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 83
83: def xml_http_request?
84: not /XMLHttpRequest/i.match(env['HTTP_X_REQUESTED_WITH']).nil?
85: end
Is this a POST request formatted as XML?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 71
71: def xml_post?
72: post_format == :xml && post?
73: end