Sha256: 73fc892d4eb1e2f15f78fedd4f2c4802a7ac4e87f936174fbcd3010c36e8534a

Contents?: true

Size: 1.72 KB

Versions: 6

Compression:

Stored size: 1.72 KB

Contents

module ApiAuth

  module RequestDrivers # :nodoc:

    class NetHttpRequest # :nodoc:

      include ApiAuth::Helpers

      def initialize(request)
        @request = request
        @headers = fetch_headers
        true
      end

      def set_auth_header(header)
        @request["Authorization"] = header
        @headers = fetch_headers
        @request
      end

      def calculated_md5
        if @request.respond_to?(:body_stream) && @request.body_stream
          body = @request.body_stream.read
          @request.body_stream.rewind
        else
          body = @request.body
        end

        md5_base64digest(body || '')
      end

      def populate_content_md5
        if @request.class::REQUEST_HAS_BODY
          @request["Content-MD5"] = calculated_md5
        end
      end

      def md5_mismatch?
        if @request.class::REQUEST_HAS_BODY
          calculated_md5 != content_md5
        else
          false
        end
      end

      def fetch_headers
        @request
      end

      def content_type
        value = find_header(%w(CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE))
        value.nil? ? "" : value
      end

      def content_md5
        value = find_header(%w(CONTENT-MD5 CONTENT_MD5))
        value.nil? ? "" : value
      end

      def request_uri
        @request.path
      end

      def set_date
        @request["DATE"] = Time.now.utc.httpdate
      end

      def timestamp
        value = find_header(%w(DATE HTTP_DATE))
        value.nil? ? "" : value
      end

      def authorization_header
        find_header %w(Authorization AUTHORIZATION HTTP_AUTHORIZATION)
      end

    private

      def find_header(keys)
        keys.map {|key| @headers[key] }.compact.first
      end

    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
api-auth-1.3.2 lib/api_auth/request_drivers/net_http.rb
api-auth-1.3.1 lib/api_auth/request_drivers/net_http.rb
api-auth-1.3.0 lib/api_auth/request_drivers/net_http.rb
api-auth-1.2.6 lib/api_auth/request_drivers/net_http.rb
api-auth-1.2.5 lib/api_auth/request_drivers/net_http.rb
api-auth-1.2.4 lib/api_auth/request_drivers/net_http.rb