-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclient.rb
More file actions
663 lines (607 loc) · 23.1 KB
/
client.rb
File metadata and controls
663 lines (607 loc) · 23.1 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# See COPYING for license information.
require 'rubygems'
require 'uri'
require 'json'
require "cgi"
module SoftLayer
class Swift
class ClientException < StandardError
attr_reader :scheme, :host, :port, :path, :query, :status, :reason, :devices
def initialize(msg, params={})
@msg = msg
@scheme = params[:http_scheme]
@host = params[:http_host]
@port = params[:http_port]
@path = params[:http_path]
@query = params[:http_query]
@status = params[:http_status]
@reason = params[:http_reason]
@device = params[:http_device]
end
def to_s
a = @msg
b = ''
b += "#{@scheme}://" if @scheme
b += @host if @host
b += ":#{@port}" if @port
b += @path if @path
b += "?#{@query}" if @query
b ? b = "#{b} #{@status}" : b = @status.to_s if @status
b ? b = "#{b} #{@reason}" : b = "- #{@reason}" if @reason
b ? b = "#{b}: device #{@device}" : b = "device #{@device}" if @device
b ? "#{a} #{b}" : a
end
end
class ChunkedConnectionWrapper
def initialize(data, chunk_size)
@size = chunk_size
@file = data
end
def read(foo, out = nil)
if out && out.respond_to?("write")
@file.read(@size, out)
else
@file.read(@size)
end
end
def eof!
@file.eof!
end
def eof?
@file.eof?
end
end
def self.quote(value)
CGI.escape(value)
end
class Query
def initialize(url_params)
if url_params
@params = Query.from_url_params(url_params)
else
@params = {}
end
end
def to_s
to_url_params
end
def to_url_params
elements = []
@params.each_pair {|k,v| elements << "#{k}=#{v}"}
elements.join('&')
end
def self.from_url_params(url_params)
result = {}
url_params.split('&').each do |element|
element = element.split('=')
result[element[0]] = element[1]
end
result
end
def has_key?(key)
@params.has_key? key
end
def add(key, value)
@params[key] = value
end
def delete(key)
@params.delete(key)
end
end
class Client
def initialize(authurl, user, key, retries=5, preauthurl=nil, preauthtoken=nil, snet=false, starting_backoff=1)
@authurl = authurl
@user = user
@key = key
@retries = retries
@http_conn = nil
@url = preauthurl
@token = preauthtoken
@attempts = 0
@snet = snet
@starting_backoff = starting_backoff
end
private
def _retry(reset, func, args=nil)
@attempts = 0
backoff = @starting_backoff
while @attempts < @retries
@attempts += 1
begin
if !@url or !@token
@url, @token = self.get_auth()
@http_conn = nil
end
@http_conn = self.http_connection() if !@http_conn
return Client.method(func).call(@url, @token, *args)
rescue Net::HTTPExceptions
if @attempts > @retries
raise
end
@http_conn = nil
rescue ClientException => err
if @attempts > @retries
raise
end
if err.status.to_i == 401
@url = @token = nil
if @attempts > 1
raise
end
elsif err.status.to_i == 408
@http_conn = nil
elsif err.status.to_i >= 500 and err.status.to_i <= 599
return nil
else
raise
end
end
sleep(backoff)
backoff *= 2
if reset
reset.call(args)
end
end
end
public
def self.http_connection(url, proxy_host=nil, proxy_port=nil)
parsed = URI::parse(url)
if parsed.scheme == 'http'
require 'net/http'
conn = Net::HTTP::Proxy(proxy_host, proxy_port).new(parsed.host, parsed.port)
[parsed, conn]
elsif parsed.scheme == 'https'
require 'net/https'
conn = Net::HTTP::Proxy(proxy_host, proxy_port).new(parsed.host, parsed.port)
conn.use_ssl = true
conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
[parsed, conn]
else
raise ClientException.new(
"Cannot handle protocol scheme #{parsed.scheme} for #{url} %s")
end
end
def http_connection
if !@http_conn
@http_conn = Client.http_connection(@url)
else
@http_conn
end
end
def self.get_auth(url, user, key, snet=false)
parsed, conn = http_connection(url)
conn.start if not conn.started?
resp = conn.get(URI.encode(parsed.request_uri), {"x-auth-user" => user, "x-auth-key" => key })
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Account GET failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code,
:http_reason=>resp.message)
end
url = URI::parse(resp.header['x-storage-url'])
if snet
url.host = "snet-#{url.host}"
end
[url.to_s, resp.header['x-auth-token'], resp.header]
end
def get_auth
@url, @token = Client.get_auth(@authurl, @user, @key, @snet)
end
def self.get_account(url, token, marker=nil, limit=nil, prefix=nil,
http_conn=nil, full_listing=false, cdn_only = false)
#todo: add in rest of functionality
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
if full_listing
rv = get_account(url, token, marker, limit, prefix, http_conn)
listing = rv[1]
while listing.length > 0
marker = listing[-1]['name']
listing = get_account(url, token, marker, limit, prefix, http_conn)[1]
if listing.length > 0
rv[1] += listing
end
end
return rv
end
query = Query.new(parsed.query)
query.add('format', 'json')
query.add('marker', SoftLayer::Swift.quote(marker.to_s)) if marker
query.add('limit', SoftLayer::Swift.quote(limit.to_s)) if limit
query.add('prefix', SoftLayer::Swift.quote(prefix.to_s)) if prefix
parsed.query = query.to_url_params
conn.start if !conn.started?
headers = {'x-auth-token' => token}
headers['X-Context'] = 'cdn' if cdn_only
resp = conn.get(parsed.request_uri, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Account GET failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code,
:http_reason=>resp.message)
end
resp_headers = {}
resp.header.each do |k,v|
resp_headers[k.downcase] = v
end
if resp.code.to_i == 204
[resp_headers, []]
else
[resp_headers, JSON.parse(resp.body)]
end
end
def get_account(marker=nil, limit=nil, prefix=nil, full_listing=false)
_retry(nil, :get_account, [marker, limit, prefix, @http_conn, full_listing])
end
def self.head_account(url, token, http_conn=nil)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
conn.start if !conn.started?
resp = conn.head(parsed.request_uri, {'x-auth-token' => token})
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Account HEAD failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
resp_headers = {}
resp.header.each do |k,v|
resp_headers[k.downcase] = v
end
resp_headers
end
def head_account
_retry(nil, :head_account, [@http_conn])
end
def self.post_account(url, token, headers, http_conn=nil)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
headers['x-auth-token'] = token
conn.start if !conn.started?
resp = conn.post(parsed.request_uri, nil, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Account POST failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
resp.body
end
def post_account(headers=nil)
_retry(nil, :post_account, [headers, @http_conn])
end
def self.get_container(url, token, container, marker=nil, limit=nil,
prefix=nil, delimiter=nil, http_conn=nil, full_listing=nil)
#todo: add in rest of functionality
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
if full_listing
rv = get_account(url, token, marker, limit, prefix, http_conn)
listing = rv[1]
while listing.length > 0
marker = listing[-1]['name']
listing = get_account(url, token, marker, limit, prefix, http_conn)[1]
if listing.length > 0
rv[1] += listing
end
end
return rv
end
query = Query.new(parsed.query)
query.add('format', 'json')
query.add('marker', SoftLayer::Swift.quote(marker.to_s)) if marker
query.add('limit', SoftLayer::Swift.quote(limit.to_s)) if limit
query.add('prefix', SoftLayer::Swift.quote(prefix.to_s)) if prefix
parsed.query = query.to_url_params
conn.start if !conn.started?
parsed.path += "/#{container}"
resp = conn.get(parsed.request_uri, {'x-auth-token' => token})
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Container GET failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code,
:http_reason=>resp.message)
end
resp_headers = {}
resp.header.each do |k,v|
resp_headers[k.downcase] = v
end
if resp.code.to_i == 204
[resp_headers, []]
else
[resp_headers, JSON.parse(resp.body())]
end
end
def get_container(container, marker=nil, limit=nil, prefix=nil, delimiter=nil, full_listing=nil)
_retry(nil, :get_container, [container, marker, limit, prefix, delimiter, full_listing])
end
def self.head_container(url, token, container, http_conn=nil, cdn = false)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
conn.start if !conn.started?
parsed.path += "/#{container}"
headers = {'x-auth-token' => token}
headers['x-context'] = 'cdn' if cdn
resp = conn.head(parsed.request_uri, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Container HEAD failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message + headers.inspect)
end
resp_headers = {}
resp.header.each do |k,v|
resp_headers[k.downcase] = v
end
resp_headers
end
def head_container(container)
_retry(nil, :head_container, [container, @http_conn])
end
def self.put_container(url, token, container, headers={}, http_conn=nil)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
conn.start if !conn.started?
parsed.path += "/#{container}"
headers['x-auth-token'] = token
# headers['content-length'] = 0
resp = conn.put(parsed.request_uri, nil, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Container PUT failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
end
def put_container(container, headers={})
_retry(nil, :put_container, [container, headers, @http_conn])
end
def self.post_container(url, token, container, headers={}, http_conn=nil)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
conn.start if !conn.started?
parsed.path += "/#{container}"
headers['x-auth-token'] = token
resp = conn.post(parsed.request_uri, nil, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Container POST failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message + "\n#{headers.inspect}")
end
end
def post_container(container, headers={})
_retry(nil, :post_container, [container, headers])
end
def self.delete_container(url, token, container, headers={}, http_conn=nil, recursive = false)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
conn.start if !conn.started?
parsed.path += "/#{container}"
headers['x-auth-token'] = token
parsed.query = "recursive=true" if recursive
resp = conn.delete(parsed.request_uri, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Container DELETE failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
end
def delete_container(container)
_retry(nil, :delete_container, [container])
end
def self.get_object(url, token, container, name, http_conn=nil, resp_chunk_size=nil, &block)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
parsed.path += "/#{container}/#{name}"
conn.start if not conn.started?
headers = {'x-auth-token' => token}
if block_given?
resp = conn.request_get(parsed.request_uri, headers) do |r|
r.read_body do |b|
yield b
end
end
object_body = nil
else
resp = conn.request_get(parsed.request_uri, headers)
object_body = resp.body
end
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Object GET failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
resp_headers = {}
resp.header.each do |k,v|
resp_headers[k.downcase] = v
end
[resp_headers, object_body]
end
def get_object(container, name, resp_chunk_size=nil)
_retry(nil, :get_object, [container, name, resp_chunk_size])
end
def self.head_object(url, token, container, name, http_conn=nil)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
parsed.path += "/#{container}/#{name}"
conn.start if not conn.started?
resp = conn.head(parsed.request_uri, {'x-auth-token' => token})
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Object HEAD failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
resp_headers = {}
resp.header.each do |k,v|
resp_headers[k.downcase] = v
end
resp_headers
end
def head_object(container, name)
_retry(nil, :head_object, [container, name])
end
def self.put_object(url, token=nil, container=nil, name=nil, contents=nil,
content_length=nil, etag=nil, chunk_size=nil,
content_type=nil, headers={}, http_conn=nil, proxy=nil)
chunk_size ||= 65536
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
parsed.path += "/#{container}" if container
parsed.path += "/#{name}" if name
headers['x-auth-token'] = token if token
headers['etag'] = etag if etag
if content_length != nil
headers['content-length'] = content_length.to_s
else
headers.each do |k,v|
if k.downcase == 'content-length'
content_length = v.to_i
end
end
end
headers['content-type'] = content_type if content_type
headers['content-length'] = '0' if not contents
if contents.respond_to? :read
request = Net::HTTP::Put.new(parsed.request_uri, headers)
chunked = ChunkedConnectionWrapper.new(contents, chunk_size)
if content_length == nil
request['Transfer-Encoding'] = 'chunked'
request.delete('content-length')
end
request.body_stream = chunked
resp = conn.start do |http|
http.request(request)
end
else
conn.start if not conn.started?
resp = conn.put(parsed.request_uri, contents, headers)
end
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Object PUT failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
resp.header['etag']
end
def put_object(container, obj, contents, content_length=nil, etag=nil, chunk_size=65536, content_type=nil, headers={})
_default_reset = Proc.new do |args|
raise ClientException("put_object(#{container}, #{obj}, ...) failure and no ability to reset contents for reupload.")
end
reset_func = _default_reset
if (contents.respond_to? :seek) and (contents.respond_to? :tell)
orig_pos = contents.tell
reset_func = Proc.new {|a| contents.seek(orig_pos)}
elsif !contents
reset_func = Proc.new {|a| nil }
end
_retry(reset_func, :put_object, [container, obj, contents, content_length, etag, chunk_size, content_type, headers])
end
def self.post_object(url, token=nil, container=nil, name=nil, headers={}, http_conn=nil)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
parsed.path += "/#{container}" if container
parsed.path += "/#{name}" if name
headers['x-auth-token'] = token if token
resp = conn.post(parsed.request_uri, nil, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Object POST failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
end
def post_object(container, name, headers={})
_retry(nil, :post_object, [container, name, headers])
end
def self.delete_object(url, token=nil, container=nil, name=nil, http_conn=nil, headers={}, proxy=nil)
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
conn.start if !conn.started?
parsed.path += "/#{container}" if container
parsed.path += "/#{name}" if name
headers['x-auth-token'] = token if token
resp = conn.delete(parsed.request_uri, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('Object DELETE failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
end
def delete_object(container, name, headers={})
_retry(nil, :delete_object, [container, name, headers])
end
def self.search(url, options = {}, token = nil, http_conn = nil, headers={})
if not http_conn
http_conn = http_connection(url)
end
parsed = http_conn[0].clone
conn = http_conn[1]
conn.start if !conn.started?
options['format'] = 'json'
parsed.query = URI.encode_www_form(options)
headers['x-auth-token'] = token if token
headers['X-Context'] = 'search'
resp = conn.get(parsed.request_uri, headers)
if resp.code.to_i < 200 or resp.code.to_i > 300
raise ClientException.new('search failed', :http_scheme=>parsed.scheme,
:http_host=>conn.address, :http_port=>conn.port,
:http_path=>parsed.path, :http_status=>resp.code,
:http_reason=>resp.message)
end
response = {
:count => resp.header["X-Search-Items-Count"].to_i,
:total => resp.header["X-Search-Items-Total"].to_i,
}
if resp.body
response[:items] = JSON.parse(resp.body)
end
response
end
end
end
end