format_parser 0.29.0 → 0.29.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: b8efef8742cfefdcde0aa05ab32c726004325c2210b19e25ca2cf866ad786074
4
- data.tar.gz: bbd59f6f046a35cff93ea4e20a5168c2912007b06b3fabe7a5b23f7a0ed70c34
3
+ metadata.gz: 75ee83f55840e3031d4d60d8dc07ca038812188613e2b740079e1c965efb2886
4
+ data.tar.gz: 31c3ee84434560c18e6ea74a23160b909e6f880f52b2ed6f0e888e847c557bd9
5
5
  SHA512:
6
- metadata.gz: b4a7d965be6ff0cb0abf8cdec5f4d31259aada155ae465c1b31d5eba718f9d0071ee8b7a6b2061e6024add45d32b901c0f95a37373727a59fd4a6562041f52e9
7
- data.tar.gz: d314cb6876185ddedcef01c67205cf0635c2d8aebd0871dfd7834f0f54c15c38c31db7cc730c6aea8f8a72aff85949fa6937d970dfb320120a5b8c843f003753
6
+ metadata.gz: 536cfb1bac7926f56ba760959d7c5a0905d3b2b0944b16248b6c81be00b722dad894a8d6d5773134fb0471e42d016a20be15e3d5a01c671f0cc65658f2fc05b4
7
+ data.tar.gz: cb3f73df051b8612cb6d0e1a4e55c045e461bf2c5e4667dd6e461e779b1f39d01be8d70d084e252e5198a966ac590129286811f5822808a0c980c2ad72a087a1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.29.1
2
+ * Fix handling of 200 responses with `parse_http` as well as handling of very small responses which do not need range access
3
+
1
4
  ## 0.29.0
2
5
  * Add option `headers:` to `FormatParser.parse_http`
3
6
 
@@ -1,3 +1,3 @@
1
1
  module FormatParser
2
- VERSION = '0.29.0'
2
+ VERSION = '0.29.1'
3
3
  end
data/lib/remote_io.rb CHANGED
@@ -89,18 +89,32 @@ class FormatParser::RemoteIO
89
89
  response = conn.get(@uri, nil, range: 'bytes=%d-%d' % [range.begin, range.end])
90
90
 
91
91
  case response.status
92
- when 200, 206
92
+ when 200
93
+ # S3 returns 200 when you request a Range that is fully satisfied by the entire object,
94
+ # we take that into account here. Also, for very tiny responses (and also for empty responses)
95
+ # the responses are going to be 200 which does not mean we cannot proceed
96
+ # To have a good check for both of these conditions we need to know whether the ranges overlap fully
97
+ response_size = response.body.bytesize
98
+ requested_range_size = range.end - range.begin + 1
99
+ if response_size > requested_range_size
100
+ error_message = [
101
+ "We requested #{requested_range_size} bytes, but the server sent us more",
102
+ "(#{response_size} bytes) - it likely has no `Range:` support.",
103
+ "The error occurred when talking to #{@uri})"
104
+ ]
105
+ raise InvalidRequest.new(response.status, error_message.join("\n"))
106
+ end
107
+ [response_size, response.body]
108
+ when 206
93
109
  # Figure out of the server supports content ranges, if it doesn't we have no
94
110
  # business working with that server
95
111
  range_header = response.headers['Content-Range']
96
- raise InvalidRequest.new(response.status, "No range support at #{@uri}") unless range_header
112
+ raise InvalidRequest.new(response.status, "The server replied with 206 status but no Content-Range at #{@uri}") unless range_header
97
113
 
98
114
  # "Content-Range: bytes 0-0/307404381" is how the response header is structured
99
115
  size = range_header[/\/(\d+)$/, 1].to_i
100
116
 
101
- # S3 returns 200 when you request a Range that is fully satisfied by the entire object,
102
- # we take that into account here. For other servers, 206 is the expected response code.
103
- # Also, if we request a _larger_ range than what can be satisfied by the server,
117
+ # If we request a _larger_ range than what can be satisfied by the server,
104
118
  # the response is going to only contain what _can_ be sent and the status is also going
105
119
  # to be 206
106
120
  return [size, response.body]
@@ -19,18 +19,27 @@ describe 'Fetching data from HTTP remotes' do
19
19
  res.status = 302
20
20
  res.header['Location'] = req.path.sub('/redirect', '')
21
21
  end
22
+ @server.mount_proc '/empty' do |_req, res|
23
+ res.status = 200
24
+ res.body = ''
25
+ end
26
+ @server.mount_proc '/tiny' do |_req, res|
27
+ res.status = 200
28
+ res.body = File.read(fixtures_dir + '/test.gif')
29
+ end
30
+
22
31
  trap('INT') { @server.stop }
23
32
  @server_thread = Thread.new { @server.start }
24
33
  end
25
34
 
26
- it '#parse_http is called without any option' do
35
+ it 'works with .parse_http called without any options' do
27
36
  result = FormatParser.parse_http('http://localhost:9399/PNG/anim.png')
28
37
 
29
38
  expect(result.format).to eq(:png)
30
39
  expect(result.height_px).to eq(180)
31
40
  end
32
41
 
33
- it '#parse_http is called with hash options' do
42
+ it 'works with .parse_http called with additional options' do
34
43
  fake_result = double(nature: :audio, format: :aiff)
35
44
  expect_any_instance_of(FormatParser::AIFFParser).to receive(:call).and_return(fake_result)
36
45
  results = FormatParser.parse_http('http://localhost:9399/PNG/anim.png', results: :all)
@@ -39,6 +48,18 @@ describe 'Fetching data from HTTP remotes' do
39
48
  expect(results).to include(fake_result)
40
49
  end
41
50
 
51
+ it 'is able to cope with a 0-size resource which does not provide Content-Range' do
52
+ file_information = FormatParser.parse_http('http://localhost:9399/empty')
53
+
54
+ expect(file_information).to be_nil
55
+ end
56
+
57
+ it 'is able to cope with a tiny resource which fits into the first requested range completely' do
58
+ file_information = FormatParser.parse_http('http://localhost:9399/tiny')
59
+ expect(file_information).not_to be_nil
60
+ expect(file_information.nature).to eq(:image)
61
+ end
62
+
42
63
  it 'parses the animated PNG over HTTP' do
43
64
  file_information = FormatParser.parse_http('http://localhost:9399/PNG/anim.png')
44
65
  expect(file_information).not_to be_nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: format_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.29.0
4
+ version: 0.29.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Berman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-02-18 00:00:00.000000000 Z
12
+ date: 2021-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ks