Module: HTTPX::Plugins::DigestAuthentication::InstanceMethods

Defined in:
lib/httpx/plugins/digest_authentication.rb

Instance Method Summary collapse

Instance Method Details

#digest_authentication(user, password) ⇒ Object Also known as: digest_auth



14
15
16
17
# File 'lib/httpx/plugins/digest_authentication.rb', line 14

def digest_authentication(user, password)
  @_digest = Digest.new(user, password)
  self
end

#request(*args, keep_open: @keep_open, **options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/httpx/plugins/digest_authentication.rb', line 20

def request(*args, keep_open: @keep_open, **options)
  return super unless @_digest
  begin
    requests = __build_reqs(*args, **options)
    probe_request = requests.first
    prev_response = __send_reqs(*probe_request).first

    unless prev_response.status == 401
      raise Error, "request doesn't require authentication (status: #{prev_response})"
    end

    probe_request.transition(:idle)
    responses = []

    requests.each do |request|
      token = @_digest.generate_header(request, prev_response)
      request.headers["authorization"] = "Digest #{token}"
      response = __send_reqs(*request).first
      responses << response
      prev_response = response
    end
    return responses.first if responses.size == 1
    responses
  ensure
    close unless keep_open
  end
end