@@ -23,6 +23,7 @@ def initialize(url, on_redirect: nil, auto_inflate: true, username: nil, passwor
23
23
@user_agent = user_agent
24
24
@last_modified = last_modified
25
25
@etag = etag
26
+ @redirects = [ ]
26
27
end
27
28
28
29
def download
@@ -32,7 +33,7 @@ def download
32
33
33
34
response = request
34
35
if response . status . code == 304
35
- Response . new ( tempfile : Tempfile . new , response : response , parsed_url : @parsed_url )
36
+ Response . new ( tempfile : Tempfile . new , response : response , parsed_url : @parsed_url , redirects : @redirects )
36
37
else
37
38
download_to_file ( response )
38
39
end
@@ -59,7 +60,7 @@ def download_to_file(response)
59
60
tempfile . open # flush written content
60
61
tempfile . rewind
61
62
62
- Response . new ( tempfile : tempfile , response : response , parsed_url : @parsed_url )
63
+ Response . new ( tempfile : tempfile , response : response , parsed_url : @parsed_url , redirects : @redirects )
63
64
rescue
64
65
tempfile &.close
65
66
raise
@@ -68,7 +69,7 @@ def download_to_file(response)
68
69
def client
69
70
http = HTTP
70
71
. headers ( headers )
71
- . follow ( max_hops : 4 , on_redirect : @ on_redirect)
72
+ . follow ( max_hops : 4 , on_redirect : on_redirect )
72
73
. timeout ( connect : 5 , write : 5 , read : 30 )
73
74
. encoding ( Encoding ::BINARY )
74
75
@@ -93,6 +94,12 @@ def basic_auth
93
94
end
94
95
end
95
96
97
+ def on_redirect
98
+ proc do |from , to |
99
+ @redirects . push Redirect . new ( status : from . status . code , from : from . uri . to_s , to : to . uri . to_s )
100
+ end
101
+ end
102
+
96
103
def request
97
104
response = client . get ( @parsed_url . url , ssl_context : ssl_context )
98
105
response_error! ( response ) unless success? ( response )
0 commit comments