Class: ActionController::AbstractRequest

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

Overview

Subclassing AbstractRequest makes these methods available to the request objects used in production and testing, CgiRequest and TestRequest

Direct Known Subclasses

CgiRequest, TestRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject (readonly)

Returns the hash of environment variables for this request, such as { ‘RAILS_ENV’ => ‘production’ }.



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

def env
  @env
end

Instance Method Details

#acceptsObject

Returns the accepted MIME type for the request



70
71
72
73
74
75
76
77
# File 'lib/action_controller/request.rb', line 70

def accepts
  @accepts ||=
    if @env['HTTP_ACCEPT'].to_s.strip.empty?
      [ content_type, Mime::ALL ]
    else
      Mime::Type.parse(@env['HTTP_ACCEPT'])
    end
end

#content_typeObject

Determine whether the body of a HTTP call is URL-encoded (default) or matches one of the registered param_parsers.

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/action_controller/request.rb', line 51

def content_type
  @content_type ||=
    begin
      content_type = @env['CONTENT_TYPE'].to_s.downcase
      
      if x_post_format = @env['HTTP_X_POST_DATA_FORMAT']
        case x_post_format.to_s.downcase
        when 'yaml'
          content_type = 'application/x-yaml'
        when 'xml'
          content_type = 'application/xml'
        end
      end
      
      Mime::Type.lookup(content_type)
    end
end

#cookiesObject

:nodoc:



244
245
# File 'lib/action_controller/request.rb', line 244

def cookies #:nodoc:
end

#delete?Boolean

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

Returns:

  • (Boolean)


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

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



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

def domain(tld_length = 1)
  return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil?

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

#formatted_post?Boolean

Is this a POST request formatted as XML or YAML?

Returns:

  • (Boolean)


20
21
22
# File 'lib/action_controller/deprecated_request_methods.rb', line 20

def formatted_post?
  post? && (post_format == :yaml || post_format == :xml)
end

#get?Boolean

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

Returns:

  • (Boolean)


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

def get?
  method == :get
end

#head?Boolean

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/action_controller/request.rb', line 42

def head?
  method == :head
end

#hostObject

Returns the host for this request, such as example.com.



241
242
# File 'lib/action_controller/request.rb', line 241

def host
end

#host_with_portObject

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



203
204
205
# File 'lib/action_controller/request.rb', line 203

def host_with_port
  host + port_string
end

#methodObject

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



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

def method
  @request_method ||= @env['REQUEST_METHOD'].downcase.to_sym
end

#parametersObject

Returns both GET and POST parameters in a single hash.



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

def parameters
  @parameters ||= request_parameters.update(query_parameters).update(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



158
159
160
161
162
163
164
165
# File 'lib/action_controller/request.rb', line 158

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

  # Cut off the path to the installation directory if given
  root = relative_url_root
  path[0, root.length] = '' if root
  path || ''
end

#path_parametersObject

Returns a hash with the parameters used to form the path of the request

Example:

{:action => 'my_action', :controller => 'my_controller'}


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

def path_parameters
  @path_parameters ||= {}
end

#path_parameters=(parameters) ⇒ Object

:nodoc:



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

def path_parameters=(parameters) #:nodoc:
  @path_parameters = parameters
  @symbolized_path_parameters = @parameters = nil
end

#portObject

Returns the port number of this request as an integer.



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

def port
  @port_as_int ||= @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.



197
198
199
# File 'lib/action_controller/request.rb', line 197

def port_string
  (port == standard_port) ? '' : ":#{port}"
end

#post?Boolean

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

Returns:

  • (Boolean)


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

def post?
  method == :post
end

#post_formatObject

Determine whether the body of a HTTP call is URL-encoded (default) or matches one of the registered param_parsers.

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



8
9
10
11
12
13
14
15
16
17
# File 'lib/action_controller/deprecated_request_methods.rb', line 8

def post_format
  case content_type.to_s
  when 'application/xml'
    :xml
  when 'application/x-yaml'
    :yaml
  else
    :url_encoded
  end
end

#protocolObject

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



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

def protocol
  ssl? ? 'https://' : 'http://'
end

#put?Boolean

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

Returns:

  • (Boolean)


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

def put?
  method == :put
end

#query_parametersObject

– Must be implemented in the concrete request ++



234
235
# File 'lib/action_controller/request.rb', line 234

def query_parameters #:nodoc:
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.



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

def raw_post
  @env['RAW_POST_DATA']
end

#relative_url_rootObject

Returns the path minus the web server relative installation directory. This can be set with the environment variable RAILS_RELATIVE_URL_ROOT. It can be automatically extracted for Apache setups. If the server is not Apache, this method returns an empty string.



171
172
173
174
175
176
177
178
179
180
# File 'lib/action_controller/request.rb', line 171

def relative_url_root
  @@relative_url_root ||= case
    when @env["RAILS_RELATIVE_URL_ROOT"]
      @env["RAILS_RELATIVE_URL_ROOT"]
    when server_software == 'apache'
      @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '')
    else
      ''
  end
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.



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

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

  @env['REMOTE_ADDR']
end

#request_parametersObject

:nodoc:



237
238
# File 'lib/action_controller/request.rb', line 237

def request_parameters #:nodoc:
end

#request_uriObject

Returns the request URI correctly, taking into account the idiosyncracies of the various servers.



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/action_controller/request.rb', line 133

def request_uri
  if uri = @env['REQUEST_URI']
    (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : 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{[^/]+$})
    uri = @env['PATH_INFO']
    uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
    unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
      uri << '?' << env_qs
    end
    uri
  end
end

#reset_sessionObject

:nodoc:



254
255
# File 'lib/action_controller/request.rb', line 254

def reset_session #:nodoc:
end

#server_softwareObject

Returns the lowercase name of the HTTP server software.



227
228
229
# File 'lib/action_controller/request.rb', line 227

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

#sessionObject

:nodoc:



247
248
# File 'lib/action_controller/request.rb', line 247

def session #:nodoc:
end

#session=(session) ⇒ Object

:nodoc:



250
251
252
# File 'lib/action_controller/request.rb', line 250

def session=(session) #:nodoc:
  @session = session
end

#ssl?Boolean

Is this an SSL request?

Returns:

  • (Boolean)


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

def ssl?
  @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https'
end

#standard_portObject

Returns the standard port number for this request’s protocol



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

def standard_port
  case protocol
    when 'https://' then 443
    else 80
  end
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”.



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

def subdomains(tld_length = 1)
  return [] unless host
  parts = host.split('.')
  parts[0..-(tld_length+2)]
end

#symbolized_path_parametersObject

The same as path_parameters with explicitly symbolized keys



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

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)


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

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)


25
26
27
# File 'lib/action_controller/deprecated_request_methods.rb', line 25

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

#yaml_post?Boolean

Is this a POST request formatted as YAML?

Returns:

  • (Boolean)


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

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