CgiRequest and TestRequest provide concrete implementations.
- accepts
- body
- cache_format
- content_length
- content_type
- content_type_with_parameters
- content_type_without_parameters
- delete?
- domain
- etag_matches?
- format
- format=
- fresh?
- get?
- head?
- headers
- host
- host_with_port
- if_modified_since
- if_none_match
- method
- not_modified?
- parameters
- path
- path_parameters
- port
- port_string
- post?
- protocol
- put?
- query_parameters
- query_string
- raw_host_with_port
- raw_post
- referer
- referrer
- relative_url_root=
- remote_addr
- remote_ip
- request_method
- request_parameters
- request_uri
- server_software
- 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 | ||
| MULTIPART_BOUNDARY | = | %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n |
| EOL | = | "\015\012" |
| [R] | env | The hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }. |
[ show source ]
# 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.
[ show source ]
# 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 418
418: def body
419: if raw_post = env['RAW_POST_DATA']
420: raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
421: StringIO.new(raw_post)
422: else
423: body_stream
424: end
425: end
[ show source ]
# 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.
[ show source ]
# 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.
[ show source ]
# 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.
[ show source ]
# 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".
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 318
318: def domain(tld_length = 1)
319: return nil unless named_host?(host)
320:
321: host.split('.').last(1 + tld_length).join('.')
322: end
[ show source ]
# 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>
[ show source ]
# 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
[ show source ]
# 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.
[ show source ]
# 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 44
44: def get?
45: method == :get
46: end
Is this a HEAD request? Since request.method sees HEAD as :get, this \method checks the actual HTTP \method directly.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 65
65: def head?
66: request_method == :head
67: end
Provides access to the request‘s HTTP headers, for example:
request.headers["Content-Type"] # => "text/plain"
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 72
72: def headers
73: ActionController::Http::Headers.new(@env)
74: end
Returns the host for this request, such as example.com.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 280
280: def host
281: raw_host_with_port.sub(/:\d+$/, '')
282: end
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 287
287: def host_with_port
288: "#{host}#{port_string}"
289: end
[ show source ]
# 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
[ show source ]
# 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 39
39: def method
40: request_method == :head ? :get : request_method
41: end
[ show source ]
# 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 392
392: def parameters
393: @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access
394: end
Returns the interpreted \path to requested resource after all the installation directory of this application was taken into account.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 372
372: def path
373: path = (uri = request_uri) ? uri.split('?').first.to_s : ''
374:
375: # Cut off the path to the installation directory if given
376: path.sub!(%r/^#{ActionController::Base.relative_url_root}/, '')
377: path || ''
378: 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 vendor/rails/actionpack/lib/action_controller/request.rb, line 412
412: def path_parameters
413: @path_parameters ||= {}
414: end
Returns the port number of this request as an integer.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 293
293: def port
294: if raw_host_with_port =~ /:(\d+)$/
295: $1.to_i
296: else
297: standard_port
298: end
299: 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 vendor/rails/actionpack/lib/action_controller/request.rb, line 312
312: def port_string
313: port == standard_port ? '' : ":#{port}"
314: end
Is this a POST request? Equivalent to request.method == :post.
[ show source ]
# 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.
[ show source ]
# 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 54
54: def put?
55: request_method == :put
56: end
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 437
437: def query_parameters
438: @query_parameters ||= self.class.parse_query_parameters(query_string)
439: end
Returns the query string, accounting for server idiosyncrasies.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 335
335: def query_string
336: if uri = @env['REQUEST_URI']
337: uri.split('?', 2)[1] || ''
338: else
339: @env['QUERY_STRING'] || ''
340: end
341: end
Returns the \host for this request, such as "example.com".
[ show source ]
# 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 383
383: def raw_post
384: unless env.include? 'RAW_POST_DATA'
385: env['RAW_POST_DATA'] = body.read(content_length)
386: body.rewind if body.respond_to?(:rewind)
387: end
388: env['RAW_POST_DATA']
389: end
Alias for referrer
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 431
431: def referrer
432: @env['HTTP_REFERER']
433: end
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 427
427: def remote_addr
428: @env['REMOTE_ADDR']
429: 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 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.
[ show source ]
# 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
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 441
441: def request_parameters
442: @request_parameters ||= parse_formatted_request_parameters
443: end
Returns the request URI, accounting for server idiosyncrasies. WEBrick includes the full URL. IIS leaves REQUEST_URI blank.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 346
346: def request_uri
347: if uri = @env['REQUEST_URI']
348: # Remove domain, which webrick puts into the request_uri.
349: (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
350: else
351: # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
352: uri = @env['PATH_INFO'].to_s
353:
354: if script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
355: uri = uri.sub(/#{script_filename}\//, '')
356: end
357:
358: env_qs = @env['QUERY_STRING'].to_s
359: uri += "?#{env_qs}" unless env_qs.empty?
360:
361: if uri.blank?
362: @env.delete('REQUEST_URI')
363: else
364: @env['REQUEST_URI'] = uri
365: end
366: end
367: end
Returns the lowercase name of the HTTP server software.
[ show source ]
# 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?
[ show source ]
# 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
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 303
303: def standard_port
304: case protocol
305: when 'https://' then 443
306: else 80
307: end
308: 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 vendor/rails/actionpack/lib/action_controller/request.rb, line 328
328: def subdomains(tld_length = 1)
329: return [] unless named_host?(host)
330: parts = host.split('.')
331: parts[0..-(tld_length+2)]
332: end
The same as path_parameters with explicitly symbolized keys.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 402
402: def symbolized_path_parameters
403: @symbolized_path_parameters ||= path_parameters.symbolize_keys
404: 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 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.
[ show source ]
# 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.)
[ show source ]
# 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 470
470: def content_type_with_parameters
471: content_type_from_legacy_post_data_format_header ||
472: env['CONTENT_TYPE'].to_s
473: end
The raw content type string with its parameters stripped off.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/request.rb, line 476
476: def content_type_without_parameters
477: self.class.extract_content_type_without_parameters(content_type_with_parameters)
478: end