/
connection.rb
318 lines (284 loc) · 11.5 KB
/
connection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
module Excon
class Connection
attr_reader :connection, :proxy
# Initializes a new Connection instance
# @param [String] url The destination URL
# @param [Hash<Symbol, >] params One or more optional params
# @option params [String] :body Default text to be sent over a socket. Only used if :body absent in Connection#request params
# @option params [Hash<Symbol, String>] :headers The default headers to supply in a request. Only used if params[:headers] is not supplied to Connection#request
# @option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
# @option params [String] :path Default path; appears after 'scheme://host:port/'. Only used if params[:path] is not supplied to Connection#request
# @option params [Fixnum] :port The port on which to connect, to the destination host
# @option params [Hash] :query Default query; appended to the 'scheme://host:port/path/' in the form of '?key=value'. Will only be used if params[:query] is not supplied to Connection#request
# @option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
# @option params [String] :proxy Proxy server; e.g. 'http://myproxy.com:8888'
# @option params [Fixnum] :retry_limit Set how many times we'll retry a failed request. (Default 4)
# @option params [Class] :instrumentor Responds to #instrument as in ActiveSupport::Notifications
# @option params [String] :instrumentor_name Name prefix for #instrument events. Defaults to 'excon'
def initialize(url, params = {})
uri = URI.parse(url)
@connection = Excon.defaults.merge({
:host => uri.host,
:path => uri.path,
:port => uri.port.to_s,
:query => uri.query,
:scheme => uri.scheme,
}).merge!(params)
# merge does not deep-dup, so make sure headers is not the original
@connection[:headers] = @connection[:headers].dup
@proxy = nil
# use proxy from the environment if present
if ENV.has_key?('http_proxy')
@proxy = setup_proxy(ENV['http_proxy'])
elsif params.has_key?(:proxy)
@proxy = setup_proxy(params[:proxy])
end
if @connection[:scheme] == HTTPS
# use https_proxy if that has been specified
if ENV.has_key?('https_proxy')
@proxy = setup_proxy(ENV['https_proxy'])
end
end
if @proxy
@connection[:headers]['Proxy-Connection'] ||= 'Keep-Alive'
end
# Use Basic Auth if url contains a login
if uri.user || uri.password
auth = ["#{uri.user}:#{uri.password}"].pack('m').delete("\r\n")
@connection[:headers]['Authorization'] ||= "Basic #{auth}"
end
@socket_key = '' << @connection[:host] << ':' << @connection[:port]
reset
end
# Sends the supplied request to the destination host.
# @yield [chunk] @see Response#self.parse
# @param [Hash<Symbol, >] params One or more optional params, override defaults set in Connection.new
# @option params [String] :body text to be sent over a socket
# @option params [Hash<Symbol, String>] :headers The default headers to supply in a request
# @option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
# @option params [String] :path appears after 'scheme://host:port/'
# @option params [Fixnum] :port The port on which to connect, to the destination host
# @option params [Hash] :query appended to the 'scheme://host:port/path/' in the form of '?key=value'
# @option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
def request(params, &block)
# connection has defaults, merge in new params to override
params = @connection.merge(params)
params[:headers] = @connection[:headers].merge(params[:headers] || {})
params[:headers]['Host'] ||= '' << params[:host] << ':' << params[:port]
# if path is empty or doesn't start with '/', insert one
unless params[:path][0, 1] == '/'
params[:path].insert(0, '/')
end
if params.has_key?(:instrumentor)
if (retries_remaining ||= params[:retry_limit]) < params[:retry_limit]
event_name = "#{params[:instrumentor_name]}.retry"
else
event_name = "#{params[:instrumentor_name]}.request"
end
params[:instrumentor].instrument(event_name, params) do
request_kernel(params, &block)
end
else
request_kernel(params, &block)
end
rescue => request_error
if params[:idempotent] && [Excon::Errors::SocketError,
Excon::Errors::HTTPStatusError].any? {|ex| request_error.kind_of? ex }
retries_remaining ||= params[:retry_limit]
retries_remaining -= 1
if retries_remaining > 0
if params[:body].respond_to?(:pos=)
params[:body].pos = 0
end
retry
else
if params.has_key?(:instrumentor)
params[:instrumentor].instrument("#{params[:instrumentor_name]}.error", :error => request_error)
end
raise(request_error)
end
else
if params.has_key?(:instrumentor)
params[:instrumentor].instrument("#{params[:instrumentor_name]}.error", :error => request_error)
end
raise(request_error)
end
end
def reset
(old_socket = sockets.delete(@socket_key)) && old_socket.close
end
# Generate HTTP request verb methods
Excon::HTTP_VERBS.each do |method|
eval <<-DEF
def #{method}(params={}, &block)
request(params.merge!(:method => :#{method}), &block)
end
DEF
end
def retry_limit=(new_retry_limit)
puts("Excon::Connection#retry_limit= is deprecated, pass :retry_limit to the initializer (#{caller.first})")
@connection[:retry_limit] = new_retry_limit
end
def retry_limit
puts("Excon::Connection#retry_limit is deprecated, pass :retry_limit to the initializer (#{caller.first})")
@connection[:retry_limit] ||= DEFAULT_RETRY_LIMIT
end
private
def request_kernel(params, &block)
begin
response = if params[:mock]
invoke_stub(params, &block)
else
socket.params = params
# start with "METHOD /path"
request = params[:method].to_s.upcase << ' '
if @proxy
request << params[:scheme] << '://' << params[:host] << ':' << params[:port]
end
request << params[:path]
# add query to path, if there is one
case params[:query]
when String
request << '?' << params[:query]
when Hash
request << '?'
for key, values in params[:query]
if values.nil?
request << key.to_s << '&'
else
for value in [*values]
request << key.to_s << '=' << CGI.escape(value.to_s) << '&'
end
end
end
request.chop! # remove trailing '&'
end
# finish first line with "HTTP/1.1\r\n"
request << HTTP_1_1
# calculate content length and set to handle non-ascii
unless params[:headers].has_key?('Content-Length') || (params[:method].to_s.upcase == "GET" && params[:body].to_s.empty?)
params[:headers]['Content-Length'] = case params[:body]
when File
params[:body].binmode
File.size(params[:body])
when String
if FORCE_ENC
params[:body].force_encoding('BINARY')
end
params[:body].length
else
0
end
end
# add headers to request
for key, values in params[:headers]
for value in [*values]
request << key.to_s << ': ' << value.to_s << CR_NL
end
end
# add additional "\r\n" to indicate end of headers
request << CR_NL
# write out the request, sans body
socket.write(request)
# write out the body
if params[:headers]['Content-Length'] != 0
if params[:body].is_a?(String)
socket.write(params[:body])
else
while chunk = params[:body].read(CHUNK_SIZE)
socket.write(chunk)
end
end
end
# read the response
response = Excon::Response.parse(socket, params, &block)
if response.headers['Connection'] == 'close'
reset
end
response
end
rescue Excon::Errors::StubNotFound => stub_not_found
raise(stub_not_found)
rescue => socket_error
reset
raise(Excon::Errors::SocketError.new(socket_error))
end
if params.has_key?(:expects) && ![*params[:expects]].include?(response.status)
reset
raise(Excon::Errors.status_error(params, response))
else
response
end
end
def invoke_stub(params)
block_given = block_given?
params[:captures] = {:headers => {}} # setup data to hold captures
for stub, response in Excon.stubs
headers_match = !stub.has_key?(:headers) || stub[:headers].keys.all? do |key|
case value = stub[:headers][key]
when Regexp
if match = value.match(params[:headers][key])
params[:captures][:headers][key] = match.captures
end
match
else
value == params[:headers][key]
end
end
non_headers_match = (stub.keys - [:headers]).all? do |key|
case value = stub[key]
when Regexp
if match = value.match(params[key])
params[:captures][key] = match.captures
end
match
else
value == params[key]
end
end
if headers_match && non_headers_match
response_attributes = case response
when Proc
response.call(params)
else
response
end
# don't pass stuff into a block if there was an error
if params[:expects] && ![*params[:expects]].include?(response_attributes[:status])
block_given = false
end
if block_given && response_attributes.has_key?(:body)
body = response_attributes.delete(:body)
content_length = remaining = body.bytesize
i = 0
while i < body.length
yield(body[i, CHUNK_SIZE], [remaining - CHUNK_SIZE, 0].max, content_length)
remaining -= CHUNK_SIZE
i += CHUNK_SIZE
end
end
return Excon::Response.new(response_attributes)
end
end
# if we reach here no stubs matched
raise(Excon::Errors::StubNotFound.new('no stubs matched ' << params.inspect))
end
def socket
sockets[@socket_key] ||= if @connection[:scheme] == HTTPS
Excon::SSLSocket.new(@connection, @proxy)
else
Excon::Socket.new(@connection, @proxy)
end
end
def sockets
Thread.current[:_excon_sockets] ||= {}
end
def setup_proxy(proxy)
uri = URI.parse(proxy)
unless uri.host and uri.port and uri.scheme
raise Excon::Errors::ProxyParseError, "Proxy is invalid"
end
{:host => uri.host, :port => uri.port, :scheme => uri.scheme}
end
end
end