Class: Excon::Connection
- Inherits:
-
Object
show all
- Includes:
- Utils
- Defined in:
- lib/excon/connection.rb
Constant Summary
Constants included
from Utils
Utils::CONTROL, Utils::DELIMS, Utils::ESCAPED, Utils::NONASCII, Utils::UNESCAPED, Utils::UNWISE
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
#binary_encode, #connection_uri, #escape_uri, #headers_hash_to_s, #port_string, #query_string, #redact, #request_uri, #split_header_value, #unescape_form, #unescape_uri
Constructor Details
#initialize(params = {}) ⇒ Connection
Initializes a new Connection instance
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
|
# File 'lib/excon/connection.rb', line 64
def initialize(params = {})
@pid = Process.pid
@data = Excon.defaults.dup
@data[:headers] = @data[:headers].dup
@data[:middlewares] = @data[:middlewares].dup
@data.merge!(params)
validate_params(:connection, @data, @data[:middlewares])
if @data.key?(:host) && !@data.key?(:hostname)
Excon.display_warning('hostname is missing! For IPv6 support, provide both host and hostname: Excon::Connection#new(:host => uri.host, :hostname => uri.hostname, ...).')
@data[:hostname] = @data[:host]
end
setup_proxy
if ENV.has_key?('EXCON_STANDARD_INSTRUMENTOR')
@data[:instrumentor] = Excon::StandardInstrumentor
end
if @data[:debug] || ENV.has_key?('EXCON_DEBUG')
@data[:debug_request] = @data[:debug_response] = true
@data[:instrumentor] = Excon::StandardInstrumentor
end
if @data[:scheme] == UNIX
if @data[:host] && !@data[:host].empty?
raise ArgumentError, "The `:host` parameter should not be set for `unix://` connections.\n" +
"When supplying a `unix://` URI, it should start with `unix:/` or `unix:///`."
elsif !@data[:socket]
raise ArgumentError, 'You must provide a `:socket` for `unix://` connections'
else
@socket_key = "#{@data[:scheme]}://#{@data[:socket]}"
end
else
@socket_key = "#{@data[:scheme]}://#{@data[:host]}#{port_string(@data)}"
end
reset
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
8
9
10
|
# File 'lib/excon/connection.rb', line 8
def data
@data
end
|
Instance Method Details
#batch_requests(pipeline_params, limit = nil) ⇒ Object
Sends the supplied requests to the destination host using pipelining in batches of @limit [Numeric] requests. This is your soft file descriptor limit by default, typically 256.
350
351
352
353
354
355
356
357
358
359
|
# File 'lib/excon/connection.rb', line 350
def batch_requests(pipeline_params, limit = nil)
limit ||= Process.respond_to?(:getrlimit) ? Process.getrlimit(:NOFILE).first : 256
responses = []
pipeline_params.each_slice(limit) do |params|
responses.concat(requests(params))
end
responses
end
|
#connection ⇒ Object
10
11
12
13
|
# File 'lib/excon/connection.rb', line 10
def connection
Excon.display_warning('Excon::Connection#connection is deprecated use Excon::Connection#data instead.')
@data
end
|
#connection=(new_params) ⇒ Object
14
15
16
17
|
# File 'lib/excon/connection.rb', line 14
def connection=(new_params)
Excon.display_warning('Excon::Connection#connection= is deprecated. Use of this method may cause unexpected results.')
@data = new_params
end
|
#error_call(datum) ⇒ Object
109
110
111
112
113
|
# File 'lib/excon/connection.rb', line 109
def error_call(datum)
if datum[:error]
raise(datum[:error])
end
end
|
#inspect ⇒ Object
387
388
389
390
391
392
393
394
395
396
397
398
399
|
# File 'lib/excon/connection.rb', line 387
def inspect
vars = instance_variables.inject({}) do |accum, var|
accum.merge!(var.to_sym => instance_variable_get(var))
end
vars[:'@data'] = Utils.redact(vars[:'@data'])
inspection = '#<Excon::Connection:'
inspection += (object_id << 1).to_s(16)
vars.each do |key, value|
inspection += " #{key}=#{value.inspect}"
end
inspection += '>'
inspection
end
|
#logger ⇒ Object
37
38
39
40
41
|
# File 'lib/excon/connection.rb', line 37
def logger
if @data[:instrumentor] && @data[:instrumentor].respond_to?(:logger)
@data[:instrumentor].logger
end
end
|
#logger=(logger) ⇒ Object
42
43
44
45
|
# File 'lib/excon/connection.rb', line 42
def logger=(logger)
@data[:instrumentor] = Excon::LoggingInstrumentor
@data[:logger] = logger
end
|
#params ⇒ Object
19
20
21
22
|
# File 'lib/excon/connection.rb', line 19
def params
Excon.display_warning('Excon::Connection#params is deprecated use Excon::Connection#data instead.')
@data
end
|
#params=(new_params) ⇒ Object
23
24
25
26
|
# File 'lib/excon/connection.rb', line 23
def params=(new_params)
Excon.display_warning('Excon::Connection#params= is deprecated. Use of this method may cause unexpected results.')
@data = new_params
end
|
#proxy ⇒ Object
28
29
30
31
|
# File 'lib/excon/connection.rb', line 28
def proxy
Excon.display_warning('Excon::Connection#proxy is deprecated use Excon::Connection#data[:proxy] instead.')
@data[:proxy]
end
|
#proxy=(new_proxy) ⇒ Object
32
33
34
35
|
# File 'lib/excon/connection.rb', line 32
def proxy=(new_proxy)
Excon.display_warning('Excon::Connection#proxy= is deprecated. Use of this method may cause unexpected results.')
@data[:proxy] = new_proxy
end
|
#request(params = {}) {|chunk| ... } ⇒ Object
Sends the supplied request to the destination host.
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
319
|
# File 'lib/excon/connection.rb', line 230
def request(params={}, &block)
datum = @data.merge(params)
datum[:headers] = @data[:headers].merge(datum[:headers] || {})
validate_params(:request, params, datum[:middlewares])
if params[:middlewares]
validate_params(:connection, @data, datum[:middlewares])
end
if datum[:user] || datum[:password]
user, pass = Utils.unescape_uri(datum[:user].to_s), Utils.unescape_uri(datum[:password].to_s)
datum[:headers]['Authorization'] ||= 'Basic ' + ["#{user}:#{pass}"].pack('m').delete(Excon::CR_NL)
end
if datum[:scheme] == UNIX
datum[:headers]['Host'] ||= ''
else
datum[:headers]['Host'] ||= datum[:host] + port_string(datum)
end
if (host = datum[:headers].delete('Host'))
datum[:headers] = { 'Host' => host }.merge(datum[:headers])
end
unless datum[:method]
datum[:method] = :get
end
unless datum[:path][0, 1] == '/'
datum[:path] = datum[:path].dup.insert(0, '/')
end
if block_given?
Excon.display_warning('Excon requests with a block are deprecated, pass :response_block instead.')
datum[:response_block] = block
end
datum[:connection] = self
if datum[:persistent] && !@persistent_socket_reusable
reset
end
datum[:stack] = datum[:middlewares].map do |middleware|
lambda {|stack| middleware.new(stack)}
end.reverse.inject(self) do |middlewares, middleware|
middleware.call(middlewares)
end
datum = datum[:stack].request_call(datum)
unless datum[:pipeline]
@persistent_socket_reusable = false
datum = response(datum)
@persistent_socket_reusable = true
if datum[:persistent]
if (key = datum[:response][:headers].keys.detect {|k| k.casecmp('Connection') == 0 })
if datum[:response][:headers][key].casecmp('close') == 0
reset
end
end
else
reset
end
Excon::Response.new(datum[:response])
else
datum
end
rescue => error
reset
raise error if !datum
datum[:error] = error
if datum[:stack]
datum[:stack].error_call(datum)
else
raise error
end
end
|
#request_call(datum) ⇒ Object
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
|
# File 'lib/excon/connection.rb', line 115
def request_call(datum)
begin
if datum.has_key?(:response)
return datum
else
socket(datum).data = datum
request = datum[:method].to_s.upcase + ' '
if datum[:proxy] && datum[:scheme] != HTTPS
request << datum[:scheme] << '://' << datum[:host] << port_string(datum)
end
request << datum[:path]
request << query_string(datum)
request << HTTP_1_1
if datum.has_key?(:request_block)
datum[:headers]['Transfer-Encoding'] = 'chunked'
else
body = datum[:body].is_a?(String) ? StringIO.new(datum[:body]) : datum[:body]
unless datum[:method].to_s.casecmp('GET') == 0 && body.nil?
unless datum[:headers].has_key?('Content-Length')
datum[:headers]['Content-Length'] = detect_content_length(body)
end
end
end
request << Utils.(datum[:headers])
request << CR_NL
if datum.has_key?(:request_block)
socket(datum).write(request) while true chunk = datum[:request_block].call
chunk = binary_encode(chunk)
if chunk.length > 0
socket(datum).write(chunk.length.to_s(16) << CR_NL << chunk << CR_NL)
else
socket(datum).write(String.new("0#{CR_NL}#{CR_NL}"))
break
end
end
elsif body.nil?
socket(datum).write(request) else if body.respond_to?(:binmode) && !body.is_a?(StringIO)
body.binmode
end
if body.respond_to?(:rewind)
body.rewind rescue nil
end
request = binary_encode(request)
chunk = body.read([datum[:chunk_size] - request.length, 0].max)
if chunk
chunk = binary_encode(chunk)
socket(datum).write(request << chunk)
else
socket(datum).write(request) end
while (chunk = body.read(datum[:chunk_size]))
socket(datum).write(chunk)
end
end
end
rescue => error
case error
when Excon::Errors::InvalidHeaderKey, Excon::Errors::InvalidHeaderValue, Excon::Errors::StubNotFound, Excon::Errors::Timeout
raise(error)
when Errno::EPIPE
response = socket.read
error = Excon::Error.new(response + error.message)
raise_socket_error(error)
else
raise_socket_error(error)
end
end
datum
end
|
#requests(pipeline_params) ⇒ Object
Sends the supplied requests to the destination host using pipelining.
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/excon/connection.rb', line 323
def requests(pipeline_params)
pipeline_params.each {|params| params.merge!(:pipeline => true, :persistent => true) }
pipeline_params.last.merge!(:persistent => @data[:persistent])
responses = pipeline_params.map do |params|
request(params)
end.map do |datum|
Excon::Response.new(response(datum)[:response])
end
if @data[:persistent]
if (key = responses.last[:headers].keys.detect {|k| k.casecmp('Connection') == 0 })
if responses.last[:headers][key].casecmp('close') == 0
reset
end
end
else
reset
end
responses
end
|
#reset ⇒ Object
361
362
363
364
365
366
|
# File 'lib/excon/connection.rb', line 361
def reset
if (old_socket = sockets.delete(@socket_key))
old_socket.close rescue nil
end
@persistent_socket_reusable = true
end
|
#response_call(datum) ⇒ Object
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/excon/connection.rb', line 209
def response_call(datum)
if datum.has_key?(:response_block) && !(datum[:response][:body].nil? || datum[:response][:body].empty?)
response_body = datum[:response][:body].dup
datum[:response][:body] = ''
content_length = remaining = response_body.bytesize
while remaining > 0
datum[:response_block].call(response_body.slice!(0, [datum[:chunk_size], remaining].min), [remaining - datum[:chunk_size], 0].max, content_length)
remaining -= datum[:chunk_size]
end
end
datum
end
|
#retry_limit ⇒ Object
382
383
384
385
|
# File 'lib/excon/connection.rb', line 382
def retry_limit
Excon.display_warning('Excon::Connection#retry_limit is deprecated, use Excon::Connection#data[:retry_limit].')
@data[:retry_limit] ||= DEFAULT_RETRY_LIMIT
end
|
#retry_limit=(new_retry_limit) ⇒ Object
377
378
379
380
|
# File 'lib/excon/connection.rb', line 377
def retry_limit=(new_retry_limit)
Excon.display_warning('Excon::Connection#retry_limit= is deprecated, pass :retry_limit to the initializer.')
@data[:retry_limit] = new_retry_limit
end
|
#valid_request_keys(middlewares) ⇒ Object
401
402
403
|
# File 'lib/excon/connection.rb', line 401
def valid_request_keys(middlewares)
valid_middleware_keys(middlewares) + Excon::VALID_REQUEST_KEYS
end
|