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



219
220
# File 'lib/action_controller/request.rb', line 219

def cookies
end

#delete?Boolean

Is this a DELETE request? Equivalent to request.method == :delete

Returns:

  • (Boolean)


32
33
34
# File 'lib/action_controller/request.rb', line 32

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



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

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

#envObject



213
214
# File 'lib/action_controller/request.rb', line 213

def env
end

#formatted_post?Boolean

Is this a POST request formatted as XML or YAML?

Returns:

  • (Boolean)


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

def formatted_post?
  [ :xml, :yaml ].include?(post_format) && post?
end

#get?Boolean

Is this a GET request? Equivalent to request.method == :get

Returns:

  • (Boolean)


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

def get?
  method == :get
end

#head?Boolean

Is this a HEAD request? Equivalent to request.method == :head

Returns:

  • (Boolean)


37
38
39
# File 'lib/action_controller/request.rb', line 37

def head?
  method == :head
end

#hostObject



216
217
# File 'lib/action_controller/request.rb', line 216

def host
end

#host_with_portObject

Returns a host:port string for this request, such as example.com or example.com:8080.



182
183
184
# File 'lib/action_controller/request.rb', line 182

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

#methodObject

Returns the HTTP request method as a lowercase symbol (:get, for example)



12
13
14
# File 'lib/action_controller/request.rb', line 12

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

#parametersObject

Returns both GET and POST parameters in a single hash.



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

def parameters
  @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



152
153
154
155
156
157
158
159
160
161
# File 'lib/action_controller/request.rb', line 152

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

  # Cut off the path to the installation directory if given
  if root = relative_url_root
    path[root.length..-1]
  else
    path
  end
end

#path_parametersObject



195
196
197
# File 'lib/action_controller/request.rb', line 195

def path_parameters
  @path_parameters ||= {}
end

#path_parameters=(parameters) ⇒ Object



186
187
188
189
# File 'lib/action_controller/request.rb', line 186

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

#portObject

Returns the port number of this request as an integer.



170
171
172
# File 'lib/action_controller/request.rb', line 170

def port
  env['SERVER_PORT'].to_i
end

#port_stringObject

Returns a port suffix like “:8080” if the port number of this request is not the default HTTP port 80 or HTTPS port 443.



176
177
178
# File 'lib/action_controller/request.rb', line 176

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

#post?Boolean

Is this a POST request? Equivalent to request.method == :post

Returns:

  • (Boolean)


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

def post?
  method == :post
end

#post_formatObject

Determine whether the body of a POST request is URL-encoded (default), XML, or YAML by checking the Content-Type HTTP header:

Content-Type        Post Format
application/xml     :xml
text/xml            :xml
application/x-yaml  :yaml
text/x-yaml         :yaml
*                   :url_encoded

For backward compatibility, the post format is extracted from the X-Post-Data-Format HTTP header if present.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/action_controller/request.rb', line 53

def post_format
  if env['HTTP_X_POST_DATA_FORMAT']
    env['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym
  else
    case env['CONTENT_TYPE'].to_s.downcase
      when 'application/xml', 'text/xml'        then :xml
      when 'application/x-yaml', 'text/x-yaml'  then :yaml
      else :url_encoded
    end
  end
end

#protocolObject

Return ‘https://’ if this is an SSL request and ‘http://’ otherwise.



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

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

#put?Boolean

Is this a PUT request? Equivalent to request.method == :put

Returns:

  • (Boolean)


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

def put?
  method == :put
end

#query_parametersObject

– Must be implemented in the concrete request ++



207
208
# File 'lib/action_controller/request.rb', line 207

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.



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

def raw_post
  env['RAW_POST_DATA']
end

#relative_url_rootObject

Returns the path minus the web server relative installation directory. This method returns nil unless the web server is apache.



165
166
167
# File 'lib/action_controller/request.rb', line 165

def relative_url_root
  @@relative_url_root ||= File.dirname(env["SCRIPT_NAME"].to_s).gsub(/(^\.$|^\/$)/, '') if server_software == 'apache'
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.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/action_controller/request.rb', line 94

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



210
211
# File 'lib/action_controller/request.rb', line 210

def request_parameters
end

#request_uriObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/action_controller/request.rb', line 129

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



225
226
# File 'lib/action_controller/request.rb', line 225

def reset_session
end

#server_softwareObject

Returns the lowercase name of the HTTP server software.



200
201
202
# File 'lib/action_controller/request.rb', line 200

def server_software
  (env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ env['SERVER_SOFTWARE']) ? $1.downcase : nil
end

#sessionObject



222
223
# File 'lib/action_controller/request.rb', line 222

def session
end

#ssl?Boolean

Is this an SSL request?

Returns:

  • (Boolean)


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

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



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

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

#symbolized_path_parametersObject



191
192
193
# File 'lib/action_controller/request.rb', line 191

def symbolized_path_parameters
  @symbolized_path_parameters ||= path_parameters.symbolize_keys
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)


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

def xml_http_request?
  not /XMLHttpRequest/i.match(env['HTTP_X_REQUESTED_WITH']).nil?
end

#xml_post?Boolean

Is this a POST request formatted as XML?

Returns:

  • (Boolean)


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

def xml_post?
  post_format == :xml && post?
end

#yaml_post?Boolean

Is this a POST request formatted as YAML?

Returns:

  • (Boolean)


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

def yaml_post?
  post_format == :yaml && post?
end