interval_response 0.1.0 → 0.1.7

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: 2ead31e0eeba26a2752056561f041073652cc485aa7d34f9e939792ec990d0c1
4
- data.tar.gz: 1bc76de0fe94c1943be22fc9674007021778b80f494f2aeacfc31f81db08e0e9
3
+ metadata.gz: 05cde032dcb1552e0814acf6904f300a840a9bca04399d932c4860f985adea1f
4
+ data.tar.gz: 8f04da33316e49efd6d15145ab19adec358f7035f7d5e07721b216e1f7ec9796
5
5
  SHA512:
6
- metadata.gz: d217a6cc297e54d1955b76c9919d1ee7259f3a9d157d13a473a2410656824316906e3805b37d7fd14ae9f1c3f7681900740368e619e56bb023d071d2274ed4d2
7
- data.tar.gz: 21e8766bf398ef4a00f7e81b34214da43f13bbc20b1ed8b2e89a83c305f0a5ce43764cd7024626c7367e1c40a70d4c73334e23b6d811c67f14a898e736f9cb28
6
+ metadata.gz: c74910d5332103d7240e28f848d092d70299178735a9ba38dcd5e307e5d11859890201bc0ba6072b0c94fe96885994fb9195d4b83a90316b10986dfd29d69ebf
7
+ data.tar.gz: 6835c6fc24fe5874de7b7b5e653b75b41cf2ef3c78d7909fb4194bee6dd764baf4bb7e4634ef7fc5ccdf7c720bc292152ac6b9d046d36bdb65140ccbede9e391
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # 0.1.7
2
+
3
+ * Move `RackResponseWrapper` into the main namespace
4
+ * Add `#satisfied_with_first_interval?` so that certain Range: requests can be served using a redirect
5
+ * Add `#multiple_ranges?` so that one can choose not to honor multipart Range requests
6
+
7
+ # 0.1.6
8
+
9
+ * Create a base response type (`Abstract`) which has the same interface as the rest of the responses
10
+
11
+ # 0.1.5
12
+
13
+ * Change the API of `IntervalResponse.new` to accept the Rack `env` hash directly, without having the caller extract the header values manually.
14
+ * Allow intervals to set ETags which contribute to the final ETag
15
+ * Make #etag available on return value from IntervalResponse.new so that the ETag check can be performed without having to keep the interval sequence object at hand
16
+ * Switch the license to MIT-Hippocratic
17
+
18
+ # 0.1.4
19
+
20
+ Initial public release
data/LICENSE.txt CHANGED
@@ -1,21 +1,33 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 Julik Tarkhanov
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ interval_response - Copyright (2020) - (WeTransfer)(“Licensor”)
2
+
3
+ Hippocratic License Version Number: 2.1.
4
+
5
+ Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
6
+
7
+ Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
8
+
9
+ * Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
10
+
11
+ * Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
12
+
13
+ * Compliance with Human Rights Principles and Human Rights Laws.
14
+
15
+ 1. Human Rights Principles.
16
+
17
+ (a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
18
+
19
+ (b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
20
+
21
+ Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
22
+
23
+ 2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
24
+
25
+ 3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
26
+
27
+ * Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
28
+
29
+ * Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
30
+
31
+ * Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
32
+
33
+ This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # IntervalResponse
2
2
 
3
3
  is a little piece of machinery which allows your Rack/Rails application to correctly
4
- serve HTTP `Range:` responses. Features:
4
+ serve HTTP `Range:` responses. It allows you to "stitch" together multiple spans of
5
+ data (such as long strings, files or remote resources) and then serve them out in response
6
+ to requests with `Range:` headers. Features:
5
7
 
6
8
  * Strong ETags depending on response composition
7
9
  * Correct response codes/headers/offsets
@@ -19,7 +21,7 @@ Wrap them in an `IntervalResponse` and return it to Rack:
19
21
  verses_app = ->(env) {
20
22
  all_verses = ImportantVerse.all.map(&:verse_text)
21
23
  interval_sequence = IntervalResponse::Sequence.new(*all_verses)
22
- response = IntervalResponse.new(interval_sequence, env['HTTP_RANGE'], env['HTTP_IF_RANGE'])
24
+ response = IntervalResponse.new(interval_sequence, env)
23
25
  response.to_rack_response_triplet
24
26
  }
25
27
  ```
@@ -32,7 +34,7 @@ Or imagine you want to serve out a few very large log files, concatenated togeth
32
34
  # do not have to stay open during the entire response output
33
35
  lazy_files = log_paths.map { |path| IntervalResponse::LazyFile.new(path) }
34
36
  interval_sequence = IntervalResponse::Sequence.new(*lazy_files)
35
- response = IntervalResponse.new(interval_sequence, env['HTTP_RANGE'], env['HTTP_IF_RANGE'])
37
+ response = IntervalResponse.new(interval_sequence, env)
36
38
  response.to_rack_response_triplet(headers: {'X-Server' => 'teapot'})
37
39
  ```
38
40
 
@@ -72,7 +74,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
72
74
 
73
75
  ## Contributing
74
76
 
75
- Bug reports and pull requests are welcome on GitHub at https://github.com/julik/interval_response.
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/WeTransfer/interval_response.
76
78
 
77
79
  ## License
78
80
 
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = %q{Assemble HTTP responses from spliced sequences of payloads}
13
13
  spec.description = %q{Assemble HTTP responses from spliced sequences of payloads}
14
14
  spec.homepage = "https://github.com/WeTransfer/interval_response"
15
- spec.license = "MIT"
15
+ spec.licenses = ['MIT (Hippocratic)']
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
18
  # to allow pushing to a single host or delete this section to allow pushing to any host.
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
37
37
  spec.add_runtime_dependency "rack"
38
38
  spec.add_runtime_dependency "measurometer"
39
39
 
40
- spec.add_development_dependency "bundler", "~> 1.17"
40
+ spec.add_development_dependency "bundler"
41
41
  spec.add_development_dependency "rake", "~> 12"
42
42
  spec.add_development_dependency "rspec", "~> 3"
43
43
  spec.add_development_dependency "complexity_assert"
@@ -5,8 +5,9 @@ module IntervalResponse
5
5
  class Error < StandardError; end
6
6
 
7
7
  require_relative "interval_response/version"
8
- require_relative "interval_response/to_rack_response_triplet"
8
+ require_relative "interval_response/rack_body_wrapper"
9
9
  require_relative "interval_response/sequence"
10
+ require_relative "interval_response/abstract"
10
11
  require_relative "interval_response/empty"
11
12
  require_relative "interval_response/single"
12
13
  require_relative "interval_response/invalid"
@@ -16,47 +17,68 @@ module IntervalResponse
16
17
 
17
18
  ENTIRE_RESOURCE_RANGE = 'bytes=0-'
18
19
 
19
- def self.new(interval_map, http_range_header_value_or_nil, http_if_range_header_or_nil)
20
+ # Creates a new IntervalResponse object. The object returned does not
21
+ # have a specific class, but is one of the following objects, which
22
+ # all support the same interface:
23
+ #
24
+ # * IntervalResponse::Empty for an empty response
25
+ # * IntervalResponse::Single for a single HTTP range
26
+ # * IntervalResponse::Full for the entire resource
27
+ # * IntervalResponse::Multi for multipart ranges response with multiple HTTP ranges
28
+ # * IntervalResponse::Invalid for responses that are 416 (Unsatisfiable range)
29
+ #
30
+ # @param interval_sequence[IntervalResponse::Sequence] the sequence of segments
31
+ # @param rack_env_headers[Hash] the Rack env, or a Hash containing 'HTTP_RANGE' and 'HTTP_IF_RANGE' headers
32
+ # @return [Empty, Single, Full, Multi, Invalid]
33
+ def self.new(interval_sequence, rack_env_headers)
34
+ http_range_header_value = rack_env_headers['HTTP_RANGE']
35
+ http_if_range_header_value = rack_env_headers['HTTP_IF_RANGE']
36
+
20
37
  # If the 'If-Range' header is provided but does not match, discard the Range header. It means
21
38
  # that the client is requesting a certain representation of the resource and wants a range
22
39
  # _within_ that representation, but the representation has since changed and the offsets
23
40
  # no longer make sense. In that case we are supposed to answer with a 200 and the full
24
41
  # monty.
25
- return new(interval_map, ENTIRE_RESOURCE_RANGE, nil) if http_if_range_header_or_nil && http_if_range_header_or_nil != interval_map.etag
42
+ if http_if_range_header_value && http_if_range_header_value != interval_sequence.etag
43
+ Measurometer.increment_counter('interval_response.if_range_mismatch', 1)
44
+ return new(interval_sequence, 'HTTP_RANGE' => ENTIRE_RESOURCE_RANGE)
45
+ end
26
46
 
27
- if http_if_range_header_or_nil
28
- Measurometer.increment_counter('interval_response.if_range_provided')
29
- elsif http_range_header_value_or_nil
30
- Measurometer.increment_counter('interval_response.if_range_not_provided')
47
+ if http_if_range_header_value
48
+ Measurometer.increment_counter('interval_response.if_range_match', 1)
49
+ elsif http_range_header_value
50
+ Measurometer.increment_counter('interval_response.if_range_not_provided', 1)
31
51
  end
32
52
 
33
- prepare_response(interval_map, http_range_header_value_or_nil, http_if_range_header_or_nil).tap do |res|
53
+ prepare_response(interval_sequence, http_range_header_value).tap do |res|
34
54
  response_type_name_for_metric = res.class.to_s.split('::').last.downcase # Some::Module::Empty => empty
35
- Measurometer.increment_counter('interval_response.resp_%s' % response_type_name_for_metric)
55
+ Measurometer.increment_counter('interval_response.resp_%s' % response_type_name_for_metric, 1)
36
56
  end
37
57
  end
38
58
 
39
- def self.prepare_response(interval_map, http_range_header_value_or_nil, _http_if_range_header_or_nil)
59
+ def self.prepare_response(interval_sequence, http_range_header_value)
40
60
  # Case 1 - response of 0 bytes (empty resource).
41
61
  # We don't even have to parse the Range header for this since
42
62
  # the response will be the same, always.
43
- return Empty.new(interval_map) if interval_map.empty?
63
+ return Empty.new(interval_sequence) if interval_sequence.empty?
44
64
 
45
65
  # Parse the HTTP Range: header
46
- range_request_header = http_range_header_value_or_nil || ENTIRE_RESOURCE_RANGE
47
- http_ranges = Rack::Utils.get_byte_ranges(range_request_header, interval_map.size)
66
+ range_request_header = http_range_header_value || ENTIRE_RESOURCE_RANGE
67
+ http_ranges = Rack::Utils.get_byte_ranges(range_request_header, interval_sequence.size)
48
68
 
49
69
  # Case 2 - Client did send us a Range header, but Rack discarded
50
70
  # it because it is invalid and cannot be satisfied
51
- return Invalid.new(interval_map) if http_range_header_value_or_nil && http_ranges.empty?
71
+ return Invalid.new(interval_sequence) if http_range_header_value && (http_ranges.nil? || http_ranges.empty?)
52
72
 
53
73
  # Case 3 - entire resource
54
- return Full.new(interval_map) if http_ranges.length == 1 && http_ranges.first == (0..(interval_map.size - 1))
74
+ return Full.new(interval_sequence) if http_ranges.length == 1 && http_ranges.first == (0..(interval_sequence.size - 1))
55
75
 
56
76
  # Case 4 - one content range
57
- return Single.new(interval_map, http_ranges) if http_ranges.length == 1
77
+ return Single.new(interval_sequence, http_ranges[0]) if http_ranges.length == 1
58
78
 
59
79
  # Case 5 - MIME multipart with multiple content ranges
60
- Multi.new(interval_map, http_ranges)
80
+ Multi.new(interval_sequence, http_ranges)
61
81
  end
82
+
83
+ private_class_method :prepare_response
62
84
  end
@@ -0,0 +1,77 @@
1
+ # Base class for all response types, primarily for ease of documentation
2
+ class IntervalResponse::Abstract
3
+ def to_rack_response_triplet(headers: nil, chunk_size: IntervalResponse::RackBodyWrapper::CHUNK_SIZE)
4
+ [status_code, headers.to_h.merge(self.headers), IntervalResponse::RackBodyWrapper.new(self, chunk_size: chunk_size)]
5
+ end
6
+
7
+ # Tells whether this response is responding with multiple ranges. If you want to simulate S3 for example,
8
+ # it might be relevant to deny a response from being served if it does respond with multiple ranges -
9
+ # IntervalResponse supports these responses just fine, but S3 doesn't.
10
+ def multiple_ranges?
11
+ false
12
+ end
13
+
14
+ # Tells whether this entire requested range can be satisfied with the first available segment within the given Sequence.
15
+ # If it is, then you can redirect to the URL of the first segment instead of streaming the response
16
+ # through - which can be cheaper for your application server. Note that you can redirect to the resource of the first
17
+ # interval only, because otherwise your `Range` header will no longer match. Suppose you have a stitched resource
18
+ # consisting of two segments:
19
+ #
20
+ # [bytes 0..456]
21
+ # [bytes 457..890]
22
+ #
23
+ # and your client requests `Range: bytes=0-33`. You can redirect the client to the location of the first interval,
24
+ # and the `Range:` header will be retransmitted to that location and will be satisfied. However, imagine you are requesting
25
+ # the `Range: bytes=510-512` - you _could_ redirect just to the second interval, but the `Range` header is not going to be
26
+ # adjusted by the client, and you are not going to receive the correct slice of the resource. That's why you can only
27
+ # redirect to the first interval only.
28
+ def satisfied_with_first_interval?
29
+ false
30
+ end
31
+
32
+ # @param interval_sequence[IntervalResponse::Sequence] the sequence the response is built for
33
+ def initialize(interval_sequence)
34
+ @interval_sequence = interval_sequence
35
+ end
36
+
37
+ # Returns the ETag of the interval sequence
38
+ def etag
39
+ @interval_sequence.etag
40
+ end
41
+
42
+ # Yields every segment and the range within that segment to be returned to the client. For multipart
43
+ # responses the envelopes of the parts will be returned as segments as well
44
+ #
45
+ # @yield [Object, Range]
46
+ def each
47
+ # No-op
48
+ end
49
+
50
+ # Returns the HTTP status code of the response
51
+ #
52
+ # @return [Integer]
53
+ def status_code
54
+ 200
55
+ end
56
+
57
+ # Returns the exact number of bytes that the response is. If the response
58
+ # is a range it will be the length of the range. If the response is a multipart
59
+ # byte range response it will be the content length of the ranges plus the content
60
+ # length of all the envelopes.
61
+ #
62
+ # @return [Integer]
63
+ def content_length
64
+ 0
65
+ end
66
+
67
+ # Returns headers for the HTTP response
68
+ # @return [Hash]
69
+ def headers
70
+ {
71
+ 'Accept-Ranges' => 'bytes',
72
+ 'Content-Length' => '0',
73
+ 'Content-Type' => 'binary/octet-stream',
74
+ 'ETag' => etag,
75
+ }
76
+ end
77
+ end
@@ -1,15 +1,5 @@
1
1
  # Serves out a response that is of size 0
2
- class IntervalResponse::Empty
3
- include IntervalResponse::ToRackResponseTriplet
4
-
5
- def initialize(interval_map)
6
- @interval_map = interval_map
7
- end
8
-
9
- def each
10
- # No-op
11
- end
12
-
2
+ class IntervalResponse::Empty < IntervalResponse::Abstract
13
3
  def status_code
14
4
  200
15
5
  end
@@ -23,7 +13,7 @@ class IntervalResponse::Empty
23
13
  'Accept-Ranges' => 'bytes',
24
14
  'Content-Length' => '0',
25
15
  'Content-Type' => 'binary/octet-stream',
26
- 'ETag' => @interval_map.etag,
16
+ 'ETag' => etag,
27
17
  }
28
18
  end
29
19
  end
@@ -1,15 +1,12 @@
1
1
  # Serves out a response that contains the entire resource
2
- class IntervalResponse::Full
3
- include IntervalResponse::ToRackResponseTriplet
4
-
5
- def initialize(interval_map, *)
6
- @interval_map = interval_map
2
+ class IntervalResponse::Full < IntervalResponse::Abstract
3
+ def initialize(*)
4
+ super
5
+ @http_range_for_entire_resource = 0..(@interval_sequence.size - 1)
7
6
  end
8
7
 
9
8
  def each
10
- # serve the part of the interval map
11
- full_range = 0..(@interval_map.size - 1)
12
- @interval_map.each_in_range(full_range) do |segment, range_in_segment|
9
+ @interval_sequence.each_in_range(@http_range_for_entire_resource) do |segment, range_in_segment|
13
10
  yield(segment, range_in_segment)
14
11
  end
15
12
  end
@@ -19,15 +16,23 @@ class IntervalResponse::Full
19
16
  end
20
17
 
21
18
  def content_length
22
- @interval_map.size
19
+ @interval_sequence.size
20
+ end
21
+
22
+ def satisfied_with_first_interval?
23
+ @interval_sequence.first_interval_only?(@http_range_for_entire_resource)
24
+ end
25
+
26
+ def multiple_ranges?
27
+ false
23
28
  end
24
29
 
25
30
  def headers
26
31
  {
27
32
  'Accept-Ranges' => 'bytes',
28
- 'Content-Length' => @interval_map.size.to_s,
33
+ 'Content-Length' => @interval_sequence.size.to_s,
29
34
  'Content-Type' => 'binary/octet-stream',
30
- 'ETag' => @interval_map.etag,
35
+ 'ETag' => etag,
31
36
  }
32
37
  end
33
38
  end
@@ -1,13 +1,7 @@
1
- # Serves out a response that is of size 0
2
- class IntervalResponse::Invalid
3
- include IntervalResponse::ToRackResponseTriplet
4
-
1
+ # Serves out a response for 416 responses (invalid range request)
2
+ class IntervalResponse::Invalid < IntervalResponse::Abstract
5
3
  ERROR_JSON = '{"message": "Ranges cannot be satisfied"}'
6
4
 
7
- def initialize(segment_map)
8
- @interval_map = segment_map
9
- end
10
-
11
5
  def each
12
6
  full_segment_range = (0..(ERROR_JSON.bytesize - 1))
13
7
  yield(ERROR_JSON, full_segment_range)
@@ -26,8 +20,8 @@ class IntervalResponse::Invalid
26
20
  'Accept-Ranges' => 'bytes',
27
21
  'Content-Length' => ERROR_JSON.bytesize.to_s,
28
22
  'Content-Type' => 'application/json',
29
- 'Content-Range' => "bytes */#{@interval_map.size}",
30
- 'ETag' => @interval_map.etag,
23
+ 'Content-Range' => "bytes */#{@interval_sequence.size}",
24
+ 'ETag' => etag,
31
25
  }
32
26
  end
33
27
  end
@@ -1,18 +1,15 @@
1
- require 'securerandom'
2
-
3
- class IntervalResponse::Multi
4
- include IntervalResponse::ToRackResponseTriplet
5
-
1
+ class IntervalResponse::Multi < IntervalResponse::Abstract
6
2
  ALPHABET = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
7
3
 
8
- def initialize(interval_map, http_ranges)
9
- @interval_map = interval_map
4
+ # @param http_ranges[Array<Range>]
5
+ def initialize(interval_sequence, http_ranges)
6
+ @interval_sequence = interval_sequence
10
7
  @http_ranges = http_ranges
11
8
  # RFC1521 says that a boundary "must be no longer than 70 characters,
12
9
  # not counting the two leading hyphens".
13
- # Modulo-based random is biased but it doesn't matter much for us (we do not need to
10
+ # Standard Ruby randomness is biased but it doesn't matter much for us (we do not need to
14
11
  # be extremely secure here)
15
- @boundary = SecureRandom.bytes(24).unpack("C*").map { |b| ALPHABET[b % ALPHABET.length] }.join
12
+ @boundary = ALPHABET.sample(24).join
16
13
  end
17
14
 
18
15
  def each
@@ -21,7 +18,7 @@ class IntervalResponse::Multi
21
18
  part_header = part_header(range_i, http_range)
22
19
  entire_header_range = 0..(part_header.bytesize - 1)
23
20
  yield(part_header, entire_header_range)
24
- @interval_map.each_in_range(http_range) do |segment, range_in_segment|
21
+ @interval_sequence.each_in_range(http_range) do |segment, range_in_segment|
25
22
  yield(segment, range_in_segment)
26
23
  end
27
24
  end
@@ -46,10 +43,18 @@ class IntervalResponse::Multi
46
43
  'Accept-Ranges' => 'bytes',
47
44
  'Content-Length' => content_length.to_s,
48
45
  'Content-Type' => "multipart/byte-ranges; boundary=#{@boundary}",
49
- 'ETag' => @interval_map.etag,
46
+ 'ETag' => etag,
50
47
  }
51
48
  end
52
49
 
50
+ def satisfied_with_first_interval?
51
+ @interval_sequence.first_interval_only?(*@http_ranges)
52
+ end
53
+
54
+ def multiple_ranges?
55
+ true
56
+ end
57
+
53
58
  private
54
59
 
55
60
  def compute_envelope_size
@@ -65,7 +70,7 @@ class IntervalResponse::Multi
65
70
  part_index > 0 ? "\r\n" : "", # Parts follwing the first have to be delimited "at the top"
66
71
  "--%s\r\n" % @boundary,
67
72
  "Content-Type: binary/octet-stream\r\n",
68
- "Content-Range: bytes %d-%d/%d\r\n" % [http_r.begin, http_r.end, @interval_map.size],
73
+ "Content-Range: bytes %d-%d/%d\r\n" % [http_r.begin, http_r.end, @interval_sequence.size],
69
74
  "\r\n",
70
75
  ].join
71
76
  end
@@ -0,0 +1,65 @@
1
+ # The Rack body wrapper is intended to be returned as the third element
2
+ # of the Rack response triplet. It supports the #each method
3
+ # and will call to the IntervalResponse object given to it
4
+ # at instantiation, filling up a pre-allocated String object
5
+ # with the bytes to be served out. The String object will then be repeatedly
6
+ # yielded to the Rack webserver with the response data. Since Ruby strings
7
+ # are mutable, the String object will be sized to a certain capacity and reused
8
+ # across calls to save allocations.
9
+ class IntervalResponse::RackBodyWrapper
10
+ # Default size of the chunk (String buffer) which is going to be
11
+ # yielded to the caller of the `each` method.
12
+ # Set toroughly one TCP kernel buffer
13
+ CHUNK_SIZE = 65 * 1024
14
+
15
+ def initialize(with_interval_response, chunk_size:)
16
+ @chunk_size = chunk_size
17
+ @interval_response = with_interval_response
18
+ end
19
+
20
+ def each
21
+ buf = String.new(capacity: @chunk_size)
22
+ @interval_response.each do |segment, range_in_segment|
23
+ case segment
24
+ when IntervalResponse::LazyFile
25
+ segment.with do |file_handle|
26
+ with_each_chunk(range_in_segment) do |offset, read_n|
27
+ file_handle.seek(offset, IO::SEEK_SET)
28
+ yield file_handle.read(read_n, buf)
29
+ end
30
+ end
31
+ when String
32
+ with_each_chunk(range_in_segment) do |offset, read_n|
33
+ yield segment.slice(offset, read_n)
34
+ end
35
+ when IO, Tempfile
36
+ with_each_chunk(range_in_segment) do |offset, read_n|
37
+ segment.seek(offset, IO::SEEK_SET)
38
+ yield segment.read(read_n, buf)
39
+ end
40
+ else
41
+ raise TypeError, "RackBodyWrapper only supports IOs or Strings"
42
+ end
43
+ end
44
+ ensure
45
+ buf.clear
46
+ end
47
+
48
+ private
49
+
50
+ def with_each_chunk(range_in_segment)
51
+ range_size = range_in_segment.end - range_in_segment.begin + 1
52
+ start_at_offset = range_in_segment.begin
53
+ n_whole_segments, remainder = range_size.divmod(@chunk_size)
54
+
55
+ n_whole_segments.times do |n|
56
+ unit_offset = start_at_offset + (n * @chunk_size)
57
+ yield unit_offset, @chunk_size
58
+ end
59
+
60
+ if remainder > 0
61
+ unit_offset = start_at_offset + (n_whole_segments * @chunk_size)
62
+ yield unit_offset, remainder
63
+ end
64
+ end
65
+ end
@@ -1,57 +1,102 @@
1
1
  require 'digest'
2
2
 
3
- # An interval sequence represents a linear sequence of non-overlapping,
3
+ # Represents a linear sequence of non-overlapping,
4
4
  # joined intervals. For example, an HTTP response which consists of
5
5
  # multiple edge included segments, or a timeline with clips joined together.
6
6
  # Every interval contains a *segment* - an arbitrary object which responds to
7
7
  # `#size` at time of adding to the IntervalSequence.
8
8
  class IntervalResponse::Sequence
9
- MULTIPART_GENRATOR_FINGERPRINT = 'boo'
10
- Interval = Struct.new(:segment, :size, :offset, :position)
9
+ Interval = Struct.new(:segment, :size, :offset, :position, :etag)
10
+ private_constant :Interval
11
11
 
12
+ # @return [Integer] the sum of sizes of all the segments of the sequence
12
13
  attr_reader :size
13
14
 
15
+ # Creates a new Sequence with given segments.
16
+ #
17
+ # @param segments[Array<#size,#bytesize>] Segments which respond to #size or #bytesize
14
18
  def initialize(*segments)
15
19
  @intervals = []
16
20
  @size = 0
17
21
  segments.each { |s| self << s }
18
22
  end
19
23
 
24
+ # Adds a segment to the sequence. The segment gets added at the end of the sequence.
25
+ #
26
+ # @param segment[#size,#bytesize] Segment which responds to #size or #bytesize
27
+ # @return self
20
28
  def <<(segment)
21
- return self if segment.size == 0
22
29
  segment_size_or_bytesize = segment.respond_to?(:bytesize) ? segment.bytesize : segment.size
23
- @intervals << Interval.new(segment, segment_size_or_bytesize, @size, @intervals.length)
24
- @size += segment.size
30
+ add_segment(segment, size: segment_size_or_bytesize)
31
+ end
32
+
33
+ # Adds a segment to the sequence with specifying the size and optionally the ETag value
34
+ # of the segment. ETag defaults to the size of the segment. Segment can be any object
35
+ # as the size gets passed as a keyword argument
36
+ #
37
+ # @param segment[Object] Any object can be used as the segment
38
+ # @param size[Integer] The size of the segment
39
+ # @param etag[Object] An object that defines the ETag for the segment. Can be any object that can
40
+ # be Marshal.dump - ed.
41
+ # @return self
42
+ def add_segment(segment, size:, etag: size)
43
+ if size > 0
44
+ etag_quoted = '"%s"' % etag
45
+ # We save the index of the interval inside the Struct so that we can
46
+ # use `bsearch` later instead of requiring `bsearch_index` to be available
47
+ @intervals << Interval.new(segment, size, @size, @intervals.length, etag_quoted)
48
+ @size += size
49
+ end
25
50
  self
26
51
  end
27
52
 
53
+ # Yields every segment which is touched by the given Range in resource in sequence,
54
+ # together with a Range object which defines the necessary part of the segment.
55
+ # For example, calling `each_in_range(0..2)` with 2 segments of size 1 and 2
56
+ # will successively yield [segment1, 0..0] then [segment2, 0..1]
57
+ #
58
+ # Interval sequences can be nested - you can place a Sequence inside another Sequence
59
+ # as a segment. In that case when you call `each_in_range` on the outer Sequence and you
60
+ # need to retrieve data from the inner Sequence which is one of the segments, the call will
61
+ # yield the segments from the inner Sequence, "drilling down" as deep as is appropriate.
62
+ #
63
+ # Three arguments will be yielded to the block - the segment (the "meat" of an interval, which
64
+ # is the object given when the interval was added to the Sequence), the range within the interval
65
+ # (which is always going to be an inclusive `Range` of integers) and a boolean flag indicating whether
66
+ # this interval is the very first interval in the requested subset of the sequence. This flag honors nesting
67
+ # (if you have arbitrarily nested interval Sequences and you request something from the first interval of
68
+ # several Sequences deep it will still indicate `true`).
69
+ #
70
+ # @param from_range_in_resource[Range] an inclusive Range that specifies the range within the segment map
71
+ # @yield segment[Object], range_in_segment[Range], is_first_interval[Boolean]
28
72
  def each_in_range(from_range_in_resource)
29
73
  # Skip empty ranges
30
74
  requested_range_size = (from_range_in_resource.end - from_range_in_resource.begin) + 1
31
75
  return if requested_range_size < 1
32
76
 
33
- # ...and if the range misses our intervals completely
77
+ # Then walk through included intervals. If the range misses
78
+ # our intervals completely included_intervals will be empty.
34
79
  included_intervals = intervals_within_range(from_range_in_resource)
35
-
36
- # And normal case - walk through included intervals
37
80
  included_intervals.each do |interval|
38
81
  int_start = interval.offset
39
82
  int_end = interval.offset + interval.size - 1
40
83
  req_start = from_range_in_resource.begin
41
84
  req_end = from_range_in_resource.end
42
85
  range_within_interval = (max(int_start, req_start) - int_start)..(min(int_end, req_end) - int_start)
86
+ is_first_interval = interval.position == 0
43
87
 
44
88
  # Allow Sequences to be composed together
45
89
  if interval.segment.respond_to?(:each_in_range)
46
- interval.segment.each_in_range(range_within_interval) do |sub_segment, sub_range|
47
- yield(sub_segment, sub_range)
90
+ interval.segment.each_in_range(range_within_interval) do |sub_segment, sub_range, is_first_nested_interval|
91
+ yield(sub_segment, sub_range, is_first_interval && is_first_nested_interval)
48
92
  end
49
93
  else
50
- yield(interval.segment, range_within_interval)
94
+ yield(interval.segment, range_within_interval, is_first_interval)
51
95
  end
52
96
  end
53
97
  end
54
98
 
99
+ # Tells whether the size of the entire sequence is 0
55
100
  def empty?
56
101
  @size == 0
57
102
  end
@@ -82,13 +127,33 @@ class IntervalResponse::Sequence
82
127
  # enclosed in double-quotes.
83
128
  #
84
129
  # See for more https://blogs.msdn.microsoft.com/ieinternals/2011/06/03/download-resumption-in-internet-explorer/
130
+ #
131
+ # The ETag value gets derived from the ETags of the segments, which will be Marshal.dump'ed together
132
+ # and then added to the hash digest to produce the final ETag value.
133
+ #
134
+ # @return [String] a string delimited with double-quotes
85
135
  def etag
86
136
  d = Digest::SHA1.new
87
137
  d << IntervalResponse::VERSION
88
- d << Marshal.dump(@intervals.map(&:size))
138
+ @intervals.each do |interval|
139
+ d << interval.etag
140
+ end
89
141
  '"%s"' % d.hexdigest
90
142
  end
91
143
 
144
+ # Tells whether all of the given `ranges` will be satisfied from the first interval only. This can be used to
145
+ # redirect to the resource at that interval instead of proxying it through, since the `Range` header won't need to
146
+ # be adjusted
147
+ def first_interval_only?(*ranges)
148
+ ranges.map do |range|
149
+ each_in_range(range) do |_, _, is_first_interval|
150
+ return false unless is_first_interval
151
+ end
152
+ end
153
+
154
+ true
155
+ end
156
+
92
157
  private
93
158
 
94
159
  def max(a, b)
@@ -100,6 +165,10 @@ class IntervalResponse::Sequence
100
165
  end
101
166
 
102
167
  def interval_under(offset)
168
+ # For our purposes we would be better served by `bsearch_index`, but it is not available
169
+ # on older Ruby versions which we otherwise can splendidly support. Since when we retrieve
170
+ # the interval under offset we are going to need the index anyway, and since calling `Array#index`
171
+ # will incur another linear scan of the array, we save the index of the interval with the interval itself.
103
172
  @intervals.bsearch do |interval|
104
173
  # bsearch expects a 0 return value for "exact match".
105
174
  # -1 tells it "look to my left" and 1 "look to my right",
@@ -1,16 +1,15 @@
1
1
  # Serves out a response that consists of one HTTP Range,
2
2
  # which is always not the entire resource
3
- class IntervalResponse::Single
4
- include IntervalResponse::ToRackResponseTriplet
5
-
6
- def initialize(interval_map, http_ranges)
7
- @interval_map = interval_map
8
- @http_range = http_ranges.first
3
+ class IntervalResponse::Single < IntervalResponse::Abstract
4
+ # @param http_range[Range]
5
+ def initialize(interval_sequence, http_range)
6
+ @interval_sequence = interval_sequence
7
+ @http_range = http_range
9
8
  end
10
9
 
11
10
  # Serve the part of the interval map
12
11
  def each
13
- @interval_map.each_in_range(@http_range) do |segment, range_in_segment|
12
+ @interval_sequence.each_in_range(@http_range) do |segment, range_in_segment|
14
13
  yield(segment, range_in_segment)
15
14
  end
16
15
  end
@@ -24,13 +23,17 @@ class IntervalResponse::Single
24
23
  end
25
24
 
26
25
  def headers
27
- c_range = ('bytes %d-%d/%d' % [@http_range.begin, @http_range.end, @interval_map.size])
26
+ c_range = ('bytes %d-%d/%d' % [@http_range.begin, @http_range.end, @interval_sequence.size])
28
27
  {
29
28
  'Accept-Ranges' => 'bytes',
30
29
  'Content-Length' => (@http_range.end - @http_range.begin + 1).to_s,
31
30
  'Content-Type' => 'binary/octet-stream',
32
31
  'Content-Range' => c_range,
33
- 'ETag' => @interval_map.etag,
32
+ 'ETag' => etag,
34
33
  }
35
34
  end
35
+
36
+ def satisfied_with_first_interval?
37
+ @interval_sequence.first_interval_only?(@http_range)
38
+ end
36
39
  end
@@ -1,3 +1,3 @@
1
1
  module IntervalResponse
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -17,19 +17,35 @@ RSpec.describe IntervalResponse::Sequence do
17
17
  expect(seq.size).to eq(6 + 12 + 17)
18
18
  expect { |b|
19
19
  seq.each_in_range(0..0, &b)
20
- }.to yield_with_args(a, 0..0)
20
+ }.to yield_with_args(a, 0..0, true)
21
21
 
22
22
  expect { |b|
23
23
  seq.each_in_range(0..7, &b)
24
- }.to yield_successive_args([a, 0..5], [b, 0..1])
24
+ }.to yield_successive_args([a, 0..5, true], [b, 0..1, false])
25
25
 
26
26
  expect { |b|
27
27
  seq.each_in_range(7..27, &b)
28
- }.to yield_successive_args([b, 1..11], [c, 0..9])
28
+ }.to yield_successive_args([b, 1..11, false], [c, 0..9, false])
29
29
 
30
30
  expect { |b|
31
31
  seq.each_in_range(0..(6 + 12 - 1), &b)
32
- }.to yield_successive_args([a, 0..5], [b, 0..11])
32
+ }.to yield_successive_args([a, 0..5, true], [b, 0..11, false])
33
+ end
34
+
35
+ it 'indicates whether the first interval will satisfy a set of Ranges' do
36
+ seq = described_class.new
37
+
38
+ a = double(:a, size: 6)
39
+ b = double(:b, size: 12)
40
+ c = double(:c, size: 17)
41
+ seq << a << b << c
42
+
43
+ expect(seq).to be_first_interval_only(0..0)
44
+ expect(seq).to be_first_interval_only(0..0, 0..5)
45
+ expect(seq).not_to be_first_interval_only(0..6)
46
+ expect(seq).not_to be_first_interval_only(3..8)
47
+ expect(seq).not_to be_first_interval_only(15..16)
48
+ expect(seq).not_to be_first_interval_only(0..0, 15..16)
33
49
  end
34
50
 
35
51
  it 'generates the ETag for an empty sequence, and the etag contains data' do
@@ -40,6 +56,14 @@ RSpec.describe IntervalResponse::Sequence do
40
56
  expect(etag_for_sequence.bytesize).to be > 8
41
57
  end
42
58
 
59
+ it 'accepts objects that only respond to #bytesize and not #size' do
60
+ a = double(:a, bytesize: 6)
61
+ b = double(:b, bytesize: 12)
62
+ c = double(:c, bytesize: 17)
63
+ seq = described_class.new(a, b, c)
64
+ expect(seq.size).to eq(6 + 12 + 17)
65
+ end
66
+
43
67
  it 'generates the ETag dependent on the sequence composition' do
44
68
  a = double(:a, size: 6)
45
69
  b = double(:b, size: 12)
@@ -58,6 +82,23 @@ RSpec.describe IntervalResponse::Sequence do
58
82
  expect(etag_for_sequence_of_same_sizes).not_to eq(etag_for_sequence)
59
83
  end
60
84
 
85
+ it 'takes explicit etags into account if they are set on the intervals' do
86
+ seq = described_class.new
87
+ seq.add_segment(:a, size: 6)
88
+ etag_of_size_6 = seq.etag
89
+
90
+ seq = described_class.new
91
+ seq.add_segment(:a, size: 6)
92
+ another_etag_of_size_6 = seq.etag
93
+
94
+ seq = described_class.new
95
+ seq.add_segment(:a, size: 6, etag: "Some random etag")
96
+ etag_set_explicitly = seq.etag
97
+
98
+ expect(etag_of_size_6).to eq(another_etag_of_size_6)
99
+ expect(etag_of_size_6).not_to eq(etag_set_explicitly)
100
+ end
101
+
61
102
  it 'can handle a range that stretches outside of the available range' do
62
103
  a = double('a', size: 3)
63
104
  b = double('b', size: 4)
@@ -66,7 +107,7 @@ RSpec.describe IntervalResponse::Sequence do
66
107
  seq = described_class.new(a, b, c)
67
108
  expect { |b|
68
109
  seq.each_in_range(0..27, &b)
69
- }.to yield_successive_args([a, 0..2], [b, 0..3], [c, 0..0])
110
+ }.to yield_successive_args([a, 0..2, true], [b, 0..3, false], [c, 0..0, false])
70
111
  end
71
112
 
72
113
  it 'is composable' do
@@ -78,7 +119,7 @@ RSpec.describe IntervalResponse::Sequence do
78
119
 
79
120
  expect { |b|
80
121
  seq.each_in_range(0..27, &b)
81
- }.to yield_successive_args([a, 0..2], [b, 0..3], [c, 0..0])
122
+ }.to yield_successive_args([a, 0..2, true], [b, 0..3, false], [c, 0..0, false])
82
123
  end
83
124
 
84
125
  it 'has close to linear performance with large number of ranges and intervals' do
@@ -6,7 +6,7 @@ RSpec.describe IntervalResponse do
6
6
  context 'with an empty resource' do
7
7
  let(:seq) { IntervalResponse::Sequence.new }
8
8
  it 'always returns the empty response' do
9
- response = IntervalResponse.new(seq, _http_range_header = nil, _if_range_header = nil)
9
+ response = IntervalResponse.new(seq, {})
10
10
  expect(response.status_code).to eq(200)
11
11
  expect(response.content_length).to eq(0)
12
12
  expect(response.headers).to eq(
@@ -15,11 +15,12 @@ RSpec.describe IntervalResponse do
15
15
  "Content-Type" => "binary/octet-stream",
16
16
  'ETag' => seq.etag,
17
17
  )
18
+ expect(response.etag).to eq(seq.etag)
18
19
  expect { |b|
19
20
  response.each(&b)
20
21
  }.not_to yield_control
21
22
 
22
- response = IntervalResponse.new(seq, 'bytes=0-', _if_range = nil)
23
+ response = IntervalResponse.new(seq, 'HTTP_RANGE' => 'bytes=0-')
23
24
  expect(response.status_code).to eq(200)
24
25
  expect(response.content_length).to eq(0)
25
26
  expect(response.headers).to eq(
@@ -28,6 +29,7 @@ RSpec.describe IntervalResponse do
28
29
  "Content-Type" => "binary/octet-stream",
29
30
  'ETag' => seq.etag,
30
31
  )
32
+ expect(response.etag).to eq(seq.etag)
31
33
  expect { |b|
32
34
  response.each(&b)
33
35
  }.not_to yield_control
@@ -44,7 +46,7 @@ RSpec.describe IntervalResponse do
44
46
  end
45
47
 
46
48
  it 'returns the full response if the client did not ask for a Range' do
47
- response = IntervalResponse.new(seq, _http_range_header = nil, _if_range = nil)
49
+ response = IntervalResponse.new(seq, {})
48
50
  expect(response.status_code).to eq(200)
49
51
  expect(response.content_length).to eq(3 + 4 + 1)
50
52
  expect(response.headers).to eq(
@@ -53,13 +55,32 @@ RSpec.describe IntervalResponse do
53
55
  "Content-Type" => "binary/octet-stream",
54
56
  'ETag' => seq.etag,
55
57
  )
58
+ expect(response.etag).to eq(seq.etag)
59
+ expect(response).not_to be_multiple_ranges
60
+ expect(response).not_to be_satisfied_with_first_interval
61
+
56
62
  expect { |b|
57
63
  response.each(&b)
58
64
  }.to yield_successive_args([segment_a, 0..2], [segment_b, 0..3], [segment_c, 0..0])
59
65
  end
60
66
 
67
+ it 'returns 416 if the requested range is invalid' do
68
+ response = IntervalResponse.new(seq, 'HTTP_RANGE' => "bytes=6-5")
69
+ expect(response.status_code).to eq(416)
70
+ expect(response.headers).to eq(
71
+ "Accept-Ranges" => "bytes",
72
+ 'Content-Length' => IntervalResponse::Invalid::ERROR_JSON.bytesize.to_s,
73
+ "Content-Type" => "application/json",
74
+ 'Content-Range' => "bytes */#{seq.size}",
75
+ 'ETag' => seq.etag
76
+ )
77
+ expect(response.etag).to eq(seq.etag)
78
+ expect(response).not_to be_multiple_ranges
79
+ expect(response).not_to be_satisfied_with_first_interval
80
+ end
81
+
61
82
  it 'returns a single HTTP range if the client asked for it and it can be satisfied' do
62
- response = IntervalResponse.new(seq, "bytes=2-4", _if_range = nil)
83
+ response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=2-4")
63
84
  expect(response.status_code).to eq(206)
64
85
  expect(response.content_length).to eq(3)
65
86
  expect(response.headers).to eq(
@@ -69,13 +90,37 @@ RSpec.describe IntervalResponse do
69
90
  "Content-Type" => "binary/octet-stream",
70
91
  'ETag' => seq.etag,
71
92
  )
93
+ expect(response.etag).to eq(seq.etag)
94
+ expect(response).not_to be_multiple_ranges
95
+ expect(response).not_to be_satisfied_with_first_interval
96
+
72
97
  expect { |b|
73
98
  response.each(&b)
74
99
  }.to yield_successive_args([segment_a, 2..2], [segment_b, 0..1])
75
100
  end
76
101
 
102
+ it 'returns a single HTTP range if the client asked for it and hints it can be satisfied from the first interval' do
103
+ response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=0-0")
104
+ expect(response.status_code).to eq(206)
105
+ expect(response.content_length).to eq(1)
106
+ expect(response.headers).to eq(
107
+ "Accept-Ranges" => "bytes",
108
+ "Content-Length" => "1",
109
+ "Content-Range" => "bytes 0-0/8",
110
+ "Content-Type" => "binary/octet-stream",
111
+ 'ETag' => seq.etag,
112
+ )
113
+ expect(response.etag).to eq(seq.etag)
114
+ expect(response).not_to be_multiple_ranges
115
+ expect(response).to be_satisfied_with_first_interval
116
+
117
+ expect { |b|
118
+ response.each(&b)
119
+ }.to yield_successive_args([segment_a, 0..0])
120
+ end
121
+
77
122
  it 'returns a single HTTP range if the client asked for it and it can be satisfied, ETag matches' do
78
- response = IntervalResponse.new(seq, "bytes=2-4", _if_range = seq.etag)
123
+ response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=2-4", "HTTP_IF_RANGE" => seq.etag)
79
124
  expect(response.status_code).to eq(206)
80
125
  expect(response.content_length).to eq(3)
81
126
  expect(response.headers).to eq(
@@ -85,13 +130,14 @@ RSpec.describe IntervalResponse do
85
130
  "Content-Type" => "binary/octet-stream",
86
131
  'ETag' => seq.etag,
87
132
  )
133
+ expect(response.etag).to eq(seq.etag)
88
134
  expect { |b|
89
135
  response.each(&b)
90
136
  }.to yield_successive_args([segment_a, 2..2], [segment_b, 0..1])
91
137
  end
92
138
 
93
- it 'responss with the entier resource if the Range is satisfiable but the If-Range specifies a different ETag than the sequence' do
94
- response = IntervalResponse.new(seq, _http_range_header = "bytes=12901-", _http_if_range = '"different"')
139
+ it 'responds with the entire resource if the Range is satisfiable but the If-Range specifies a different ETag than the sequence' do
140
+ response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=12901-", "HTTP_IF_RANGE" => '"different"')
95
141
  expect(response.status_code).to eq(200)
96
142
  expect(response.content_length).to eq(8)
97
143
  expect(response.headers).to eq(
@@ -100,10 +146,13 @@ RSpec.describe IntervalResponse do
100
146
  "Content-Type" => "binary/octet-stream",
101
147
  'ETag' => seq.etag,
102
148
  )
149
+ expect(response.etag).to eq(seq.etag)
150
+ expect(response).not_to be_multiple_ranges
151
+ expect(response).not_to be_satisfied_with_first_interval
103
152
  end
104
153
 
105
- it 'responds with the range that can be satisfied if asked for 2 of which one is unsatisfiable' do
106
- response = IntervalResponse.new(seq, _http_range_header = "bytes=0-5,12901-", _http_if_range = nil)
154
+ it 'responds with the range that can be satisfied if asked for 2, of which one is unsatisfiable' do
155
+ response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=0-5,12901-")
107
156
  expect(response.status_code).to eq(206)
108
157
  expect(response.content_length).to eq(6)
109
158
  expect(response.headers).to eq(
@@ -113,6 +162,9 @@ RSpec.describe IntervalResponse do
113
162
  "Content-Type" => "binary/octet-stream",
114
163
  'ETag' => seq.etag,
115
164
  )
165
+ expect(response.etag).to eq(seq.etag)
166
+ expect(response).not_to be_multiple_ranges
167
+ expect(response).not_to be_satisfied_with_first_interval
116
168
 
117
169
  expect { |b|
118
170
  response.each(&b)
@@ -120,7 +172,7 @@ RSpec.describe IntervalResponse do
120
172
  end
121
173
 
122
174
  it 'responds with MIME multipart of ranges if the client asked for it and it can be satisfied' do
123
- response = IntervalResponse.new(seq, _http_range_header = "bytes=0-0,2-2", _http_if_range = nil)
175
+ response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=0-0,2-2")
124
176
  response.instance_variable_set('@boundary', 'tcROXEYMdRNXRRYstW296yM1')
125
177
 
126
178
  expect(response.status_code).to eq(206)
@@ -131,6 +183,9 @@ RSpec.describe IntervalResponse do
131
183
  "Content-Type" => "multipart/byte-ranges; boundary=tcROXEYMdRNXRRYstW296yM1",
132
184
  'ETag' => seq.etag,
133
185
  )
186
+ expect(response.etag).to eq(seq.etag)
187
+ expect(response).to be_multiple_ranges
188
+ expect(response).to be_satisfied_with_first_interval
134
189
 
135
190
  output = StringIO.new
136
191
  response.each do |segment, range|
@@ -7,7 +7,7 @@ RSpec.describe 'IntervalResponse used in a Rack application' do
7
7
  let(:app) do
8
8
  ->(env) {
9
9
  interval_sequence = IntervalResponse::Sequence.new(*segments)
10
- response = IntervalResponse.new(interval_sequence, env['HTTP_RANGE'], env['HTTP_IF_RANGE'])
10
+ response = IntervalResponse.new(interval_sequence, env)
11
11
  response.to_rack_response_triplet
12
12
  }
13
13
  end
@@ -49,4 +49,19 @@ RSpec.describe 'IntervalResponse used in a Rack application' do
49
49
  expect(last_response.status).to eq(206)
50
50
  expect(last_response.content_length).to eq(56897)
51
51
  end
52
+
53
+ it 'serves from LazyFile objects' do
54
+ tiny = "tiny string"
55
+ file_a = tempfile_with_random_bytes(4 * 1024 * 1024)
56
+ file_b = tempfile_with_random_bytes(7 * 1024 * 1024)
57
+
58
+ @segments = [tiny, IntervalResponse::LazyFile.new(file_a.path), IntervalResponse::LazyFile.new(file_b.path)]
59
+ get '/big', nil, 'HTTP_RANGE' => 'bytes=1-5'
60
+ expect(last_response.status).to eq(206)
61
+ expect(last_response.content_length).to eq(5)
62
+
63
+ get '/big', nil, 'HTTP_RANGE' => 'bytes=2-56898'
64
+ expect(last_response.status).to eq(206)
65
+ expect(last_response.content_length).to eq(56897)
66
+ end
52
67
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interval_response
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julik Tarkhanov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-25 00:00:00.000000000 Z
11
+ date: 2021-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.17'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.17'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +133,7 @@ files:
133
133
  - ".rspec"
134
134
  - ".rubocop.yml"
135
135
  - ".travis.yml"
136
+ - CHANGELOG.md
136
137
  - Gemfile
137
138
  - LICENSE.txt
138
139
  - README.md
@@ -141,14 +142,15 @@ files:
141
142
  - bin/setup
142
143
  - interval_response.gemspec
143
144
  - lib/interval_response.rb
145
+ - lib/interval_response/abstract.rb
144
146
  - lib/interval_response/empty.rb
145
147
  - lib/interval_response/full.rb
146
148
  - lib/interval_response/invalid.rb
147
149
  - lib/interval_response/lazy_file.rb
148
150
  - lib/interval_response/multi.rb
151
+ - lib/interval_response/rack_body_wrapper.rb
149
152
  - lib/interval_response/sequence.rb
150
153
  - lib/interval_response/single.rb
151
- - lib/interval_response/to_rack_response_triplet.rb
152
154
  - lib/interval_response/version.rb
153
155
  - spec/interval_response/sequence_spec.rb
154
156
  - spec/interval_response_spec.rb
@@ -156,12 +158,12 @@ files:
156
158
  - spec/spec_helper.rb
157
159
  homepage: https://github.com/WeTransfer/interval_response
158
160
  licenses:
159
- - MIT
161
+ - MIT (Hippocratic)
160
162
  metadata:
161
163
  homepage_uri: https://github.com/WeTransfer/interval_response
162
164
  source_code_uri: https://github.com/WeTransfer/interval_response
163
165
  changelog_uri: https://github.com/WeTransfer/interval_response
164
- post_install_message:
166
+ post_install_message:
165
167
  rdoc_options: []
166
168
  require_paths:
167
169
  - lib
@@ -176,8 +178,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
178
  - !ruby/object:Gem::Version
177
179
  version: '0'
178
180
  requirements: []
179
- rubygems_version: 3.0.6
180
- signing_key:
181
+ rubygems_version: 3.0.3
182
+ signing_key:
181
183
  specification_version: 4
182
184
  summary: Assemble HTTP responses from spliced sequences of payloads
183
185
  test_files: []
@@ -1,58 +0,0 @@
1
- module IntervalResponse::ToRackResponseTriplet
2
- CHUNK_SIZE = 65 * 1024 # Roughly one TCP kernel buffer
3
-
4
- class RackBodyWrapper
5
- def initialize(with_interval_response, chunk_size:)
6
- @chunk_size = chunk_size
7
- @interval_response = with_interval_response
8
- end
9
-
10
- def each
11
- buf = String.new(capacity: @chunk_size)
12
- @interval_response.each do |segment, range_in_segment|
13
- case segment
14
- when IntervalResponse::LazyFile
15
- segment.with do |_file_handle|
16
- with_each_chunk(range_in_segment) do |offset, read_n|
17
- segment.seek(offset, IO::SEEK_SET)
18
- yield segment.read_nonblock(read_n, buf)
19
- end
20
- end
21
- when String
22
- with_each_chunk(range_in_segment) do |offset, read_n|
23
- yield segment.slice(offset, read_n)
24
- end
25
- when IO, Tempfile
26
- with_each_chunk(range_in_segment) do |offset, read_n|
27
- segment.seek(offset, IO::SEEK_SET)
28
- yield segment.read_nonblock(read_n, buf)
29
- end
30
- else
31
- raise TypeError, "RackBodyWrapper only supports IOs or Strings"
32
- end
33
- end
34
- ensure
35
- buf.clear
36
- end
37
-
38
- def with_each_chunk(range_in_segment)
39
- range_size = range_in_segment.end - range_in_segment.begin + 1
40
- start_at_offset = range_in_segment.begin
41
- n_whole_segments, remainder = range_size.divmod(@chunk_size)
42
-
43
- n_whole_segments.times do |n|
44
- unit_offset = start_at_offset + (n * @chunk_size)
45
- yield unit_offset, @chunk_size
46
- end
47
-
48
- if remainder > 0
49
- unit_offset = start_at_offset + (n_whole_segments * @chunk_size)
50
- yield unit_offset, remainder
51
- end
52
- end
53
- end
54
-
55
- def to_rack_response_triplet(headers: nil, chunk_size: CHUNK_SIZE)
56
- [status_code, headers.to_h.merge(self.headers), RackBodyWrapper.new(self, chunk_size: chunk_size)]
57
- end
58
- end