glebtv-httpclient 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +108 -0
- data/bin/httpclient +65 -0
- data/lib/glebtv-httpclient.rb +1 -0
- data/lib/hexdump.rb +50 -0
- data/lib/http-access2/cookie.rb +1 -0
- data/lib/http-access2/http.rb +1 -0
- data/lib/http-access2.rb +55 -0
- data/lib/httpclient/auth.rb +899 -0
- data/lib/httpclient/cacert.p7s +1912 -0
- data/lib/httpclient/connection.rb +88 -0
- data/lib/httpclient/cookie.rb +438 -0
- data/lib/httpclient/http.rb +1050 -0
- data/lib/httpclient/include_client.rb +83 -0
- data/lib/httpclient/session.rb +1031 -0
- data/lib/httpclient/ssl_config.rb +403 -0
- data/lib/httpclient/timeout.rb +140 -0
- data/lib/httpclient/util.rb +186 -0
- data/lib/httpclient/version.rb +3 -0
- data/lib/httpclient.rb +1157 -0
- data/lib/oauthclient.rb +110 -0
- data/sample/async.rb +8 -0
- data/sample/auth.rb +11 -0
- data/sample/cookie.rb +18 -0
- data/sample/dav.rb +103 -0
- data/sample/howto.rb +49 -0
- data/sample/oauth_buzz.rb +57 -0
- data/sample/oauth_friendfeed.rb +59 -0
- data/sample/oauth_twitter.rb +61 -0
- data/sample/ssl/0cert.pem +22 -0
- data/sample/ssl/0key.pem +30 -0
- data/sample/ssl/1000cert.pem +19 -0
- data/sample/ssl/1000key.pem +18 -0
- data/sample/ssl/htdocs/index.html +10 -0
- data/sample/ssl/ssl_client.rb +22 -0
- data/sample/ssl/webrick_httpsd.rb +29 -0
- data/sample/stream.rb +21 -0
- data/sample/thread.rb +27 -0
- data/sample/wcat.rb +21 -0
- data/test/ca-chain.cert +44 -0
- data/test/ca.cert +23 -0
- data/test/client.cert +19 -0
- data/test/client.key +15 -0
- data/test/helper.rb +129 -0
- data/test/htdigest +1 -0
- data/test/htpasswd +2 -0
- data/test/runner.rb +2 -0
- data/test/server.cert +19 -0
- data/test/server.key +15 -0
- data/test/sslsvr.rb +65 -0
- data/test/subca.cert +21 -0
- data/test/test_auth.rb +321 -0
- data/test/test_cookie.rb +412 -0
- data/test/test_hexdump.rb +14 -0
- data/test/test_http-access2.rb +507 -0
- data/test/test_httpclient.rb +1801 -0
- data/test/test_include_client.rb +52 -0
- data/test/test_ssl.rb +235 -0
- metadata +102 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'httpclient/include_client'
|
5
|
+
class TestIncludeClient < Test::Unit::TestCase
|
6
|
+
class Widget
|
7
|
+
extend HTTPClient::IncludeClient
|
8
|
+
|
9
|
+
include_http_client("http://example.com") do |client|
|
10
|
+
client.cookie_manager = nil
|
11
|
+
client.agent_name = "iMonkey 4k"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class OtherWidget
|
16
|
+
extend HTTPClient::IncludeClient
|
17
|
+
|
18
|
+
include_http_client
|
19
|
+
include_http_client(:method_name => :other_http_client)
|
20
|
+
end
|
21
|
+
|
22
|
+
class UnrelatedBlankClass ; end
|
23
|
+
|
24
|
+
def test_client_class_level_singleton
|
25
|
+
assert_equal Widget.http_client.object_id, Widget.http_client.object_id
|
26
|
+
|
27
|
+
assert_equal Widget.http_client.object_id, Widget.new.http_client.object_id
|
28
|
+
|
29
|
+
assert_not_equal Widget.http_client.object_id, OtherWidget.http_client.object_id
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_configured
|
33
|
+
assert_equal Widget.http_client.agent_name, "iMonkey 4k"
|
34
|
+
assert_nil Widget.http_client.cookie_manager
|
35
|
+
assert_equal Widget.http_client.proxy.to_s, "http://example.com"
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_two_includes
|
39
|
+
assert_not_equal OtherWidget.http_client.object_id, OtherWidget.other_http_client.object_id
|
40
|
+
|
41
|
+
assert_equal OtherWidget.other_http_client.object_id, OtherWidget.new.other_http_client.object_id
|
42
|
+
end
|
43
|
+
|
44
|
+
# meta-programming gone wrong sometimes accidentally
|
45
|
+
# adds the class method to _everyone_, a mistake we've made before.
|
46
|
+
def test_not_infected_class_hieararchy
|
47
|
+
assert ! Class.respond_to?(:http_client)
|
48
|
+
assert ! UnrelatedBlankClass.respond_to?(:http_client)
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
end
|
data/test/test_ssl.rb
ADDED
@@ -0,0 +1,235 @@
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
2
|
+
require 'webrick/https'
|
3
|
+
|
4
|
+
|
5
|
+
class TestSSL < Test::Unit::TestCase
|
6
|
+
include Helper
|
7
|
+
DIR = File.dirname(File.expand_path(__FILE__))
|
8
|
+
|
9
|
+
def setup
|
10
|
+
super
|
11
|
+
@serverpid = @client = nil
|
12
|
+
@verify_callback_called = false
|
13
|
+
@verbose, $VERBOSE = $VERBOSE, nil
|
14
|
+
setup_server
|
15
|
+
setup_client
|
16
|
+
@url = "https://localhost:#{serverport}/hello"
|
17
|
+
end
|
18
|
+
|
19
|
+
def teardown
|
20
|
+
super
|
21
|
+
$VERBOSE = @verbose
|
22
|
+
end
|
23
|
+
|
24
|
+
def path(filename)
|
25
|
+
File.expand_path(filename, DIR)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_options
|
29
|
+
cfg = @client.ssl_config
|
30
|
+
assert_nil(cfg.client_cert)
|
31
|
+
assert_nil(cfg.client_key)
|
32
|
+
assert_nil(cfg.client_ca)
|
33
|
+
assert_equal(OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT, cfg.verify_mode)
|
34
|
+
assert_nil(cfg.verify_callback)
|
35
|
+
assert_nil(cfg.timeout)
|
36
|
+
assert_equal(OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_SSLv2, cfg.options)
|
37
|
+
assert_equal("ALL:!aNULL:!eNULL:!SSLv2", cfg.ciphers)
|
38
|
+
assert_instance_of(OpenSSL::X509::Store, cfg.cert_store)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_sync
|
42
|
+
cfg = @client.ssl_config
|
43
|
+
cfg.set_client_cert_file(path('client.cert'), path('client.key'))
|
44
|
+
cfg.add_trust_ca(path('ca.cert'))
|
45
|
+
cfg.add_trust_ca(path('subca.cert'))
|
46
|
+
assert_equal("hello", @client.get_content(@url))
|
47
|
+
|
48
|
+
@client.socket_sync = false
|
49
|
+
@client.reset_all
|
50
|
+
assert_equal("hello", @client.get_content(@url))
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_debug_dev
|
54
|
+
str = @client.debug_dev = ''
|
55
|
+
cfg = @client.ssl_config
|
56
|
+
cfg.client_cert = cert("client.cert")
|
57
|
+
cfg.client_key = key("client.key")
|
58
|
+
cfg.add_trust_ca(path('ca.cert'))
|
59
|
+
cfg.add_trust_ca(path('subca.cert'))
|
60
|
+
assert_equal("hello", @client.get_content(@url))
|
61
|
+
assert(str.scan(/^hello$/)[0])
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_verification
|
65
|
+
cfg = @client.ssl_config
|
66
|
+
cfg.verify_callback = method(:verify_callback).to_proc
|
67
|
+
begin
|
68
|
+
@verify_callback_called = false
|
69
|
+
@client.get(@url)
|
70
|
+
assert(false)
|
71
|
+
rescue OpenSSL::SSL::SSLError => ssle
|
72
|
+
assert_match(/certificate verify failed/, ssle.message)
|
73
|
+
assert(@verify_callback_called)
|
74
|
+
end
|
75
|
+
#
|
76
|
+
cfg.client_cert = cert("client.cert")
|
77
|
+
cfg.client_key = key("client.key")
|
78
|
+
@verify_callback_called = false
|
79
|
+
begin
|
80
|
+
@client.get(@url)
|
81
|
+
assert(false)
|
82
|
+
rescue OpenSSL::SSL::SSLError => ssle
|
83
|
+
assert_match(/certificate verify failed/, ssle.message)
|
84
|
+
assert(@verify_callback_called)
|
85
|
+
end
|
86
|
+
#
|
87
|
+
cfg.add_trust_ca(path('ca.cert'))
|
88
|
+
@verify_callback_called = false
|
89
|
+
begin
|
90
|
+
@client.get(@url)
|
91
|
+
assert(false)
|
92
|
+
rescue OpenSSL::SSL::SSLError => ssle
|
93
|
+
assert_match(/certificate verify failed/, ssle.message)
|
94
|
+
assert(@verify_callback_called)
|
95
|
+
end
|
96
|
+
#
|
97
|
+
cfg.add_trust_ca(path('subca.cert'))
|
98
|
+
@verify_callback_called = false
|
99
|
+
assert_equal("hello", @client.get_content(@url))
|
100
|
+
assert(@verify_callback_called)
|
101
|
+
#
|
102
|
+
unless ENV['TRAVIS']
|
103
|
+
# On travis environment, verify_depth seems to not work properly.
|
104
|
+
# Ubuntu 10.04 + OpenSSL 0.9.8k issue?
|
105
|
+
cfg.verify_depth = 1 # 2 required: root-sub
|
106
|
+
@verify_callback_called = false
|
107
|
+
begin
|
108
|
+
@client.get(@url)
|
109
|
+
assert(false, "verify_depth is not supported? #{OpenSSL::OPENSSL_VERSION}")
|
110
|
+
rescue OpenSSL::SSL::SSLError => ssle
|
111
|
+
assert_match(/certificate verify failed/, ssle.message)
|
112
|
+
assert(@verify_callback_called)
|
113
|
+
end
|
114
|
+
#
|
115
|
+
cfg.verify_depth = 2 # 2 required: root-sub
|
116
|
+
@verify_callback_called = false
|
117
|
+
@client.get(@url)
|
118
|
+
assert(@verify_callback_called)
|
119
|
+
#
|
120
|
+
end
|
121
|
+
cfg.verify_depth = nil
|
122
|
+
cfg.cert_store = OpenSSL::X509::Store.new
|
123
|
+
cfg.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
124
|
+
begin
|
125
|
+
@client.get_content(@url)
|
126
|
+
assert(false)
|
127
|
+
rescue OpenSSL::SSL::SSLError => ssle
|
128
|
+
assert_match(/certificate verify failed/, ssle.message)
|
129
|
+
end
|
130
|
+
#
|
131
|
+
cfg.verify_mode = nil
|
132
|
+
assert_equal("hello", @client.get_content(@url))
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_ciphers
|
136
|
+
cfg = @client.ssl_config
|
137
|
+
cfg.set_client_cert_file(path('client.cert'), path('client.key'))
|
138
|
+
cfg.add_trust_ca(path('ca.cert'))
|
139
|
+
cfg.add_trust_ca(path('subca.cert'))
|
140
|
+
cfg.timeout = 123
|
141
|
+
assert_equal("hello", @client.get_content(@url))
|
142
|
+
#
|
143
|
+
cfg.ciphers = "!ALL"
|
144
|
+
begin
|
145
|
+
@client.get(@url)
|
146
|
+
assert(false)
|
147
|
+
rescue OpenSSL::SSL::SSLError => ssle
|
148
|
+
assert_match(/no cipher match/, ssle.message)
|
149
|
+
end
|
150
|
+
#
|
151
|
+
cfg.ciphers = "ALL"
|
152
|
+
assert_equal("hello", @client.get_content(@url))
|
153
|
+
#
|
154
|
+
cfg.ciphers = "DEFAULT"
|
155
|
+
assert_equal("hello", @client.get_content(@url))
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_set_default_paths
|
159
|
+
assert_raise(OpenSSL::SSL::SSLError) do
|
160
|
+
@client.get(@url)
|
161
|
+
end
|
162
|
+
escape_env do
|
163
|
+
ENV['SSL_CERT_FILE'] = File.join(DIR, 'ca-chain.cert')
|
164
|
+
@client.ssl_config.set_default_paths
|
165
|
+
@client.get(@url)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
|
171
|
+
def cert(filename)
|
172
|
+
OpenSSL::X509::Certificate.new(File.read(File.join(DIR, filename)))
|
173
|
+
end
|
174
|
+
|
175
|
+
def key(filename)
|
176
|
+
OpenSSL::PKey::RSA.new(File.read(File.join(DIR, filename)))
|
177
|
+
end
|
178
|
+
|
179
|
+
def q(str)
|
180
|
+
%Q["#{str}"]
|
181
|
+
end
|
182
|
+
|
183
|
+
def setup_server
|
184
|
+
logger = Logger.new(STDERR)
|
185
|
+
logger.level = Logger::Severity::FATAL # avoid logging SSLError (ERROR level)
|
186
|
+
@server = WEBrick::HTTPServer.new(
|
187
|
+
:BindAddress => "localhost",
|
188
|
+
:Logger => logger,
|
189
|
+
:Port => 0,
|
190
|
+
:AccessLog => [],
|
191
|
+
:DocumentRoot => DIR,
|
192
|
+
:SSLEnable => true,
|
193
|
+
:SSLCACertificateFile => File.join(DIR, 'ca.cert'),
|
194
|
+
:SSLCertificate => cert('server.cert'),
|
195
|
+
:SSLPrivateKey => key('server.key'),
|
196
|
+
:SSLVerifyClient => nil, #OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT|OpenSSL::SSL::VERIFY_PEER,
|
197
|
+
:SSLClientCA => cert('ca.cert'),
|
198
|
+
:SSLCertName => nil
|
199
|
+
)
|
200
|
+
@serverport = @server.config[:Port]
|
201
|
+
[:hello].each do |sym|
|
202
|
+
@server.mount(
|
203
|
+
"/#{sym}",
|
204
|
+
WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)
|
205
|
+
)
|
206
|
+
end
|
207
|
+
@server_thread = start_server_thread(@server)
|
208
|
+
end
|
209
|
+
|
210
|
+
def do_hello(req, res)
|
211
|
+
res['content-type'] = 'text/html'
|
212
|
+
res.body = "hello"
|
213
|
+
end
|
214
|
+
|
215
|
+
def start_server_thread(server)
|
216
|
+
t = Thread.new {
|
217
|
+
Thread.current.abort_on_exception = true
|
218
|
+
server.start
|
219
|
+
}
|
220
|
+
while server.status != :Running
|
221
|
+
sleep 0.1
|
222
|
+
unless t.alive?
|
223
|
+
t.join
|
224
|
+
raise
|
225
|
+
end
|
226
|
+
end
|
227
|
+
t
|
228
|
+
end
|
229
|
+
|
230
|
+
def verify_callback(ok, cert)
|
231
|
+
@verify_callback_called = true
|
232
|
+
p ["client", ok, cert] if $DEBUG
|
233
|
+
ok
|
234
|
+
end
|
235
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glebtv-httpclient
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- glebtv
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: glebtv@gmail.com
|
15
|
+
executables:
|
16
|
+
- httpclient
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/httpclient
|
21
|
+
- lib/httpclient.rb
|
22
|
+
- lib/hexdump.rb
|
23
|
+
- lib/glebtv-httpclient.rb
|
24
|
+
- lib/httpclient/cacert.p7s
|
25
|
+
- lib/httpclient/http.rb
|
26
|
+
- lib/httpclient/cookie.rb
|
27
|
+
- lib/httpclient/timeout.rb
|
28
|
+
- lib/httpclient/version.rb
|
29
|
+
- lib/httpclient/include_client.rb
|
30
|
+
- lib/httpclient/auth.rb
|
31
|
+
- lib/httpclient/connection.rb
|
32
|
+
- lib/httpclient/ssl_config.rb
|
33
|
+
- lib/httpclient/session.rb
|
34
|
+
- lib/httpclient/util.rb
|
35
|
+
- lib/oauthclient.rb
|
36
|
+
- lib/http-access2.rb
|
37
|
+
- lib/http-access2/http.rb
|
38
|
+
- lib/http-access2/cookie.rb
|
39
|
+
- sample/cookie.rb
|
40
|
+
- sample/stream.rb
|
41
|
+
- sample/async.rb
|
42
|
+
- sample/auth.rb
|
43
|
+
- sample/oauth_friendfeed.rb
|
44
|
+
- sample/howto.rb
|
45
|
+
- sample/oauth_buzz.rb
|
46
|
+
- sample/dav.rb
|
47
|
+
- sample/oauth_twitter.rb
|
48
|
+
- sample/thread.rb
|
49
|
+
- sample/ssl/1000key.pem
|
50
|
+
- sample/ssl/0key.pem
|
51
|
+
- sample/ssl/webrick_httpsd.rb
|
52
|
+
- sample/ssl/1000cert.pem
|
53
|
+
- sample/ssl/0cert.pem
|
54
|
+
- sample/ssl/ssl_client.rb
|
55
|
+
- sample/ssl/htdocs/index.html
|
56
|
+
- sample/wcat.rb
|
57
|
+
- test/test_http-access2.rb
|
58
|
+
- test/test_auth.rb
|
59
|
+
- test/htpasswd
|
60
|
+
- test/helper.rb
|
61
|
+
- test/ca-chain.cert
|
62
|
+
- test/test_hexdump.rb
|
63
|
+
- test/subca.cert
|
64
|
+
- test/server.cert
|
65
|
+
- test/server.key
|
66
|
+
- test/runner.rb
|
67
|
+
- test/test_include_client.rb
|
68
|
+
- test/test_httpclient.rb
|
69
|
+
- test/test_cookie.rb
|
70
|
+
- test/sslsvr.rb
|
71
|
+
- test/ca.cert
|
72
|
+
- test/test_ssl.rb
|
73
|
+
- test/htdigest
|
74
|
+
- test/client.key
|
75
|
+
- test/client.cert
|
76
|
+
- README.rdoc
|
77
|
+
homepage: http://github.com/glebtv/httpclient
|
78
|
+
licenses:
|
79
|
+
- ruby
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.0.3
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Fork of httpclient with some fixes and patches I needed. Please use original
|
101
|
+
gem instead
|
102
|
+
test_files: []
|