2004-11-23 20:04:44 -05:00
|
|
|
module ActionController
|
|
|
|
# These methods are available in both the production and test Request objects.
|
|
|
|
class AbstractRequest
|
2005-05-22 03:43:05 -04:00
|
|
|
cattr_accessor :relative_url_root
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# Returns both GET and POST parameters in a single hash.
|
|
|
|
def parameters
|
2005-02-14 20:45:35 -05:00
|
|
|
@parameters ||= request_parameters.merge(query_parameters).merge(path_parameters).with_indifferent_access
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Returns the HTTP request method as a lowercase symbol (:get, for example)
|
2004-11-23 20:04:44 -05:00
|
|
|
def method
|
2005-06-24 10:43:15 -04:00
|
|
|
env['REQUEST_METHOD'].downcase.to_sym
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a GET request? Equivalent to request.method == :get
|
2004-11-23 20:04:44 -05:00
|
|
|
def get?
|
|
|
|
method == :get
|
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a POST request? Equivalent to request.method == :post
|
2004-11-23 20:04:44 -05:00
|
|
|
def post?
|
|
|
|
method == :post
|
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a PUT request? Equivalent to request.method == :put
|
2004-11-23 20:04:44 -05:00
|
|
|
def put?
|
|
|
|
method == :put
|
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a DELETE request? Equivalent to request.method == :delete
|
2004-11-23 20:04:44 -05:00
|
|
|
def delete?
|
|
|
|
method == :delete
|
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a HEAD request? Equivalent to request.method == :head
|
2004-12-07 05:50:26 -05:00
|
|
|
def head?
|
|
|
|
method == :head
|
|
|
|
end
|
2005-05-14 04:36:19 -04:00
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# 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.
|
2005-05-14 04:36:19 -04:00
|
|
|
def post_format
|
2005-07-23 05:00:05 -04:00
|
|
|
@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
|
2005-05-14 04:36:19 -04:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a POST request formatted as XML or YAML?
|
2005-05-14 04:36:19 -04:00
|
|
|
def formatted_post?
|
2005-07-23 05:00:05 -04:00
|
|
|
post? && (post_format == :xml || post_format == :yaml)
|
2005-05-14 04:36:19 -04:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a POST request formatted as XML?
|
2005-05-14 04:36:19 -04:00
|
|
|
def xml_post?
|
2005-07-23 05:00:05 -04:00
|
|
|
post? && post_format == :xml
|
2005-05-14 04:36:19 -04:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this a POST request formatted as YAML?
|
2005-05-14 04:36:19 -04:00
|
|
|
def yaml_post?
|
2005-07-23 05:00:05 -04:00
|
|
|
post? && post_format == :yaml
|
2005-05-14 04:36:19 -04:00
|
|
|
end
|
2005-05-22 03:43:05 -04:00
|
|
|
|
2005-06-24 12:40:01 -04:00
|
|
|
# Returns true if the request's "X-Requested-With" header contains
|
|
|
|
# "XMLHttpRequest". (The Prototype Javascript library sends this header with
|
|
|
|
# every Ajax request.)
|
2005-05-22 03:43:05 -04:00
|
|
|
def xml_http_request?
|
2005-06-24 10:43:15 -04:00
|
|
|
not /XMLHttpRequest/i.match(env['HTTP_X_REQUESTED_WITH']).nil?
|
2005-05-22 03:43:05 -04:00
|
|
|
end
|
|
|
|
alias xhr? :xml_http_request?
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# 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.
|
|
|
|
def remote_ip
|
2004-11-25 11:39:18 -05:00
|
|
|
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|
|
2005-02-24 07:03:17 -05:00
|
|
|
ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
|
2004-11-25 11:39:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
return remote_ips.first.strip unless remote_ips.empty?
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2004-11-25 11:39:18 -05:00
|
|
|
|
2005-07-23 05:00:05 -04:00
|
|
|
env['REMOTE_ADDR']
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2004-12-18 12:19:15 -05:00
|
|
|
# Returns the domain part of a host, such as rubyonrails.org in "www.rubyonrails.org". You can specify
|
|
|
|
# a different <tt>tld_length</tt>, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk".
|
|
|
|
def domain(tld_length = 1)
|
2005-09-20 02:44:53 -04:00
|
|
|
return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil?
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2005-01-25 06:15:26 -05:00
|
|
|
host.split('.').last(1 + tld_length).join('.')
|
2004-12-18 12:19:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns all the subdomains as an array, so ["dev", "www"] would be returned for "dev.www.rubyonrails.org".
|
|
|
|
# You can specify a different <tt>tld_length</tt>, such as 2 to catch ["www"] instead of ["www", "rubyonrails"]
|
|
|
|
# in "www.rubyonrails.co.uk".
|
|
|
|
def subdomains(tld_length = 1)
|
2005-09-20 02:44:53 -04:00
|
|
|
return [] unless host
|
2005-01-25 06:15:26 -05:00
|
|
|
parts = host.split('.')
|
2005-07-12 12:16:13 -04:00
|
|
|
parts[0..-(tld_length+2)]
|
2004-12-18 12:19:15 -05:00
|
|
|
end
|
|
|
|
|
2005-11-20 00:31:33 -05:00
|
|
|
# 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.
|
2005-01-25 06:15:26 -05:00
|
|
|
def raw_post
|
|
|
|
env['RAW_POST_DATA']
|
|
|
|
end
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2005-10-16 11:42:03 -04:00
|
|
|
# Returns the request URI correctly, taking into account the idiosyncracies
|
|
|
|
# of the various servers.
|
2004-11-23 20:04:44 -05:00
|
|
|
def request_uri
|
2005-07-23 05:00:05 -04:00
|
|
|
if uri = env['REQUEST_URI']
|
|
|
|
(%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri.
|
2005-04-18 11:43:07 -04:00
|
|
|
else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME
|
2005-07-23 05:00:05 -04:00
|
|
|
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
|
2005-04-18 11:43:07 -04:00
|
|
|
end
|
2005-06-24 10:43:15 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Return 'https://' if this is an SSL request and 'http://' otherwise.
|
2004-11-23 20:04:44 -05:00
|
|
|
def protocol
|
2005-07-23 05:00:05 -04:00
|
|
|
ssl? ? 'https://' : 'http://'
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Is this an SSL request?
|
2004-11-24 14:46:30 -05:00
|
|
|
def ssl?
|
2005-11-20 00:31:33 -05:00
|
|
|
env['HTTPS'] == 'on'
|
2004-11-24 14:46:30 -05:00
|
|
|
end
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2005-05-22 03:43:05 -04:00
|
|
|
# Returns the interpreted path to requested resource after all the installation directory of this application was taken into account
|
2004-11-23 20:04:44 -05:00
|
|
|
def path
|
2005-05-22 03:43:05 -04:00
|
|
|
path = (uri = request_uri) ? uri.split('?').first : ''
|
2005-06-24 10:43:15 -04:00
|
|
|
|
2005-06-18 01:17:18 -04:00
|
|
|
# Cut off the path to the installation directory if given
|
2005-09-11 10:10:20 -04:00
|
|
|
root = relative_url_root
|
|
|
|
path[0, root.length] = '' if root
|
|
|
|
path || ''
|
|
|
|
end
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2005-06-18 01:17:18 -04:00
|
|
|
# Returns the path minus the web server relative installation directory.
|
|
|
|
# This method returns nil unless the web server is apache.
|
|
|
|
def relative_url_root
|
2005-09-15 01:32:16 -04:00
|
|
|
@@relative_url_root ||= server_software == 'apache' ? File.dirname(env["SCRIPT_NAME"].to_s).gsub(/(^\.$|^\/$)/, '') : ''
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Returns the port number of this request as an integer.
|
2004-11-23 20:04:44 -05:00
|
|
|
def port
|
2005-07-23 05:00:05 -04:00
|
|
|
@port_as_int ||= env['SERVER_PORT'].to_i
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2005-10-14 21:00:25 -04:00
|
|
|
# Returns the standard port number for this request's protocol
|
|
|
|
def standard_port
|
|
|
|
case protocol
|
|
|
|
when 'https://' then 443
|
|
|
|
else 80
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Returns a port suffix like ":8080" if the port number of this request
|
|
|
|
# is not the default HTTP port 80 or HTTPS port 443.
|
2004-12-18 13:01:28 -05:00
|
|
|
def port_string
|
2005-10-14 21:00:25 -04:00
|
|
|
(port == standard_port) ? '' : ":#{port}"
|
2004-12-18 13:01:28 -05:00
|
|
|
end
|
|
|
|
|
2005-06-24 10:43:15 -04:00
|
|
|
# Returns a host:port string for this request, such as example.com or
|
|
|
|
# example.com:8080.
|
2004-11-23 20:04:44 -05:00
|
|
|
def host_with_port
|
2005-10-14 21:00:25 -04:00
|
|
|
host + port_string
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
def path_parameters=(parameters)
|
|
|
|
@path_parameters = parameters
|
2005-06-24 12:40:01 -04:00
|
|
|
@symbolized_path_parameters = @parameters = nil
|
|
|
|
end
|
2005-11-20 00:31:33 -05:00
|
|
|
|
2005-06-24 12:40:01 -04:00
|
|
|
def symbolized_path_parameters
|
|
|
|
@symbolized_path_parameters ||= path_parameters.symbolize_keys
|
2005-02-14 20:45:35 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
def path_parameters
|
|
|
|
@path_parameters ||= {}
|
|
|
|
end
|
2005-06-24 10:43:15 -04:00
|
|
|
|
|
|
|
# Returns the lowercase name of the HTTP server software.
|
2005-06-18 01:17:18 -04:00
|
|
|
def server_software
|
|
|
|
(env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ env['SERVER_SOFTWARE']) ? $1.downcase : nil
|
|
|
|
end
|
2005-06-24 10:43:15 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
#--
|
|
|
|
# Must be implemented in the concrete request
|
|
|
|
#++
|
2005-10-16 11:42:03 -04:00
|
|
|
def query_parameters #:nodoc:
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-10-16 11:42:03 -04:00
|
|
|
def request_parameters #:nodoc:
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-11-20 00:31:33 -05:00
|
|
|
# Returns the hash of environment variables for this request,
|
|
|
|
# such as { 'RAILS_ENV' => 'production' }.
|
|
|
|
def env
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-11-20 00:31:33 -05:00
|
|
|
# Returns the host for this request, such as example.com.
|
|
|
|
def host
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-10-16 11:42:03 -04:00
|
|
|
def cookies #:nodoc:
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-10-16 11:42:03 -04:00
|
|
|
def session #:nodoc:
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-10-16 11:42:03 -04:00
|
|
|
def reset_session #:nodoc:
|
2005-11-20 00:31:33 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|