excon 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of excon might be problematic. Click here for more details.
- data/excon.gemspec +4 -2
- data/lib/excon.rb +1 -0
- data/lib/excon/connection.rb +5 -1
- data/lib/excon/constants.rb +2 -2
- data/lib/excon/socket.rb +14 -57
- data/lib/excon/ssl_socket.rb +73 -0
- data/tests/basic_tests.rb +6 -0
- data/tests/rackups/ssl.ru +18 -0
- data/tests/test_helper.rb +2 -2
- metadata +6 -4
data/excon.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'excon'
|
16
|
-
s.version = '0.7.
|
17
|
-
s.date = '2011-09-
|
16
|
+
s.version = '0.7.1'
|
17
|
+
s.date = '2011-09-13'
|
18
18
|
s.rubyforge_project = 'excon'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
@@ -91,6 +91,7 @@ Gem::Specification.new do |s|
|
|
91
91
|
lib/excon/errors.rb
|
92
92
|
lib/excon/response.rb
|
93
93
|
lib/excon/socket.rb
|
94
|
+
lib/excon/ssl_socket.rb
|
94
95
|
tests/basic_tests.rb
|
95
96
|
tests/header_tests.rb
|
96
97
|
tests/idempotent_tests.rb
|
@@ -101,6 +102,7 @@ Gem::Specification.new do |s|
|
|
101
102
|
tests/rackups/query_string.ru
|
102
103
|
tests/rackups/request_methods.ru
|
103
104
|
tests/rackups/response_header.ru
|
105
|
+
tests/rackups/ssl.ru
|
104
106
|
tests/rackups/thread_safety.ru
|
105
107
|
tests/request_method_tests.rb
|
106
108
|
tests/stub_tests.rb
|
data/lib/excon.rb
CHANGED
data/lib/excon/connection.rb
CHANGED
@@ -227,7 +227,11 @@ module Excon
|
|
227
227
|
private
|
228
228
|
|
229
229
|
def socket
|
230
|
-
sockets[@socket_key] ||=
|
230
|
+
sockets[@socket_key] ||= if @connection[:scheme] == 'https'
|
231
|
+
Excon::SSLSocket.new(@connection, @proxy)
|
232
|
+
else
|
233
|
+
Excon::Socket.new(@connection, @proxy)
|
234
|
+
end
|
231
235
|
end
|
232
236
|
|
233
237
|
def sockets
|
data/lib/excon/constants.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
module Excon
|
2
2
|
unless const_defined?(:VERSION)
|
3
|
-
VERSION = '0.7.
|
3
|
+
VERSION = '0.7.1'
|
4
4
|
end
|
5
5
|
|
6
6
|
unless const_defined?(:CHUNK_SIZE)
|
7
7
|
CHUNK_SIZE = 1048576 # 1 megabyte
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
unless const_defined?(:HTTP_VERBS)
|
11
11
|
HTTP_VERBS = %w{connect delete get head options post put trace}
|
12
12
|
end
|
data/lib/excon/socket.rb
CHANGED
@@ -10,73 +10,30 @@ module Excon
|
|
10
10
|
@connection_params, @proxy = connection_params, proxy
|
11
11
|
@read_buffer, @write_buffer = '', ''
|
12
12
|
|
13
|
+
@sockaddr = if @proxy
|
14
|
+
::Socket.sockaddr_in(@proxy[:port], @proxy[:host])
|
15
|
+
else
|
16
|
+
::Socket.sockaddr_in(@connection_params[:port], @connection_params[:host])
|
17
|
+
end
|
18
|
+
|
13
19
|
@socket = ::Socket.new(::Socket::Constants::AF_INET, ::Socket::Constants::SOCK_STREAM, 0)
|
14
20
|
|
21
|
+
connect
|
22
|
+
|
23
|
+
@socket
|
24
|
+
end
|
25
|
+
|
26
|
+
def connect
|
15
27
|
# nonblocking connect
|
16
|
-
if @proxy
|
17
|
-
sockaddr = ::Socket.sockaddr_in(@proxy[:port], @proxy[:host])
|
18
|
-
else
|
19
|
-
sockaddr = ::Socket.sockaddr_in(@connection_params[:port], @connection_params[:host])
|
20
|
-
end
|
21
28
|
begin
|
22
|
-
@socket.connect_nonblock(sockaddr)
|
29
|
+
@socket.connect_nonblock(@sockaddr)
|
23
30
|
rescue Errno::EINPROGRESS
|
24
31
|
IO.select(nil, [@socket], nil, @connection_params[:connect_timeout])
|
25
32
|
begin
|
26
|
-
@socket.connect_nonblock(sockaddr)
|
33
|
+
@socket.connect_nonblock(@sockaddr)
|
27
34
|
rescue Errno::EISCONN
|
28
35
|
end
|
29
36
|
end
|
30
|
-
|
31
|
-
# ssl setup
|
32
|
-
if @connection_params[:scheme] == 'https'
|
33
|
-
# create ssl context
|
34
|
-
ssl_context = OpenSSL::SSL::SSLContext.new
|
35
|
-
|
36
|
-
if Excon.ssl_verify_peer
|
37
|
-
# turn verification on
|
38
|
-
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
39
|
-
|
40
|
-
if Excon.ssl_ca_path
|
41
|
-
ssl_context.ca_path = Excon.ssl_ca_path
|
42
|
-
else
|
43
|
-
# use default cert store
|
44
|
-
store = OpenSSL::X509::Store.new
|
45
|
-
store.set_default_paths
|
46
|
-
ssl_context.cert_store = store
|
47
|
-
end
|
48
|
-
else
|
49
|
-
# turn verification off
|
50
|
-
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
51
|
-
end
|
52
|
-
|
53
|
-
if @connection_params.has_key?(:client_cert) && @connection_params.has_key?(:client_key)
|
54
|
-
ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(@connection_params[:client_cert]))
|
55
|
-
ssl_context.key = OpenSSL::PKey::RSA.new(File.read(@connection_params[:client_key]))
|
56
|
-
end
|
57
|
-
|
58
|
-
@socket = OpenSSL::SSL::SSLSocket.new(@socket, ssl_context)
|
59
|
-
@socket.sync_close = true
|
60
|
-
|
61
|
-
if @proxy
|
62
|
-
@socket << "CONNECT " << @connection_params[:host] << ":" << @connection_params[:port] << HTTP_1_1
|
63
|
-
@socket << "Host: " << @connection_params[:host] << ":" << @connection_params[:port] << CR_NL << CR_NL
|
64
|
-
|
65
|
-
# eat the proxy's connection response
|
66
|
-
while line = @socket.readline.strip
|
67
|
-
break if line.empty?
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
if @connection_params[:scheme] == 'https'
|
73
|
-
# verify connection
|
74
|
-
if Excon.ssl_verify_peer
|
75
|
-
@socket.post_connection_check(@connection_params[:host])
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
@socket
|
80
37
|
end
|
81
38
|
|
82
39
|
def read(max_length)
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Excon
|
2
|
+
class SSLSocket < Socket
|
3
|
+
|
4
|
+
# backwards compatability for 1.8.x SSLSocket (which lack nonblock)
|
5
|
+
unless OpenSSL::SSL::SSLSocket.public_method_defined?(:connect_nonblock)
|
6
|
+
|
7
|
+
undef_method :connect
|
8
|
+
def connect
|
9
|
+
@socket.connect(@sockaddr)
|
10
|
+
end
|
11
|
+
|
12
|
+
undef_method :read
|
13
|
+
def_delegators(:@socket, :read, :read)
|
14
|
+
|
15
|
+
undef_method :write
|
16
|
+
def_delegators(:@socket, :write, :write)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(connection_params = {}, proxy = {})
|
21
|
+
super
|
22
|
+
|
23
|
+
# create ssl context
|
24
|
+
ssl_context = OpenSSL::SSL::SSLContext.new
|
25
|
+
|
26
|
+
if Excon.ssl_verify_peer
|
27
|
+
# turn verification on
|
28
|
+
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
29
|
+
|
30
|
+
if Excon.ssl_ca_path
|
31
|
+
ssl_context.ca_path = Excon.ssl_ca_path
|
32
|
+
else
|
33
|
+
# use default cert store
|
34
|
+
store = OpenSSL::X509::Store.new
|
35
|
+
store.set_default_paths
|
36
|
+
ssl_context.cert_store = store
|
37
|
+
end
|
38
|
+
else
|
39
|
+
# turn verification off
|
40
|
+
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
41
|
+
end
|
42
|
+
|
43
|
+
if @connection_params.has_key?(:client_cert) && @connection_params.has_key?(:client_key)
|
44
|
+
ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(@connection_params[:client_cert]))
|
45
|
+
ssl_context.key = OpenSSL::PKey::RSA.new(File.read(@connection_params[:client_key]))
|
46
|
+
end
|
47
|
+
|
48
|
+
@socket = OpenSSL::SSL::SSLSocket.new(@socket, ssl_context)
|
49
|
+
@socket.sync_close = true
|
50
|
+
|
51
|
+
if @proxy
|
52
|
+
@socket << "CONNECT " << @connection_params[:host] << ":" << @connection_params[:port] << HTTP_1_1
|
53
|
+
@socket << "Host: " << @connection_params[:host] << ":" << @connection_params[:port] << CR_NL << CR_NL
|
54
|
+
|
55
|
+
# eat the proxy's connection response
|
56
|
+
while line = @socket.readline.strip
|
57
|
+
break if line.empty?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# connect the new OpenSSL::SSL::SSLSocket
|
62
|
+
@socket.connect
|
63
|
+
|
64
|
+
# verify connection
|
65
|
+
if Excon.ssl_verify_peer
|
66
|
+
@socket.post_connection_check(@connection_params[:host])
|
67
|
+
end
|
68
|
+
|
69
|
+
@socket
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
data/tests/basic_tests.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'sinatra'
|
3
|
+
require 'webrick'
|
4
|
+
require 'webrick/https'
|
5
|
+
|
6
|
+
class App < Sinatra::Base
|
7
|
+
get('/content-length/:value') do |value|
|
8
|
+
headers("Custom" => "Foo: bar")
|
9
|
+
'x' * value.to_i
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
Rack::Handler::WEBrick.run(App, {
|
14
|
+
:Port => 9443,
|
15
|
+
:SSLCertName => [["CN", WEBrick::Utils::getservername]],
|
16
|
+
:SSLEnable => true,
|
17
|
+
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE
|
18
|
+
})
|
data/tests/test_helper.rb
CHANGED
@@ -3,10 +3,10 @@ require 'bundler'
|
|
3
3
|
|
4
4
|
Bundler.require(:default, :development)
|
5
5
|
|
6
|
-
def basic_tests
|
6
|
+
def basic_tests(url = 'http://127.0.0.1:9292')
|
7
7
|
tests('GET /content-length/100') do
|
8
8
|
|
9
|
-
connection = Excon.new(
|
9
|
+
connection = Excon.new(url)
|
10
10
|
response = connection.request(:method => :get, :path => '/content-length/100')
|
11
11
|
|
12
12
|
tests('response.status').returns(200) do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: excon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 1
|
10
|
+
version: 0.7.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- geemus (Wesley Beary)
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-13 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: open4
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- lib/excon/errors.rb
|
113
113
|
- lib/excon/response.rb
|
114
114
|
- lib/excon/socket.rb
|
115
|
+
- lib/excon/ssl_socket.rb
|
115
116
|
- tests/basic_tests.rb
|
116
117
|
- tests/header_tests.rb
|
117
118
|
- tests/idempotent_tests.rb
|
@@ -122,6 +123,7 @@ files:
|
|
122
123
|
- tests/rackups/query_string.ru
|
123
124
|
- tests/rackups/request_methods.ru
|
124
125
|
- tests/rackups/response_header.ru
|
126
|
+
- tests/rackups/ssl.ru
|
125
127
|
- tests/rackups/thread_safety.ru
|
126
128
|
- tests/request_method_tests.rb
|
127
129
|
- tests/stub_tests.rb
|