Class: ActionController::AbstractRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/request.rb

Overview

These methods are available in both the production and test Request objects.

Direct Known Subclasses

CgiRequest, TestRequest

Instance Method Summary collapse

Instance Method Details

#cookiesObject



140
141
# File 'lib/action_controller/request.rb', line 140

def cookies
end

#delete?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/action_controller/request.rb', line 26

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”.



57
58
59
# File 'lib/action_controller/request.rb', line 57

def domain(tld_length = 1)
  host.split('.').last(1 + tld_length).join('.')
end

#envObject



134
135
# File 'lib/action_controller/request.rb', line 134

def env
end

#get?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/action_controller/request.rb', line 14

def get?
  method == :get
end

#head?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/action_controller/request.rb', line 30

def head?
  method == :head
end

#hostObject



137
138
# File 'lib/action_controller/request.rb', line 137

def host
end

#host_with_portObject



112
113
114
# File 'lib/action_controller/request.rb', line 112

def host_with_port
  env['HTTP_HOST'] || host + port_string
end

#methodObject



10
11
12
# File 'lib/action_controller/request.rb', line 10

def method
  env['REQUEST_METHOD'].downcase.intern
end

#parametersObject

Returns both GET and POST parameters in a single hash.



5
6
7
8
# File 'lib/action_controller/request.rb', line 5

def parameters
  # puts "#{request_parameters.inspect} | #{query_parameters.inspect} | #{path_parameters.inspect}"
  @parameters ||= request_parameters.merge(query_parameters).merge(path_parameters).with_indifferent_access
end

#pathObject

returns the interpreted path to requested resource after all the installation directory of this application was taken into account



90
91
92
93
94
95
# File 'lib/action_controller/request.rb', line 90

def path
  path = request_uri ? request_uri.split('?').first : ''

  # cut off the part of the url which leads to the installation directory of this app
  path[relative_url_root.length..-1]
end

#path_parametersObject



121
122
123
# File 'lib/action_controller/request.rb', line 121

def path_parameters
  @path_parameters ||= {}
end

#path_parameters=(parameters) ⇒ Object



116
117
118
119
# File 'lib/action_controller/request.rb', line 116

def path_parameters=(parameters)
  @path_parameters = parameters
  @parameters = nil
end

#portObject



103
104
105
# File 'lib/action_controller/request.rb', line 103

def port
  env['SERVER_PORT'].to_i
end

#port_stringObject

Returns a string like “:8080” if the port is not 80 or 443 while on https.



108
109
110
# File 'lib/action_controller/request.rb', line 108

def port_string
  (protocol == 'http://' && port == 80) || (protocol == 'https://' && port == 443) ? '' : ":#{port}"
end

#post?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/action_controller/request.rb', line 18

def post?
  method == :post
end

#protocolObject



80
81
82
# File 'lib/action_controller/request.rb', line 80

def protocol
  env["HTTPS"] == "on" ? 'https://' : 'http://'
end

#put?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/action_controller/request.rb', line 22

def put?
  method == :put
end

#query_parametersObject

– Must be implemented in the concrete request ++



128
129
# File 'lib/action_controller/request.rb', line 128

def query_parameters
end

#raw_postObject

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.



72
73
74
# File 'lib/action_controller/request.rb', line 72

def raw_post
  env['RAW_POST_DATA']
end

#relative_url_rootObject

returns the path minus the web server relative installation directory



99
100
101
# File 'lib/action_controller/request.rb', line 99

def relative_url_root
  File.dirname(env["SCRIPT_NAME"].to_s).gsub /(^\.$|^\/$)/, ''
end

#remote_ipObject

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_controller/request.rb', line 41

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\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
    end

    return remote_ips.first.strip unless remote_ips.empty?
  end

  return env['REMOTE_ADDR']
end

#request_parametersObject



131
132
# File 'lib/action_controller/request.rb', line 131

def request_parameters
end

#request_uriObject



76
77
78
# File 'lib/action_controller/request.rb', line 76

def request_uri
  (%r{^\w+\://[^/]+(/.*|$)$} =~ env['REQUEST_URI']) ? $1 : env['REQUEST_URI'] # Remove domain, which webrick puts into the request_uri.
end

#reset_sessionObject



146
147
# File 'lib/action_controller/request.rb', line 146

def reset_session
end

#sessionObject



143
144
# File 'lib/action_controller/request.rb', line 143

def session
end

#ssl?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/action_controller/request.rb', line 84

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”.



64
65
66
67
# File 'lib/action_controller/request.rb', line 64

def subdomains(tld_length = 1)
  parts = host.split('.')
  parts - parts.last(1 + tld_length)
end