Class: ActionController::AbstractRequest
- Defined in:
- lib/action_controller/request.rb
Overview
These methods are available in both the production and test Request objects.
Direct Known Subclasses
Instance Method Summary collapse
- #cookies ⇒ Object
- #delete? ⇒ Boolean
-
#domain(tld_length = 1) ⇒ Object
Returns the domain part of a host, such as rubyonrails.org in “www.rubyonrails.org”.
- #env ⇒ Object
- #get? ⇒ Boolean
- #head? ⇒ Boolean
- #host ⇒ Object
- #host_with_port ⇒ Object
- #method ⇒ Object
-
#parameters ⇒ Object
Returns both GET and POST parameters in a single hash.
- #path ⇒ Object
- #port ⇒ Object
-
#port_string ⇒ Object
Returns a string like “:8080” if the port is not 80 or 443 while on https.
- #post? ⇒ Boolean
- #protocol ⇒ Object
- #put? ⇒ Boolean
-
#query_parameters ⇒ Object
– Must be implemented in the concrete request ++.
-
#raw_post ⇒ Object
Recieve the raw post data.
-
#remote_ip ⇒ Object
Determine originating IP address.
- #request_parameters ⇒ Object
- #request_uri ⇒ Object
- #reset_session ⇒ Object
- #session ⇒ Object
- #ssl? ⇒ Boolean
-
#subdomains(tld_length = 1) ⇒ Object
Returns all the subdomains as an array, so [“dev”, “www”] would be returned for “dev.www.rubyonrails.org”.
Instance Method Details
#cookies ⇒ Object
119 120 |
# File 'lib/action_controller/request.rb', line 119 def end |
#delete? ⇒ Boolean
25 26 27 |
# File 'lib/action_controller/request.rb', line 25 def delete? method == :delete end |
#domain(tld_length = 1) ⇒ Object
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”.
56 57 58 |
# File 'lib/action_controller/request.rb', line 56 def domain(tld_length = 1) host.split('.').last(1 + tld_length).join('.') end |
#env ⇒ Object
113 114 |
# File 'lib/action_controller/request.rb', line 113 def env end |
#get? ⇒ Boolean
13 14 15 |
# File 'lib/action_controller/request.rb', line 13 def get? method == :get end |
#head? ⇒ Boolean
29 30 31 |
# File 'lib/action_controller/request.rb', line 29 def head? method == :head end |
#host ⇒ Object
116 117 |
# File 'lib/action_controller/request.rb', line 116 def host end |
#host_with_port ⇒ Object
100 101 102 |
# File 'lib/action_controller/request.rb', line 100 def host_with_port env['HTTP_HOST'] || host + port_string end |
#method ⇒ Object
9 10 11 |
# File 'lib/action_controller/request.rb', line 9 def method env['REQUEST_METHOD'].downcase.intern end |
#parameters ⇒ Object
Returns both GET and POST parameters in a single hash.
5 6 7 |
# File 'lib/action_controller/request.rb', line 5 def parameters @parameters ||= request_parameters.update(query_parameters) end |
#path ⇒ Object
87 88 89 |
# File 'lib/action_controller/request.rb', line 87 def path request_uri ? request_uri.split('?').first : '' end |
#port ⇒ Object
91 92 93 |
# File 'lib/action_controller/request.rb', line 91 def port env['SERVER_PORT'].to_i end |
#port_string ⇒ Object
Returns a string like “:8080” if the port is not 80 or 443 while on https.
96 97 98 |
# File 'lib/action_controller/request.rb', line 96 def port_string (protocol == 'http://' && port == 80) || (protocol == 'https://' && port == 443) ? '' : ":#{port}" end |
#post? ⇒ Boolean
17 18 19 |
# File 'lib/action_controller/request.rb', line 17 def post? method == :post end |
#protocol ⇒ Object
79 80 81 |
# File 'lib/action_controller/request.rb', line 79 def protocol port == 443 ? 'https://' : 'http://' end |
#put? ⇒ Boolean
21 22 23 |
# File 'lib/action_controller/request.rb', line 21 def put? method == :put end |
#query_parameters ⇒ Object
– Must be implemented in the concrete request ++
107 108 |
# File 'lib/action_controller/request.rb', line 107 def query_parameters end |
#raw_post ⇒ Object
Recieve 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.
71 72 73 |
# File 'lib/action_controller/request.rb', line 71 def raw_post env['RAW_POST_DATA'] end |
#remote_ip ⇒ Object
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.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/action_controller/request.rb', line 40 def remote_ip return env['HTTP_CLIENT_IP'] if env.include? 'HTTP_CLIENT_IP' if env.include? 'HTTP_X_FORWARDED_FOR' then remote_ips = env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| ip =~ /^unknown$|^(10|172\.16|192\.168)\./i end return remote_ips.first.strip unless remote_ips.empty? end return env['REMOTE_ADDR'] end |
#request_parameters ⇒ Object
110 111 |
# File 'lib/action_controller/request.rb', line 110 def request_parameters end |
#request_uri ⇒ Object
75 76 77 |
# File 'lib/action_controller/request.rb', line 75 def request_uri env['REQUEST_URI'] end |
#reset_session ⇒ Object
125 126 |
# File 'lib/action_controller/request.rb', line 125 def reset_session end |
#session ⇒ Object
122 123 |
# File 'lib/action_controller/request.rb', line 122 def session end |
#ssl? ⇒ Boolean
83 84 85 |
# File 'lib/action_controller/request.rb', line 83 def ssl? protocol == 'https://' end |
#subdomains(tld_length = 1) ⇒ Object
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”.
63 64 65 66 |
# File 'lib/action_controller/request.rb', line 63 def subdomains(tld_length = 1) parts = host.split('.') parts - parts.last(1 + tld_length) end |