Class | ActionController::AbstractRequest |
In: |
vendor/rails/actionpack/lib/action_controller/request.rb
vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.rb |
Parent: | Object |
These methods are available in both the production and test Request objects.
env | [R] | Returns the hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }. |
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 68 68: def accepts 69: @accepts ||= 70: if @env['HTTP_ACCEPT'].to_s.strip.empty? 71: [ content_type, Mime::ALL ] 72: else 73: Mime::Type.parse(@env['HTTP_ACCEPT']) 74: end 75: 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 50 50: def content_type 51: @content_type ||= 52: begin 53: content_type = @env['CONTENT_TYPE'].to_s.downcase 54: 55: if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] 56: case x_post_format.to_s.downcase 57: when 'yaml' 58: content_type = 'application/x-yaml' 59: when 'xml' 60: content_type = 'application/xml' 61: end 62: end 63: 64: Mime::Type.lookup(content_type) 65: end 66: end
Is this a DELETE request? Equivalent to request.method == :delete
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 36 36: def delete? 37: method == :delete 38: 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 107 107: def domain(tld_length = 1) 108: return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil? 109: 110: host.split('.').last(1 + tld_length).join('.') 111: 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 21 21: def get? 22: method == :get 23: end
Is this a HEAD request? Equivalent to request.method == :head
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 41 41: def head? 42: method == :head 43: end
Returns the host for this request, such as example.com.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 233 233: def host 234: 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 201 201: def host_with_port 202: host + port_string 203: end
Returns the HTTP request method as a lowercase symbol (:get, for example)
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 16 16: def method 17: @request_method ||= @env['REQUEST_METHOD'].downcase.to_sym 18: end
Returns both GET and POST parameters in a single hash.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 11 11: def parameters 12: @parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access 13: 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 156 156: def path 157: path = (uri = request_uri) ? uri.split('?').first : '' 158: 159: # Cut off the path to the installation directory if given 160: root = relative_url_root 161: path[0, root.length] = '' if root 162: path || '' 163: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 214 214: def path_parameters 215: @path_parameters ||= {} 216: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 205 205: def path_parameters=(parameters) 206: @path_parameters = parameters 207: @symbolized_path_parameters = @parameters = nil 208: end
Returns the port number of this request as an integer.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 181 181: def port 182: @port_as_int ||= @env['SERVER_PORT'].to_i 183: 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 195 195: def port_string 196: (port == standard_port) ? '' : ":#{port}" 197: end
Is this a POST request? Equivalent to request.method == :post
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 26 26: def post? 27: method == :post 28: 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 146 146: def protocol 147: ssl? ? 'https://' : 'http://' 148: end
Is this a PUT request? Equivalent to request.method == :put
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 31 31: def put? 32: method == :put 33: 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 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 169 169: def relative_url_root 170: @@relative_url_root ||= case 171: when @env["RAILS_RELATIVE_URL_ROOT"] 172: @env["RAILS_RELATIVE_URL_ROOT"] 173: when server_software == 'apache' 174: @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') 175: else 176: '' 177: end 178: 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 91 91: def remote_ip 92: return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP' 93: 94: if @env.include? 'HTTP_X_FORWARDED_FOR' then 95: remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| 96: ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i 97: end 98: 99: return remote_ips.first.strip unless remote_ips.empty? 100: end 101: 102: @env['REMOTE_ADDR'] 103: 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 131 131: def request_uri 132: if uri = @env['REQUEST_URI'] 133: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri. 134: else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME 135: script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) 136: uri = @env['PATH_INFO'] 137: uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil? 138: unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty? 139: uri << '?' << env_qs 140: end 141: uri 142: end 143: end
Returns the lowercase name of the HTTP server software.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 219 219: def server_software 220: (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil 221: end
Is this an SSL request?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 151 151: def ssl? 152: @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' 153: end
Returns the standard port number for this request’s protocol
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 186 186: def standard_port 187: case protocol 188: when 'https://' then 443 189: else 80 190: end 191: 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 116 116: def subdomains(tld_length = 1) 117: return [] unless host 118: parts = host.split('.') 119: parts[0..-(tld_length+2)] 120: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 210 210: def symbolized_path_parameters 211: @symbolized_path_parameters ||= path_parameters.symbolize_keys 212: 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 80 80: def xml_http_request? 81: not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil? 82: 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