connection.rb 9.0 KB
Newer Older
1
require 'active_support/core_ext/benchmark'
2 3 4 5 6 7
require 'net/https'
require 'date'
require 'time'
require 'uri'

module ActiveResource
8 9 10
  # Class to handle connections to remote web services.
  # This class is used by ActiveResource::Base to interface with REST
  # services.
11
  class Connection
12 13 14 15

    HTTP_FORMAT_HEADER_NAMES = {  :get => 'Accept',
      :put => 'Content-Type',
      :post => 'Content-Type',
16 17
      :delete => 'Accept',
      :head => 'Accept'
18 19
    }

P
pivotal 已提交
20
    attr_reader :site, :user, :password, :auth_type, :timeout, :proxy, :ssl_options
21
    attr_accessor :format
22

23 24 25 26 27 28
    class << self
      def requests
        @@requests ||= []
      end
    end

29 30
    # The +site+ parameter is required and will set the +site+
    # attribute to the URI for the remote resource service.
31
    def initialize(site, format = ActiveResource::Formats::XmlFormat)
32
      raise ArgumentError, 'Missing site URI' unless site
33
      @user = @password = nil
34
      @uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
35
      self.site = site
36
      self.format = format
37
    end
38

39
    # Set URI for remote service.
40
    def site=(site)
41 42 43
      @site = site.is_a?(URI) ? site : @uri_parser.parse(site)
      @user = @uri_parser.unescape(@site.user) if @site.user
      @password = @uri_parser.unescape(@site.password) if @site.password
44 45
    end

M
Marshall Huss 已提交
46 47
    # Set the proxy for remote service.
    def proxy=(proxy)
48
      @proxy = proxy.is_a?(URI) ? proxy : @uri_parser.parse(proxy)
M
Marshall Huss 已提交
49 50
    end

P
Pratik Naik 已提交
51
    # Sets the user for remote service.
52 53 54 55
    def user=(user)
      @user = user
    end

P
Pratik Naik 已提交
56
    # Sets the password for remote service.
57 58
    def password=(password)
      @password = password
59
    end
60

P
pivotal 已提交
61 62 63 64 65
    # Sets the auth type for remote service.
    def auth_type=(auth_type)
      @auth_type = legitimize_auth_type(auth_type)
    end

P
Pratik Naik 已提交
66
    # Sets the number of seconds after which HTTP requests to the remote service should time out.
67 68 69 70
    def timeout=(timeout)
      @timeout = timeout
    end

71 72 73 74 75
    # Hash of options applied to Net::HTTP instance when +site+ protocol is 'https'.
    def ssl_options=(opts={})
      @ssl_options = opts
    end

P
Pratik Naik 已提交
76
    # Executes a GET request.
77
    # Used to get (find) resources.
78
    def get(path, headers = {})
P
pivotal 已提交
79
      with_auth { format.decode(request(:get, path, build_request_headers(headers, :get, self.site.merge(path))).body) }
80
    end
81

P
Pratik Naik 已提交
82
    # Executes a DELETE request (see HTTP protocol documentation if unfamiliar).
83
    # Used to delete resources.
84
    def delete(path, headers = {})
P
pivotal 已提交
85
      with_auth { request(:delete, path, build_request_headers(headers, :delete, self.site.merge(path))) }
86
    end
87

P
Pratik Naik 已提交
88
    # Executes a PUT request (see HTTP protocol documentation if unfamiliar).
89
    # Used to update resources.
90
    def put(path, body = '', headers = {})
P
pivotal 已提交
91
      with_auth { request(:put, path, body.to_s, build_request_headers(headers, :put, self.site.merge(path))) }
92 93
    end

P
Pratik Naik 已提交
94
    # Executes a POST request.
95
    # Used to create new resources.
96
    def post(path, body = '', headers = {})
P
pivotal 已提交
97
      with_auth { request(:post, path, body.to_s, build_request_headers(headers, :post, self.site.merge(path))) }
98
    end
99

P
Pratik Naik 已提交
100
    # Executes a HEAD request.
101 102
    # Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).
    def head(path, headers = {})
P
pivotal 已提交
103
      with_auth { request(:head, path, build_request_headers(headers, :head, self.site.merge(path))) }
104 105
    end

106
    private
P
Pratik Naik 已提交
107
      # Makes a request to the remote service.
108
      def request(method, path, *arguments)
109 110 111
        result = ActiveSupport::Notifications.instrument("active_resource.request",
          :method => method, :path => path, :site => site) do |payload|
          payload[:result] = http.send(method, path, *arguments)
112
        end
113
        handle_response(result)
114 115
      rescue Timeout::Error => e
        raise TimeoutError.new(e.message)
116 117
      rescue OpenSSL::SSL::SSLError => e
        raise SSLError.new(e.message)
118
      end
119

P
Pratik Naik 已提交
120
      # Handles response and error codes from the remote service.
121
      def handle_response(response)
122
        case response.code.to_i
123 124
          when 301,302
            raise(Redirection.new(response))
125
          when 200...400
126
            response
127 128 129 130 131 132
          when 400
            raise(BadRequest.new(response))
          when 401
            raise(UnauthorizedAccess.new(response))
          when 403
            raise(ForbiddenAccess.new(response))
133 134
          when 404
            raise(ResourceNotFound.new(response))
135 136
          when 405
            raise(MethodNotAllowed.new(response))
137 138
          when 409
            raise(ResourceConflict.new(response))
139 140
          when 410
            raise(ResourceGone.new(response))
141 142
          when 422
            raise(ResourceInvalid.new(response))
143
          when 401...500
144 145 146 147 148 149 150 151
            raise(ClientError.new(response))
          when 500...600
            raise(ServerError.new(response))
          else
            raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
        end
      end

P
Pratik Naik 已提交
152
      # Creates new Net::HTTP instance for communication with the
153
      # remote service and resources.
154
      def http
155 156 157 158 159 160 161 162 163 164 165 166
        configure_http(new_http)
      end

      def new_http
        if @proxy
          Net::HTTP.new(@site.host, @site.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password)
        else
          Net::HTTP.new(@site.host, @site.port)
        end
      end

      def configure_http(http)
167
        http = apply_ssl_options(http)
168 169 170 171 172 173 174

        # Net::HTTP timeouts default to 60 seconds.
        if @timeout
          http.open_timeout = @timeout
          http.read_timeout = @timeout
        end

175
        http
176
      end
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
      def apply_ssl_options(http)
        return http unless @site.is_a?(URI::HTTPS)

        http.use_ssl     = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        return http unless defined?(@ssl_options)

        http.ca_path     = @ssl_options[:ca_path] if @ssl_options[:ca_path]
        http.ca_file     = @ssl_options[:ca_file] if @ssl_options[:ca_file]

        http.cert        = @ssl_options[:cert] if @ssl_options[:cert]
        http.key         = @ssl_options[:key]  if @ssl_options[:key]

        http.cert_store  = @ssl_options[:cert_store]  if @ssl_options[:cert_store]
        http.ssl_timeout = @ssl_options[:ssl_timeout] if @ssl_options[:ssl_timeout]

        http.verify_mode     = @ssl_options[:verify_mode]     if @ssl_options[:verify_mode]
        http.verify_callback = @ssl_options[:verify_callback] if @ssl_options[:verify_callback]
        http.verify_depth    = @ssl_options[:verify_depth]    if @ssl_options[:verify_depth]

        http
      end

201
      def default_header
202
        @default_header ||= {}
203
      end
204

205
      # Builds headers for request to remote service.
P
pivotal 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
      def build_request_headers(headers, http_method, uri)
        authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers)
      end

      def response_auth_header
        @response_auth_header ||= ""
      end

      def with_auth
        retried ||= false
        yield
      rescue UnauthorizedAccess => e
        raise if retried || auth_type != :digest
        @response_auth_header = e.response['WWW-Authenticate']
        retried = true
        retry
      end

      def authorization_header(http_method, uri)
        if @user || @password
          if auth_type == :digest
            { 'Authorization' => digest_auth_header(http_method, uri) }
          else
            { 'Authorization' => 'Basic ' + ["#{@user}:#{@password}"].pack('m').delete("\r\n") }
          end
        else
          {}
        end
234
      end
235

P
pivotal 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      def digest_auth_header(http_method, uri)
        params = extract_params_from_response

        ha1 = Digest::MD5.hexdigest("#{@user}:#{params['realm']}:#{@password}")
        ha2 = Digest::MD5.hexdigest("#{http_method.to_s.upcase}:#{uri.path}")

        params.merge!('cnonce' => client_nonce)
        request_digest = Digest::MD5.hexdigest([ha1, params['nonce'], "0", params['cnonce'], params['qop'], ha2].join(":"))
        "Digest #{auth_attributes_for(uri, request_digest, params)}"
      end

      def client_nonce
        Digest::MD5.hexdigest("%x" % (Time.now.to_i + rand(65535)))
      end

      def extract_params_from_response
        params = {}
        if response_auth_header =~ /^(\w+) (.*)/
          $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
        end
        params
      end

      def auth_attributes_for(uri, request_digest, params)
        [
          %Q(username="#{@user}"),
          %Q(realm="#{params['realm']}"),
          %Q(qop="#{params['qop']}"),
          %Q(uri="#{uri.path}"),
          %Q(nonce="#{params['nonce']}"),
          %Q(nc="0"),
          %Q(cnonce="#{params['cnonce']}"),
          %Q(opaque="#{params['opaque']}"),
          %Q(response="#{request_digest}")].join(", ")
270
      end
271

272 273 274 275
      def http_format_header(http_method)
        {HTTP_FORMAT_HEADER_NAMES[http_method] => format.mime_type}
      end

P
pivotal 已提交
276 277 278 279 280
      def legitimize_auth_type(auth_type)
        return :basic if auth_type.nil?
        auth_type = auth_type.to_sym
        [:basic, :digest].include?(auth_type) ? auth_type : :basic
      end
281
  end
282
end