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
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. # Runs given block, then resets the page history as it was before.
private def transact private def transact
# save the previous history status. # save the previous history status.

View File

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

View File

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