down 5.2.3 → 5.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e7b49c1d00aeebc4f368c816a422aa2525461786d7530ee4e02c7cfaf2fe475
4
- data.tar.gz: '009d5288c3aa7dbacd6cecf223352ecf05be0ab164b9e1f5e9d6f3d489c1c695'
3
+ metadata.gz: 69943db91082a470760e9f809f3cd96f05bd57c4f76869522cdb996215f93c48
4
+ data.tar.gz: 4f7b62080a645c26d5a4ace80e723d707ad6fcdbeaf845f247d6ed4977d96c89
5
5
  SHA512:
6
- metadata.gz: 21f7f6691c45cf9a78fca48004839fdc510c6041ebbf42d15bf6f6f1334ed8a62ce8177c2888f06db7f53b8c43b605deb0a99db57545f6b7b1b391befaa2d48b
7
- data.tar.gz: ac1cd77390f8a28ffcbdadb2c5ccede6a43af015986a6582360214c14050f3d4b118df3e5bc777a9a3abffb6050cea64b789757cf757f25215304c7e8fd54102
6
+ metadata.gz: b6a0a24ade6b6f67bf7e6c91d8aff07e3d18a7b8d67521a78ec0da2516d6518c62d45a0d1ef77176523f64d4a83862ca289aa730c05eaeb6e51e655967c396e3
7
+ data.tar.gz: dafabbb4c78cd47d40cd7b74c1df15270ebc2485dccc52e754879cfe0e844edb3cd1322c7a1f4bc81075f81fd980215b8e8189d4db664537273d473d109dbd55
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 5.3.1 (2022-03-25)
2
+
3
+ * Correctly split cookie headers on `;` instead of `,` when forwarding them on redirects (@ermolaev)
4
+
5
+ ## 5.3.0 (2022-02-20)
6
+
7
+ * Add `:extension` argument to `Down.download` for overriding tempfile extension (@razum2um)
8
+
9
+ * Normalize response header names for http.rb and wget backends (@zarqman)
10
+
11
+ ## 5.2.4 (2021-09-12)
12
+
13
+ * Keep original cookies between redirections (@antprt)
14
+
1
15
  ## 5.2.3 (2021-08-03)
2
16
 
3
17
  * Bump addressable version requirement to 2.8+ to remediate vulnerability (@aldodelgado)
data/README.md CHANGED
@@ -63,6 +63,13 @@ Down.download("http://example.com/image.jpg", destination: "/path/to/destination
63
63
  In this case `Down.download` won't have any return value, so if you need a File
64
64
  object you'll have to create it manually.
65
65
 
66
+ You can also keep the tempfile, but override the extension:
67
+
68
+ ```rb
69
+ tempfile = Down.download("http://example.com/some/file", extension: "txt")
70
+ File.extname(tempfile.path) #=> ".txt"
71
+ ```
72
+
66
73
  ### Basic authentication
67
74
 
68
75
  `Down.download` and `Down.open` will automatically detect and apply HTTP basic
@@ -157,7 +164,7 @@ You can access the response status and headers of the HTTP request that was made
157
164
  ```rb
158
165
  remote_file = Down.open("http://example.com/image.jpg")
159
166
  remote_file.data[:status] #=> 200
160
- remote_file.data[:headers] #=> { ... }
167
+ remote_file.data[:headers] #=> { "Content-Type" => "image/jpeg", ... } (header names are normalized)
161
168
  remote_file.data[:response] # returns the response object
162
169
  ```
163
170
 
data/down.gemspec CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "http", "~> 4.3"
28
28
  end
29
29
  spec.add_development_dependency "posix-spawn" unless RUBY_ENGINE == "jruby"
30
- spec.add_development_dependency "http_parser.rb"
30
+ spec.add_development_dependency "http_parser.rb" unless RUBY_ENGINE == "jruby"
31
31
  spec.add_development_dependency "docker-api"
32
32
  spec.add_development_dependency "warning" if RUBY_VERSION >= "2.4"
33
33
  end
data/lib/down/backend.rb CHANGED
@@ -29,5 +29,12 @@ module Down
29
29
 
30
30
  nil
31
31
  end
32
+
33
+ def normalize_headers(response_headers)
34
+ response_headers.inject({}) do |headers, (downcased_name, value)|
35
+ name = downcased_name.split("-").map(&:capitalize).join("-")
36
+ headers.merge!(name => value)
37
+ end
38
+ end
32
39
  end
33
40
  end
data/lib/down/http.rb CHANGED
@@ -25,7 +25,7 @@ module Down
25
25
 
26
26
  # Downlods the remote file to disk. Accepts HTTP.rb options via a hash or a
27
27
  # block, and some additional options as well.
28
- def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, **options, &block)
28
+ def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, extension: nil, **options, &block)
29
29
  response = request(url, **options, &block)
30
30
 
31
31
  content_length_proc.call(response.content_length) if content_length_proc && response.content_length
@@ -34,7 +34,7 @@ module Down
34
34
  raise Down::TooLarge, "file is too large (#{response.content_length/1024/1024}MB, max is #{max_size/1024/1024}MB)"
35
35
  end
36
36
 
37
- extname = File.extname(response.uri.path)
37
+ extname = extension ? ".#{extension}" : File.extname(response.uri.path)
38
38
  tempfile = Tempfile.new(["down-http", extname], binmode: true)
39
39
 
40
40
  stream_body(response) do |chunk|
@@ -71,7 +71,11 @@ module Down
71
71
  size: response.content_length,
72
72
  encoding: response.content_type.charset,
73
73
  rewindable: rewindable,
74
- data: { status: response.code, headers: response.headers.to_h, response: response },
74
+ data: {
75
+ status: response.code,
76
+ headers: normalize_headers(response.headers.to_h),
77
+ response: response
78
+ },
75
79
  )
76
80
  end
77
81
 
data/lib/down/net_http.rb CHANGED
@@ -40,6 +40,7 @@ module Down
40
40
  destination = options.delete(:destination)
41
41
  headers = options.delete(:headers)
42
42
  uri_normalizer = options.delete(:uri_normalizer)
43
+ extension = options.delete(:extension)
43
44
 
44
45
  # Use open-uri's :content_lenth_proc or :progress_proc to raise an
45
46
  # exception early if the file is too large.
@@ -93,7 +94,8 @@ module Down
93
94
  open_uri_file = open_uri(uri, open_uri_options, follows_remaining: max_redirects)
94
95
 
95
96
  # Handle the fact that open-uri returns StringIOs for small files.
96
- tempfile = ensure_tempfile(open_uri_file, File.extname(open_uri_file.base_uri.path))
97
+ extname = extension ? ".#{extension}" : File.extname(open_uri_file.base_uri.path)
98
+ tempfile = ensure_tempfile(open_uri_file, extname)
97
99
  OpenURI::Meta.init tempfile, open_uri_file # add back open-uri methods
98
100
  tempfile.extend Down::NetHttp::DownloadedFile
99
101
 
@@ -130,10 +132,7 @@ module Down
130
132
  on_close: -> { request.resume }, # close HTTP connnection
131
133
  data: {
132
134
  status: response.code.to_i,
133
- headers: response.each_header.inject({}) { |headers, (downcased_name, value)|
134
- name = downcased_name.split("-").map(&:capitalize).join("-")
135
- headers.merge!(name => value)
136
- },
135
+ headers: normalize_headers(response.each_header),
137
136
  response: response,
138
137
  },
139
138
  )
@@ -158,7 +157,11 @@ module Down
158
157
 
159
158
  # forward cookies on the redirect
160
159
  if !exception.io.meta["set-cookie"].to_s.empty?
161
- options["Cookie"] = exception.io.meta["set-cookie"]
160
+ options["Cookie"] ||= ''
161
+ # Add new cookies avoiding duplication
162
+ new_cookies = exception.io.meta["set-cookie"].to_s.split(';').map(&:strip)
163
+ old_cookies = options["Cookie"].split(';')
164
+ options["Cookie"] = (old_cookies | new_cookies).join(';')
162
165
  end
163
166
 
164
167
  follows_remaining -= 1
@@ -202,13 +205,13 @@ module Down
202
205
 
203
206
  begin
204
207
  response = http.start do
205
- http.request(request) do |response|
206
- unless response.is_a?(Net::HTTPRedirection)
207
- yield response
208
+ http.request(request) do |resp|
209
+ unless resp.is_a?(Net::HTTPRedirection)
210
+ yield resp
208
211
  # In certain cases the caller wants to download only one portion
209
212
  # of the file and close the connection, so we tell Net::HTTP that
210
213
  # it shouldn't continue retrieving it.
211
- response.instance_variable_set("@read", true)
214
+ resp.instance_variable_set("@read", true)
212
215
  end
213
216
  end
214
217
  end
@@ -310,8 +313,8 @@ module Down
310
313
  def rebuild_response_from_open_uri_exception(exception)
311
314
  code, message = exception.io.status
312
315
 
313
- response_class = Net::HTTPResponse::CODE_TO_OBJ.fetch(code) do |code|
314
- Net::HTTPResponse::CODE_CLASS_TO_OBJ.fetch(code[0]) do
316
+ response_class = Net::HTTPResponse::CODE_TO_OBJ.fetch(code) do |c|
317
+ Net::HTTPResponse::CODE_CLASS_TO_OBJ.fetch(c[0]) do
315
318
  Net::HTTPUnknownResponse
316
319
  end
317
320
  end
data/lib/down/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  module Down
4
- VERSION = "5.2.3"
4
+ VERSION = "5.3.1"
5
5
  end
data/lib/down/wget.rb CHANGED
@@ -29,7 +29,7 @@ module Down
29
29
 
30
30
  # Downlods the remote file to disk. Accepts wget command-line options and
31
31
  # some additional options as well.
32
- def download(url, *args, max_size: nil, content_length_proc: nil, progress_proc: nil, destination: nil, **options)
32
+ def download(url, *args, max_size: nil, content_length_proc: nil, progress_proc: nil, destination: nil, extension: nil, **options)
33
33
  io = open(url, *args, **options, rewindable: false)
34
34
 
35
35
  content_length_proc.call(io.size) if content_length_proc && io.size
@@ -38,7 +38,7 @@ module Down
38
38
  raise Down::TooLarge, "file is too large (#{io.size/1024/1024}MB, max is #{max_size/1024/1024}MB)"
39
39
  end
40
40
 
41
- extname = File.extname(URI(url).path)
41
+ extname = extension ? ".#{extension}" : File.extname(URI(url).path)
42
42
  tempfile = Tempfile.new(["down-wget", extname], binmode: true)
43
43
 
44
44
  until io.eof?
@@ -94,7 +94,7 @@ module Down
94
94
  raise Down::Error, "failed to parse response headers"
95
95
  end
96
96
 
97
- headers = parser.headers
97
+ headers = normalize_headers(parser.headers)
98
98
  status = parser.status_code
99
99
 
100
100
  content_length = headers["Content-Length"].to_i if headers["Content-Length"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: down
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.3
4
+ version: 5.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-03 00:00:00.000000000 Z
11
+ date: 2022-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  - !ruby/object:Gem::Version
176
176
  version: '0'
177
177
  requirements: []
178
- rubygems_version: 3.2.15
178
+ rubygems_version: 3.3.3
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: Robust streaming downloads using Net::HTTP, HTTP.rb or wget.