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



156
157
# File 'lib/action_controller/request.rb', line 156

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



150
151
# File 'lib/action_controller/request.rb', line 150

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



153
154
# File 'lib/action_controller/request.rb', line 153

def host
end

#host_with_portObject



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

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



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

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



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

def path_parameters
  @path_parameters ||= {}
end

#path_parameters=(parameters) ⇒ Object



124
125
126
127
# File 'lib/action_controller/request.rb', line 124

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

#portObject



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

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.



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

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



88
89
90
# File 'lib/action_controller/request.rb', line 88

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 ++



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

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



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

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



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

def request_parameters
end

#request_uriObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/action_controller/request.rb', line 76

def request_uri
 unless env['REQUEST_URI'].nil?
   (%r{^\w+\://[^/]+(/.*|$)$} =~ env['REQUEST_URI']) ? $1 : env['REQUEST_URI'] # Remove domain, which webrick puts into the request_uri.
 else  # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME
   script_filename = env["SCRIPT_NAME"].to_s.match(%r{[^/]+$})
   request_uri = env["PATH_INFO"]
   request_uri.sub!(/#{script_filename}\//, '') unless script_filename.nil?
   request_uri += '?' + env["QUERY_STRING"] unless env["QUERY_STRING"].nil? || env["QUERY_STRING"].empty?
   return request_uri
 end
end

#reset_sessionObject



162
163
# File 'lib/action_controller/request.rb', line 162

def reset_session
end

#sessionObject



159
160
# File 'lib/action_controller/request.rb', line 159

def session
end

#ssl?Boolean

Returns:

  • (Boolean)


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

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

#xml_http_request?Boolean Also known as: xhr?

Returns true if the request’s “X-Requested-With” header contains “XMLHttpRequest”. (The Prototype Javascript library sends this header with every Ajax request.)

Returns:

  • (Boolean)


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

def xml_http_request?
  env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/i
end