- GET
- POST
- accepts
- body
- cache_format
- content_length
- content_type
- delete?
- domain
- etag_matches?
- form_data?
- format
- format=
- fresh?
- get?
- head?
- headers
- host
- host_with_port
- if_modified_since
- if_none_match
- key?
- media_type
- method
- not_modified?
- parameters
- params
- path
- path_parameters
- port
- port_string
- post?
- protocol
- put?
- query_parameters
- query_string
- raw_host_with_port
- raw_post
- remote_ip
- request_method
- request_parameters
- request_uri
- reset_session
- server_port
- server_software
- session
- session_options
- session_options=
- ssl?
- standard_port
- subdomains
- symbolized_path_parameters
- template_format
- url
- xhr?
- xml_http_request?
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 |
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 426 426: def GET 427: @env["action_controller.request.query_parameters"] ||= normalize_parameters(super) 428: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 432 432: def POST 433: @env["action_controller.request.request_parameters"] ||= normalize_parameters(super) 434: end
Returns the accepted MIME type for the request.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 103 103: def accepts 104: @accepts ||= begin 105: header = @env['HTTP_ACCEPT'].to_s.strip 106: 107: if header.empty? 108: [content_type, Mime::ALL].compact 109: else 110: Mime::Type.parse(header) 111: end 112: end 113: end
The request body is an IO input stream. If the RAW_POST_DATA environment variable is already set, wrap it in a StringIO.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 412 412: def body 413: if raw_post = @env['RAW_POST_DATA'] 414: raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding) 415: StringIO.new(raw_post) 416: else 417: @env['rack.input'] 418: end 419: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 199 199: def cache_format 200: parameters[:format] 201: end
Returns the content length of the request as an integer.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 80 80: def content_length 81: super.to_i 82: 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.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 88 88: def content_type 89: @content_type ||= begin 90: if @env['CONTENT_TYPE'] =~ /^([^,\;]*)/ 91: Mime::Type.lookup($1.strip.downcase) 92: else 93: nil 94: end 95: end 96: end
Is this a DELETE request? Equivalent to request.method == :delete.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 62 62: def delete? 63: request_method == :delete 64: 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".
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 320 320: def domain(tld_length = 1) 321: return nil unless named_host?(host) 322: 323: host.split('.').last(1 + tld_length).join('.') 324: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 129 129: def etag_matches?(etag) 130: if_none_match && if_none_match == etag 131: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 421 421: def form_data? 422: FORM_DATA_MEDIA_TYPES.include?(content_type.to_s) 423: 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>
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 154 154: def format 155: @format ||= 156: if parameters[:format] 157: Mime::Type.lookup_by_extension(parameters[:format]) 158: elsif ActionController::Base.use_accept_header 159: accepts.first 160: elsif xhr? 161: Mime::Type.lookup_by_extension("js") 162: else 163: Mime::Type.lookup_by_extension("html") 164: end 165: 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
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 179 179: def format=(extension) 180: parameters[:format] = extension.to_s 181: @format = Mime::Type.lookup_by_extension(parameters[:format]) 182: 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.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 136 136: def fresh?(response) 137: case 138: when if_modified_since && if_none_match 139: not_modified?(response.last_modified) && etag_matches?(response.etag) 140: when if_modified_since 141: not_modified?(response.last_modified) 142: when if_none_match 143: etag_matches?(response.etag) 144: else 145: false 146: end 147: end
Is this a GET (or HEAD) request? Equivalent to request.method == :get.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 47 47: def get? 48: method == :get 49: end
Is this a HEAD request? Since request.method sees HEAD as :get, this \method checks the actual HTTP \method directly.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 68 68: def head? 69: request_method == :head 70: end
Provides access to the request‘s HTTP headers, for example:
request.headers["Content-Type"] # => "text/plain"
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 75 75: def headers 76: @headers ||= ActionController::Http::Headers.new(@env) 77: end
Returns the host for this request, such as example.com.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 285 285: def host 286: raw_host_with_port.sub(/:\d+$/, '') 287: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 291 291: def host_with_port 292: "#{host}#{port_string}" 293: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 115 115: def if_modified_since 116: if since = env['HTTP_IF_MODIFIED_SINCE'] 117: Time.rfc2822(since) rescue nil 118: end 119: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 121 121: def if_none_match 122: env['HTTP_IF_NONE_MATCH'] 123: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 24 24: def key?(key) 25: @env.key?(key) 26: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 98 98: def media_type 99: content_type.to_s 100: end
Returns the HTTP request \method used for action processing as a lowercase symbol, such as :post. (Unlike request_method, this method returns :get for a HEAD request because the two are functionally equivalent from the application‘s perspective.)
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 42 42: def method 43: request_method == :head ? :get : request_method 44: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 125 125: def not_modified?(modified_at) 126: if_modified_since && modified_at && if_modified_since >= modified_at 127: end
Returns both GET and POST \parameters in a single hash.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 385 385: def parameters 386: @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access 387: end
Alias for parameters
Returns the interpreted \path to requested resource after all the installation directory of this application was taken into account.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 368 368: def path 369: path = request_uri.to_s[/\A[^\?]*/] 370: path.sub!(/\A#{ActionController::Base.relative_url_root}/, '') 371: path 372: 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.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 406 406: def path_parameters 407: @env["action_controller.request.path_parameters"] ||= {} 408: end
Returns the port number of this request as an integer.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 296 296: def port 297: if raw_host_with_port =~ /:(\d+)$/ 298: $1.to_i 299: else 300: standard_port 301: end 302: 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.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 314 314: def port_string 315: port == standard_port ? '' : ":#{port}" 316: end
Is this a POST request? Equivalent to request.method == :post.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 52 52: def post? 53: request_method == :post 54: end
Returns ‘https://’ if this is an SSL request and ‘http://’ otherwise.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 266 266: def protocol 267: ssl? ? 'https://' : 'http://' 268: end
Is this a PUT request? Equivalent to request.method == :put.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 57 57: def put? 58: request_method == :put 59: end
Alias for GET
Returns the query string, accounting for server idiosyncrasies.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 337 337: def query_string 338: @env['QUERY_STRING'].present? ? @env['QUERY_STRING'] : (@env['REQUEST_URI'].split('?', 2)[1] || '') 339: end
Returns the \host for this request, such as "example.com".
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 276 276: def raw_host_with_port 277: if forwarded = env["HTTP_X_FORWARDED_HOST"] 278: forwarded.split(/,\s?/).last 279: else 280: env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}" 281: end 282: end
Read the request \body. This is useful for web services that need to work with raw requests directly.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 376 376: def raw_post 377: unless @env.include? 'RAW_POST_DATA' 378: @env['RAW_POST_DATA'] = body.read(@env['CONTENT_LENGTH'].to_i) 379: body.rewind if body.respond_to?(:rewind) 380: end 381: @env['RAW_POST_DATA'] 382: 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.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 221 221: def remote_ip 222: remote_addr_list = @env['REMOTE_ADDR'] && @env['REMOTE_ADDR'].scan(/[^,\s]+/) 223: 224: unless remote_addr_list.blank? 225: not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES} 226: return not_trusted_addrs.first unless not_trusted_addrs.empty? 227: end 228: remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',') 229: 230: if @env.include? 'HTTP_CLIENT_IP' 231: if ActionController::Base.ip_spoofing_check && remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP']) 232: # We don't know which came from the proxy, and which from the user 233: 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") 234: end 235: 236: return @env['HTTP_CLIENT_IP'] 237: end 238: 239: if remote_ips 240: while remote_ips.size > 1 && TRUSTED_PROXIES =~ remote_ips.last.strip 241: remote_ips.pop 242: end 243: 244: return remote_ips.last.strip 245: end 246: 247: @env['REMOTE_ADDR'] 248: end
Returns the true HTTP request \method as a lowercase symbol, such as :get. If the request \method is not listed in the HTTP_METHODS constant above, an UnknownHttpMethod exception is raised.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 34 34: def request_method 35: @request_method ||= HTTP_METHOD_LOOKUP[super] || raise(UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence(:locale => :en)}") 36: end
Alias for POST
Returns the request URI, accounting for server idiosyncrasies. WEBrick includes the full URL. IIS leaves REQUEST_URI blank.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 343 343: def request_uri 344: if uri = @env['REQUEST_URI'] 345: # Remove domain, which webrick puts into the request_uri. 346: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri 347: else 348: # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO. 349: uri = @env['PATH_INFO'].to_s 350: 351: if script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) 352: uri = uri.sub(/#{script_filename}\//, '') 353: end 354: 355: env_qs = @env['QUERY_STRING'].to_s 356: uri += "?#{env_qs}" unless env_qs.empty? 357: 358: if uri.blank? 359: @env.delete('REQUEST_URI') 360: else 361: @env['REQUEST_URI'] = uri 362: end 363: end 364: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 449 449: def reset_session 450: @env['rack.session.options'].delete(:id) 451: @env['rack.session'] = {} 452: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 462 462: def server_port 463: @env['SERVER_PORT'].to_i 464: end
Returns the lowercase name of the HTTP server software.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 256 256: def server_software 257: (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil 258: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 441 441: def session 442: @env['rack.session'] ||= {} 443: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 454 454: def session_options 455: @env['rack.session.options'] ||= {} 456: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 458 458: def session_options=(options) 459: @env['rack.session.options'] = options 460: end
Is this an SSL request?
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 271 271: def ssl? 272: @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' 273: end
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 305 305: def standard_port 306: case protocol 307: when 'https://' then 443 308: else 80 309: end 310: 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".
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 330 330: def subdomains(tld_length = 1) 331: return [] unless named_host?(host) 332: parts = host.split('.') 333: parts[0..-(tld_length+2)] 334: end
The same as path_parameters with explicitly symbolized keys.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 396 396: def symbolized_path_parameters 397: @symbolized_path_parameters ||= path_parameters.symbolize_keys 398: 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.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 187 187: def template_format 188: parameter_format = parameters[:format] 189: 190: if parameter_format 191: parameter_format 192: elsif xhr? 193: :js 194: else 195: :html 196: end 197: end
Returns the complete URL used for this request.
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 261 261: def url 262: protocol + host_with_port + request_uri 263: end
Returns true if the request‘s "X-Requested-With" header contains "XMLHttpRequest". (The Prototype Javascript library sends this header with every Ajax request.)
[ show source ]
# File actionpack/lib/action_controller/request.rb, line 206 206: def xml_http_request? 207: !(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i) 208: end