interval_response 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/LICENSE.txt +33 -21
- data/README.md +6 -4
- data/interval_response.gemspec +1 -1
- data/lib/interval_response.rb +33 -15
- data/lib/interval_response/abstract.rb +115 -0
- data/lib/interval_response/empty.rb +2 -12
- data/lib/interval_response/full.rb +6 -12
- data/lib/interval_response/invalid.rb +4 -10
- data/lib/interval_response/multi.rb +9 -12
- data/lib/interval_response/sequence.rb +48 -10
- data/lib/interval_response/single.rb +8 -9
- data/lib/interval_response/version.rb +1 -1
- data/spec/interval_response/sequence_spec.rb +17 -0
- data/spec/interval_response_spec.rb +19 -10
- data/spec/rack_stack_spec.rb +16 -1
- metadata +5 -4
- data/lib/interval_response/to_rack_response_triplet.rb +0 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 627cc1b428d5adbde188c9718a9fabeacf4d852126500439ec672f1caea8b88f
|
4
|
+
data.tar.gz: e42cae558b987ef86bc9bb351a3e5196bf68af9bfa2f0918a6f16d8fbbb52036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 991b923a00ccb2b510e45044cf578c19e38a6ce65669188c91c16b736dfee0aba1352126d5552e114acc6a30d8ecc322d478c1af3091e3b7761d395cd521d5ec
|
7
|
+
data.tar.gz: e229ea7a13015012c7ecf8125b0f401e8b24b236de19a5a65b9c67afac6121966c269d31062a1a84a713e5caa25485e30eaf9e7c73a6bf3e452c2dd25ea4f67c
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# 0.1.5
|
2
|
+
|
3
|
+
* Change the API of `IntervalResponse.new` to accept the Rack `env` hash directly, without having the caller extract the header values manually.
|
4
|
+
* Allow intervals to set ETags which contribute to the final ETag
|
5
|
+
* 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
|
6
|
+
* Switch the license to MIT-Hippocratic
|
7
|
+
|
8
|
+
# 0.1.4
|
9
|
+
|
10
|
+
Initial public release
|
data/LICENSE.txt
CHANGED
@@ -1,21 +1,33 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
in the Software
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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.
|
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
|
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
|
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/
|
77
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/WeTransfer/interval_response.
|
76
78
|
|
77
79
|
## License
|
78
80
|
|
data/interval_response.gemspec
CHANGED
@@ -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.
|
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.
|
data/lib/interval_response.rb
CHANGED
@@ -5,8 +5,8 @@ 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"
|
9
8
|
require_relative "interval_response/sequence"
|
9
|
+
require_relative "interval_response/abstract"
|
10
10
|
require_relative "interval_response/empty"
|
11
11
|
require_relative "interval_response/single"
|
12
12
|
require_relative "interval_response/invalid"
|
@@ -16,50 +16,68 @@ module IntervalResponse
|
|
16
16
|
|
17
17
|
ENTIRE_RESOURCE_RANGE = 'bytes=0-'
|
18
18
|
|
19
|
-
|
19
|
+
# Creates a new IntervalResponse object. The object returned does not
|
20
|
+
# have a specific class, but is one of the following objects, which
|
21
|
+
# all support the same interface:
|
22
|
+
#
|
23
|
+
# * IntervalResponse::Empty for an empty response
|
24
|
+
# * IntervalResponse::Single for a single HTTP range
|
25
|
+
# * IntervalResponse::Full for the entire resource
|
26
|
+
# * IntervalResponse::Multi for multipart ranges response with multiple HTTP ranges
|
27
|
+
# * IntervalResponse::Invalid for responses that are 416 (Unsatisfiable range)
|
28
|
+
#
|
29
|
+
# @param interval_sequence[IntervalResponse::Sequence] the sequence of segments
|
30
|
+
# @param rack_env_headers[Hash] the Rack env, or a Hash containing 'HTTP_RANGE' and 'HTTP_IF_RANGE' headers
|
31
|
+
# @return [Empty, Single, Full, Multi, Invalid]
|
32
|
+
def self.new(interval_sequence, rack_env_headers)
|
33
|
+
http_range_header_value = rack_env_headers['HTTP_RANGE']
|
34
|
+
http_if_range_header_value = rack_env_headers['HTTP_IF_RANGE']
|
35
|
+
|
20
36
|
# If the 'If-Range' header is provided but does not match, discard the Range header. It means
|
21
37
|
# that the client is requesting a certain representation of the resource and wants a range
|
22
38
|
# _within_ that representation, but the representation has since changed and the offsets
|
23
39
|
# no longer make sense. In that case we are supposed to answer with a 200 and the full
|
24
40
|
# monty.
|
25
|
-
if
|
41
|
+
if http_if_range_header_value && http_if_range_header_value != interval_sequence.etag
|
26
42
|
Measurometer.increment_counter('interval_response.if_range_mismatch', 1)
|
27
|
-
return new(
|
43
|
+
return new(interval_sequence, 'HTTP_RANGE' => ENTIRE_RESOURCE_RANGE)
|
28
44
|
end
|
29
45
|
|
30
|
-
if
|
46
|
+
if http_if_range_header_value
|
31
47
|
Measurometer.increment_counter('interval_response.if_range_match', 1)
|
32
|
-
elsif
|
48
|
+
elsif http_range_header_value
|
33
49
|
Measurometer.increment_counter('interval_response.if_range_not_provided', 1)
|
34
50
|
end
|
35
51
|
|
36
|
-
prepare_response(
|
52
|
+
prepare_response(interval_sequence, http_range_header_value).tap do |res|
|
37
53
|
response_type_name_for_metric = res.class.to_s.split('::').last.downcase # Some::Module::Empty => empty
|
38
54
|
Measurometer.increment_counter('interval_response.resp_%s' % response_type_name_for_metric, 1)
|
39
55
|
end
|
40
56
|
end
|
41
57
|
|
42
|
-
def self.prepare_response(
|
58
|
+
def self.prepare_response(interval_sequence, http_range_header_value)
|
43
59
|
# Case 1 - response of 0 bytes (empty resource).
|
44
60
|
# We don't even have to parse the Range header for this since
|
45
61
|
# the response will be the same, always.
|
46
|
-
return Empty.new(
|
62
|
+
return Empty.new(interval_sequence) if interval_sequence.empty?
|
47
63
|
|
48
64
|
# Parse the HTTP Range: header
|
49
|
-
range_request_header =
|
50
|
-
http_ranges = Rack::Utils.get_byte_ranges(range_request_header,
|
65
|
+
range_request_header = http_range_header_value || ENTIRE_RESOURCE_RANGE
|
66
|
+
http_ranges = Rack::Utils.get_byte_ranges(range_request_header, interval_sequence.size)
|
51
67
|
|
52
68
|
# Case 2 - Client did send us a Range header, but Rack discarded
|
53
69
|
# it because it is invalid and cannot be satisfied
|
54
|
-
return Invalid.new(
|
70
|
+
return Invalid.new(interval_sequence) if http_range_header_value && (http_ranges.nil? || http_ranges.empty?)
|
55
71
|
|
56
72
|
# Case 3 - entire resource
|
57
|
-
return Full.new(
|
73
|
+
return Full.new(interval_sequence) if http_ranges.length == 1 && http_ranges.first == (0..(interval_sequence.size - 1))
|
58
74
|
|
59
75
|
# Case 4 - one content range
|
60
|
-
return Single.new(
|
76
|
+
return Single.new(interval_sequence, http_ranges[0]) if http_ranges.length == 1
|
61
77
|
|
62
78
|
# Case 5 - MIME multipart with multiple content ranges
|
63
|
-
Multi.new(
|
79
|
+
Multi.new(interval_sequence, http_ranges)
|
64
80
|
end
|
81
|
+
|
82
|
+
private_class_method :prepare_response
|
65
83
|
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# Base class for all response types, primarily for ease of documentation
|
2
|
+
class IntervalResponse::Abstract
|
3
|
+
# The Rack body wrapper is intended to return as the third element
|
4
|
+
# of the Rack response triplet. It supports the #each method
|
5
|
+
# and will call to the IntervalResponse object given to it
|
6
|
+
# at instantiation, filling up a pre-allocated String object
|
7
|
+
# with the bytes to be served out.
|
8
|
+
class RackBodyWrapper
|
9
|
+
# Default size of the chunk (String buffer) which is going to be
|
10
|
+
# yielded to the caller of the `each` method.
|
11
|
+
# Set toroughly one TCP kernel buffer
|
12
|
+
CHUNK_SIZE = 65 * 1024
|
13
|
+
|
14
|
+
def initialize(with_interval_response, chunk_size:)
|
15
|
+
@chunk_size = chunk_size
|
16
|
+
@interval_response = with_interval_response
|
17
|
+
end
|
18
|
+
|
19
|
+
def each
|
20
|
+
buf = String.new(capacity: @chunk_size)
|
21
|
+
@interval_response.each do |segment, range_in_segment|
|
22
|
+
case segment
|
23
|
+
when IntervalResponse::LazyFile
|
24
|
+
segment.with do |file_handle|
|
25
|
+
with_each_chunk(range_in_segment) do |offset, read_n|
|
26
|
+
file_handle.seek(offset, IO::SEEK_SET)
|
27
|
+
yield file_handle.read(read_n, buf)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
when String
|
31
|
+
with_each_chunk(range_in_segment) do |offset, read_n|
|
32
|
+
yield segment.slice(offset, read_n)
|
33
|
+
end
|
34
|
+
when IO, Tempfile
|
35
|
+
with_each_chunk(range_in_segment) do |offset, read_n|
|
36
|
+
segment.seek(offset, IO::SEEK_SET)
|
37
|
+
yield segment.read(read_n, buf)
|
38
|
+
end
|
39
|
+
else
|
40
|
+
raise TypeError, "RackBodyWrapper only supports IOs or Strings"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
ensure
|
44
|
+
buf.clear
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def with_each_chunk(range_in_segment)
|
50
|
+
range_size = range_in_segment.end - range_in_segment.begin + 1
|
51
|
+
start_at_offset = range_in_segment.begin
|
52
|
+
n_whole_segments, remainder = range_size.divmod(@chunk_size)
|
53
|
+
|
54
|
+
n_whole_segments.times do |n|
|
55
|
+
unit_offset = start_at_offset + (n * @chunk_size)
|
56
|
+
yield unit_offset, @chunk_size
|
57
|
+
end
|
58
|
+
|
59
|
+
if remainder > 0
|
60
|
+
unit_offset = start_at_offset + (n_whole_segments * @chunk_size)
|
61
|
+
yield unit_offset, remainder
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_rack_response_triplet(headers: nil, chunk_size: RackBodyWrapper::CHUNK_SIZE)
|
67
|
+
[status_code, headers.to_h.merge(self.headers), RackBodyWrapper.new(self, chunk_size: chunk_size)]
|
68
|
+
end
|
69
|
+
|
70
|
+
# @param interval_sequence[IntervalResponse::Sequence] the sequence the response is built for
|
71
|
+
def initialize(interval_sequence)
|
72
|
+
@interval_sequence = interval_sequence
|
73
|
+
end
|
74
|
+
|
75
|
+
# Returns the ETag of the interval sequence
|
76
|
+
def etag
|
77
|
+
@interval_sequence.etag
|
78
|
+
end
|
79
|
+
|
80
|
+
# Yields every segment and the range within that segment to be returned to the client. For multipart
|
81
|
+
# responses the envelopes of the parts will be returned as segments as well
|
82
|
+
#
|
83
|
+
# @yield [Object, Range]
|
84
|
+
def each
|
85
|
+
# No-op
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns the HTTP status code of the response
|
89
|
+
#
|
90
|
+
# @return [Integer]
|
91
|
+
def status_code
|
92
|
+
200
|
93
|
+
end
|
94
|
+
|
95
|
+
# Returns the exact number of bytes that the response is. If the response
|
96
|
+
# is a range it will be the length of the range. If the response is a multipart
|
97
|
+
# byte range response it will be the content length of the ranges plus the content
|
98
|
+
# length of all the envelopes.
|
99
|
+
#
|
100
|
+
# @return [Integer]
|
101
|
+
def content_length
|
102
|
+
0
|
103
|
+
end
|
104
|
+
|
105
|
+
# Returns headers for the HTTP response
|
106
|
+
# @return [Hash]
|
107
|
+
def headers
|
108
|
+
{
|
109
|
+
'Accept-Ranges' => 'bytes',
|
110
|
+
'Content-Length' => '0',
|
111
|
+
'Content-Type' => 'binary/octet-stream',
|
112
|
+
'ETag' => etag,
|
113
|
+
}
|
114
|
+
end
|
115
|
+
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' =>
|
16
|
+
'ETag' => etag,
|
27
17
|
}
|
28
18
|
end
|
29
19
|
end
|
@@ -1,15 +1,9 @@
|
|
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
|
7
|
-
end
|
8
|
-
|
2
|
+
class IntervalResponse::Full < IntervalResponse::Abstract
|
9
3
|
def each
|
10
4
|
# serve the part of the interval map
|
11
|
-
full_range = 0..(@
|
12
|
-
@
|
5
|
+
full_range = 0..(@interval_sequence.size - 1)
|
6
|
+
@interval_sequence.each_in_range(full_range) do |segment, range_in_segment|
|
13
7
|
yield(segment, range_in_segment)
|
14
8
|
end
|
15
9
|
end
|
@@ -19,15 +13,15 @@ class IntervalResponse::Full
|
|
19
13
|
end
|
20
14
|
|
21
15
|
def content_length
|
22
|
-
@
|
16
|
+
@interval_sequence.size
|
23
17
|
end
|
24
18
|
|
25
19
|
def headers
|
26
20
|
{
|
27
21
|
'Accept-Ranges' => 'bytes',
|
28
|
-
'Content-Length' => @
|
22
|
+
'Content-Length' => @interval_sequence.size.to_s,
|
29
23
|
'Content-Type' => 'binary/octet-stream',
|
30
|
-
'ETag' =>
|
24
|
+
'ETag' => etag,
|
31
25
|
}
|
32
26
|
end
|
33
27
|
end
|
@@ -1,13 +1,7 @@
|
|
1
|
-
# Serves out a response
|
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 */#{@
|
30
|
-
'ETag' =>
|
23
|
+
'Content-Range' => "bytes */#{@interval_sequence.size}",
|
24
|
+
'ETag' => etag,
|
31
25
|
}
|
32
26
|
end
|
33
27
|
end
|
@@ -1,18 +1,15 @@
|
|
1
|
-
|
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
|
-
|
9
|
-
|
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
|
-
#
|
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 =
|
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
|
-
@
|
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,7 +43,7 @@ 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' =>
|
46
|
+
'ETag' => etag,
|
50
47
|
}
|
51
48
|
end
|
52
49
|
|
@@ -65,7 +62,7 @@ class IntervalResponse::Multi
|
|
65
62
|
part_index > 0 ? "\r\n" : "", # Parts follwing the first have to be delimited "at the top"
|
66
63
|
"--%s\r\n" % @boundary,
|
67
64
|
"Content-Type: binary/octet-stream\r\n",
|
68
|
-
"Content-Range: bytes %d-%d/%d\r\n" % [http_r.begin, http_r.end, @
|
65
|
+
"Content-Range: bytes %d-%d/%d\r\n" % [http_r.begin, http_r.end, @interval_sequence.size],
|
69
66
|
"\r\n",
|
70
67
|
].join
|
71
68
|
end
|
@@ -1,35 +1,65 @@
|
|
1
1
|
require 'digest'
|
2
2
|
|
3
|
-
#
|
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
|
-
|
10
|
-
|
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
29
|
segment_size_or_bytesize = segment.respond_to?(:bytesize) ? segment.bytesize : segment.size
|
22
|
-
return self if segment_size_or_bytesize == 0
|
23
|
-
|
24
30
|
add_segment(segment, size: segment_size_or_bytesize)
|
25
|
-
self
|
26
31
|
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
+
@intervals << Interval.new(segment, size, @size, @intervals.length, etag_quoted)
|
46
|
+
@size += size
|
47
|
+
end
|
48
|
+
self
|
31
49
|
end
|
32
50
|
|
51
|
+
# Yields every segment which is touched by the given Range in resource in sequence,
|
52
|
+
# together with a Range object which defines the necessary part of the segment.
|
53
|
+
# For example, calling `each_in_range(0..2)` with 2 segments of size 1 and 2
|
54
|
+
# will successively yield [segment1, 0..0] then [segment2, 0..1]
|
55
|
+
#
|
56
|
+
# Interval sequences can be nested - you can place a Sequence inside another Sequence
|
57
|
+
# as a segment. In that case when you call `each_in_range` on the outer Sequence and you
|
58
|
+
# need to retrieve data from the inner Sequence which is one of the segments, the call will
|
59
|
+
# yield the segments from the inner Sequence, "drilling down" as deep as is appropriate.
|
60
|
+
#
|
61
|
+
# @param from_range_in_resource[Range] an inclusive Range that specifies the range within the segment map
|
62
|
+
# @yield segment[Object], range_in_segment[Range]
|
33
63
|
def each_in_range(from_range_in_resource)
|
34
64
|
# Skip empty ranges
|
35
65
|
requested_range_size = (from_range_in_resource.end - from_range_in_resource.begin) + 1
|
@@ -57,6 +87,7 @@ class IntervalResponse::Sequence
|
|
57
87
|
end
|
58
88
|
end
|
59
89
|
|
90
|
+
# Tells whether the size of the entire sequence is 0
|
60
91
|
def empty?
|
61
92
|
@size == 0
|
62
93
|
end
|
@@ -87,10 +118,17 @@ class IntervalResponse::Sequence
|
|
87
118
|
# enclosed in double-quotes.
|
88
119
|
#
|
89
120
|
# See for more https://blogs.msdn.microsoft.com/ieinternals/2011/06/03/download-resumption-in-internet-explorer/
|
121
|
+
#
|
122
|
+
# The ETag value gets derived from the ETags of the segments, which will be Marshal.dump'ed together
|
123
|
+
# and then added to the hash digest to produce the final ETag value.
|
124
|
+
#
|
125
|
+
# @return [String] a string delimited with double-quotes
|
90
126
|
def etag
|
91
127
|
d = Digest::SHA1.new
|
92
128
|
d << IntervalResponse::VERSION
|
93
|
-
|
129
|
+
@intervals.each do |interval|
|
130
|
+
d << interval.etag
|
131
|
+
end
|
94
132
|
'"%s"' % d.hexdigest
|
95
133
|
end
|
96
134
|
|
@@ -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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@
|
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
|
-
@
|
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,13 @@ 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, @
|
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' =>
|
32
|
+
'ETag' => etag,
|
34
33
|
}
|
35
34
|
end
|
36
35
|
end
|
@@ -66,6 +66,23 @@ RSpec.describe IntervalResponse::Sequence do
|
|
66
66
|
expect(etag_for_sequence_of_same_sizes).not_to eq(etag_for_sequence)
|
67
67
|
end
|
68
68
|
|
69
|
+
it 'takes explicit etags into account if they are set on the intervals' do
|
70
|
+
seq = described_class.new
|
71
|
+
seq.add_segment(:a, size: 6)
|
72
|
+
etag_of_size_6 = seq.etag
|
73
|
+
|
74
|
+
seq = described_class.new
|
75
|
+
seq.add_segment(:a, size: 6)
|
76
|
+
another_etag_of_size_6 = seq.etag
|
77
|
+
|
78
|
+
seq = described_class.new
|
79
|
+
seq.add_segment(:a, size: 6, etag: "Some random etag")
|
80
|
+
etag_set_explicitly = seq.etag
|
81
|
+
|
82
|
+
expect(etag_of_size_6).to eq(another_etag_of_size_6)
|
83
|
+
expect(etag_of_size_6).not_to eq(etag_set_explicitly)
|
84
|
+
end
|
85
|
+
|
69
86
|
it 'can handle a range that stretches outside of the available range' do
|
70
87
|
a = double('a', size: 3)
|
71
88
|
b = double('b', size: 4)
|
@@ -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,
|
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-'
|
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,
|
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,14 @@ 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)
|
56
59
|
expect { |b|
|
57
60
|
response.each(&b)
|
58
61
|
}.to yield_successive_args([segment_a, 0..2], [segment_b, 0..3], [segment_c, 0..0])
|
59
62
|
end
|
60
63
|
|
61
64
|
it 'returns 416 if the requested range is invalid' do
|
62
|
-
response = IntervalResponse.new(seq, "bytes=6-5"
|
65
|
+
response = IntervalResponse.new(seq, 'HTTP_RANGE' => "bytes=6-5")
|
63
66
|
expect(response.status_code).to eq(416)
|
64
67
|
expect(response.headers).to eq(
|
65
68
|
"Accept-Ranges" => "bytes",
|
@@ -68,10 +71,11 @@ RSpec.describe IntervalResponse do
|
|
68
71
|
'Content-Range' => "bytes */#{seq.size}",
|
69
72
|
'ETag' => seq.etag
|
70
73
|
)
|
74
|
+
expect(response.etag).to eq(seq.etag)
|
71
75
|
end
|
72
76
|
|
73
77
|
it 'returns a single HTTP range if the client asked for it and it can be satisfied' do
|
74
|
-
response = IntervalResponse.new(seq, "bytes=2-4"
|
78
|
+
response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=2-4")
|
75
79
|
expect(response.status_code).to eq(206)
|
76
80
|
expect(response.content_length).to eq(3)
|
77
81
|
expect(response.headers).to eq(
|
@@ -81,13 +85,14 @@ RSpec.describe IntervalResponse do
|
|
81
85
|
"Content-Type" => "binary/octet-stream",
|
82
86
|
'ETag' => seq.etag,
|
83
87
|
)
|
88
|
+
expect(response.etag).to eq(seq.etag)
|
84
89
|
expect { |b|
|
85
90
|
response.each(&b)
|
86
91
|
}.to yield_successive_args([segment_a, 2..2], [segment_b, 0..1])
|
87
92
|
end
|
88
93
|
|
89
94
|
it 'returns a single HTTP range if the client asked for it and it can be satisfied, ETag matches' do
|
90
|
-
response = IntervalResponse.new(seq, "bytes=2-4",
|
95
|
+
response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=2-4", "HTTP_IF_RANGE" => seq.etag)
|
91
96
|
expect(response.status_code).to eq(206)
|
92
97
|
expect(response.content_length).to eq(3)
|
93
98
|
expect(response.headers).to eq(
|
@@ -97,13 +102,14 @@ RSpec.describe IntervalResponse do
|
|
97
102
|
"Content-Type" => "binary/octet-stream",
|
98
103
|
'ETag' => seq.etag,
|
99
104
|
)
|
105
|
+
expect(response.etag).to eq(seq.etag)
|
100
106
|
expect { |b|
|
101
107
|
response.each(&b)
|
102
108
|
}.to yield_successive_args([segment_a, 2..2], [segment_b, 0..1])
|
103
109
|
end
|
104
110
|
|
105
111
|
it 'responss with the entier resource if the Range is satisfiable but the If-Range specifies a different ETag than the sequence' do
|
106
|
-
response = IntervalResponse.new(seq,
|
112
|
+
response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=12901-", "HTTP_IF_RANGE" => '"different"')
|
107
113
|
expect(response.status_code).to eq(200)
|
108
114
|
expect(response.content_length).to eq(8)
|
109
115
|
expect(response.headers).to eq(
|
@@ -112,10 +118,11 @@ RSpec.describe IntervalResponse do
|
|
112
118
|
"Content-Type" => "binary/octet-stream",
|
113
119
|
'ETag' => seq.etag,
|
114
120
|
)
|
121
|
+
expect(response.etag).to eq(seq.etag)
|
115
122
|
end
|
116
123
|
|
117
|
-
it 'responds with the range that can be satisfied if asked for 2 of which one is unsatisfiable' do
|
118
|
-
response = IntervalResponse.new(seq,
|
124
|
+
it 'responds with the range that can be satisfied if asked for 2, of which one is unsatisfiable' do
|
125
|
+
response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=0-5,12901-")
|
119
126
|
expect(response.status_code).to eq(206)
|
120
127
|
expect(response.content_length).to eq(6)
|
121
128
|
expect(response.headers).to eq(
|
@@ -125,6 +132,7 @@ RSpec.describe IntervalResponse do
|
|
125
132
|
"Content-Type" => "binary/octet-stream",
|
126
133
|
'ETag' => seq.etag,
|
127
134
|
)
|
135
|
+
expect(response.etag).to eq(seq.etag)
|
128
136
|
|
129
137
|
expect { |b|
|
130
138
|
response.each(&b)
|
@@ -132,7 +140,7 @@ RSpec.describe IntervalResponse do
|
|
132
140
|
end
|
133
141
|
|
134
142
|
it 'responds with MIME multipart of ranges if the client asked for it and it can be satisfied' do
|
135
|
-
response = IntervalResponse.new(seq,
|
143
|
+
response = IntervalResponse.new(seq, "HTTP_RANGE" => "bytes=0-0,2-2")
|
136
144
|
response.instance_variable_set('@boundary', 'tcROXEYMdRNXRRYstW296yM1')
|
137
145
|
|
138
146
|
expect(response.status_code).to eq(206)
|
@@ -143,6 +151,7 @@ RSpec.describe IntervalResponse do
|
|
143
151
|
"Content-Type" => "multipart/byte-ranges; boundary=tcROXEYMdRNXRRYstW296yM1",
|
144
152
|
'ETag' => seq.etag,
|
145
153
|
)
|
154
|
+
expect(response.etag).to eq(seq.etag)
|
146
155
|
|
147
156
|
output = StringIO.new
|
148
157
|
response.each do |segment, range|
|
data/spec/rack_stack_spec.rb
CHANGED
@@ -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
|
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.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julik Tarkhanov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -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,6 +142,7 @@ 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
|
@@ -148,7 +150,6 @@ files:
|
|
148
150
|
- lib/interval_response/multi.rb
|
149
151
|
- lib/interval_response/sequence.rb
|
150
152
|
- lib/interval_response/single.rb
|
151
|
-
- lib/interval_response/to_rack_response_triplet.rb
|
152
153
|
- lib/interval_response/version.rb
|
153
154
|
- spec/interval_response/sequence_spec.rb
|
154
155
|
- spec/interval_response_spec.rb
|
@@ -156,7 +157,7 @@ files:
|
|
156
157
|
- spec/spec_helper.rb
|
157
158
|
homepage: https://github.com/WeTransfer/interval_response
|
158
159
|
licenses:
|
159
|
-
- MIT
|
160
|
+
- MIT (Hippocratic)
|
160
161
|
metadata:
|
161
162
|
homepage_uri: https://github.com/WeTransfer/interval_response
|
162
163
|
source_code_uri: https://github.com/WeTransfer/interval_response
|
@@ -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
|