0
unless RUBY_VERSION > "1.8.2"
0
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "mechanize", "net-overrides")
0
+# Monkey patch for ruby 1.8.4
0
+unless RUBY_VERSION > "1.8.4"
0
+ class HTTPResponse # :nodoc:
0
+ CODE_TO_OBJ['500'] = HTTPInternalServerError
0
require 'webrick/httputils'
0
-require 'mechanize/hpricot'
0
-require 'mechanize/mech_version'
0
+require 'mechanize/monkey_patch'
0
require 'mechanize/cookie'
0
require 'mechanize/errors'
0
require 'mechanize/pluggable_parsers'
0
require 'mechanize/form'
0
require 'mechanize/form_elements'
0
+require 'mechanize/history'
0
require 'mechanize/list'
0
require 'mechanize/page'
0
require 'mechanize/page_elements'
0
@@ -50,20 +61,27 @@ module WWW
0
# search_results = agent.submit(search_form)
0
# puts search_results.body
0
+ # The version of Mechanize you are using.
0
'Windows IE 6' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
0
+ 'Windows IE 7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
0
'Windows Mozilla' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6',
0
'Mac Safari' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3',
0
'Mac FireFox' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3',
0
'Mac Mozilla' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401',
0
'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624',
0
'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)',
0
- 'Mechanize' => "WWW-Mechanize/#{V
ersion} (http://rubyforge.org/projects/mechanize/)"
0
+ 'Mechanize' => "WWW-Mechanize/#{V
ERSION} (http://rubyforge.org/projects/mechanize/)"
0
attr_accessor :cookie_jar
0
- attr_accessor :max_history
0
attr_accessor :open_timeout, :read_timeout
0
attr_accessor :user_agent
0
attr_accessor :watch_for_set
0
@@ -71,15 +89,24 @@ class Mechanize
0
+ attr_accessor :redirect_ok
0
+ attr_accessor :keep_alive_time
0
+ attr_accessor :keep_alive
0
+ attr_accessor :conditional_requests
0
+ attr_accessor :follow_meta_refresh
0
attr_reader :pluggable_parser
0
+ alias :follow_redirect? :redirect_ok
0
+ CNONCE = Digest::MD5.hexdigest("%x" % (Time.now.to_i + rand(65535)))
0
- @cookie_jar
= CookieJar.new
0
+ @cookie_jar
= CookieJar.new
0
@user_agent = AGENT_ALIASES['Mechanize']
0
@@ -88,14 +115,17 @@ class Mechanize
0
@cert = nil # OpenSSL Certificate
0
@key = nil # OpenSSL Private Key
0
@pass = nil # OpenSSL Password
0
+ @redirect_ok = true # Should we follow redirects?
0
+ @history =
WWW::Mechanize::History.new0
@pluggable_parser = PluggableParser.new
0
- # Basic Auth variables
0
- @user = nil # Basic Auth User
0
- @password = nil # Basic Auth Password
0
+ @user = nil # Auth User
0
+ @password = nil # Auth Password
0
+ @digest = nil # DigestAuth Digest
0
+ @auth_hash = {} # Keep track of urls for sending auth
0
@@ -103,9 +133,21 @@ class Mechanize
0
+ @conditional_requests = true
0
+ @follow_meta_refresh = false
0
+ # Connection Cache & Keep alive
0
+ @connection_cache = {}
0
+ @keep_alive_time = 300
0
yield self if block_given?
0
+ def max_history=(length); @history.max_size = length; end
0
+ def max_history; @history.max_size; end
0
# Sets the proxy address, port, user, and password
0
def set_proxy(addr, port, user = nil, pass = nil)
0
@proxy_addr, @proxy_port, @proxy_user, @proxy_pass = addr, port, user, pass
0
@@ -124,18 +166,23 @@ class Mechanize
0
# Sets the user and password to be used for basic authentication.
0
def basic_auth(user, password)
0
+ def auth(user, password)
0
# Fetches the URL passed in and returns a page.
0
- cur_page = current_page || Page.new( nil, {'content-type'=>'text/html'})
0
+ def get(url, referer=nil, &block)
0
+ cur_page = referer || current_page ||
0
+ Page.new( nil, {'content-type'=>'text/html'})
0
abs_uri = to_absolute_uri(url, cur_page)
0
request = fetch_request(abs_uri)
0
- page = fetch_page(abs_uri, request, cur_page
)
0
+ page = fetch_page(abs_uri, request, cur_page
, &block)
0
@@ -149,10 +196,17 @@ class Mechanize
0
# Clicks the WWW::Mechanize::Link object passed in and returns the
0
- link.attributes['href'] || link.attributes['src'] || link.href
0
+ link.attributes['href'] || link.attributes['src'] || link.href,
0
+ referer || current_page()
0
# Equivalent to the browser back button. Returns the most recent page
0
@@ -169,9 +223,8 @@ class Mechanize
0
# agent.post('http://example.com/', [ ["foo", "bar"] ])
0
def post(url, query={})
0
node = Hpricot::Elem.new(Hpricot::STag.new('form'))
0
- node.attributes['method'] = 'POST'
0
- node.attributes['enctype'] = 'application/x-www-form-urlencoded'
0
+ node['method'] = 'POST'
0
+ node['enctype'] = 'application/x-www-form-urlencoded'
0
@@ -188,17 +241,12 @@ class Mechanize
0
# agent.submit(page.forms.first, page.forms.first.buttons.first)
0
def submit(form, button=nil)
0
form.add_button_to_query(button) if button
0
- uri = to_absolute_uri(form.action
)
0
+ uri = to_absolute_uri(form.action
, form.page)
0
case form.method.upcase
0
- uri.query = WWW::Mechanize.build_query_string(form.build_query)
0
- uri.query = uri.query + "&" +
0
- WWW::Mechanize.build_query_string(form.build_query)
0
+ uri.query = WWW::Mechanize.build_query_string(form.build_query)
0
raise "unsupported method: #{form.method.upcase}"
0
@@ -212,9 +260,15 @@ class Mechanize
0
# Returns whether or not a url has been visited
0
- url = url.uri if url.respond_to? :uri
0
- uri = to_absolute_uri(url).to_s
0
- ! @history.find { |h| h.uri.to_s == uri }.nil?
0
+ ! visited_page(url).nil?
0
+ # Returns a visited page for the url passed in, otherwise nil
0
+ if url.respond_to? :href
0
+ @history.visited_page(to_absolute_uri(url))
0
# Runs given block, then resets the page history as it was before. self is
0
@@ -230,22 +284,136 @@ class Mechanize
0
alias :page :current_page
0
+ def set_headers(uri, request, cur_page)
0
+ request.add_field('Connection', 'keep-alive')
0
+ request.add_field('Keep-Alive', keep_alive_time.to_s)
0
+ request.add_field('Connection', 'close')
0
+ request.add_field('Accept-Encoding', 'gzip,identity')
0
+ request.add_field('Accept-Language', 'en-us,en;q0.5')
0
+ request.add_field('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
0
+ unless @cookie_jar.empty?(uri)
0
+ cookies = @cookie_jar.cookies(uri)
0
+ cookie = cookies.length > 0 ? cookies.join("; ") : nil
0
+ log.debug("using cookie: #{c}")
0
+ request.add_field('Cookie', cookie)
0
+ # Add Referer header to request
0
+ unless cur_page.uri.nil?
0
+ request.add_field('Referer', cur_page.uri.to_s)
0
+ # Add User-Agent header to request
0
+ request.add_field('User-Agent', @user_agent) if @user_agent
0
+ # Add If-Modified-Since if page is in history
0
+ if @conditional_requests
0
+ if( (page = visited_page(uri)) && page.response['Last-Modified'] )
0
+ request.add_field('If-Modified-Since', page.response['Last-Modified'])
0
+ if( @auth_hash[uri.host] )
0
+ case @auth_hash[uri.host]
0
+ request.basic_auth(@user, @password)
0
+ @digest_response ||= nil
0
+ @digest_response = self.gen_auth_header(uri,request,@digest) if @digest
0
+ request.add_field('Authorization', @digest_response) if @digest_response
0
+ def gen_auth_header(uri, request, auth_header, is_IIS = false)
0
+ password = @digest_password
0
+ auth_header =~ /^(\w+) (.*)/
0
+ $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
0
+ a_1 = "#{@user}:#{params['realm']}:#{@password}"
0
+ a_2 = "#{request.method}:#{uri.path}"
0
+ request_digest << Digest::MD5.hexdigest(a_1)
0
+ request_digest << ':' << params['nonce']
0
+ request_digest << ':' << ('%08x' % @@nonce_count)
0
+ request_digest << ':' << CNONCE