public
Fork of tenderlove/mechanize
Description: Mechanize is a ruby library that makes automated web interaction easy.
Homepage: http://mechanize.rubyforge.org/
Clone URL: git://github.com/github/mechanize.git
merging REL-0.9.0 -> trunk 308:HEAD

git-svn-id: svn+ssh://rubyforge.org/var/svn/mechanize/trunk@451 
f1cf478b-080f-0410-abad-959bfeec9ea8
aaronp (author)
Tue Dec 04 20:17:00 -0800 2007
commit  63d4e1539fdd02e128b4ace7d346806d355deaa2
tree    8a49a5128e4a07f214d804ab73cc601da262fcd7
parent  7af819fa236093fe989f4a98272f970c2e61a1fb
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
66
67
68
69
70
71
72
73
74
75
76
 
 
 
 
 
 
 
 
 
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
 
47
48
 
 
 
 
 
 
 
 
49
50
51
52
53
54
55
56
57
58
59
 
 
 
 
 
 
 
 
 
 
 
60
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,101 +1,60 @@
0
 require 'rubygems'
0
-require 'rake'
0
-require 'rake/testtask'
0
-require 'rake/gempackagetask'
0
-require 'rake/rdoctask'
0
-require 'rake/contrib/sshpublisher'
0
-
0
-def announce(msg='')
0
- STDERR.puts msg
0
-end
0
-
0
-PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
0
-PKG_NAME = 'mechanize'
0
-PKG_VERSION = '0.6.0' + PKG_BUILD
0
-PKG_FILES = FileList["{doc,lib,test}/**/*"].exclude("rdoc").to_a
0
-
0
-spec = Gem::Specification.new do |s|
0
- s.name = PKG_NAME
0
- s.version = PKG_VERSION
0
- s.author = "Aaron Patterson"
0
- s.email = "aaronp@rubyforge.org"
0
- s.homepage = "#{PKG_NAME}.rubyforge.org"
0
- s.platform = Gem::Platform::RUBY
0
- s.summary = "Mechanize provides automated web-browsing"
0
- s.files = Dir.glob("{bin,test,lib,doc}/**/*").delete_if {|item| item.include?(".svn") }
0
- s.require_path = "lib"
0
- s.has_rdoc = true
0
- s.extra_rdoc_files = ["README", "EXAMPLES", "CHANGELOG", "LICENSE", "NOTES",
0
- "GUIDE"]
0
- s.rdoc_options << "--main" << 'README' << "--title" << "'WWW::Mechanize RDoc'"
0
- s.rubyforge_project = PKG_NAME
0
- s.add_dependency('hpricot')
0
-end
0
-
0
-Rake::GemPackageTask.new(spec) do |p|
0
- p.gem_spec = spec
0
- p.need_tar = true
0
- p.need_zip = true
0
-end
0
-
0
-Rake::RDocTask.new do |p|
0
- p.main = "README"
0
- p.rdoc_dir = "doc"
0
- p.rdoc_files.include("README", "CHANGELOG", "LICENSE", "EXAMPLES", "NOTES",
0
- "GUIDE", "lib/**/*.rb")
0
- p.options << "--main" << 'README' << "--title" << "WWW::Mechanize RDoc"
0
-end
0
-
0
-desc "Publish the API documentation"
0
-task :pubrdoc => [ :rdoc ] do
0
- Rake::SshDirPublisher.new(
0
- "#{ENV['USER']}@rubyforge.org",
0
- "/var/www/gforge-projects/#{PKG_NAME}/",
0
- "doc" ).upload
0
-end
0
-
0
-task :update_version do
0
- announce "Updating Mechanize Version to #{PKG_VERSION}"
0
- File.open("lib/mechanize/mech_version.rb", "w") do |f|
0
- f.puts "module WWW"
0
- f.puts " class Mechanize"
0
- f.puts " Version = '#{PKG_VERSION}'"
0
- f.puts " end"
0
- f.puts "end"
0
+require 'hoe'
0
+
0
+$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "lib")
0
+require 'mechanize'
0
+
0
+class MechHoe < Hoe
0
+ def define_tasks
0
+ super
0
+
0
+ desc "Tag code"
0
+ task :tag do |p|
0
+ abort "Must supply VERSION=x.y.z" unless ENV['VERSION']
0
+ v = ENV['VERSION'].gsub(/\./, '_')
0
+
0
+ rf = RubyForge.new
0
+ user = rf.userconfig['username']
0
+
0
+ baseurl = "svn+ssh://#{user}@rubyforge.org//var/svn/#{name}"
0
+ sh "svn cp -m 'tagged REL-#{v}' . #{ baseurl }/tags/REL-#{ v }"
0
+ end
0
+
0
+ desc "Branch code"
0
+ Rake::Task.define_task("branch") do |p|
0
+ abort "Must supply VERSION=x.y.z" unless ENV['VERSION']
0
+ v = ENV['VERSION'].split(/\./)[0..1].join('_')
0
+
0
+ rf = RubyForge.new
0
+ user = rf.userconfig['username']
0
+
0
+ baseurl = "svn+ssh://#{user}@rubyforge.org/var/svn/#{name}"
0
+ sh "svn cp -m'branched #{v}' #{baseurl}/trunk #{baseurl}/branches/RB-#{v}"
0
+ end
0
+
0
+ desc "Update SSL Certificate"
0
+ Rake::Task.define_task('ssl_cert') do |p|
0
+ sh "openssl genrsa -des3 -out server.key 1024"
0
+ sh "openssl req -new -key server.key -out server.csr"
0
+ sh "cp server.key server.key.org"
0
+ sh "openssl rsa -in server.key.org -out server.key"
0
+ sh "openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt"
0
+ sh "cp server.key server.pem"
0
+ sh "mv server.key server.csr server.crt server.pem test/data/"
0
+ sh "rm server.key.org"
0
+ end
0
   end
0
- sh 'svn commit -m"updating version" lib/mechanize/mech_version.rb'
0
 end
0
 
0
-desc "Create a new release"
0
-task :release => [ :clobber, :update_version, :package, :tag ] do
0
- announce
0
- announce "**************************************************************"
0
- announce "* Release #{PKG_VERSION} Complete."
0
- announce "* Packages ready to upload."
0
- announce "**************************************************************"
0
- announce
0
+MechHoe.new('mechanize', WWW::Mechanize::VERSION) do |p|
0
+ p.rubyforge_name = 'mechanize'
0
+ p.author = 'Aaron Patterson'
0
+ p.email = 'aaronp@rubyforge.org'
0
+ p.summary = "Mechanize provides automated web-browsing"
0
+ p.description = p.paragraphs_of('README.txt', 3).join("\n\n")
0
+ p.url = p.paragraphs_of('README.txt', 1).first.strip
0
+ p.changes = p.paragraphs_of('CHANGELOG.txt', 0..2).join("\n\n")
0
+ p.extra_deps = [['hpricot', '>= 0.5.0']]
0
 end
0
 
0
-desc "Tag code"
0
-Rake::Task.define_task("tag") do |p|
0
- baseurl = "svn+ssh://#{ENV['USER']}@rubyforge.org//var/svn/#{PKG_NAME}"
0
- sh "svn cp -m 'tagged #{ PKG_VERSION }' . #{ baseurl }/tags/REL-#{ PKG_VERSION }"
0
-end
0
-
0
-desc "Branch code"
0
-Rake::Task.define_task("branch") do |p|
0
- baseurl = "svn+ssh://#{ENV['USER']}@rubyforge.org/var/svn/#{PKG_NAME}"
0
- sh "svn cp -m 'branched #{ PKG_VERSION }' #{baseurl}/trunk #{ baseurl }/branches/RB-#{ PKG_VERSION }"
0
-end
0
 
0
-desc "Update SSL Certificate"
0
-Rake::Task.define_task('ssl_cert') do |p|
0
- sh "openssl genrsa -des3 -out server.key 1024"
0
- sh "openssl req -new -key server.key -out server.csr"
0
- sh "cp server.key server.key.org"
0
- sh "openssl rsa -in server.key.org -out server.key"
0
- sh "openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt"
0
- sh "cp server.key server.pem"
0
- sh "mv server.key server.csr server.crt server.pem test/data/"
0
- sh "rm server.key.org"
0
-end
...
11
12
13
 
14
15
16
 
 
 
 
 
 
 
 
 
17
18
19
20
21
22
 
 
23
24
25
26
27
 
28
29
30
...
50
51
52
 
 
 
 
 
 
 
53
54
 
55
56
57
58
59
60
61
 
62
63
64
65
66
67
68
69
...
71
72
73
 
 
 
 
 
74
75
76
77
 
 
 
 
 
78
79
80
 
81
82
83
84
85
...
88
89
90
 
91
92
93
 
94
95
96
97
98
 
 
 
 
 
99
100
101
...
103
104
105
 
 
 
 
 
 
 
 
 
106
107
108
 
 
 
109
110
111
...
124
125
126
127
128
 
 
 
 
 
 
129
130
131
132
133
 
 
 
134
135
136
137
138
 
139
140
141
...
149
150
151
 
 
 
 
 
 
152
153
 
 
154
155
 
156
157
158
...
169
170
171
172
173
174
 
 
175
176
177
...
188
189
190
191
 
192
193
194
195
196
197
198
199
200
201
 
202
203
204
...
212
213
214
215
216
217
 
 
 
 
 
 
 
 
 
218
219
220
...
230
231
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
234
235
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
238
239
240
 
 
 
 
241
 
 
242
243
244
245
246
247
248
 
 
249
250
251
...
276
277
278
279
 
280
281
282
283
284
285
286
287
288
289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
291
 
292
293
294
...
301
302
303
304
 
 
 
 
 
 
 
 
305
306
307
308
309
310
311
312
313
314
315
 
 
316
317
318
319
 
320
321
322
323
 
324
325
 
326
327
328
...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
 
346
347
348
349
350
351
352
 
 
 
 
 
 
 
 
 
 
 
353
354
 
 
 
355
356
357
358
359
360
 
 
 
 
 
361
362
363
364
365
366
367
368
369
370
 
 
 
 
 
 
 
 
371
372
 
373
 
 
 
374
375
376
377
378
379
380
381
 
 
 
 
 
 
 
 
 
 
 
 
382
383
 
384
385
386
387
 
 
 
 
 
 
 
 
388
389
390
391
392
393
394
395
396
397
398
399
400
401
 
 
 
 
402
403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
405
406
...
416
417
418
419
420
421
422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
424
 
425
426
427
...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
 
31
32
33
34
35
36
37
38
39
40
41
...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
80
81
82
83
84
 
85
86
87
...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
108
109
 
110
111
112
...
115
116
117
118
119
120
 
121
122
123
 
 
 
124
125
126
127
128
129
130
131
...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
...
166
167
168
 
 
169
170
171
172
173
174
175
176
177
 
 
178
179
180
181
182
183
184
 
185
186
187
188
...
196
197
198
199
200
201
202
203
204
205
 
206
207
208
 
209
210
211
212
...
223
224
225
 
 
 
226
227
228
229
230
...
241
242
243
 
244
245
246
247
248
 
 
 
 
 
 
249
250
251
252
...
260
261
262
 
 
 
263
264
265
266
267
268
269
270
271
272
273
274
...
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
 
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
 
415
416
417
418
419
...
444
445
446
 
447
448
449
450
 
 
 
 
 
 
 
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
 
472
473
474
475
...
482
483
484
 
485
486
487
488
489
490
491
492
493
 
 
 
 
 
 
 
 
 
 
494
495
496
 
 
 
497
498
499
 
 
500
501
 
502
503
504
505
...
508
509
510
 
 
 
 
 
 
 
 
 
 
 
 
511
512
 
 
 
 
 
 
513
514
515
516
517
518
519
520
521
522
523
524
 
525
526
527
528
 
 
 
 
 
529
530
531
532
533
534
 
 
 
 
 
 
 
 
 
535
536
537
538
539
540
541
542
543
 
544
545
546
547
548
549
 
 
 
 
 
 
 
550
551
552
553
554
555
556
557
558
559
560
561
562
 
563
564
 
 
 
565
566
567
568
569
570
571
572
573
 
 
 
 
 
 
 
 
 
 
 
 
 
574
575
576
577
578
 
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
...
632
633
634
 
 
 
 
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
0
@@ -11,20 +11,31 @@
0
 unless RUBY_VERSION > "1.8.2"
0
   $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "mechanize", "net-overrides")
0
 end
0
+
0
 require 'net/http'
0
 require 'net/https'
0
 
0
+# Monkey patch for ruby 1.8.4
0
+unless RUBY_VERSION > "1.8.4"
0
+module Net # :nodoc:
0
+ class HTTPResponse # :nodoc:
0
+ CODE_TO_OBJ['500'] = HTTPInternalServerError
0
+ end
0
+end
0
+end
0
+
0
 require 'uri'
0
 require 'webrick/httputils'
0
 require 'zlib'
0
 require 'stringio'
0
-require 'mechanize/hpricot'
0
-require 'mechanize/mech_version'
0
+require 'digest/md5'
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
 class Mechanize
0
+ ##
0
+ # The version of Mechanize you are using.
0
+
0
+ VERSION = '0.6.11'
0
+
0
+ ##
0
+ # User Agent aliases
0
   AGENT_ALIASES = {
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/#{Version} (http://rubyforge.org/projects/mechanize/)"
0
+ 'Mechanize' => "WWW-Mechanize/#{VERSION} (http://rubyforge.org/projects/mechanize/)"
0
   }
0
 
0
   attr_accessor :cookie_jar
0
   attr_accessor :log
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 :key
0
   attr_accessor :cert
0
   attr_accessor :pass
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
 
0
   attr_reader :history
0
   attr_reader :pluggable_parser
0
 
0
+ alias :follow_redirect? :redirect_ok
0
+
0
+ @@nonce_count = -1
0
+ CNONCE = Digest::MD5.hexdigest("%x" % (Time.now.to_i + rand(65535)))
0
+
0
   def initialize
0
     # attr_accessors
0
- @cookie_jar = CookieJar.new
0
+ @cookie_jar = CookieJar.new
0
     @log = nil
0
- @max_history = nil
0
     @open_timeout = nil
0
     @read_timeout = nil
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
     
0
     # attr_readers
0
- @history = []
0
+ @history = WWW::Mechanize::History.new
0
     @pluggable_parser = PluggableParser.new
0
 
0
- # Basic Auth variables
0
- @user = nil # Basic Auth User
0
- @password = nil # Basic Auth Password
0
+ # Auth variables
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
 
0
     # Proxy settings
0
     @proxy_addr = nil
0
@@ -103,9 +133,21 @@ class Mechanize
0
     @proxy_port = nil
0
     @proxy_user = nil
0
 
0
+ @conditional_requests = true
0
+
0
+ @follow_meta_refresh = false
0
+
0
+ # Connection Cache & Keep alive
0
+ @connection_cache = {}
0
+ @keep_alive_time = 300
0
+ @keep_alive = true
0
+
0
     yield self if block_given?
0
   end
0
 
0
+ def max_history=(length); @history.max_size = length; end
0
+ def max_history; @history.max_size; end
0
+
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
 
0
   # Sets the user and password to be used for basic authentication.
0
   def basic_auth(user, password)
0
- @user = user
0
- @password = password
0
+ auth(user, password)
0
+ end
0
+
0
+ def auth(user, password)
0
+ @user = user
0
+ @password = password
0
   end
0
 
0
   # Fetches the URL passed in and returns a page.
0
- def get(url)
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
 
0
     # fetch the page
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
     add_to_history(page)
0
     page
0
   end
0
@@ -149,10 +196,17 @@ class Mechanize
0
   # Clicks the WWW::Mechanize::Link object passed in and returns the
0
   # page fetched.
0
   def click(link)
0
+ referer =
0
+ begin
0
+ link.page
0
+ rescue
0
+ nil
0
+ end
0
     uri = to_absolute_uri(
0
- link.attributes['href'] || link.attributes['src'] || link.href
0
+ link.attributes['href'] || link.attributes['src'] || link.href,
0
+ referer || current_page()
0
     )
0
- get(uri)
0
+ get(uri, referer)
0
   end
0
 
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 = {}
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
 
0
     form = Form.new(node)
0
     query.each { |k,v|
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
     when 'POST'
0
       post_form(uri, form)
0
     when 'GET'
0
- if uri.query.nil?
0
- uri.query = WWW::Mechanize.build_query_string(form.build_query)
0
- else
0
- uri.query = uri.query + "&" +
0
- WWW::Mechanize.build_query_string(form.build_query)
0
- end
0
+ uri.query = WWW::Mechanize.build_query_string(form.build_query)
0
       get(uri)
0
     else
0
       raise "unsupported method: #{form.method.upcase}"
0
@@ -212,9 +260,15 @@ class Mechanize
0
 
0
   # Returns whether or not a url has been visited
0
   def visited?(url)
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
+ end
0
+
0
+ # Returns a visited page for the url passed in, otherwise nil
0
+ def visited_page(url)
0
+ if url.respond_to? :href
0
+ url = url.href
0
+ end
0
+ @history.visited_page(to_absolute_uri(url))
0
   end
0
 
0
   # Runs given block, then resets the page history as it was before. self is
0
@@ -230,22 +284,136 @@ class Mechanize
0
 
0
   alias :page :current_page
0
 
0
+ protected
0
+ def set_headers(uri, request, cur_page)
0
+ if @keep_alive
0
+ request.add_field('Connection', 'keep-alive')
0
+ request.add_field('Keep-Alive', keep_alive_time.to_s)
0
+ else
0
+ request.add_field('Connection', 'close')
0
+ end
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
+
0
+ unless @cookie_jar.empty?(uri)
0
+ cookies = @cookie_jar.cookies(uri)
0
+ cookie = cookies.length > 0 ? cookies.join("; ") : nil
0
+ if log
0
+ cookies.each do |c|
0
+ log.debug("using cookie: #{c}")
0
+ end
0
+ end
0
+ request.add_field('Cookie', cookie)
0
+ end
0
+
0
+ # Add Referer header to request
0
+ unless cur_page.uri.nil?
0
+ request.add_field('Referer', cur_page.uri.to_s)
0
+ end
0
+
0
+ # Add User-Agent header to request
0
+ request.add_field('User-Agent', @user_agent) if @user_agent
0
+
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
+ end
0
+ end
0
+
0
+ if( @auth_hash[uri.host] )
0
+ case @auth_hash[uri.host]
0
+ when :basic
0
+ request.basic_auth(@user, @password)
0
+ when :digest
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
+ end
0
+ end
0
+
0
+ request
0
+ end
0
+
0
+ def gen_auth_header(uri, request, auth_header, is_IIS = false)
0
+ @@nonce_count += 1
0
+
0
+ user = @digest_user
0
+ password = @digest_password
0
+
0
+ auth_header =~ /^(\w+) (.*)/
0
+
0
+ params = {}
0
+ $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
0
+
0
+ a_1 = "#{@user}:#{params['realm']}:#{@password}"
0
+ a_2 = "#{request.method}:#{uri.path}"
0
+ request_digest = ''
0
+ request_digest << Digest::MD5.hexdigest(a_1)
0
+ request_digest << ':' << params['nonce']
0
+ request_digest << ':' << ('%08x' % @@nonce_count)
0
+ request_digest << ':' << CNONCE
0
+ request_digest << ':' << params['qop']
0
+ request_digest << ':' << Digest::MD5.hexdigest(a_2)
0
+
0
+ header = ''
0
+ header << "Digest username=\"#{@user}\", "
0
+ header << "realm=\"#{params['realm']}\", "
0
+ if is_IIS then
0
+ header << "qop=\"#{params['qop']}\", "
0
+ else
0
+ header << "qop=#{params['qop']}, "
0
+ end
0
+ header << "uri=\"#{uri.path}\", "
0
+ header << "algorithm=MD5, "
0
+ header << "nonce=\&q