Class | ActionController::AbstractRequest |
In: |
vendor/rails/actionpack/lib/action_controller/request.rb
|
Parent: | Object |
CgiRequest and TestRequest provide concrete implementations.
HTTP_METHODS | = | %w(get head put post delete options) | ||
HTTP_METHOD_LOOKUP | = | HTTP_METHODS.inject({}) { |h, m| h[m] = h[m.upcase] = m.to_sym; | ||
TRUSTED_PROXIES | = | /^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i | Which IP addresses are "trusted proxies" that can be stripped from the right-hand-side of X-Forwarded-For | |
MULTIPART_BOUNDARY | = | %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n | ||
EOL | = | "\015\012" |
env | [R] | The hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }. |
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 12 12: def self.relative_url_root=(relative_url_root) 13: ActiveSupport::Deprecation.warn( 14: "ActionController::AbstractRequest.relative_url_root= has been renamed." + 15: "You can now set it with config.action_controller.relative_url_root=", caller) 16: ActionController::Base.relative_url_root=relative_url_root 17: end
Returns the accepted MIME type for the request.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 93 93: def accepts 94: header = @env['HTTP_ACCEPT'].to_s.strip 95: 96: if header.empty? 97: [content_type, Mime::ALL].compact 98: else 99: Mime::Type.parse(header) 100: end 101: end
The request body is an IO input stream. If the RAW_POST_DATA environment variable is already set, wrap it in a StringIO.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 427 427: def body 428: if raw_post = env['RAW_POST_DATA'] 429: raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding) 430: StringIO.new(raw_post) 431: else 432: body_stream 433: end 434: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 189 189: def cache_format 190: parameters[:format] 191: end
Returns the content length of the request as an integer.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 78 78: def content_length 79: @env['CONTENT_LENGTH'].to_i 80: end
The MIME type of the HTTP request, such as Mime::XML.
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 87 87: def content_type 88: Mime::Type.lookup(content_type_without_parameters) 89: end
Is this a DELETE request? Equivalent to request.method == :delete.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 59 59: def delete? 60: request_method == :delete 61: 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 327 327: def domain(tld_length = 1) 328: return nil unless named_host?(host) 329: 330: host.split('.').last(1 + tld_length).join('.') 331: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 119 119: def etag_matches?(etag) 120: if_none_match && if_none_match == etag 121: end
Returns the Mime type for the \format used in the request.
GET /posts/5.xml | request.format => Mime::XML GET /posts/5.xhtml | request.format => Mime::HTML GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of <tt>ActionController::Base.use_accept_header</tt>
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 144 144: def format 145: @format ||= 146: if parameters[:format] 147: Mime::Type.lookup_by_extension(parameters[:format]) 148: elsif ActionController::Base.use_accept_header 149: accepts.first 150: elsif xhr? 151: Mime::Type.lookup_by_extension("js") 152: else 153: Mime::Type.lookup_by_extension("html") 154: end 155: end
Sets the \format by string extension, which can be used to force custom formats that are not controlled by the extension.
class ApplicationController < ActionController::Base before_filter :adjust_format_for_iphone private def adjust_format_for_iphone request.format = :iphone if request.env["HTTP_USER_AGENT"][/iPhone/] end end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 169 169: def format=(extension) 170: parameters[:format] = extension.to_s 171: @format = Mime::Type.lookup_by_extension(parameters[:format]) 172: end
Check response freshness (Last-Modified and ETag) against request If-Modified-Since and If-None-Match conditions. If both headers are supplied, both must match, or the request is not considered fresh.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 126 126: def fresh?(response) 127: case 128: when if_modified_since && if_none_match 129: not_modified?(response.last_modified) && etag_matches?(response.etag) 130: when if_modified_since 131: not_modified?(response.last_modified) 132: when if_none_match 133: etag_matches?(response.etag) 134: else 135: false 136: end 137: end
Is this a GET (or HEAD) request? Equivalent to request.method == :get.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 44 44: def get? 45: method == :get 46: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 104 104: def if_modified_since 105: if since = env['HTTP_IF_MODIFIED_SINCE'] 106: Time.rfc2822(since) rescue nil 107: end 108: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 111 111: def if_none_match 112: env['HTTP_IF_NONE_MATCH'] 113: end
The HTTP request \method as a lowercase symbol, such as :get. Note, HEAD is returned as :get since the two are functionally equivalent from the application‘s perspective.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 39 39: def method 40: request_method == :head ? :get : request_method 41: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 115 115: def not_modified?(modified_at) 116: if_modified_since && modified_at && if_modified_since >= modified_at 117: end
Returns both GET and POST \parameters in a single hash.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 401 401: def parameters 402: @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access 403: 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 381 381: def path 382: path = (uri = request_uri) ? uri.split('?').first.to_s : '' 383: 384: # Cut off the path to the installation directory if given 385: path.sub!(%r/^#{ActionController::Base.relative_url_root}/, '') 386: path || '' 387: end
Returns a hash with the \parameters used to form the \path of the request. Returned hash keys are strings:
{'action' => 'my_action', 'controller' => 'my_controller'}
See symbolized_path_parameters for symbolized keys.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 421 421: def path_parameters 422: @path_parameters ||= {} 423: end
Is this a POST request? Equivalent to request.method == :post.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 49 49: def post? 50: request_method == :post 51: end
Returns ‘https://’ if this is an SSL request and ‘http://’ otherwise.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 260 260: def protocol 261: ssl? ? 'https://' : 'http://' 262: end
Is this a PUT request? Equivalent to request.method == :put.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 54 54: def put? 55: request_method == :put 56: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 446 446: def query_parameters 447: @query_parameters ||= self.class.parse_query_parameters(query_string) 448: end
Returns the query string, accounting for server idiosyncrasies.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 344 344: def query_string 345: if uri = @env['REQUEST_URI'] 346: uri.split('?', 2)[1] || '' 347: else 348: @env['QUERY_STRING'] || '' 349: end 350: end
Returns the \host for this request, such as "example.com".
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 271 271: def raw_host_with_port 272: if forwarded = env["HTTP_X_FORWARDED_HOST"] 273: forwarded.split(/,\s?/).last 274: else 275: env['HTTP_HOST'] || env['SERVER_NAME'] || "#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}" 276: end 277: end
Read the request \body. This is useful for web services that need to work with raw requests directly.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 392 392: def raw_post 393: unless env.include? 'RAW_POST_DATA' 394: env['RAW_POST_DATA'] = body.read(content_length) 395: body.rewind if body.respond_to?(:rewind) 396: end 397: env['RAW_POST_DATA'] 398: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 440 440: def referrer 441: @env['HTTP_REFERER'] 442: end
Returns the value of ActionController::Base.relative_url_root. This method is deprecated as the value is an application wide setting, not something which changes per request.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 313 313: def relative_url_root 314: ActiveSupport::Deprecation.warn( 315: "relative_url_root is now set application-wide, use ActionController::Base.relative_url_root instead.", caller) 316: ActionController::Base.relative_url_root 317: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 436 436: def remote_addr 437: @env['REMOTE_ADDR'] 438: end
Determines 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 if REMOTE_ADDR is a proxy. HTTP_X_FORWARDED_FOR may be a comma- delimited list in the case of multiple chained proxies; the last address which is not trusted is the originating IP.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 211 211: def remote_ip 212: remote_addr_list = @env['REMOTE_ADDR'] && @env['REMOTE_ADDR'].split(',').collect(&:strip) 213: 214: unless remote_addr_list.blank? 215: not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES} 216: return not_trusted_addrs.first unless not_trusted_addrs.empty? 217: end 218: remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',') 219: 220: if @env.include? 'HTTP_CLIENT_IP' 221: if remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP']) 222: # We don't know which came from the proxy, and which from the user 223: raise ActionControllerError.new("IP spoofing attack?!\nHTTP_CLIENT_IP=\#{@env['HTTP_CLIENT_IP'].inspect}\nHTTP_X_FORWARDED_FOR=\#{@env['HTTP_X_FORWARDED_FOR'].inspect}\n") 224: end 225: 226: return @env['HTTP_CLIENT_IP'] 227: end 228: 229: if remote_ips 230: while remote_ips.size > 1 && TRUSTED_PROXIES =~ remote_ips.last.strip 231: remote_ips.pop 232: end 233: 234: return remote_ips.last.strip 235: end 236: 237: @env['REMOTE_ADDR'] 238: end
The true HTTP request \method as a lowercase symbol, such as :get. UnknownHttpMethod is raised for invalid methods not listed in ACCEPTED_HTTP_METHODS.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 28 28: def request_method 29: method = @env['REQUEST_METHOD'] 30: method = parameters[:_method] if method == 'POST' && !parameters[:_method].blank? 31: 32: HTTP_METHOD_LOOKUP[method] || raise(UnknownHttpMethod, "#{method}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}") 33: end
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 450 450: def request_parameters 451: @request_parameters ||= parse_formatted_request_parameters 452: end
Returns the request URI, accounting for server idiosyncrasies. WEBrick includes the full URL. IIS leaves REQUEST_URI blank.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 355 355: def request_uri 356: if uri = @env['REQUEST_URI'] 357: # Remove domain, which webrick puts into the request_uri. 358: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri 359: else 360: # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO. 361: uri = @env['PATH_INFO'].to_s 362: 363: if script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) 364: uri = uri.sub(/#{script_filename}\//, '') 365: end 366: 367: env_qs = @env['QUERY_STRING'].to_s 368: uri += "?#{env_qs}" unless env_qs.empty? 369: 370: if uri.blank? 371: @env.delete('REQUEST_URI') 372: else 373: @env['REQUEST_URI'] = uri 374: end 375: end 376: end
Returns the lowercase name of the HTTP server software.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 247 247: def server_software 248: (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil 249: end
Is this an SSL request?
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 266 266: def ssl? 267: @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' 268: 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 337 337: def subdomains(tld_length = 1) 338: return [] unless named_host?(host) 339: parts = host.split('.') 340: parts[0..-(tld_length+2)] 341: end
The same as path_parameters with explicitly symbolized keys.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 411 411: def symbolized_path_parameters 412: @symbolized_path_parameters ||= path_parameters.symbolize_keys 413: end
Returns a symbolized version of the :format parameter of the request. If no \format is given it returns :jsfor Ajax requests and :html otherwise.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 177 177: def template_format 178: parameter_format = parameters[:format] 179: 180: if parameter_format 181: parameter_format 182: elsif xhr? 183: :js 184: else 185: :html 186: end 187: end
Returns the complete URL used for this request.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 254 254: def url 255: protocol + host_with_port + request_uri 256: 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 196 196: def xml_http_request? 197: !(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i) 198: end
The raw content type string. Use when you need parameters such as charset or boundary which aren‘t included in the content_type MIME type. Overridden by the X-POST_DATA_FORMAT header for backward compatibility.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 479 479: def content_type_with_parameters 480: content_type_from_legacy_post_data_format_header || 481: env['CONTENT_TYPE'].to_s 482: end
The raw content type string with its parameters stripped off.
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 485 485: def content_type_without_parameters 486: self.class.extract_content_type_without_parameters(content_type_with_parameters) 487: end