request.rb 2.1 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
module ActionController
  # These methods are available in both the production and test Request objects.
  class AbstractRequest
    # Returns both GET and POST parameters in a single hash.
    def parameters
      @parameters ||= request_parameters.update(query_parameters)
    end

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

    def get?
      method == :get
    end

    def post?
      method == :post
    end

    def put?
      method == :put
    end

    def delete?
      method == :delete
    end

29 30 31 32
    def head?
      method == :head
    end

D
Initial  
David Heinemeier Hansson 已提交
33 34 35 36 37 38 39
    # 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
40 41 42 43
      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|
D
Initial  
David Heinemeier Hansson 已提交
44
            ip =~ /^unknown$|^(10|172\.16|192\.168)\./i
45 46 47
        end

        return remote_ips.first.strip unless remote_ips.empty?
D
Initial  
David Heinemeier Hansson 已提交
48
      end
49 50

      return env['REMOTE_ADDR']
D
Initial  
David Heinemeier Hansson 已提交
51 52 53 54 55 56 57 58 59 60
    end

    def request_uri
      env["REQUEST_URI"]
    end

    def protocol
      port == 443 ? "https://" : "http://"
    end

61 62 63 64
    def ssl?
      protocol == "https://"
    end

D
Initial  
David Heinemeier Hansson 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    def path
      request_uri ? request_uri.split("?").first : ""
    end

    def port
      env["SERVER_PORT"].to_i
    end

    def host_with_port
      if env['HTTP_HOST']
        env['HTTP_HOST']
      elsif (protocol == "http://" && port == 80) || (protocol == "https://" && port == 443)
        host
      else
        host + ":#{port}"
      end
    end

    #--
    # Must be implemented in the concrete request
    #++
    def query_parameters
    end

    def request_parameters
    end

    def env
    end

    def host
    end

    def cookies
    end

    def session
    end

    def reset_session
    end    
  end
end