Class | ActionController::AbstractRequest |
In: |
vendor/rails/actionpack/lib/action_controller/request.rb
vendor/rails/actionpack/lib/action_controller/deprecated_request_methods.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 77 77: def accepts 78: @accepts ||= 79: if @env['HTTP_ACCEPT'].to_s.strip.empty? 80: [ content_type, Mime::ALL ] 81: else 82: Mime::Type.parse(@env['HTTP_ACCEPT']) 83: end 84: 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 58 58: def content_type 59: @content_type ||= 60: begin 61: content_type = @env['CONTENT_TYPE'].to_s.downcase 62: 63: if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] 64: case x_post_format.to_s.downcase 65: when 'yaml' 66: content_type = 'application/x-yaml' 67: when 'xml' 68: content_type = 'application/xml' 69: end 70: end 71: 72: Mime::Type.lookup(content_type) 73: end 74: end
Is this a DELETE request? Equivalent to request.method == :delete
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 43 43: def delete? 44: method == :delete 45: 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 116 116: def domain(tld_length = 1) 117: return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil? 118: 119: host.split('.').last(1 + tld_length).join('.') 120: 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 (or HEAD) request? Equivalent to request.method == :get
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 28 28: def get? 29: method == :get 30: end
Is this a HEAD request? HEAD is mapped as :get for request.method, so here we ask the REQUEST_METHOD header directly. Thus, for head, both get? and head? will return true.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 49 49: def head? 50: @env['REQUEST_METHOD'].downcase.to_sym == :head 51: end
Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get since the two are supposedly to be functionaly equivilent for all purposes except that HEAD won‘t return a response body (which Rails also takes care of elsewhere).
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 19 19: def method 20: @request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ? 21: parameters[:_method].to_s.downcase.to_sym : 22: @env['REQUEST_METHOD'].downcase.to_sym 23: 24: @request_method == :head ? :get : @request_method 25: 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 167 167: def path 168: path = (uri = request_uri) ? uri.split('?').first : '' 169: 170: # Cut off the path to the installation directory if given 171: path.sub!(%r/^#{relative_url_root}/, '') 172: path || '' 173: 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 230 230: def path_parameters 231: @path_parameters ||= {} 232: end
Is this a POST request? Equivalent to request.method == :post
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 33 33: def post? 34: method == :post 35: 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 157 157: def protocol 158: ssl? ? 'https://' : 'http://' 159: end
Is this a PUT request? Equivalent to request.method == :put
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 38 38: def put? 39: method == :put 40: 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 134 134: def raw_post 135: @env['RAW_POST_DATA'] 136: 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 179 179: def relative_url_root 180: @@relative_url_root ||= case 181: when @env["RAILS_RELATIVE_URL_ROOT"] 182: @env["RAILS_RELATIVE_URL_ROOT"] 183: when server_software == 'apache' 184: @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') 185: else 186: '' 187: end 188: 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 100 100: def remote_ip 101: return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP' 102: 103: if @env.include? 'HTTP_X_FORWARDED_FOR' then 104: remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| 105: ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i 106: end 107: 108: return remote_ips.first.strip unless remote_ips.empty? 109: end 110: 111: @env['REMOTE_ADDR'] 112: end
Return the request URI, accounting for server idiosyncracies. WEBrick includes the full URL. IIS leaves REQUEST_URI blank.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 140 140: def request_uri 141: if uri = @env['REQUEST_URI'] 142: # Remove domain, which webrick puts into the request_uri. 143: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri 144: else 145: # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO. 146: script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) 147: uri = @env['PATH_INFO'] 148: uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil? 149: unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty? 150: uri << '?' << env_qs 151: end 152: @env['REQUEST_URI'] = uri 153: end 154: end
Returns the lowercase name of the HTTP server software.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 235 235: def server_software 236: (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil 237: end
Is this an SSL request?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 162 162: def ssl? 163: @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' 164: 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 125 125: def subdomains(tld_length = 1) 126: return [] unless host 127: parts = host.split('.') 128: parts[0..-(tld_length+2)] 129: end
The same as path_parameters with explicitly symbolized keys
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 221 221: def symbolized_path_parameters 222: @symbolized_path_parameters ||= path_parameters.symbolize_keys 223: 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 89 89: def xml_http_request? 90: not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil? 91: 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