Class: ActionController::AbstractRequest
- Inherits:
-
Object
- Object
- ActionController::AbstractRequest
- Defined in:
- lib/action_controller/request.rb,
lib/action_controller/deprecated_request_methods.rb
Overview
These methods are available in both the production and test Request objects.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }.
Instance Method Summary collapse
- #accepts ⇒ Object
-
#content_type ⇒ Object
Determine whether the body of a HTTP call is URL-encoded (default) or matches one of the registered param_parsers.
-
#cookies ⇒ Object
:nodoc:.
-
#delete? ⇒ Boolean
Is this a DELETE request? Equivalent to request.method == :delete.
-
#domain(tld_length = 1) ⇒ Object
Returns the domain part of a host, such as rubyonrails.org in “www.rubyonrails.org”.
-
#formatted_post? ⇒ Boolean
Is this a POST request formatted as XML or YAML?.
-
#get? ⇒ Boolean
Is this a GET request? Equivalent to request.method == :get.
-
#head? ⇒ Boolean
Is this a HEAD request? Equivalent to request.method == :head.
-
#host ⇒ Object
Returns the host for this request, such as example.com.
-
#host_with_port ⇒ Object
Returns a host:port string for this request, such as example.com or example.com:8080.
-
#method ⇒ Object
Returns the HTTP request method as a lowercase symbol (:get, for example).
-
#parameters ⇒ Object
Returns both GET and POST parameters in a single hash.
-
#path ⇒ Object
Returns the interpreted path to requested resource after all the installation directory of this application was taken into account.
- #path_parameters ⇒ Object
- #path_parameters=(parameters) ⇒ Object
-
#port ⇒ Object
Returns the port number of this request as an integer.
-
#port_string ⇒ Object
Returns a port suffix like “:8080” if the port number of this request is not the default HTTP port 80 or HTTPS port 443.
-
#post? ⇒ Boolean
Is this a POST request? Equivalent to request.method == :post.
-
#post_format ⇒ Object
Determine whether the body of a HTTP call is URL-encoded (default) or matches one of the registered param_parsers.
-
#protocol ⇒ Object
Return ‘https://’ if this is an SSL request and ‘http://’ otherwise.
-
#put? ⇒ Boolean
Is this a PUT request? Equivalent to request.method == :put.
-
#query_parameters ⇒ Object
– Must be implemented in the concrete request ++.
-
#raw_post ⇒ Object
Receive the raw post data.
-
#relative_url_root ⇒ Object
Returns the path minus the web server relative installation directory.
-
#remote_ip ⇒ Object
Determine originating IP address.
-
#request_parameters ⇒ Object
:nodoc:.
-
#request_uri ⇒ Object
Returns the request URI correctly, taking into account the idiosyncracies of the various servers.
-
#reset_session ⇒ Object
:nodoc:.
-
#server_software ⇒ Object
Returns the lowercase name of the HTTP server software.
-
#session ⇒ Object
:nodoc:.
-
#session=(session) ⇒ Object
:nodoc:.
-
#ssl? ⇒ Boolean
Is this an SSL request?.
-
#standard_port ⇒ Object
Returns the standard port number for this request’s protocol.
-
#subdomains(tld_length = 1) ⇒ Object
Returns all the subdomains as an array, so [“dev”, “www”] would be returned for “dev.www.rubyonrails.org”.
- #symbolized_path_parameters ⇒ Object
-
#xml_http_request? ⇒ Boolean
(also: #xhr?)
Returns true if the request’s “X-Requested-With” header contains “XMLHttpRequest”.
-
#xml_post? ⇒ Boolean
Is this a POST request formatted as XML?.
-
#yaml_post? ⇒ Boolean
Is this a POST request formatted as YAML?.
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }.
8 9 10 |
# File 'lib/action_controller/request.rb', line 8 def env @env end |
Instance Method Details
#accepts ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/action_controller/request.rb', line 68 def accepts @accepts ||= if @env['HTTP_ACCEPT'].to_s.strip.empty? [ content_type, Mime::ALL ] else Mime::Type.parse(@env['HTTP_ACCEPT']) end end |
#content_type ⇒ Object
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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/action_controller/request.rb', line 50 def content_type @content_type ||= begin content_type = @env['CONTENT_TYPE'].to_s.downcase if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] case x_post_format.to_s.downcase when 'yaml' content_type = 'application/x-yaml' when 'xml' content_type = 'application/xml' end end Mime::Type.lookup(content_type) end end |
#cookies ⇒ Object
:nodoc:
236 237 |
# File 'lib/action_controller/request.rb', line 236 def #:nodoc: end |
#delete? ⇒ Boolean
Is this a DELETE request? Equivalent to request.method == :delete
36 37 38 |
# File 'lib/action_controller/request.rb', line 36 def delete? method == :delete end |
#domain(tld_length = 1) ⇒ Object
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”.
107 108 109 110 111 |
# File 'lib/action_controller/request.rb', line 107 def domain(tld_length = 1) return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil? host.split('.').last(1 + tld_length).join('.') end |
#formatted_post? ⇒ Boolean
Is this a POST request formatted as XML or YAML?
20 21 22 |
# File 'lib/action_controller/deprecated_request_methods.rb', line 20 def formatted_post? post? && (post_format == :yaml || post_format == :xml) end |
#get? ⇒ Boolean
Is this a GET request? Equivalent to request.method == :get
21 22 23 |
# File 'lib/action_controller/request.rb', line 21 def get? method == :get end |
#head? ⇒ Boolean
Is this a HEAD request? Equivalent to request.method == :head
41 42 43 |
# File 'lib/action_controller/request.rb', line 41 def head? method == :head end |
#host ⇒ Object
Returns the host for this request, such as example.com.
233 234 |
# File 'lib/action_controller/request.rb', line 233 def host end |
#host_with_port ⇒ Object
Returns a host:port string for this request, such as example.com or example.com:8080.
201 202 203 |
# File 'lib/action_controller/request.rb', line 201 def host_with_port host + port_string end |
#method ⇒ Object
Returns the HTTP request method as a lowercase symbol (:get, for example)
16 17 18 |
# File 'lib/action_controller/request.rb', line 16 def method @request_method ||= @env['REQUEST_METHOD'].downcase.to_sym end |
#parameters ⇒ Object
Returns both GET and POST parameters in a single hash.
11 12 13 |
# File 'lib/action_controller/request.rb', line 11 def parameters @parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access end |
#path ⇒ Object
Returns the interpreted path to requested resource after all the installation directory of this application was taken into account
156 157 158 159 160 161 162 163 |
# File 'lib/action_controller/request.rb', line 156 def path path = (uri = request_uri) ? uri.split('?').first : '' # Cut off the path to the installation directory if given root = relative_url_root path[0, root.length] = '' if root path || '' end |
#path_parameters ⇒ Object
214 215 216 |
# File 'lib/action_controller/request.rb', line 214 def path_parameters @path_parameters ||= {} end |
#path_parameters=(parameters) ⇒ Object
205 206 207 208 |
# File 'lib/action_controller/request.rb', line 205 def path_parameters=(parameters) @path_parameters = parameters @symbolized_path_parameters = @parameters = nil end |
#port ⇒ Object
Returns the port number of this request as an integer.
181 182 183 |
# File 'lib/action_controller/request.rb', line 181 def port @port_as_int ||= @env['SERVER_PORT'].to_i end |
#port_string ⇒ Object
Returns a port suffix like “:8080” if the port number of this request is not the default HTTP port 80 or HTTPS port 443.
195 196 197 |
# File 'lib/action_controller/request.rb', line 195 def port_string (port == standard_port) ? '' : ":#{port}" end |
#post? ⇒ Boolean
Is this a POST request? Equivalent to request.method == :post
26 27 28 |
# File 'lib/action_controller/request.rb', line 26 def post? method == :post end |
#post_format ⇒ Object
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.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/action_controller/deprecated_request_methods.rb', line 8 def post_format case content_type.to_s when 'application/xml' :xml when 'application/x-yaml' :yaml else :url_encoded end end |
#protocol ⇒ Object
Return ‘https://’ if this is an SSL request and ‘http://’ otherwise.
146 147 148 |
# File 'lib/action_controller/request.rb', line 146 def protocol ssl? ? 'https://' : 'http://' end |
#put? ⇒ Boolean
Is this a PUT request? Equivalent to request.method == :put
31 32 33 |
# File 'lib/action_controller/request.rb', line 31 def put? method == :put end |
#query_parameters ⇒ Object
– Must be implemented in the concrete request ++
226 227 |
# File 'lib/action_controller/request.rb', line 226 def query_parameters #:nodoc: end |
#raw_post ⇒ Object
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.
125 126 127 |
# File 'lib/action_controller/request.rb', line 125 def raw_post @env['RAW_POST_DATA'] end |
#relative_url_root ⇒ Object
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.
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/action_controller/request.rb', line 169 def relative_url_root @@relative_url_root ||= case when @env["RAILS_RELATIVE_URL_ROOT"] @env["RAILS_RELATIVE_URL_ROOT"] when server_software == 'apache' @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') else '' end end |
#remote_ip ⇒ Object
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.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/action_controller/request.rb', line 91 def remote_ip return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP' if @env.include? 'HTTP_X_FORWARDED_FOR' then remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i end return remote_ips.first.strip unless remote_ips.empty? end @env['REMOTE_ADDR'] end |
#request_parameters ⇒ Object
:nodoc:
229 230 |
# File 'lib/action_controller/request.rb', line 229 def request_parameters #:nodoc: end |
#request_uri ⇒ Object
Returns the request URI correctly, taking into account the idiosyncracies of the various servers.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/action_controller/request.rb', line 131 def request_uri if uri = @env['REQUEST_URI'] (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri. else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) uri = @env['PATH_INFO'] uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil? unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty? uri << '?' << env_qs end uri end end |
#reset_session ⇒ Object
:nodoc:
246 247 |
# File 'lib/action_controller/request.rb', line 246 def reset_session #:nodoc: end |
#server_software ⇒ Object
Returns the lowercase name of the HTTP server software.
219 220 221 |
# File 'lib/action_controller/request.rb', line 219 def server_software (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil end |
#session ⇒ Object
:nodoc:
239 240 |
# File 'lib/action_controller/request.rb', line 239 def session #:nodoc: end |
#session=(session) ⇒ Object
:nodoc:
242 243 244 |
# File 'lib/action_controller/request.rb', line 242 def session=(session) #:nodoc: @session = session end |
#ssl? ⇒ Boolean
Is this an SSL request?
151 152 153 |
# File 'lib/action_controller/request.rb', line 151 def ssl? @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' end |
#standard_port ⇒ Object
Returns the standard port number for this request’s protocol
186 187 188 189 190 191 |
# File 'lib/action_controller/request.rb', line 186 def standard_port case protocol when 'https://' then 443 else 80 end end |
#subdomains(tld_length = 1) ⇒ Object
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”.
116 117 118 119 120 |
# File 'lib/action_controller/request.rb', line 116 def subdomains(tld_length = 1) return [] unless host parts = host.split('.') parts[0..-(tld_length+2)] end |
#symbolized_path_parameters ⇒ Object
210 211 212 |
# File 'lib/action_controller/request.rb', line 210 def symbolized_path_parameters @symbolized_path_parameters ||= path_parameters.symbolize_keys end |
#xml_http_request? ⇒ Boolean Also known as: xhr?
Returns true if the request’s “X-Requested-With” header contains “XMLHttpRequest”. (The Prototype Javascript library sends this header with every Ajax request.)
80 81 82 |
# File 'lib/action_controller/request.rb', line 80 def xml_http_request? not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil? end |
#xml_post? ⇒ Boolean
Is this a POST request formatted as XML?
25 26 27 |
# File 'lib/action_controller/deprecated_request_methods.rb', line 25 def xml_post? post? && post_format == :xml end |
#yaml_post? ⇒ Boolean
Is this a POST request formatted as YAML?
30 31 32 |
# File 'lib/action_controller/deprecated_request_methods.rb', line 30 def yaml_post? post? && post_format == :yaml end |