add add_auth

master
Kanezoh 2021-11-23 14:50:50 +09:00
parent 6e00f00c5f
commit 37300e116e
3 changed files with 18 additions and 6 deletions

View File

@ -296,6 +296,10 @@ class Mechanize
end
end
def add_auth(uri, user, pass)
@agent.add_auth(uri, user, pass)
end
# Runs given block, then resets the page history as it was before.
private def transact
# save the previous history status.

View File

@ -1,5 +1,6 @@
require "../cookie"
require "../history"
require "./auth_store"
class Mechanize
module HTTP
@ -10,6 +11,7 @@ class Mechanize
property history : History
property user_agent : String
property request_cookies : ::HTTP::Cookies
getter auth_store : AuthStore
def initialize(@context : Mechanize? = nil)
@history = History.new
@ -17,6 +19,7 @@ class Mechanize
@context = context
@request_cookies = ::HTTP::Cookies.new
@user_agent = ""
@auth_store = AuthStore.new
end
# send http request and return page.
@ -135,6 +138,10 @@ class Mechanize
request_headers["Referer"] = referer.uri.to_s
end
def add_auth(uri, user, pass)
@auth_store.add_auth(uri, user, pass)
end
private def resolve_parameters(uri, method, params)
case method
when :get

View File

@ -3,6 +3,7 @@ class Mechanize
# This class store info for HTTP Authentication.
class AuthStore
getter auth_accounts : Hash(URI, Tuple(String, String))
def initialize
@auth_accounts = Hash(URI, Tuple(String, String)).new
end
@ -11,19 +12,19 @@ class Mechanize
unless uri.is_a?(URI)
uri = URI.new(uri)
end
#uri += '/'
# uri += '/'
uri.user = nil
uri.password = nil
auth_accounts[uri] = {user, pass}
end ##
end ##
# Retrieves credentials for +realm+ on the server at +uri+.
def credentials_for(uri, realm) : Tuple(String)
unless uri.is_a?(URI)
uri = URI.new(uri)
end
#uri += '/'
# uri += '/'
uri.user = nil
uri.password = nil