diff --git a/src/mechanize.cr b/src/mechanize.cr index cc9e047..84091f4 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -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. diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 1852b39..74201e8 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -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 diff --git a/src/mechanize/http/auth_store.cr b/src/mechanize/http/auth_store.cr index 7167dbc..ad9976a 100644 --- a/src/mechanize/http/auth_store.cr +++ b/src/mechanize/http/auth_store.cr @@ -3,30 +3,31 @@ 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 def add_auth(uri, user, pass) unless uri.is_a?(URI) - uri = URI.new(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) + uri = URI.new(uri) end - #uri += '/' + # uri += '/' uri.user = nil uri.password = nil - + auth_accounts[uri] end end