mechanize-ntlm 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +480 -0
- data/EXAMPLES.rdoc +171 -0
- data/FAQ.rdoc +11 -0
- data/GUIDE.rdoc +122 -0
- data/LICENSE.rdoc +340 -0
- data/Manifest.txt +169 -0
- data/README.rdoc +60 -0
- data/Rakefile +44 -0
- data/examples/flickr_upload.rb +23 -0
- data/examples/mech-dump.rb +7 -0
- data/examples/proxy_req.rb +9 -0
- data/examples/rubyforge.rb +21 -0
- data/examples/spider.rb +11 -0
- data/lib/mechanize-ntlm.rb +7 -0
- data/lib/www/mechanize.rb +582 -0
- data/lib/www/mechanize/chain.rb +34 -0
- data/lib/www/mechanize/chain/auth_headers.rb +82 -0
- data/lib/www/mechanize/chain/body_decoding_handler.rb +43 -0
- data/lib/www/mechanize/chain/connection_resolver.rb +78 -0
- data/lib/www/mechanize/chain/custom_headers.rb +23 -0
- data/lib/www/mechanize/chain/handler.rb +9 -0
- data/lib/www/mechanize/chain/header_resolver.rb +48 -0
- data/lib/www/mechanize/chain/parameter_resolver.rb +23 -0
- data/lib/www/mechanize/chain/post_connect_hook.rb +0 -0
- data/lib/www/mechanize/chain/pre_connect_hook.rb +22 -0
- data/lib/www/mechanize/chain/request_resolver.rb +32 -0
- data/lib/www/mechanize/chain/response_body_parser.rb +40 -0
- data/lib/www/mechanize/chain/response_header_handler.rb +51 -0
- data/lib/www/mechanize/chain/response_reader.rb +41 -0
- data/lib/www/mechanize/chain/ssl_resolver.rb +36 -0
- data/lib/www/mechanize/chain/uri_resolver.rb +73 -0
- data/lib/www/mechanize/content_type_error.rb +16 -0
- data/lib/www/mechanize/cookie.rb +72 -0
- data/lib/www/mechanize/cookie_jar.rb +191 -0
- data/lib/www/mechanize/file.rb +73 -0
- data/lib/www/mechanize/file_response.rb +62 -0
- data/lib/www/mechanize/file_saver.rb +39 -0
- data/lib/www/mechanize/form.rb +359 -0
- data/lib/www/mechanize/form/button.rb +8 -0
- data/lib/www/mechanize/form/check_box.rb +13 -0
- data/lib/www/mechanize/form/field.rb +28 -0
- data/lib/www/mechanize/form/file_upload.rb +24 -0
- data/lib/www/mechanize/form/image_button.rb +23 -0
- data/lib/www/mechanize/form/multi_select_list.rb +69 -0
- data/lib/www/mechanize/form/option.rb +51 -0
- data/lib/www/mechanize/form/radio_button.rb +38 -0
- data/lib/www/mechanize/form/select_list.rb +45 -0
- data/lib/www/mechanize/headers.rb +12 -0
- data/lib/www/mechanize/history.rb +67 -0
- data/lib/www/mechanize/inspect.rb +90 -0
- data/lib/www/mechanize/monkey_patch.rb +37 -0
- data/lib/www/mechanize/page.rb +145 -0
- data/lib/www/mechanize/page/base.rb +10 -0
- data/lib/www/mechanize/page/frame.rb +22 -0
- data/lib/www/mechanize/page/link.rb +50 -0
- data/lib/www/mechanize/page/meta.rb +10 -0
- data/lib/www/mechanize/pluggable_parsers.rb +103 -0
- data/lib/www/mechanize/redirect_limit_reached_error.rb +18 -0
- data/lib/www/mechanize/redirect_not_get_or_head_error.rb +20 -0
- data/lib/www/mechanize/response_code_error.rb +25 -0
- data/lib/www/mechanize/unsupported_scheme_error.rb +10 -0
- data/lib/www/mechanize/util.rb +76 -0
- data/lib/www/ntlm-http/lib/net/ntlm_http.rb +854 -0
- data/mechanize.gemspec +24 -0
- data/test/chain/test_argument_validator.rb +14 -0
- data/test/chain/test_custom_headers.rb +18 -0
- data/test/chain/test_parameter_resolver.rb +35 -0
- data/test/chain/test_request_resolver.rb +29 -0
- data/test/chain/test_response_reader.rb +24 -0
- data/test/data/htpasswd +1 -0
- data/test/data/server.crt +16 -0
- data/test/data/server.csr +12 -0
- data/test/data/server.key +15 -0
- data/test/data/server.pem +15 -0
- data/test/helper.rb +127 -0
- data/test/htdocs/alt_text.html +10 -0
- data/test/htdocs/bad_form_test.html +9 -0
- data/test/htdocs/button.jpg +0 -0
- data/test/htdocs/empty_form.html +6 -0
- data/test/htdocs/file_upload.html +26 -0
- data/test/htdocs/find_link.html +41 -0
- data/test/htdocs/form_multi_select.html +16 -0
- data/test/htdocs/form_multival.html +37 -0
- data/test/htdocs/form_no_action.html +18 -0
- data/test/htdocs/form_no_input_name.html +16 -0
- data/test/htdocs/form_select.html +16 -0
- data/test/htdocs/form_select_all.html +16 -0
- data/test/htdocs/form_select_none.html +17 -0
- data/test/htdocs/form_select_noopts.html +10 -0
- data/test/htdocs/form_set_fields.html +14 -0
- data/test/htdocs/form_test.html +188 -0
- data/test/htdocs/frame_test.html +30 -0
- data/test/htdocs/google.html +13 -0
- data/test/htdocs/iframe_test.html +16 -0
- data/test/htdocs/index.html +6 -0
- data/test/htdocs/link with space.html +5 -0
- data/test/htdocs/meta_cookie.html +11 -0
- data/test/htdocs/no_title_test.html +6 -0
- data/test/htdocs/relative/tc_relative_links.html +21 -0
- data/test/htdocs/tc_bad_links.html +5 -0
- data/test/htdocs/tc_base_link.html +8 -0
- data/test/htdocs/tc_blank_form.html +11 -0
- data/test/htdocs/tc_checkboxes.html +19 -0
- data/test/htdocs/tc_encoded_links.html +5 -0
- data/test/htdocs/tc_follow_meta.html +8 -0
- data/test/htdocs/tc_form_action.html +48 -0
- data/test/htdocs/tc_links.html +18 -0
- data/test/htdocs/tc_no_attributes.html +16 -0
- data/test/htdocs/tc_pretty_print.html +17 -0
- data/test/htdocs/tc_radiobuttons.html +17 -0
- data/test/htdocs/tc_referer.html +10 -0
- data/test/htdocs/tc_relative_links.html +19 -0
- data/test/htdocs/tc_textarea.html +23 -0
- data/test/htdocs/unusual______.html +5 -0
- data/test/servlets.rb +339 -0
- data/test/ssl_server.rb +48 -0
- data/test/test_authenticate.rb +71 -0
- data/test/test_bad_links.rb +25 -0
- data/test/test_blank_form.rb +16 -0
- data/test/test_checkboxes.rb +61 -0
- data/test/test_content_type.rb +13 -0
- data/test/test_cookie_class.rb +338 -0
- data/test/test_cookie_jar.rb +343 -0
- data/test/test_cookies.rb +123 -0
- data/test/test_encoded_links.rb +20 -0
- data/test/test_errors.rb +49 -0
- data/test/test_follow_meta.rb +69 -0
- data/test/test_form_action.rb +44 -0
- data/test/test_form_as_hash.rb +61 -0
- data/test/test_form_button.rb +38 -0
- data/test/test_form_no_inputname.rb +15 -0
- data/test/test_forms.rb +575 -0
- data/test/test_frames.rb +25 -0
- data/test/test_get_headers.rb +52 -0
- data/test/test_gzipping.rb +22 -0
- data/test/test_hash_api.rb +45 -0
- data/test/test_history.rb +142 -0
- data/test/test_history_added.rb +16 -0
- data/test/test_html_unscape_forms.rb +39 -0
- data/test/test_if_modified_since.rb +20 -0
- data/test/test_keep_alive.rb +31 -0
- data/test/test_links.rb +120 -0
- data/test/test_mech.rb +259 -0
- data/test/test_mechanize_file.rb +47 -0
- data/test/test_multi_select.rb +106 -0
- data/test/test_no_attributes.rb +13 -0
- data/test/test_option.rb +18 -0
- data/test/test_page.rb +67 -0
- data/test/test_pluggable_parser.rb +145 -0
- data/test/test_post_form.rb +34 -0
- data/test/test_pretty_print.rb +22 -0
- data/test/test_radiobutton.rb +75 -0
- data/test/test_redirect_limit_reached.rb +41 -0
- data/test/test_redirect_verb_handling.rb +45 -0
- data/test/test_referer.rb +39 -0
- data/test/test_relative_links.rb +40 -0
- data/test/test_request.rb +13 -0
- data/test/test_response_code.rb +52 -0
- data/test/test_save_file.rb +48 -0
- data/test/test_scheme.rb +48 -0
- data/test/test_select.rb +106 -0
- data/test/test_select_all.rb +15 -0
- data/test/test_select_none.rb +15 -0
- data/test/test_select_noopts.rb +16 -0
- data/test/test_set_fields.rb +44 -0
- data/test/test_ssl_server.rb +20 -0
- data/test/test_subclass.rb +14 -0
- data/test/test_textarea.rb +45 -0
- data/test/test_upload.rb +109 -0
- data/test/test_verbs.rb +25 -0
- metadata +284 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
module WWW
|
2
|
+
class Mechanize
|
3
|
+
# = Synopsis
|
4
|
+
# This is the default (and base) class for the Pluggable Parsers. If
|
5
|
+
# Mechanize cannot find an appropriate class to use for the content type,
|
6
|
+
# this class will be used. For example, if you download a JPG, Mechanize
|
7
|
+
# will not know how to parse it, so this class will be instantiated.
|
8
|
+
#
|
9
|
+
# This is a good class to use as the base class for building your own
|
10
|
+
# pluggable parsers.
|
11
|
+
#
|
12
|
+
# == Example
|
13
|
+
# require 'rubygems'
|
14
|
+
# require 'mechanize'
|
15
|
+
#
|
16
|
+
# agent = WWW::Mechanize.new
|
17
|
+
# agent.get('http://example.com/foo.jpg').class #=> WWW::Mechanize::File
|
18
|
+
#
|
19
|
+
class File
|
20
|
+
attr_accessor :uri, :response, :body, :code, :filename
|
21
|
+
alias :header :response
|
22
|
+
|
23
|
+
alias :content :body
|
24
|
+
|
25
|
+
def initialize(uri=nil, response=nil, body=nil, code=nil)
|
26
|
+
@uri, @body, @code = uri, body, code
|
27
|
+
@response = Headers.new
|
28
|
+
|
29
|
+
# Copy the headers in to a hash to prevent memory leaks
|
30
|
+
if response
|
31
|
+
response.each { |k,v|
|
32
|
+
@response[k] = v
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
@filename = 'index.html'
|
37
|
+
|
38
|
+
# Set the filename
|
39
|
+
if disposition = @response['content-disposition']
|
40
|
+
disposition.split(/;\s*/).each do |pair|
|
41
|
+
k,v = pair.split(/=/, 2)
|
42
|
+
@filename = v if k.downcase == 'filename'
|
43
|
+
end
|
44
|
+
else
|
45
|
+
if @uri
|
46
|
+
@filename = @uri.path.split(/\//).last || 'index.html'
|
47
|
+
@filename << ".html" unless @filename =~ /\./
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
yield self if block_given?
|
52
|
+
end
|
53
|
+
|
54
|
+
# Use this method to save the content of this object to filename
|
55
|
+
def save_as(filename = nil)
|
56
|
+
if filename.nil?
|
57
|
+
filename = @filename
|
58
|
+
number = 1
|
59
|
+
while(::File.exists?(filename))
|
60
|
+
filename = "#{@filename}.#{number}"
|
61
|
+
number += 1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
::File::open(filename, "wb") { |f|
|
66
|
+
f.write body
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
alias :save :save_as
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module WWW
|
2
|
+
class Mechanize
|
3
|
+
###
|
4
|
+
# Fake response for dealing with file:/// requests
|
5
|
+
class FileResponse
|
6
|
+
def initialize(file_path)
|
7
|
+
@file_path = file_path
|
8
|
+
end
|
9
|
+
|
10
|
+
def read_body
|
11
|
+
if ::File.exists?(@file_path)
|
12
|
+
if directory?
|
13
|
+
yield dir_body
|
14
|
+
else
|
15
|
+
yield ::File.read(@file_path)
|
16
|
+
end
|
17
|
+
else
|
18
|
+
yield ''
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def code
|
23
|
+
::File.exists?(@file_path) ? 200 : 400
|
24
|
+
end
|
25
|
+
|
26
|
+
def content_length
|
27
|
+
return dir_body.length if directory?
|
28
|
+
::File.exists?(@file_path) ? ::File.stat(@file_path).size : 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def each_header; end
|
32
|
+
|
33
|
+
def [](key)
|
34
|
+
return nil unless key.downcase == 'content-type'
|
35
|
+
return 'text/html' if directory?
|
36
|
+
return 'text/html' if ['.html', '.xhtml'].any? { |extn|
|
37
|
+
@file_path =~ /#{extn}$/
|
38
|
+
}
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def each
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_fields(key)
|
46
|
+
[]
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def dir_body
|
51
|
+
'<html><body>' +
|
52
|
+
Dir[::File.join(@file_path, '*')].map { |f|
|
53
|
+
"<a href=\"file://#{f}\">#{::File.basename(f)}</a>"
|
54
|
+
}.join("\n") + '</body></html>'
|
55
|
+
end
|
56
|
+
|
57
|
+
def directory?
|
58
|
+
::File.directory?(@file_path)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module WWW
|
2
|
+
class Mechanize
|
3
|
+
# = Synopsis
|
4
|
+
# This is a pluggable parser that automatically saves every file
|
5
|
+
# it encounters. It saves the files as a tree, reflecting the
|
6
|
+
# host and file path.
|
7
|
+
#
|
8
|
+
# == Example to save all PDF's
|
9
|
+
# require 'rubygems'
|
10
|
+
# require 'mechanize'
|
11
|
+
#
|
12
|
+
# agent = WWW::Mechanize.new
|
13
|
+
# agent.pluggable_parser.pdf = WWW::Mechanize::FileSaver
|
14
|
+
# agent.get('http://example.com/foo.pdf')
|
15
|
+
#
|
16
|
+
class FileSaver < File
|
17
|
+
attr_reader :filename
|
18
|
+
|
19
|
+
def initialize(uri=nil, response=nil, body=nil, code=nil)
|
20
|
+
super(uri, response, body, code)
|
21
|
+
path = uri.path.empty? ? 'index.html' : uri.path.gsub(/^[\/]*/, '')
|
22
|
+
path += 'index.html' if path =~ /\/$/
|
23
|
+
|
24
|
+
split_path = path.split(/\//)
|
25
|
+
filename = split_path.length > 0 ? split_path.pop : 'index.html'
|
26
|
+
joined_path = split_path.join(::File::SEPARATOR)
|
27
|
+
path = if joined_path.empty?
|
28
|
+
uri.host
|
29
|
+
else
|
30
|
+
"#{uri.host}#{::File::SEPARATOR}#{joined_path}"
|
31
|
+
end
|
32
|
+
|
33
|
+
@filename = "#{path}#{::File::SEPARATOR}#{filename}"
|
34
|
+
FileUtils.mkdir_p(path)
|
35
|
+
save_as(@filename)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,359 @@
|
|
1
|
+
require 'www/mechanize/form/field'
|
2
|
+
require 'www/mechanize/form/file_upload'
|
3
|
+
require 'www/mechanize/form/button'
|
4
|
+
require 'www/mechanize/form/image_button'
|
5
|
+
require 'www/mechanize/form/radio_button'
|
6
|
+
require 'www/mechanize/form/check_box'
|
7
|
+
require 'www/mechanize/form/multi_select_list'
|
8
|
+
require 'www/mechanize/form/select_list'
|
9
|
+
require 'www/mechanize/form/option'
|
10
|
+
|
11
|
+
module WWW
|
12
|
+
class Mechanize
|
13
|
+
# =Synopsis
|
14
|
+
# This class encapsulates a form parsed out of an HTML page. Each type
|
15
|
+
# of input fields available in a form can be accessed through this object.
|
16
|
+
# See GlobalForm for more methods.
|
17
|
+
#
|
18
|
+
# ==Example
|
19
|
+
# Find a form and print out its fields
|
20
|
+
# form = page.forms.first # => WWW::Mechanize::Form
|
21
|
+
# form.fields.each { |f| puts f.name }
|
22
|
+
# Set the input field 'name' to "Aaron"
|
23
|
+
# form['name'] = 'Aaron'
|
24
|
+
# puts form['name']
|
25
|
+
class Form
|
26
|
+
attr_accessor :method, :action, :name
|
27
|
+
|
28
|
+
attr_reader :fields, :buttons, :file_uploads, :radiobuttons, :checkboxes
|
29
|
+
attr_accessor :enctype
|
30
|
+
|
31
|
+
alias :elements :fields
|
32
|
+
|
33
|
+
attr_reader :form_node
|
34
|
+
attr_reader :page
|
35
|
+
|
36
|
+
def initialize(node, mech=nil, page=nil)
|
37
|
+
@enctype = node['enctype'] || 'application/x-www-form-urlencoded'
|
38
|
+
@form_node = node
|
39
|
+
@action = Util.html_unescape(node['action'])
|
40
|
+
@method = (node['method'] || 'GET').upcase
|
41
|
+
@name = node['name']
|
42
|
+
@clicked_buttons = []
|
43
|
+
@page = page
|
44
|
+
@mech = mech
|
45
|
+
|
46
|
+
parse
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns whether or not the form contains a field with +field_name+
|
50
|
+
def has_field?(field_name)
|
51
|
+
! fields.find { |f| f.name.eql? field_name }.nil?
|
52
|
+
end
|
53
|
+
|
54
|
+
alias :has_key? :has_field?
|
55
|
+
|
56
|
+
def has_value?(value)
|
57
|
+
! fields.find { |f| f.value.eql? value }.nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
def keys; fields.map { |f| f.name }; end
|
61
|
+
|
62
|
+
def values; fields.map { |f| f.value }; end
|
63
|
+
|
64
|
+
# Add a field with +field_name+ and +value+
|
65
|
+
def add_field!(field_name, value = nil)
|
66
|
+
fields << Field.new(field_name, value)
|
67
|
+
end
|
68
|
+
|
69
|
+
# This method sets multiple fields on the form. It takes a list of field
|
70
|
+
# name, value pairs. If there is more than one field found with the
|
71
|
+
# same name, this method will set the first one found. If you want to
|
72
|
+
# set the value of a duplicate field, use a value which is a Hash with
|
73
|
+
# the key as the index in to the form. The index
|
74
|
+
# is zero based. For example, to set the second field named 'foo', you
|
75
|
+
# could do the following:
|
76
|
+
# form.set_fields( :foo => { 1 => 'bar' } )
|
77
|
+
def set_fields(fields = {})
|
78
|
+
fields.each do |k,v|
|
79
|
+
case v
|
80
|
+
when Hash
|
81
|
+
v.each do |index, value|
|
82
|
+
self.fields_with(:name => k.to_s).[](index).value = value
|
83
|
+
end
|
84
|
+
else
|
85
|
+
value = nil
|
86
|
+
index = 0
|
87
|
+
[v].flatten.each do |val|
|
88
|
+
index = val.to_i unless value.nil?
|
89
|
+
value = val if value.nil?
|
90
|
+
end
|
91
|
+
self.fields_with(:name => k.to_s).[](index).value = value
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Fetch the value of the first input field with the name passed in
|
97
|
+
# ==Example
|
98
|
+
# Fetch the value set in the input field 'name'
|
99
|
+
# puts form['name']
|
100
|
+
def [](field_name)
|
101
|
+
f = field(field_name)
|
102
|
+
f && f.value
|
103
|
+
end
|
104
|
+
|
105
|
+
# Set the value of the first input field with the name passed in
|
106
|
+
# ==Example
|
107
|
+
# Set the value in the input field 'name' to "Aaron"
|
108
|
+
# form['name'] = 'Aaron'
|
109
|
+
def []=(field_name, value)
|
110
|
+
f = field(field_name)
|
111
|
+
if f.nil?
|
112
|
+
add_field!(field_name, value)
|
113
|
+
else
|
114
|
+
f.value = value
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Treat form fields like accessors.
|
119
|
+
def method_missing(id,*args)
|
120
|
+
method = id.to_s.gsub(/=$/, '')
|
121
|
+
if field(method)
|
122
|
+
return field(method).value if args.empty?
|
123
|
+
return field(method).value = args[0]
|
124
|
+
end
|
125
|
+
super
|
126
|
+
end
|
127
|
+
|
128
|
+
# Submit this form with the button passed in
|
129
|
+
def submit button=nil, headers = {}
|
130
|
+
@mech.submit(self, button, headers)
|
131
|
+
end
|
132
|
+
|
133
|
+
# Submit form using +button+. Defaults
|
134
|
+
# to the first button.
|
135
|
+
def click_button(button = buttons.first)
|
136
|
+
submit(button)
|
137
|
+
end
|
138
|
+
|
139
|
+
# This method is sub-method of build_query.
|
140
|
+
# It converts charset of query value of fields into excepted one.
|
141
|
+
def proc_query(field)
|
142
|
+
field.query_value.map{|(name, val)|
|
143
|
+
[from_native_charset(name), from_native_charset(val.to_s)]
|
144
|
+
}
|
145
|
+
end
|
146
|
+
private :proc_query
|
147
|
+
|
148
|
+
def from_native_charset(str, enc=nil)
|
149
|
+
if page
|
150
|
+
enc ||= page.encoding
|
151
|
+
Util.from_native_charset(str,enc)
|
152
|
+
else
|
153
|
+
str
|
154
|
+
end
|
155
|
+
end
|
156
|
+
private :from_native_charset
|
157
|
+
|
158
|
+
# This method builds an array of arrays that represent the query
|
159
|
+
# parameters to be used with this form. The return value can then
|
160
|
+
# be used to create a query string for this form.
|
161
|
+
def build_query(buttons = [])
|
162
|
+
query = []
|
163
|
+
|
164
|
+
fields().each do |f|
|
165
|
+
qval = proc_query(f)
|
166
|
+
query.push(*qval)
|
167
|
+
end
|
168
|
+
|
169
|
+
checkboxes().each do |f|
|
170
|
+
if f.checked
|
171
|
+
qval = proc_query(f)
|
172
|
+
query.push(*qval)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
radio_groups = {}
|
177
|
+
radiobuttons().each do |f|
|
178
|
+
fname = from_native_charset(f.name)
|
179
|
+
radio_groups[fname] ||= []
|
180
|
+
radio_groups[fname] << f
|
181
|
+
end
|
182
|
+
|
183
|
+
# take one radio button from each group
|
184
|
+
radio_groups.each_value do |g|
|
185
|
+
checked = g.select {|f| f.checked}
|
186
|
+
|
187
|
+
if checked.size == 1
|
188
|
+
f = checked.first
|
189
|
+
qval = proc_query(f)
|
190
|
+
query.push(*qval)
|
191
|
+
elsif checked.size > 1
|
192
|
+
raise "multiple radiobuttons are checked in the same group!"
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
@clicked_buttons.each { |b|
|
197
|
+
qval = proc_query(b)
|
198
|
+
query.push(*qval)
|
199
|
+
}
|
200
|
+
query
|
201
|
+
end
|
202
|
+
|
203
|
+
# This method adds a button to the query. If the form needs to be
|
204
|
+
# submitted with multiple buttons, pass each button to this method.
|
205
|
+
def add_button_to_query(button)
|
206
|
+
@clicked_buttons << button
|
207
|
+
end
|
208
|
+
|
209
|
+
# This method calculates the request data to be sent back to the server
|
210
|
+
# for this form, depending on if this is a regular post, get, or a
|
211
|
+
# multi-part post,
|
212
|
+
def request_data
|
213
|
+
query_params = build_query()
|
214
|
+
case @enctype.downcase
|
215
|
+
when /^multipart\/form-data/
|
216
|
+
boundary = rand_string(20)
|
217
|
+
@enctype = "multipart/form-data; boundary=#{boundary}"
|
218
|
+
params = []
|
219
|
+
query_params.each { |k,v| params << param_to_multipart(k, v) unless k.nil? }
|
220
|
+
@file_uploads.each { |f| params << file_to_multipart(f) }
|
221
|
+
params.collect { |p| "--#{boundary}\r\n#{p}" }.join('') +
|
222
|
+
"--#{boundary}--\r\n"
|
223
|
+
else
|
224
|
+
WWW::Mechanize::Util.build_query_string(query_params)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# Removes all fields with name +field_name+.
|
229
|
+
def delete_field!(field_name)
|
230
|
+
@fields.delete_if{ |f| f.name == field_name}
|
231
|
+
end
|
232
|
+
|
233
|
+
{ :field => :fields,
|
234
|
+
:button => :buttons,
|
235
|
+
:file_upload => :file_uploads,
|
236
|
+
:radiobutton => :radiobuttons,
|
237
|
+
:checkbox => :checkboxes,
|
238
|
+
}.each do |singular,plural|
|
239
|
+
eval(<<-eomethod)
|
240
|
+
def #{plural}_with criteria = {}
|
241
|
+
criteria = {:name => criteria} if String === criteria
|
242
|
+
f = #{plural}.find_all do |thing|
|
243
|
+
criteria.all? { |k,v| v === thing.send(k) }
|
244
|
+
end
|
245
|
+
yield f if block_given?
|
246
|
+
f
|
247
|
+
end
|
248
|
+
|
249
|
+
def #{singular}_with criteria = {}
|
250
|
+
f = #{plural}_with(criteria).first
|
251
|
+
yield f if block_given?
|
252
|
+
f
|
253
|
+
end
|
254
|
+
alias :#{singular} :#{singular}_with
|
255
|
+
eomethod
|
256
|
+
end
|
257
|
+
|
258
|
+
private
|
259
|
+
def parse
|
260
|
+
@fields = []
|
261
|
+
@buttons = []
|
262
|
+
@file_uploads = []
|
263
|
+
@radiobuttons = []
|
264
|
+
@checkboxes = []
|
265
|
+
|
266
|
+
# Find all input tags
|
267
|
+
form_node.search('input').each do |node|
|
268
|
+
type = (node['type'] || 'text').downcase
|
269
|
+
name = node['name']
|
270
|
+
next if name.nil? && !(type == 'submit' || type =='button')
|
271
|
+
case type
|
272
|
+
when 'radio'
|
273
|
+
@radiobuttons << RadioButton.new(node['name'], node['value'], !!node['checked'], self)
|
274
|
+
when 'checkbox'
|
275
|
+
@checkboxes << CheckBox.new(node['name'], node['value'], !!node['checked'], self)
|
276
|
+
when 'file'
|
277
|
+
@file_uploads << FileUpload.new(node['name'], nil)
|
278
|
+
when 'submit'
|
279
|
+
@buttons << Button.new(node['name'], node['value'])
|
280
|
+
when 'button'
|
281
|
+
@buttons << Button.new(node['name'], node['value'])
|
282
|
+
when 'image'
|
283
|
+
@buttons << ImageButton.new(node['name'], node['value'])
|
284
|
+
else
|
285
|
+
@fields << Field.new(node['name'], node['value'] || '')
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
# Find all textarea tags
|
290
|
+
form_node.search('textarea').each do |node|
|
291
|
+
next if node['name'].nil?
|
292
|
+
@fields << Field.new(node['name'], node.inner_text)
|
293
|
+
end
|
294
|
+
|
295
|
+
# Find all select tags
|
296
|
+
form_node.search('select').each do |node|
|
297
|
+
next if node['name'].nil?
|
298
|
+
if node.has_attribute? 'multiple'
|
299
|
+
@fields << MultiSelectList.new(node['name'], node)
|
300
|
+
else
|
301
|
+
@fields << SelectList.new(node['name'], node)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
# Find all submit button tags
|
306
|
+
# FIXME: what can I do with the reset buttons?
|
307
|
+
form_node.search('button').each do |node|
|
308
|
+
type = (node['type'] || 'submit').downcase
|
309
|
+
next if type == 'reset'
|
310
|
+
@buttons << Button.new(node['name'], node['value'])
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
def rand_string(len = 10)
|
315
|
+
chars = ("a".."z").to_a + ("A".."Z").to_a
|
316
|
+
string = ""
|
317
|
+
1.upto(len) { |i| string << chars[rand(chars.size-1)] }
|
318
|
+
string
|
319
|
+
end
|
320
|
+
|
321
|
+
def mime_value_quote(str)
|
322
|
+
str.gsub(/(["\r\\])/){|s| '\\' + s}
|
323
|
+
end
|
324
|
+
|
325
|
+
def param_to_multipart(name, value)
|
326
|
+
return "Content-Disposition: form-data; name=\"" +
|
327
|
+
"#{mime_value_quote(name)}\"\r\n" +
|
328
|
+
"\r\n#{value}\r\n"
|
329
|
+
end
|
330
|
+
|
331
|
+
def file_to_multipart(file)
|
332
|
+
file_name = file.file_name ? ::File.basename(file.file_name) : ''
|
333
|
+
body = "Content-Disposition: form-data; name=\"" +
|
334
|
+
"#{mime_value_quote(file.name)}\"; " +
|
335
|
+
"filename=\"#{mime_value_quote(file_name)}\"\r\n" +
|
336
|
+
"Content-Transfer-Encoding: binary\r\n"
|
337
|
+
|
338
|
+
if file.file_data.nil? and ! file.file_name.nil?
|
339
|
+
file.file_data = ::File.open(file.file_name, "rb") { |f| f.read }
|
340
|
+
file.mime_type = WEBrick::HTTPUtils.mime_type(file.file_name,
|
341
|
+
WEBrick::HTTPUtils::DefaultMimeTypes)
|
342
|
+
end
|
343
|
+
|
344
|
+
if file.mime_type != nil
|
345
|
+
body << "Content-Type: #{file.mime_type}\r\n"
|
346
|
+
end
|
347
|
+
|
348
|
+
body <<
|
349
|
+
if file.file_data.respond_to? :read
|
350
|
+
"\r\n#{file.file_data.read}\r\n"
|
351
|
+
else
|
352
|
+
"\r\n#{file.file_data}\r\n"
|
353
|
+
end
|
354
|
+
|
355
|
+
body
|
356
|
+
end
|
357
|
+
end
|
358
|
+
end
|
359
|
+
end
|