httpclient 2.10.0 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 776bb37aba928f5ec039eb09b74682084200ef1fd49132a0a6f43bbcc00ae2ea
4
- data.tar.gz: 5de606bc3e57a6cac284cd7c680f1ef3e84ca3e8f28eb970581e18445d43a969
3
+ metadata.gz: 5b259f6082cc3aafc97934b47bb8445aa2accdc96e232a1c16c5e240e923076b
4
+ data.tar.gz: 451c7ca57e10772fdb31baa533d74f27e19b78fe384bf75514d3366a79a79e99
5
5
  SHA512:
6
- metadata.gz: 7ba283f410d4d177819114f3792ac491012ebdae7a67bff29aec379450070998760e615eaf2da299339d18b5918e0e5b6ddcc8b90efcb9d010682e6089af9927
7
- data.tar.gz: f8e25fd2d3094919879bc2b5ebcc55f8b85f5e0c68191cd1192b3674f859e710bee9faff4fda7217c1566f61bc712b41ad73c8a33884f55d602fc0e5320e2160
6
+ metadata.gz: 99676b827187bc755394a46de74ec19691670d64e1ce9651cfb8d822cd0b863ff4213161bcc81483f7c89304c6ce66265d80695fa998558bd5c7e0d05c76648d
7
+ data.tar.gz: 48ab99a0d71add0559410021d85b330bbd72f510d1f01b630de128f3793e17220be0ccb27568ddc28358318b95b2ee8855296466f8f3c24cc7c6a86509bb04de
@@ -42,7 +42,7 @@ unless defined?(SSLSocket)
42
42
  @outstr = @socket.getOutputStream
43
43
  @instr = BufferedInputStream.new(@socket.getInputStream)
44
44
  @buf = (' ' * BUF_SIZE).to_java_bytes
45
- @bufstr = ''
45
+ @bufstr = ''.dup
46
46
  end
47
47
 
48
48
  def close
@@ -352,7 +352,7 @@ unless defined?(SSLSocket)
352
352
  File.read(cert_source).each_line do |line|
353
353
  case line
354
354
  when /-----BEGIN CERTIFICATE-----/
355
- pem = ''
355
+ pem = ''.dup
356
356
  when /-----END CERTIFICATE-----/
357
357
  load_pem(pem)
358
358
  # keep parsing in case where multiple certificates in a file
@@ -400,6 +400,7 @@ class HTTPClient
400
400
  if str && @debug_dev
401
401
  if str.index("\0")
402
402
  require 'hexdump'
403
+ str = str.dup
403
404
  str.force_encoding('BINARY') if str.respond_to?(:force_encoding)
404
405
  @debug_dev << HexDump.encode(str).join("\n")
405
406
  else
@@ -1,3 +1,3 @@
1
1
  class HTTPClient
2
- VERSION = '2.10.0'
2
+ VERSION = '2.11.0'
3
3
  end
data/lib/httpclient.rb CHANGED
@@ -1228,12 +1228,26 @@ private
1228
1228
  false
1229
1229
  end
1230
1230
 
1231
+ # Proxy authentication is only for forwarding requests. HTTPS authenticates
1232
+ # CONNECT separately in Session#connect_ssl_proxy, not the origin request
1233
+ # inside the tunnel. Use the same filters for the request and its response.
1234
+ def filter_request(req, proxy)
1235
+ proxy_request = proxy && !https?(req.header.request_uri)
1236
+ filters = @request_filter.reject { |filter|
1237
+ !proxy_request && filter.equal?(@proxy_auth)
1238
+ }
1239
+ filters.each do |filter|
1240
+ filter.filter_request(req)
1241
+ end
1242
+ # Also remove explicit values, including those added by custom filters.
1243
+ req.header.delete('Proxy-Authorization') unless proxy_request
1244
+ filters
1245
+ end
1246
+
1231
1247
  # !! CAUTION !!
1232
1248
  # Method 'do_get*' runs under MT conditon. Be careful to change.
1233
1249
  def do_get_block(req, proxy, conn, &block)
1234
- @request_filter.each do |filter|
1235
- filter.filter_request(req)
1236
- end
1250
+ filters = filter_request(req, proxy)
1237
1251
  if str = @test_loopback_response.shift
1238
1252
  dump_dummy_request_response(req.http_body.dump, str) if @debug_dev
1239
1253
  res = HTTP::Message.new_response(str, req.header)
@@ -1259,7 +1273,7 @@ private
1259
1273
  # there could be a race condition but it's OK to cache unreusable
1260
1274
  # connection because we do retry for that case.
1261
1275
  @session_manager.keep(sess) unless sess.closed?
1262
- commands = @request_filter.collect { |filter|
1276
+ commands = filters.collect { |filter|
1263
1277
  filter.filter_response(req, res)
1264
1278
  }
1265
1279
  if commands.find { |command| command == :retry }
@@ -1269,9 +1283,7 @@ private
1269
1283
  end
1270
1284
 
1271
1285
  def do_get_stream(req, proxy, conn)
1272
- @request_filter.each do |filter|
1273
- filter.filter_request(req)
1274
- end
1286
+ filters = filter_request(req, proxy)
1275
1287
  if str = @test_loopback_response.shift
1276
1288
  dump_dummy_request_response(req.http_body.dump, str) if @debug_dev
1277
1289
  conn.push(HTTP::Message.new_response(StringIO.new(str), req.header))
@@ -1292,7 +1304,7 @@ private
1292
1304
  end
1293
1305
  pipew.close
1294
1306
  @session_manager.keep(sess) unless sess.closed?
1295
- _ = @request_filter.collect { |filter|
1307
+ _ = filters.collect { |filter|
1296
1308
  filter.filter_response(req, res)
1297
1309
  }
1298
1310
  # ignore commands (not retryable in async mode)
data/test/helper.rb CHANGED
@@ -6,10 +6,15 @@ require 'webrick'
6
6
  require 'webrick/httpproxy.rb'
7
7
  require 'logger'
8
8
  require 'stringio'
9
- require 'cgi'
9
+ begin
10
+ require 'cgi/escape'
11
+ rescue LoadError
12
+ require 'cgi'
13
+ end
10
14
  require 'webrick/httputils'
11
15
 
12
16
  if RUBY_ENGINE == 'jruby'
17
+ require 'webrick/https'
13
18
  require 'delegate'
14
19
 
15
20
  module JRubyWEBrickSSLReadBuffer
@@ -36,9 +41,24 @@ if RUBY_ENGINE == 'jruby'
36
41
  sock.singleton_class.remove_method(:to_io) if readable_io
37
42
  end
38
43
  end
44
+
45
+ module HTTPRequestPatch
46
+ def parse(socket = nil)
47
+ skip_peer_cert_chain = socket.respond_to?(:peer_cert_chain) &&
48
+ socket.respond_to?(:peer_cert) && socket.peer_cert.nil?
49
+ if skip_peer_cert_chain
50
+ # WEBrick asks JRuby for a peer chain even when no client certificate was sent.
51
+ socket.define_singleton_method(:peer_cert_chain) { nil }
52
+ end
53
+ super
54
+ ensure
55
+ socket.singleton_class.remove_method(:peer_cert_chain) if skip_peer_cert_chain
56
+ end
57
+ end
39
58
  end
40
59
 
41
60
  WEBrick::HTTPServer.prepend(JRubyWEBrickSSLReadBuffer::HTTPServerPatch)
61
+ WEBrick::HTTPRequest.prepend(JRubyWEBrickSSLReadBuffer::HTTPRequestPatch)
42
62
  end
43
63
 
44
64
 
@@ -47,10 +67,12 @@ module Helper
47
67
  ProxyPort = 17172
48
68
 
49
69
  def serverport
70
+ setup_server unless @server
50
71
  @serverport
51
72
  end
52
73
 
53
74
  def proxyport
75
+ setup_proxyserver unless @proxyserver
54
76
  @proxyport
55
77
  end
56
78
 
@@ -75,9 +97,15 @@ module Helper
75
97
  end
76
98
 
77
99
  def teardown
78
- teardown_client if @client
79
- teardown_proxyserver if @proxyserver
80
- teardown_server if @server
100
+ begin
101
+ teardown_client if @client
102
+ ensure
103
+ begin
104
+ teardown_proxyserver if @proxyserver
105
+ ensure
106
+ teardown_server if @server
107
+ end
108
+ end
81
109
  end
82
110
 
83
111
  def setup_client
@@ -114,12 +142,12 @@ module Helper
114
142
 
115
143
  def teardown_server
116
144
  @server.shutdown
117
- #@server_thread.kill
145
+ @server_thread.join if @server_thread
118
146
  end
119
147
 
120
148
  def teardown_proxyserver
121
149
  @proxyserver.shutdown
122
- #@proxyserver_thread.kill
150
+ @proxyserver_thread.join if @proxyserver_thread
123
151
  end
124
152
 
125
153
  def start_server_thread(server)
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../helper', File.dirname(__FILE__))
2
+
3
+
4
+ class JRubyMutableBuffersTest < Test::Unit::TestCase
5
+ def setup
6
+ omit('JRuby-specific test') unless RUBY_ENGINE == 'jruby'
7
+ end
8
+
9
+ def test_socket_reads_using_mutable_buffer
10
+ input = Java::JavaIo::ByteArrayInputStream.new("hello\nworld".to_java_bytes)
11
+ output = Java::JavaIo::ByteArrayOutputStream.new
12
+ socket = Object.new
13
+ socket.define_singleton_method(:getInputStream) { input }
14
+ socket.define_singleton_method(:getOutputStream) { output }
15
+ wrapped_socket = HTTPClient::JavaSocketWrap.new(socket)
16
+
17
+ assert_equal("hello\n", wrapped_socket.gets("\n"))
18
+ assert_equal('wo', wrapped_socket.read(2))
19
+ assert_equal('rld', wrapped_socket.readpartial(3))
20
+ assert_raise(EOFError) { wrapped_socket.readpartial(1) }
21
+ end
22
+
23
+ def test_trust_store_loads_multiple_pem_certificates
24
+ loader = HTTPClient::JRubySSLSocket::TrustStoreLoader.new
25
+ loader.add(File.expand_path('../ca-chain.pem', File.dirname(__FILE__)))
26
+
27
+ assert_equal(2, loader.size)
28
+ assert_equal(2, loader.trust_store.size)
29
+ end
30
+ end
@@ -5,26 +5,27 @@ class PEMUtilsTest < Test::Unit::TestCase
5
5
  include Helper
6
6
 
7
7
  def setup
8
+ omit('JRuby-specific test') unless RUBY_ENGINE == 'jruby'
8
9
  @raw_cert = "-----BEGIN CERTIFICATE-----\nMIIDOTCCAiGgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBCMRMwEQYKCZImiZPyLGQB\nGRYDb3JnMRkwFwYKCZImiZPyLGQBGRYJcnVieS1sYW5nMRAwDgYDVQQDDAdSdWJ5\nIENBMB4XDTE2MDgxMDE3MjEzNFoXDTE3MDgxMDE3MjEzNFowSzETMBEGCgmSJomT\n8ixkARkWA29yZzEZMBcGCgmSJomT8ixkARkWCXJ1YnktbGFuZzEZMBcGA1UEAwwQ\nUnVieSBjZXJ0aWZpY2F0ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nAJCfsSXpSMpmZCVa+ZCM+QDgomnhDlvnrGDq6pasTaIspGTXgws+7r8Dt/cNe6EH\nHJpRH2cGRiO4yPcfcT9eS4X7k8OC4f33wHfACOmLu6LeoNE8ujmSk6L6WzLUI+sE\nnLZbFrXxoAo4XHsm8vEG9C+jEoXZ1p+47wrAGaDwDQTnzlMy4dT9pRQEJP2G/Rry\nUkuZn8SUWmh3/YS78iaSzsNF1cgE1ealHOrPPFDjiCGDaH/LHyUPYlbFSLZ/B7Qx\nLxi5sePLcywWq/EJrmWpgeVTDjtNijsdKv/A3qkY+fm/oD0pzt7XsfJaP9YKNyJO\nQFdxWZeiPcDF+Hwf+IwSr+kCAwEAAaMxMC8wDgYDVR0PAQH/BAQDAgeAMB0GA1Ud\nDgQWBBQNvzYzJyXemGhxbA8NMXLolDnPyjANBgkqhkiG9w0BAQsFAAOCAQEARIJV\noKejGlOTn71QutnNnu07UtTu0IHs6YqjYzzND+m4JXLN+wvYm72AFUG0b1L7dRg0\niK8XjQrlNQNVqP1Mc6tffchy20neOPOHeiO6qTdRU8P2S8D3Uwe+1qhgxjfE+cWc\nwZmWxYK4HA8c58PxWMqrkr2QqXDplG9KWLvOgrtPGiLLZcQSKhvvB63QzItHBDU6\nRayiJY3oPkK/HrIvFlySqFqzWmuyknkciOFywEHQMz/tcSFJ2QFpPj/tBz9VXohH\nZ8KscmfhZrTPBjo+ky1lz/WraWoz4LMiLnkC2ABczWLRSawu+v3Irx1NFJngt05e\npqwtqIUeg7j+JLiTaA==\n-----END CERTIFICATE-----"
9
10
  end
10
11
 
11
12
  def test_read_certificate
12
13
  assert_nothing_raised do
13
- binary = HTTPClient::JRubySSLSocket::PEMUtils.read_certificate(@raw_cert)
14
+ HTTPClient::JRubySSLSocket::PEMUtils.read_certificate(@raw_cert)
14
15
  end
15
16
  end
16
17
 
17
18
  def test_read_certificate_works_with_random_ascii_text_outside_begin_end
18
19
  raw_cert_with_ascii = "some text before begin\n" + @raw_cert + "\nsome text after end"
19
20
  assert_nothing_raised do
20
- binary = HTTPClient::JRubySSLSocket::PEMUtils.read_certificate(raw_cert_with_ascii)
21
+ HTTPClient::JRubySSLSocket::PEMUtils.read_certificate(raw_cert_with_ascii)
21
22
  end
22
23
  end
23
24
 
24
25
  def test_read_certificate_uses_all_content_if_missing_begin_end
25
26
  cert = @raw_cert.sub(/-----BEGIN CERTIFICATE-----/, '').sub(/-----END CERTIFICATE-----/, '')
26
27
  assert_nothing_raised do
27
- binary = HTTPClient::JRubySSLSocket::PEMUtils.read_certificate(cert)
28
+ HTTPClient::JRubySSLSocket::PEMUtils.read_certificate(cert)
28
29
  end
29
30
  end
30
31
 
@@ -3,6 +3,7 @@ require File.expand_path('../helper', File.dirname(__FILE__))
3
3
 
4
4
  class JRubySocketTimeoutTest < Test::Unit::TestCase
5
5
  def test_read_timeout_is_reported_as_receive_timeout
6
+ omit('JRuby-specific test') unless RUBY_ENGINE == 'jruby'
6
7
  socket = HTTPClient::JavaSocketWrap.allocate
7
8
  input = Object.new
8
9
  input.define_singleton_method(:read) do |_buffer|
data/test/test_auth.rb CHANGED
@@ -2,7 +2,14 @@ require File.expand_path('helper', File.dirname(__FILE__))
2
2
  require 'digest/md5'
3
3
  require 'rack'
4
4
  require 'rack/lint'
5
- require 'rack-ntlm'
5
+ # rack-ntlm-test-service 0.0.1 assigns an unused rescue variable while loading.
6
+ original_verbose = $VERBOSE
7
+ begin
8
+ $VERBOSE = nil
9
+ require 'rack-ntlm'
10
+ ensure
11
+ $VERBOSE = original_verbose
12
+ end
6
13
 
7
14
  class TestAuth < Test::Unit::TestCase
8
15
  include Helper
@@ -95,12 +102,15 @@ class TestAuth < Test::Unit::TestCase
95
102
  end
96
103
 
97
104
  def do_basic_auth(req, res)
105
+ # Drain uploads before an authentication challenge can close the connection.
106
+ req.body
98
107
  @basic_auth.authenticate(req, res)
99
108
  res['content-type'] = 'text/plain'
100
109
  res.body = 'basic_auth OK'
101
110
  end
102
111
 
103
112
  def do_digest_auth(req, res)
113
+ req.body
104
114
  @digest_auth.authenticate(req, res)
105
115
  res['content-type'] = 'text/plain'
106
116
  res['x-query'] = req.body
@@ -108,6 +118,7 @@ class TestAuth < Test::Unit::TestCase
108
118
  end
109
119
 
110
120
  def do_digest_sess_auth(req, res)
121
+ req.body
111
122
  @digest_sess_auth.authenticate(req, res)
112
123
  res['content-type'] = 'text/plain'
113
124
  res['x-query'] = req.body
@@ -128,6 +139,7 @@ class TestAuth < Test::Unit::TestCase
128
139
  @active && @value ? @value.size : 0
129
140
  end
130
141
 
142
+ remove_method(:value) if method_defined?(:value)
131
143
  def value
132
144
  @value ||= ""
133
145
  end
@@ -327,7 +339,7 @@ class TestAuth < Test::Unit::TestCase
327
339
  end
328
340
 
329
341
  def test_proxy_auth
330
- c = HTTPClient.new
342
+ c = HTTPClient.new('http://proxy.example.com:8080')
331
343
  c.set_proxy_auth('admin', 'admin')
332
344
  c.test_loopback_http_response << "HTTP/1.0 407 Unauthorized\nProxy-Authenticate: Basic realm=\"foo\"\nContent-Length: 2\n\nNG"
333
345
  c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
@@ -337,7 +349,7 @@ class TestAuth < Test::Unit::TestCase
337
349
  end
338
350
 
339
351
  def test_proxy_auth_force
340
- c = HTTPClient.new
352
+ c = HTTPClient.new('http://proxy.example.com:8080')
341
353
  c.set_proxy_auth('admin', 'admin')
342
354
  c.force_basic_auth = true
343
355
  c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
@@ -347,7 +359,7 @@ class TestAuth < Test::Unit::TestCase
347
359
  end
348
360
 
349
361
  def test_proxy_auth_reuses_credentials
350
- c = HTTPClient.new
362
+ c = HTTPClient.new('http://proxy.example.com:8080')
351
363
  c.set_proxy_auth('admin', 'admin')
352
364
  c.test_loopback_http_response << "HTTP/1.0 407 Unauthorized\nProxy-Authenticate: Basic realm=\"foo\"\nContent-Length: 2\n\nNG"
353
365
  c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
@@ -359,7 +371,7 @@ class TestAuth < Test::Unit::TestCase
359
371
  end
360
372
 
361
373
  def test_digest_proxy_auth_loop
362
- c = HTTPClient.new
374
+ c = HTTPClient.new('http://proxy.example.com:8080')
363
375
  c.set_proxy_auth('admin', 'admin')
364
376
  c.test_loopback_http_response << "HTTP/1.0 407 Unauthorized\nProxy-Authenticate: Digest realm=\"foo\", nonce=\"nonce\", stale=false\nContent-Length: 2\n\nNG"
365
377
  c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
@@ -390,7 +402,7 @@ class TestAuth < Test::Unit::TestCase
390
402
  end
391
403
 
392
404
  def test_prefer_digest_to_basic_proxy_auth
393
- c = HTTPClient.new
405
+ c = HTTPClient.new('http://proxy.example.com:8080')
394
406
  c.set_proxy_auth('admin', 'admin')
395
407
  c.test_loopback_http_response << "HTTP/1.0 407 Unauthorized\nProxy-Authenticate: Digest realm=\"foo\", nonce=\"nonce\", stale=false\nProxy-Authenticate: Basic realm=\"bar\"\nContent-Length: 2\n\nNG"
396
408
  c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
@@ -405,7 +417,7 @@ class TestAuth < Test::Unit::TestCase
405
417
  end
406
418
 
407
419
  def test_digest_proxy_auth_reuses_credentials
408
- c = HTTPClient.new
420
+ c = HTTPClient.new('http://proxy.example.com:8080')
409
421
  c.set_proxy_auth('admin', 'admin')
410
422
  c.test_loopback_http_response << "HTTP/1.0 407 Unauthorized\nProxy-Authenticate: Digest realm=\"foo\", nonce=\"nonce\", stale=false\nContent-Length: 2\n\nNG"
411
423
  c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
@@ -0,0 +1,42 @@
1
+ require File.expand_path('test_auth', File.dirname(__FILE__))
2
+
3
+ class TestAuthFixture < Test::Unit::TestCase
4
+ Request = Struct.new(:events) do
5
+ def body
6
+ events << :read_body
7
+ 'upload body'
8
+ end
9
+ end
10
+
11
+ Authenticator = Struct.new(:events) do
12
+ def authenticate(_request, _response)
13
+ events << :authenticate
14
+ raise WEBrick::HTTPStatus::Unauthorized
15
+ end
16
+ end
17
+
18
+ def test_basic_auth_drains_body_before_challenge
19
+ assert_body_drained_before_challenge(:basic_auth)
20
+ end
21
+
22
+ def test_digest_auth_drains_body_before_challenge
23
+ assert_body_drained_before_challenge(:digest_auth)
24
+ end
25
+
26
+ def test_digest_sess_auth_drains_body_before_challenge
27
+ assert_body_drained_before_challenge(:digest_sess_auth)
28
+ end
29
+
30
+ private
31
+
32
+ def assert_body_drained_before_challenge(kind)
33
+ events = []
34
+ fixture = TestAuth.allocate
35
+ fixture.instance_variable_set("@#{kind}", Authenticator.new(events))
36
+
37
+ assert_raise(WEBrick::HTTPStatus::Unauthorized) do
38
+ fixture.send("do_#{kind}", Request.new(events), Object.new)
39
+ end
40
+ assert_equal([:read_body, :authenticate], events)
41
+ end
42
+ end
@@ -0,0 +1,84 @@
1
+ require File.expand_path('helper', File.dirname(__FILE__))
2
+
3
+ class TestSupportLifecycle < Test::Unit::TestCase
4
+ Server = Struct.new(:events, :kind) do
5
+ def shutdown
6
+ events << [kind, :shutdown]
7
+ end
8
+ end
9
+
10
+ Listener = Struct.new(:events, :kind) do
11
+ def join
12
+ events << [kind, :join]
13
+ end
14
+ end
15
+
16
+ def setup
17
+ @events = []
18
+ @fixture = Object.new.extend(Helper)
19
+ @fixture.setup
20
+ end
21
+
22
+ def test_serverport_lazily_starts_server_once
23
+ install_lazy_server(:server, 18_081)
24
+
25
+ assert_equal(18_081, @fixture.serverport)
26
+ assert_equal(18_081, @fixture.serverport)
27
+ assert_equal([[:server, :start]], @events)
28
+ end
29
+
30
+ def test_proxyport_lazily_starts_proxy_once
31
+ install_lazy_server(:proxyserver, 18_082)
32
+
33
+ assert_equal(18_082, @fixture.proxyport)
34
+ assert_equal(18_082, @fixture.proxyport)
35
+ assert_equal([[:proxyserver, :start]], @events)
36
+ end
37
+
38
+ def test_teardown_server_joins_listener
39
+ install_server(:server)
40
+
41
+ @fixture.teardown_server
42
+
43
+ assert_equal([[:server, :shutdown], [:server, :join]], @events)
44
+ end
45
+
46
+ def test_teardown_proxyserver_joins_listener
47
+ install_server(:proxyserver)
48
+
49
+ @fixture.teardown_proxyserver
50
+
51
+ assert_equal([[:proxyserver, :shutdown], [:proxyserver, :join]], @events)
52
+ end
53
+
54
+ def test_client_cleanup_error_still_stops_both_servers
55
+ install_server(:server)
56
+ install_server(:proxyserver)
57
+ client = Object.new
58
+ def client.reset_all
59
+ raise IOError, 'client cleanup failed'
60
+ end
61
+ @fixture.instance_variable_set(:@client, client)
62
+
63
+ assert_raise(IOError) { @fixture.teardown }
64
+ assert_equal([[:proxyserver, :shutdown], [:proxyserver, :join],
65
+ [:server, :shutdown], [:server, :join]], @events)
66
+ end
67
+
68
+ private
69
+
70
+ def install_server(kind)
71
+ @fixture.instance_variable_set("@#{kind}", Server.new(@events, kind))
72
+ @fixture.instance_variable_set("@#{kind}_thread", Listener.new(@events, kind))
73
+ end
74
+
75
+ def install_lazy_server(kind, port)
76
+ events = @events
77
+ @fixture.define_singleton_method("setup_#{kind}") do
78
+ events << [kind, :start]
79
+ instance_variable_set("@#{kind}", Server.new(events, kind))
80
+ port_variable = kind == :server ? :@serverport : :@proxyport
81
+ instance_variable_set(port_variable, port)
82
+ end
83
+ end
84
+ end
@@ -143,6 +143,7 @@ class TestClient < Test::Unit::TestCase
143
143
  end
144
144
 
145
145
  def test_noproxy_for_localhost
146
+ setup_proxyserver
146
147
  @proxyio.truncate(0)
147
148
  @client.proxy = proxyurl
148
149
  assert_equal(200, @client.head(serverurl).status)
@@ -9,7 +9,6 @@ class TestHTTPClient < Test::Unit::TestCase
9
9
 
10
10
  def setup
11
11
  super
12
- setup_server
13
12
  setup_client
14
13
  end
15
14
 
@@ -65,6 +64,21 @@ class TestHTTPClient < Test::Unit::TestCase
65
64
  assert(!str.empty?)
66
65
  end
67
66
 
67
+ data('mutable' => false, 'frozen' => true)
68
+ def test_debug_socket_does_not_mutate_binary_input(freeze_payload)
69
+ debug_dev = StringIO.new
70
+ socket = Object.new
71
+ socket.extend(HTTPClient::DebugSocket)
72
+ socket.debug_dev = debug_dev
73
+ payload = "\x00".dup
74
+ payload.freeze if freeze_payload
75
+
76
+ assert_nothing_raised { socket.send(:debug, payload) }
77
+ assert_equal("\x00", payload)
78
+ assert_equal(Encoding::UTF_8, payload.encoding)
79
+ assert(!debug_dev.string.empty?)
80
+ end
81
+
68
82
  def test_protocol_version_http09
69
83
  @client.protocol_version = 'HTTP/0.9'
70
84
  @client.debug_dev = str = ''.dup
@@ -264,7 +278,6 @@ class TestHTTPClient < Test::Unit::TestCase
264
278
  end
265
279
 
266
280
  def test_empty_proxy_env
267
- setup_proxyserver
268
281
  escape_env do
269
282
  ENV['http_proxy'] = ""
270
283
  client = HTTPClient.new
@@ -768,7 +781,8 @@ EOS
768
781
  if RUBY_VERSION > "1.9"
769
782
  def test_post_async_with_default_internal
770
783
  original_encoding = Encoding.default_internal
771
- Encoding.default_internal = Encoding::UTF_8
784
+ # This test covers the legacy setter even though JRuby deprecates it.
785
+ silent { Encoding.default_internal = Encoding::UTF_8 }
772
786
  begin
773
787
  post_body = StringIO.new("こんにちは")
774
788
  conn = @client.post_async(serverurl + 'servlet', post_body)
@@ -776,7 +790,7 @@ EOS
776
790
  res = conn.pop
777
791
  assert_equal 'post,こんにちは', res.content.read
778
792
  ensure
779
- Encoding.default_internal = original_encoding
793
+ silent { Encoding.default_internal = original_encoding }
780
794
  end
781
795
  end
782
796
  end
@@ -934,7 +948,7 @@ EOS
934
948
  STDOUT.sync = true
935
949
  File.open(__FILE__) do |file|
936
950
  res = @client.post(serverurl + 'servlet', {1=>2, 3=>file})
937
- assert_match(/^Content-Disposition: form-data; name="1"\r\n/nm, res.content)
951
+ assert_match(/^Content-Disposition: form-data; name="1"\r\n/m, res.content.b)
938
952
  assert_match(/^Content-Disposition: form-data; name="3";/, res.content)
939
953
  assert_match(/FIND_TAG_IN_THIS_FILE/, res.content)
940
954
  end
@@ -0,0 +1,292 @@
1
+ require File.expand_path('helper', File.dirname(__FILE__))
2
+ require 'webrick/https'
3
+
4
+ class TestProxyAuthScope < Test::Unit::TestCase
5
+ include Helper
6
+
7
+ PROXY_CREDENTIAL = 'Basic ' + ['proxy-user:proxy-secret'].pack('m0')
8
+ ORIGIN_CREDENTIAL = 'Basic ' + ['origin-user:origin-secret'].pack('m0')
9
+
10
+ class HeaderFilter
11
+ attr_reader :requests, :responses
12
+
13
+ def initialize
14
+ @requests = @responses = 0
15
+ end
16
+
17
+ def filter_request(req)
18
+ @requests += 1
19
+ req.header.add('Proxy-Authorization', 'custom filter value')
20
+ req.header.add('X-Scope-Filter', 'present')
21
+ end
22
+
23
+ def filter_response(_req, _res)
24
+ @responses += 1
25
+ nil
26
+ end
27
+ end
28
+
29
+ def setup
30
+ super
31
+ setup_client
32
+ @client.proxy = nil
33
+ @client.no_proxy = nil
34
+ @client.set_proxy_auth('proxy-user', 'proxy-secret')
35
+ @origin_requests = Queue.new
36
+ @proxy_requests = Queue.new
37
+ end
38
+
39
+ data('Basic' => 'Basic', 'Digest' => 'Digest')
40
+ def test_redirect_to_no_proxy(scheme)
41
+ start_origin
42
+ start_proxy(scheme)
43
+ assert_equal('OK', @client.get_content('http://upstream.invalid/redirect'))
44
+ assert_proxy_authenticated(scheme)
45
+ assert_equal([[]], drain(@origin_requests).map { |request| request[:proxy_auth] })
46
+ end
47
+
48
+ data('sync Basic listed' => [:sync, 'Basic', false],
49
+ 'async Basic listed' => [:async, 'Basic', false],
50
+ 'sync Digest listed' => [:sync, 'Digest', false],
51
+ 'async Digest listed' => [:async, 'Digest', false],
52
+ 'sync Basic localhost' => [:sync, 'Basic', true],
53
+ 'async Basic localhost' => [:async, 'Basic', true])
54
+ def test_direct_request_after_proxy_authentication(settings)
55
+ mode, scheme, implicit_localhost = settings
56
+ start_origin
57
+ start_proxy(scheme)
58
+ assert_equal('OK', @client.get_content('http://upstream.invalid/warm'))
59
+ assert_proxy_authenticated(scheme)
60
+ url = @origin_url
61
+ if implicit_localhost
62
+ @client.no_proxy = nil
63
+ url = "http://localhost:#{serverport}/"
64
+ end
65
+ assert_equal(200, request(mode, url).status)
66
+ assert_equal([[]], drain(@origin_requests).map { |record| record[:proxy_auth] })
67
+ assert_equal('OK', @client.get_content('http://upstream.invalid/again'))
68
+ assert_match(/\A#{scheme} /, drain(@proxy_requests).last[:proxy_auth].first)
69
+ end
70
+
71
+ data('sync' => :sync, 'async' => :async)
72
+ def test_forced_basic_auth_preserves_origin_auth(mode)
73
+ start_origin
74
+ start_proxy
75
+ @client.force_basic_auth = true
76
+ @client.set_auth(@origin_url, 'origin-user', 'origin-secret')
77
+ assert_equal(200, request(mode, 'http://upstream.invalid/').status)
78
+ assert_equal([PROXY_CREDENTIAL], drain(@proxy_requests).first[:proxy_auth])
79
+ assert_equal(200, request(mode, @origin_url).status)
80
+ record = @origin_requests.pop
81
+ assert_equal([], record[:proxy_auth])
82
+ assert_equal([ORIGIN_CREDENTIAL], record[:origin_auth])
83
+ end
84
+
85
+ data('sync request with proxy' => [:sync, false, true],
86
+ 'async request with proxy' => [:async, false, true],
87
+ 'sync defaults with proxy' => [:sync, true, true],
88
+ 'async defaults with proxy' => [:async, true, true],
89
+ 'sync request without proxy' => [:sync, false, false],
90
+ 'async request without proxy' => [:async, false, false],
91
+ 'sync defaults without proxy' => [:sync, true, false],
92
+ 'async defaults without proxy' => [:async, true, false])
93
+ def test_explicit_headers_on_direct_requests(settings)
94
+ mode, defaults, configured_proxy = settings
95
+ start_origin
96
+ start_proxy if configured_proxy
97
+ headers = { 'pRoXy-AuThOrIzAtIoN' => ['first', 'second'],
98
+ 'Proxy-Authorization' => 'third' }
99
+ @client.default_header.update(headers) if defaults
100
+ assert_equal(200, request(mode, @origin_url, defaults ? {} : headers).status)
101
+ assert_equal([], @origin_requests.pop[:proxy_auth])
102
+ assert_equal(['first', 'second'], headers['pRoXy-AuThOrIzAtIoN'])
103
+ assert_equal('third', headers['Proxy-Authorization'])
104
+ end
105
+
106
+ data('sync' => :sync, 'async' => :async)
107
+ def test_explicit_header_on_forwarding_request(mode)
108
+ start_origin
109
+ start_proxy
110
+ assert_equal(200, request(mode, 'http://upstream.invalid/', 'Proxy-Authorization' => PROXY_CREDENTIAL).status)
111
+ records = drain(@proxy_requests)
112
+ assert_equal(1, records.size)
113
+ assert_equal([PROXY_CREDENTIAL], records.first[:proxy_auth])
114
+ assert(@origin_requests.empty?)
115
+ end
116
+
117
+ data('sync' => :sync, 'async' => :async)
118
+ def test_custom_filters_still_run_on_direct_requests(mode)
119
+ start_origin
120
+ start_proxy
121
+ filter = HeaderFilter.new
122
+ @client.request_filter << filter
123
+ assert_equal(200, request(mode, @origin_url).status)
124
+ record = @origin_requests.pop
125
+ assert_equal([], record[:proxy_auth])
126
+ assert_equal('present', record[:custom_filter])
127
+ assert_equal(1, filter.requests)
128
+ assert_equal(1, filter.responses)
129
+ end
130
+
131
+ data('sync' => :sync, 'async' => :async)
132
+ def test_direct_response_cannot_challenge_proxy(mode)
133
+ start_origin
134
+ start_proxy
135
+ assert_equal(407, request(mode, @origin_url + 'proxy_challenge').status)
136
+ assert_equal(1, drain(@origin_requests).size, 'origin 407 must not cause proxy authentication retries')
137
+ assert_nil(@client.proxy_auth.basic_auth.get(nil))
138
+ assert_equal('OK', @client.get_content('http://upstream.invalid/warm'))
139
+ assert_proxy_authenticated('Basic')
140
+ end
141
+
142
+ data('sync' => :sync, 'async' => :async)
143
+ def test_loopback_response_does_not_bypass_direct_route_protection(mode)
144
+ @client.proxy = 'http://proxy.invalid:8080'
145
+ @client.no_proxy = 'direct.invalid'
146
+ @client.force_basic_auth = true
147
+ @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\nContent-Length: 2\r\n\r\nOK"
148
+ @client.debug_dev = log = ''.dup
149
+ assert_equal(200, request(mode, 'http://direct.invalid/').status)
150
+ assert_not_match(/Proxy-Authorization:/i, log)
151
+ end
152
+
153
+ data('sync challenged' => [:sync, false], 'async challenged' => [:async, false],
154
+ 'sync forced' => [:sync, true], 'async forced' => [:async, true])
155
+ def test_https_credentials_only_reach_connect(settings)
156
+ mode, force = settings
157
+ start_origin(true)
158
+ start_proxy('Basic', true)
159
+ @client.force_basic_auth = force
160
+ @client.protocol_version = '1.1'
161
+ @client.set_auth(@origin_url, 'origin-user', 'origin-secret')
162
+ @client.www_auth.basic_auth.force_auth = true
163
+ escape_noproxy do
164
+ assert_equal(200, request(:sync, @origin_url).status)
165
+ assert_equal(200, request(mode, @origin_url, 'pRoXy-AuThOrIzAtIoN' => ['first', 'second']).status)
166
+ end
167
+ proxy_requests = drain(@proxy_requests)
168
+ assert_equal(force ? 1 : 2, proxy_requests.size, 'the authenticated tunnel should be reused')
169
+ assert(proxy_requests.all? { |record| record[:method] == 'CONNECT' })
170
+ assert_equal([PROXY_CREDENTIAL], proxy_requests.last[:proxy_auth])
171
+ records = drain(@origin_requests)
172
+ assert_equal([[], []], records.map { |record| record[:proxy_auth] })
173
+ assert_equal([[ORIGIN_CREDENTIAL], [ORIGIN_CREDENTIAL]], records.map { |record| record[:origin_auth] })
174
+ end
175
+
176
+ data('sync' => :sync, 'async' => :async)
177
+ def test_https_origin_response_cannot_challenge_proxy(mode)
178
+ start_origin(true)
179
+ start_proxy('Basic', true)
180
+ @client.protocol_version = '1.1'
181
+ escape_noproxy do
182
+ assert_equal(200, request(:sync, @origin_url).status)
183
+ @client.proxy_auth.reset_challenge
184
+ assert_equal(407, request(mode, @origin_url + 'proxy_challenge').status)
185
+ end
186
+ assert_equal(2, drain(@origin_requests).size)
187
+ assert_nil(@client.proxy_auth.basic_auth.get(nil))
188
+ assert_equal(2, drain(@proxy_requests).size, 'origin 407 must not reopen the CONNECT tunnel')
189
+ end
190
+
191
+ def test_parallel_direct_and_proxied_requests
192
+ start_origin
193
+ start_proxy
194
+ assert_equal('OK', @client.get_content('http://upstream.invalid/warm'))
195
+ assert_proxy_authenticated('Basic')
196
+ threads = 10.times.map do |index|
197
+ Thread.new do
198
+ url = index.even? ? @origin_url : 'http://upstream.invalid/again'
199
+ @client.get(url).status
200
+ end
201
+ end
202
+ assert_equal([200] * 10, threads.map(&:value))
203
+ assert_equal([[]] * 5, drain(@origin_requests).map { |record| record[:proxy_auth] })
204
+ assert_equal([[PROXY_CREDENTIAL]] * 5, drain(@proxy_requests).map { |record| record[:proxy_auth] })
205
+ ensure
206
+ threads.each { |thread| thread.join } if threads
207
+ end
208
+
209
+ private
210
+
211
+ def start_origin(ssl = false)
212
+ options = { :BindAddress => '127.0.0.1', :Port => 0, :AccessLog => [], :Logger => @logger }
213
+ if ssl
214
+ options.update(:SSLEnable => true,
215
+ :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(File.join(__dir__, 'server.cert'))),
216
+ :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(File.join(__dir__, 'server.key'))))
217
+ @client.ssl_config.add_trust_ca(File.join(__dir__, 'ca-chain.pem'))
218
+ end
219
+ @server = WEBrick::HTTPServer.new(options)
220
+ @serverport = @server.config[:Port]
221
+ @server.mount_proc('/') do |req, res|
222
+ @origin_requests << record(req)
223
+ if req.path == '/proxy_challenge'
224
+ res.status = 407
225
+ res['Proxy-Authenticate'] = 'Basic realm="untrusted origin"'
226
+ end
227
+ res.body = 'OK'
228
+ end
229
+ @server_thread = start_server_thread(@server)
230
+ @origin_url = ssl ? "https://localhost:#{serverport}/" : "http://127.0.0.1:#{serverport}/"
231
+ end
232
+
233
+ def start_proxy(scheme = 'Basic', tunnel = false)
234
+ @proxy_scheme = scheme
235
+ options = { :BindAddress => '127.0.0.1', :Port => 0, :AccessLog => [], :Logger => @proxylogger }
236
+ if tunnel
237
+ options[:ProxyAuthProc] = method(:authenticate_proxy).to_proc
238
+ @proxyserver = WEBrick::HTTPProxyServer.new(options)
239
+ else
240
+ # A fake forwarding proxy: never resolves or connects to upstream.invalid.
241
+ @proxyserver = WEBrick::HTTPServer.new(options)
242
+ @proxyserver.mount_proc('/') do |req, res|
243
+ authenticate_proxy(req, res)
244
+ if req.path == '/redirect'
245
+ res.status = 302
246
+ res['Location'] = @origin_url
247
+ end
248
+ res.body = 'OK'
249
+ end
250
+ end
251
+ @proxyport = @proxyserver.config[:Port]
252
+ @proxyserver_thread = start_server_thread(@proxyserver)
253
+ @client.proxy = "http://127.0.0.1:#{proxyport}/"
254
+ @client.no_proxy = "127.0.0.1:#{serverport}"
255
+ end
256
+
257
+ def authenticate_proxy(req, res)
258
+ @proxy_requests << record(req)
259
+ unless req['proxy-authorization']
260
+ res['Proxy-Authenticate'] = @proxy_scheme == 'Basic' ?
261
+ 'Basic realm="proxy"' : 'Digest realm="proxy", nonce="test-nonce", qop="auth"'
262
+ raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
263
+ end
264
+ end
265
+
266
+ def record(req)
267
+ { :proxy_auth => req.header['proxy-authorization'].dup,
268
+ :origin_auth => req.header['authorization'].dup, :method => req.request_method,
269
+ :custom_filter => req['x-scope-filter'] }
270
+ end
271
+
272
+ def drain(queue)
273
+ Array.new(queue.size) { queue.pop }
274
+ end
275
+
276
+ def assert_proxy_authenticated(scheme)
277
+ records = drain(@proxy_requests)
278
+ assert_equal(2, records.size)
279
+ assert_equal([], records.first[:proxy_auth])
280
+ assert_match(/\A#{scheme} /, records.last[:proxy_auth].first)
281
+ assert_equal([PROXY_CREDENTIAL], records.last[:proxy_auth]) if scheme == 'Basic'
282
+ end
283
+
284
+ def request(mode, url, headers = {})
285
+ return @client.get(url, :header => headers) if mode == :sync
286
+ connection = @client.get_async(url, :header => headers)
287
+ response = connection.pop
288
+ response.body.read
289
+ connection.join
290
+ response
291
+ end
292
+ end
data/test/test_ssl.rb CHANGED
@@ -414,7 +414,9 @@ end
414
414
  @client.ssl_config.verify_mode = nil
415
415
  @client.get("https://localhost:#{serverport}/hello")
416
416
  end
417
- assert_match(/tlsv1 alert protocol version/, ssle.message)
417
+ # OpenSSL 3.0 may reject TLSv1 with "no suitable signature algorithm",
418
+ # which sends an internal_error alert instead of protocol_version.
419
+ assert_match(/tlsv1 alert (?:protocol version|internal error)/, ssle.message)
418
420
  end
419
421
  end
420
422
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httpclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.0
4
+ version: 2.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Nakamura
@@ -89,6 +89,7 @@ files:
89
89
  - test/helper.rb
90
90
  - test/htdigest
91
91
  - test/htpasswd
92
+ - test/jruby_ssl_socket/test_mutable_buffers.rb
92
93
  - test/jruby_ssl_socket/test_pemutils.rb
93
94
  - test/jruby_ssl_socket/test_socket_timeout.rb
94
95
  - test/runner.rb
@@ -99,12 +100,15 @@ files:
99
100
  - test/subca.key
100
101
  - test/subca.srl
101
102
  - test/test_auth.rb
103
+ - test/test_auth_fixture.rb
102
104
  - test/test_cookie.rb
105
+ - test/test_helper_lifecycle.rb
103
106
  - test/test_hexdump.rb
104
107
  - test/test_http-access2.rb
105
108
  - test/test_httpclient.rb
106
109
  - test/test_include_client.rb
107
110
  - test/test_jsonclient.rb
111
+ - test/test_proxy_auth_scope.rb
108
112
  - test/test_ssl.rb
109
113
  - test/test_ssl_config.rb
110
114
  - test/test_webagent-cookie.rb
@@ -126,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
130
  - !ruby/object:Gem::Version
127
131
  version: '0'
128
132
  requirements: []
129
- rubygems_version: 4.0.3
133
+ rubygems_version: 4.0.20
130
134
  specification_version: 4
131
135
  summary: gives something like the functionality of libwww-perl (LWP) in Ruby
132
136
  test_files: []