@@ -88,9 +88,7 @@ def download(command)
88
88
89
89
print ( "Downloading '#{ command } ' under '#{ bin } '... " )
90
90
url = build_url ( command )
91
- resp = get ( url , code : 302 ) # Latest
92
- resp = get ( resp [ 'location' ] , code : 302 ) # vX.Y.Z
93
- resp = get ( resp [ 'location' ] , code : 200 ) # Binary
91
+ resp = get ( url , code : 200 , max_retries : 4 )
94
92
95
93
if url . end_with? ( '.zip' )
96
94
Zip ::File . open_buffer ( resp . body ) do |zip |
@@ -134,15 +132,19 @@ def build_url(command)
134
132
end
135
133
136
134
# TODO: Retry transient errors
137
- def get ( url , code : nil )
135
+ def get ( url , code : nil , max_retries : )
138
136
uri = URI . parse ( url )
139
- Net ::HTTP . start ( uri . host , uri . port , use_ssl : uri . scheme == 'https' ) do |http |
137
+ resp = Net ::HTTP . start ( uri . host , uri . port , use_ssl : uri . scheme == 'https' ) do |http |
140
138
http . get ( "#{ uri . path } ?#{ uri . query } " )
141
- end . tap do |resp |
142
- if code && resp . code != code . to_s
143
- raise "Expected '#{ url } ' to return #{ code } , but got #{ resp . code } : #{ resp . body } "
144
- end
145
139
end
140
+ if resp . is_a? ( Net ::HTTPRedirection ) && max_retries > 0
141
+ # Follow redirects that lead to the current repository (if sqldef/sqldef is moved),
142
+ # Latest, vX.Y.Z, and to the binary
143
+ return get ( resp [ 'location' ] , code : code , max_retries : max_retries - 1 )
144
+ elsif code && resp . code != code . to_s
145
+ raise "Expected '#{ url } ' to return #{ code } , but got #{ resp . code } : #{ resp . body } "
146
+ end
147
+ resp
146
148
end
147
149
end
148
150
end
0 commit comments