Class | ActionController::AbstractRequest |
In: |
vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.rb
vendor/rails/actionpack/lib/action_controller/request.rb |
Parent: | Object |
Subclassing AbstractRequest makes these methods available to the request objects used in production and testing, CgiRequest and TestRequest
env | [R] | Returns 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 72 72: def accepts 73: @accepts ||= 74: if @env['HTTP_ACCEPT'].to_s.strip.empty? 75: [ content_type, Mime::ALL ] 76: else 77: Mime::Type.parse(@env['HTTP_ACCEPT']) 78: end 79: 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 53 53: def content_type 54: @content_type ||= 55: begin 56: content_type = @env['CONTENT_TYPE'].to_s.downcase 57: 58: if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] 59: case x_post_format.to_s.downcase 60: when 'yaml' 61: content_type = 'application/x-yaml' 62: when 'xml' 63: content_type = 'application/xml' 64: end 65: end 66: 67: Mime::Type.lookup(content_type) 68: end 69: end
Is this a DELETE request? Equivalent to request.method == :delete
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 39 39: def delete? 40: method == :delete 41: 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 111 111: def domain(tld_length = 1) 112: return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil? 113: 114: host.split('.').last(1 + tld_length).join('.') 115: end
Is this a POST request formatted as XML or YAML?
# 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
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 24 24: def get? 25: method == :get 26: end
Is this a HEAD request? Equivalent to request.method == :head
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 44 44: def head? 45: method == :head 46: end
Returns the host for this request, such as example.com.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 243 243: def host 244: 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 205 205: def host_with_port 206: host + port_string 207: end
Returns the HTTP request method as a lowercase symbol (:get, for example)
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 17 17: def method 18: @request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ? 19: parameters[:_method].to_s.downcase.to_sym : 20: @env['REQUEST_METHOD'].downcase.to_sym 21: end
Returns both GET and POST parameters in a single hash.
# 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
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 160 160: def path 161: path = (uri = request_uri) ? uri.split('?').first : '' 162: 163: # Cut off the path to the installation directory if given 164: root = relative_url_root 165: path[0, root.length] = '' if root 166: path || '' 167: end
Returns a hash with the parameters used to form the path of the request
Example:
{:action => 'my_action', :controller => 'my_controller'}
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 224 224: def path_parameters 225: @path_parameters ||= {} 226: end
Returns the port number of this request as an integer.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 185 185: def port 186: @port_as_int ||= @env['SERVER_PORT'].to_i 187: 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 199 199: def port_string 200: (port == standard_port) ? '' : ":#{port}" 201: end
Is this a POST request? Equivalent to request.method == :post
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 29 29: def post? 30: method == :post 31: 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.
# 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.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 150 150: def protocol 151: ssl? ? 'https://' : 'http://' 152: end
Is this a PUT request? Equivalent to request.method == :put
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 34 34: def put? 35: method == :put 36: 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 129 129: def raw_post 130: @env['RAW_POST_DATA'] 131: 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 173 173: def relative_url_root 174: @@relative_url_root ||= case 175: when @env["RAILS_RELATIVE_URL_ROOT"] 176: @env["RAILS_RELATIVE_URL_ROOT"] 177: when server_software == 'apache' 178: @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') 179: else 180: '' 181: end 182: 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 95 95: def remote_ip 96: return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP' 97: 98: if @env.include? 'HTTP_X_FORWARDED_FOR' then 99: remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| 100: ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i 101: end 102: 103: return remote_ips.first.strip unless remote_ips.empty? 104: end 105: 106: @env['REMOTE_ADDR'] 107: end
Returns the request URI correctly, taking into account the idiosyncracies of the various servers.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 135 135: def request_uri 136: if uri = @env['REQUEST_URI'] 137: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri. 138: else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME 139: script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) 140: uri = @env['PATH_INFO'] 141: uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil? 142: unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty? 143: uri << '?' << env_qs 144: end 145: uri 146: end 147: end
Returns the lowercase name of the HTTP server software.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 229 229: def server_software 230: (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil 231: end
Is this an SSL request?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 155 155: def ssl? 156: @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' 157: end
Returns the standard port number for this request’s protocol
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 190 190: def standard_port 191: case protocol 192: when 'https://' then 443 193: else 80 194: end 195: 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 120 120: def subdomains(tld_length = 1) 121: return [] unless host 122: parts = host.split('.') 123: parts[0..-(tld_length+2)] 124: end
The same as path_parameters with explicitly symbolized keys
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 215 215: def symbolized_path_parameters 216: @symbolized_path_parameters ||= path_parameters.symbolize_keys 217: 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 84 84: def xml_http_request? 85: not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil? 86: end
Is this a POST request formatted as XML?
# File vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.rb, line 25 25: def xml_post? 26: post? && post_format == :xml 27: end