request.rb 7.1 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3
module ActionController
  # These methods are available in both the production and test Request objects.
  class AbstractRequest
4
    cattr_accessor :relative_url_root
5

6 7 8 9
    # Returns the hash of environment variables for this request,
    # such as { 'RAILS_ENV' => 'production' }.
    attr_reader :env

D
Initial  
David Heinemeier Hansson 已提交
10 11
    # Returns both GET and POST parameters in a single hash.
    def parameters
12
      @parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access
D
Initial  
David Heinemeier Hansson 已提交
13 14
    end

15
    # Returns the HTTP request method as a lowercase symbol (:get, for example)
D
Initial  
David Heinemeier Hansson 已提交
16
    def method
17
      @request_method ||= @env['REQUEST_METHOD'].downcase.to_sym
D
Initial  
David Heinemeier Hansson 已提交
18 19
    end

20
    # Is this a GET request?  Equivalent to request.method == :get
D
Initial  
David Heinemeier Hansson 已提交
21 22 23 24
    def get?
      method == :get
    end

25
    # Is this a POST request?  Equivalent to request.method == :post
D
Initial  
David Heinemeier Hansson 已提交
26 27 28 29
    def post?
      method == :post
    end

30
    # Is this a PUT request?  Equivalent to request.method == :put
D
Initial  
David Heinemeier Hansson 已提交
31 32 33 34
    def put?
      method == :put
    end

35
    # Is this a DELETE request?  Equivalent to request.method == :delete
D
Initial  
David Heinemeier Hansson 已提交
36 37 38 39
    def delete?
      method == :delete
    end

40
    # Is this a HEAD request?  Equivalent to request.method == :head
41 42 43
    def head?
      method == :head
    end
44

45 46
    # Determine whether the body of a HTTP call is URL-encoded (default)
    # or matches one of the registered param_parsers. 
47 48 49
    #
    # For backward compatibility, the post format is extracted from the
    # X-Post-Data-Format HTTP header if present.
50 51
    def content_type
      return @content_type if @content_type
52

53 54 55 56 57 58 59 60 61 62 63
      @content_type = @env['CONTENT_TYPE'].to_s.downcase
      
      if @env['HTTP_X_POST_DATA_FORMAT']          
        case @env['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym
        when :yaml
          @content_type = 'application/x-yaml'
        when :xml
          @content_type = 'application/xml'
        end
      end
      @content_type
64
    end
65

66 67 68
    # Returns true if the request's "X-Requested-With" header contains
    # "XMLHttpRequest". (The Prototype Javascript library sends this header with
    # every Ajax request.)
69
    def xml_http_request?
70
      not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil?
71 72 73
    end
    alias xhr? :xml_http_request?

D
Initial  
David Heinemeier Hansson 已提交
74 75 76 77 78 79 80
    # 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
81
      return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP'
82

83 84
      if @env.include? 'HTTP_X_FORWARDED_FOR' then
        remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip|
85
            ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
86 87 88
        end

        return remote_ips.first.strip unless remote_ips.empty?
D
Initial  
David Heinemeier Hansson 已提交
89
      end
90

91
      @env['REMOTE_ADDR']
D
Initial  
David Heinemeier Hansson 已提交
92 93
    end

94 95 96
    # 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)
97
      return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil?
98

99
      host.split('.').last(1 + tld_length).join('.')
100 101 102 103 104 105
    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)
106
      return [] unless host
107
      parts = host.split('.')
108
      parts[0..-(tld_length+2)]
109 110
    end

111 112 113
    # 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.
114
    def raw_post
115
      @env['RAW_POST_DATA']
116
    end
117

118 119
    # Returns the request URI correctly, taking into account the idiosyncracies
    # of the various servers.
D
Initial  
David Heinemeier Hansson 已提交
120
    def request_uri
121
      if uri = @env['REQUEST_URI']
122
        (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri.
123
      else  # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME
124 125
        script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
        uri = @env['PATH_INFO']
126
        uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
127
        unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
128 129 130
          uri << '?' << env_qs
        end
        uri
131
      end
132
    end
D
Initial  
David Heinemeier Hansson 已提交
133

134
    # Return 'https://' if this is an SSL request and 'http://' otherwise.
D
Initial  
David Heinemeier Hansson 已提交
135
    def protocol
136
      ssl? ? 'https://' : 'http://'
D
Initial  
David Heinemeier Hansson 已提交
137 138
    end

139
    # Is this an SSL request?
140
    def ssl?
141
      @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https'
142
    end
143

144
    # Returns the interpreted path to requested resource after all the installation directory of this application was taken into account
D
Initial  
David Heinemeier Hansson 已提交
145
    def path
146
      path = (uri = request_uri) ? uri.split('?').first : ''
147

148
      # Cut off the path to the installation directory if given
149 150 151 152
      root = relative_url_root
      path[0, root.length] = '' if root
      path || ''
    end
153

154 155 156
    # Returns the path minus the web server relative installation directory.
    # This method returns nil unless the web server is apache.
    def relative_url_root
157
      @@relative_url_root ||= server_software == 'apache' ? @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') : ''
D
Initial  
David Heinemeier Hansson 已提交
158 159
    end

160
    # Returns the port number of this request as an integer.
D
Initial  
David Heinemeier Hansson 已提交
161
    def port
162
      @port_as_int ||= @env['SERVER_PORT'].to_i
D
Initial  
David Heinemeier Hansson 已提交
163
    end
164

165 166 167 168 169 170 171
    # Returns the standard port number for this request's protocol
    def standard_port
      case protocol
        when 'https://' then 443
        else 80
      end
    end
D
Initial  
David Heinemeier Hansson 已提交
172

173 174
    # Returns a port suffix like ":8080" if the port number of this request
    # is not the default HTTP port 80 or HTTPS port 443.
175
    def port_string
176
      (port == standard_port) ? '' : ":#{port}"
177 178
    end

179 180
    # Returns a host:port string for this request, such as example.com or
    # example.com:8080.
D
Initial  
David Heinemeier Hansson 已提交
181
    def host_with_port
182
      host + port_string
D
Initial  
David Heinemeier Hansson 已提交
183
    end
184

185 186
    def path_parameters=(parameters)
      @path_parameters = parameters
187 188
      @symbolized_path_parameters = @parameters = nil
    end
189

190 191
    def symbolized_path_parameters
      @symbolized_path_parameters ||= path_parameters.symbolize_keys
192
    end
D
Initial  
David Heinemeier Hansson 已提交
193

194 195 196
    def path_parameters
      @path_parameters ||= {}
    end
197 198

    # Returns the lowercase name of the HTTP server software.
199
    def server_software
200
      (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil
201
    end
202

D
Initial  
David Heinemeier Hansson 已提交
203 204 205
    #--
    # Must be implemented in the concrete request
    #++
206
    def query_parameters #:nodoc:
D
Initial  
David Heinemeier Hansson 已提交
207 208
    end

209
    def request_parameters #:nodoc:
D
Initial  
David Heinemeier Hansson 已提交
210 211
    end

212 213
    # Returns the host for this request, such as example.com.
    def host
D
Initial  
David Heinemeier Hansson 已提交
214 215
    end

216
    def cookies #:nodoc:
D
Initial  
David Heinemeier Hansson 已提交
217 218
    end

219
    def session #:nodoc:
D
Initial  
David Heinemeier Hansson 已提交
220 221
    end

222 223 224 225
    def session=(session) #:nodoc:
      @session = session
    end

226
    def reset_session #:nodoc:
227
    end
D
Initial  
David Heinemeier Hansson 已提交
228 229
  end
end