Skip to content

Commit a90b53f

Browse files
author
Wesley Beary
committed
cleanup/consistancy in naming, fix rakefile
1 parent f10140d commit a90b53f

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rescue LoadError
1616
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
1717
end
1818

19-
require File.join(File.dirname(__FILE__), 'lib', 'shindo', 'rake')
19+
require 'shindo/rake'
2020
Shindo::Rake.new
2121

2222
task :test => :check_dependencies

lib/excon/connection.rb

+29-28
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ def request(params)
2626
request << "#{key}: #{value}\r\n"
2727
end
2828
request << "\r\n"
29-
connection.write(request)
29+
socket.write(request)
3030

3131
if params[:body]
3232
if params[:body].is_a?(String)
33-
connection.write(params[:body])
33+
socket.write(params[:body])
3434
else
3535
while chunk = params[:body].read(CHUNK_SIZE)
36-
connection.write(chunk)
36+
socket.write(chunk)
3737
end
3838
end
3939
end
4040

4141
response = Excon::Response.new
42-
response.status = connection.readline[9..11].to_i
42+
response.status = socket.readline[9..11].to_i
4343
while true
44-
data = connection.readline.chop!
44+
data = socket.readline.chop!
4545
unless data.empty?
4646
key, value = data.split(': ')
4747
response.headers[key] = value
@@ -61,31 +61,31 @@ def request(params)
6161
if response.headers['Content-Length']
6262
remaining = response.headers['Content-Length'].to_i
6363
while remaining > 0
64-
block.call(connection.read([CHUNK_SIZE, remaining].min))
64+
block.call(socket.read([CHUNK_SIZE, remaining].min))
6565
remaining -= CHUNK_SIZE
6666
end
6767
elsif response.headers['Transfer-Encoding'] == 'chunked'
6868
while true
69-
chunk_size = connection.readline.chop!.to_i(16)
70-
chunk = connection.read(chunk_size + 2).chop! # 2 == "/r/n".length
69+
chunk_size = socket.readline.chop!.to_i(16)
70+
chunk = socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
7171
if chunk_size > 0
7272
block.call(chunk)
7373
else
7474
break
7575
end
7676
end
7777
elsif response.headers['Connection'] == 'close'
78-
block.call(connection.read)
79-
Thread.current[:_excon_connections][@uri.to_s] = nil
78+
block.call(socket.read)
79+
reset_socket
8080
end
8181
end
82-
rescue => connection_error
83-
Thread.current[:_excon_connections][@uri.to_s] = nil
84-
raise(connection_error)
82+
rescue => socket_error
83+
reset_socket
84+
raise(socket_error)
8585
end
8686

8787
if params[:expects] && ![*params[:expects]].include?(response.status)
88-
Thread.current[:_excon_connections][@uri.to_s] = nil
88+
reset_socket
8989
raise(Excon::Errors.status_error(params, response))
9090
else
9191
response
@@ -106,27 +106,28 @@ def request(params)
106106

107107
private
108108

109-
def connection
110-
Thread.current[:_excon_connections] ||= {}
111-
if !Thread.current[:_excon_connections][@uri.to_s] || Thread.current[:_excon_connections][@uri.to_s].closed?
112-
Thread.current[:_excon_connections][@uri.to_s] = establish_connection
113-
end
114-
Thread.current[:_excon_connections][@uri.to_s]
115-
end
116-
117-
def establish_connection
118-
connection = TCPSocket.open(@uri.host, @uri.port)
109+
def reset_socket
110+
new_socket = TCPSocket.open(@uri.host, @uri.port)
119111

120112
if @uri.scheme == 'https'
121113
@ssl_context = OpenSSL::SSL::SSLContext.new
122114
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
123-
connection = OpenSSL::SSL::SSLSocket.new(connection, @ssl_context)
124-
connection.sync_close = true
125-
connection.connect
115+
new_socket = OpenSSL::SSL::SSLSocket.new(new_socket, @ssl_context)
116+
new_socket.sync_close = true
117+
new_socket.connect
126118
end
127119

128-
connection
120+
Thread.current[:_excon_sockets][@uri.to_s] = new_socket
129121
end
122+
123+
def socket
124+
Thread.current[:_excon_sockets] ||= {}
125+
if !Thread.current[:_excon_sockets][@uri.to_s] || Thread.current[:_excon_sockets][@uri.to_s].closed?
126+
reset_socket
127+
end
128+
Thread.current[:_excon_sockets][@uri.to_s]
129+
end
130+
130131
end
131132
end
132133

0 commit comments

Comments
 (0)