serve_byte_range 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +31 -7
- data/.gitignore +3 -2
- data/.standard.yml +4 -0
- data/README.md +58 -0
- data/Rakefile +19 -6
- data/Steepfile +10 -0
- data/gemfiles/rack_2.gemfile +5 -0
- data/gemfiles/rack_3.gemfile +5 -0
- data/gemfiles/typecheck.gemfile +7 -0
- data/lib/serve_byte_range/sequence.rb +152 -0
- data/lib/serve_byte_range/version.rb +5 -1
- data/lib/serve_byte_range.rb +222 -43
- data/serve_byte_range.gemspec +9 -4
- data/sig/serve_byte_range/sequence.rbs +91 -0
- data/sig/serve_byte_range/version.rbs +5 -0
- data/sig/serve_byte_range.rbs +256 -0
- data/sig_shims/rack.rbs +9 -0
- data/test/byte_comparator_test.rb +46 -0
- data/test/range_server_test.rb +123 -6
- data/test/sequence_test.rb +224 -0
- data/test/support/byte_comparator.rb +47 -0
- data/test/support/bytes_assertions.rb +10 -0
- metadata +36 -48
- data/rbi/serve_byte_range.rbi +0 -208
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# Generated from lib/serve_byte_range.rb with RBS::Inline
|
|
2
|
+
|
|
3
|
+
# Serves HTTP `Range` requests lazily - see ServeByteRange.serve_ranges and ServeByteRange.serve_sequence
|
|
4
|
+
module ServeByteRange
|
|
5
|
+
type serving_block = ^(Range[Integer] range, BlockWritableWithLimit io) -> void
|
|
6
|
+
|
|
7
|
+
type rack_response = [ Integer, Hash[String, String], ByteRangeBody | EmptyBody ]
|
|
8
|
+
|
|
9
|
+
# The IO-ish object the serving blocks write into. Passes the bytes on and makes sure the
|
|
10
|
+
# amount of bytes written matches what was advertised in `Content-Length`
|
|
11
|
+
class BlockWritableWithLimit
|
|
12
|
+
COPY_ON_WRITE_SAFE_WITH_COPY_STREAM: bool
|
|
13
|
+
|
|
14
|
+
@limit: Integer
|
|
15
|
+
|
|
16
|
+
@label: String | ^() -> String
|
|
17
|
+
|
|
18
|
+
@written: Integer
|
|
19
|
+
|
|
20
|
+
@block_that_accepts_bytes: ^(String) -> void
|
|
21
|
+
|
|
22
|
+
# Yields a writable which passes the bytes on to `sink`, and makes sure exactly `limit` bytes got written.
|
|
23
|
+
# The `label` can be a callable returning the String, so that it only gets built when there is an error.
|
|
24
|
+
#
|
|
25
|
+
# @rbs limit: Integer -- how many bytes have to be written
|
|
26
|
+
# @rbs sink: ^(String) -> void -- receives the written bytes
|
|
27
|
+
# @rbs label: String | ^() -> String -- what is being written, for the error messages
|
|
28
|
+
# @rbs &block: (BlockWritableWithLimit io) -> void
|
|
29
|
+
# @rbs return: void
|
|
30
|
+
def self.write_exactly: (Integer limit, ^(String) -> void sink, ?label: String | ^() -> String) { (BlockWritableWithLimit io) -> void } -> void
|
|
31
|
+
|
|
32
|
+
# @rbs limit: Integer
|
|
33
|
+
# @rbs label: String | ^() -> String
|
|
34
|
+
# @rbs &block_that_accepts_writes: (String) -> void
|
|
35
|
+
def initialize: (Integer limit, ?label: String | ^() -> String) { (String) -> void } -> untyped
|
|
36
|
+
|
|
37
|
+
# @rbs string: String
|
|
38
|
+
# @rbs return: Integer
|
|
39
|
+
def write: (String string) -> Integer
|
|
40
|
+
|
|
41
|
+
# @rbs return: void
|
|
42
|
+
def verify!: () -> void
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
# `b` and `dup` do not copy the bytes, they share the memory with the original until either gets
|
|
47
|
+
# modified. IO.copy_stream on Ruby 2.7 to 3.1 reads into its buffer without breaking that sharing,
|
|
48
|
+
# so our "copy" got overwritten while the webserver still held on to it. There we have to copy
|
|
49
|
+
# the bytes for real by appending, which may eat memory: every chunk gets duplicated, and so does a
|
|
50
|
+
# huge string written in one go (100MB of `body.read` briefly takes 200MB). Starting with 3.2
|
|
51
|
+
# IO.copy_stream un-shares its buffer before reading into it, so the cheap `b` is safe.
|
|
52
|
+
# @rbs string: String
|
|
53
|
+
# @rbs return: String
|
|
54
|
+
def binary_copy: (String string) -> String
|
|
55
|
+
|
|
56
|
+
# @rbs return: String
|
|
57
|
+
def label: () -> String
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
class ByteRangeBody
|
|
61
|
+
@http_range: Range[Integer]
|
|
62
|
+
|
|
63
|
+
@resource_size: Integer
|
|
64
|
+
|
|
65
|
+
@resource_content_type: String
|
|
66
|
+
|
|
67
|
+
@serving_block: serving_block
|
|
68
|
+
|
|
69
|
+
# @rbs http_range: Range[Integer]
|
|
70
|
+
# @rbs resource_size: Integer
|
|
71
|
+
# @rbs resource_content_type: String
|
|
72
|
+
# @rbs &serving_block: (Range[Integer] range, BlockWritableWithLimit io) -> void
|
|
73
|
+
def initialize: (http_range: Range[Integer], resource_size: Integer, ?resource_content_type: String) { (Range[Integer] range, BlockWritableWithLimit io) -> void } -> untyped
|
|
74
|
+
|
|
75
|
+
# @rbs return: Integer
|
|
76
|
+
def status: () -> Integer
|
|
77
|
+
|
|
78
|
+
# @rbs &blk: (String) -> void
|
|
79
|
+
# @rbs return: void
|
|
80
|
+
def each: () { (String) -> void } -> void
|
|
81
|
+
|
|
82
|
+
# @rbs return: Integer
|
|
83
|
+
def content_length: () -> Integer
|
|
84
|
+
|
|
85
|
+
# @rbs return: String
|
|
86
|
+
def content_type: () -> String
|
|
87
|
+
|
|
88
|
+
# @rbs return: Hash[String, String]
|
|
89
|
+
def headers: () -> Hash[String, String]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
class EmptyBody
|
|
93
|
+
# @rbs return: Integer
|
|
94
|
+
def status: () -> Integer
|
|
95
|
+
|
|
96
|
+
# @rbs return: Integer
|
|
97
|
+
def content_length: () -> Integer
|
|
98
|
+
|
|
99
|
+
# @rbs &blk: (String) -> void
|
|
100
|
+
# @rbs return: void
|
|
101
|
+
def each: () { (String) -> void } -> void
|
|
102
|
+
|
|
103
|
+
# @rbs return: Hash[String, String]
|
|
104
|
+
def headers: () -> Hash[String, String]
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
class NotModifiedBody < EmptyBody
|
|
108
|
+
# @rbs return: Integer
|
|
109
|
+
def status: () -> Integer
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
class WholeBody < ByteRangeBody
|
|
113
|
+
# @rbs resource_size: Integer
|
|
114
|
+
# @rbs resource_content_type: String
|
|
115
|
+
# @rbs &serving_block: (Range[Integer] range, BlockWritableWithLimit io) -> void
|
|
116
|
+
def initialize: (resource_size: Integer, ?resource_content_type: String) { (Range[Integer] range, BlockWritableWithLimit io) -> void } -> untyped
|
|
117
|
+
|
|
118
|
+
# @rbs return: Integer
|
|
119
|
+
def status: () -> Integer
|
|
120
|
+
|
|
121
|
+
# @rbs return: Hash[String, String]
|
|
122
|
+
def headers: () -> Hash[String, String]
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class Unsatisfiable < EmptyBody
|
|
126
|
+
@resource_size: Integer
|
|
127
|
+
|
|
128
|
+
# @rbs resource_size: Integer
|
|
129
|
+
def initialize: (resource_size: Integer) -> untyped
|
|
130
|
+
|
|
131
|
+
# @rbs return: Integer
|
|
132
|
+
def status: () -> Integer
|
|
133
|
+
|
|
134
|
+
# @rbs return: Hash[String, String]
|
|
135
|
+
def headers: () -> Hash[String, String]
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# See https://www.ietf.org/archive/id/draft-ietf-httpbis-p5-range-09.html
|
|
139
|
+
class MultipartByteRangesBody < ByteRangeBody
|
|
140
|
+
@http_ranges: Array[Range[Integer]]
|
|
141
|
+
|
|
142
|
+
@boundary: String
|
|
143
|
+
|
|
144
|
+
@envelope_size: Integer?
|
|
145
|
+
|
|
146
|
+
# @rbs http_ranges: Array[Range[Integer]]
|
|
147
|
+
# @rbs boundary: String
|
|
148
|
+
# @rbs resource_size: Integer
|
|
149
|
+
# @rbs resource_content_type: String
|
|
150
|
+
# @rbs &serving_block: (Range[Integer] range, BlockWritableWithLimit io) -> void
|
|
151
|
+
def initialize: (http_ranges: Array[Range[Integer]], boundary: String, resource_size: Integer, ?resource_content_type: String) { (Range[Integer] range, BlockWritableWithLimit io) -> void } -> untyped
|
|
152
|
+
|
|
153
|
+
# @rbs &blk: (String) -> void
|
|
154
|
+
# @rbs return: void
|
|
155
|
+
def each: () { (String) -> void } -> void
|
|
156
|
+
|
|
157
|
+
# @rbs return: Integer
|
|
158
|
+
def content_length: () -> Integer
|
|
159
|
+
|
|
160
|
+
# @rbs return: String
|
|
161
|
+
def content_type: () -> String
|
|
162
|
+
|
|
163
|
+
# @rbs return: Hash[String, String]
|
|
164
|
+
def headers: () -> Hash[String, String]
|
|
165
|
+
|
|
166
|
+
private
|
|
167
|
+
|
|
168
|
+
# @rbs return: Integer
|
|
169
|
+
def compute_envelope_size: () -> Integer
|
|
170
|
+
|
|
171
|
+
# @rbs return: String
|
|
172
|
+
def trailer: () -> String
|
|
173
|
+
|
|
174
|
+
# @rbs http_range: Range[Integer]
|
|
175
|
+
# @rbs part_index: Integer
|
|
176
|
+
# @rbs return: String
|
|
177
|
+
def part_header: (Range[Integer] http_range, Integer part_index) -> String
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Strictly - the boundary is supposed to not appear in any of the parts of the multipart response, so _first_
|
|
181
|
+
# you need to scan the response, pick a byte sequence that does not occur in it, and then use that. In practice,
|
|
182
|
+
# nobody does that - and a well-behaved HTTP client should honor the Content-Range header when extracting
|
|
183
|
+
# the byte range from the response. See https://stackoverflow.com/questions/37413715
|
|
184
|
+
#
|
|
185
|
+
# @rbs return: String
|
|
186
|
+
def self.generate_boundary: () -> String
|
|
187
|
+
|
|
188
|
+
# The RFC specifically gives an example of non-canonical, but still
|
|
189
|
+
# valid request for overlapping ranges:
|
|
190
|
+
#
|
|
191
|
+
# > Several legal but not canonical specifications of the second 500
|
|
192
|
+
# > bytes (byte offsets 500-999, inclusive):
|
|
193
|
+
#
|
|
194
|
+
# bytes=500-600,601-999
|
|
195
|
+
# bytes=500-700,601-999
|
|
196
|
+
#
|
|
197
|
+
# In such cases, ranges need to be collapsed together. First, to avoid serving
|
|
198
|
+
# a tiny byte range over and over - causing excessive requests to upstream,
|
|
199
|
+
# second - to optimize for doing less requests in total.
|
|
200
|
+
#
|
|
201
|
+
# @rbs ranges: Array[Range[Integer]] -- inclusive, limited ranges of integers
|
|
202
|
+
# @rbs return: Array[Range[Integer]] -- ranges squashed together honoring their overlaps
|
|
203
|
+
def self.coalesce_ranges: (Array[Range[Integer]] ranges) -> Array[Range[Integer]]
|
|
204
|
+
|
|
205
|
+
# Quotes the ETag unless it already is a quoted strong or weak (`W/"..."`) entity tag
|
|
206
|
+
#
|
|
207
|
+
# @rbs etag: untyped -- the ETag, quoted or not
|
|
208
|
+
# @rbs return: String?
|
|
209
|
+
def self.quote_etag: (untyped etag) -> String?
|
|
210
|
+
|
|
211
|
+
# If-Range must use the strong comparison, so a weak ETag never matches.
|
|
212
|
+
# See https://www.rfc-editor.org/rfc/rfc9110#section-13.1.5
|
|
213
|
+
#
|
|
214
|
+
# @rbs header_value: String -- the value of the If-Range header
|
|
215
|
+
# @rbs etag: String? -- the quoted current ETag
|
|
216
|
+
# @rbs return: bool
|
|
217
|
+
def self.strong_etag_match?: (String header_value, String? etag) -> bool
|
|
218
|
+
|
|
219
|
+
# If-None-Match is `*` or a list of entity tags, compared with the weak comparison.
|
|
220
|
+
# See https://www.rfc-editor.org/rfc/rfc9110#section-13.1.2
|
|
221
|
+
#
|
|
222
|
+
# @rbs header_value: String -- the value of the If-None-Match header
|
|
223
|
+
# @rbs etag: String? -- the quoted current ETag
|
|
224
|
+
# @rbs return: bool
|
|
225
|
+
def self.weak_etag_match?: (String header_value, String? etag) -> bool
|
|
226
|
+
|
|
227
|
+
# Serves a resource of `resource_size` bytes, yielding every range that has to be sent along with
|
|
228
|
+
# the IO-ish object to `write()` the bytes of that range into. Returns the Rack response triplet:
|
|
229
|
+
#
|
|
230
|
+
# status, headers, body = ServeByteRange.serve_ranges(env, resource_size: file.size) do |range, io|
|
|
231
|
+
# IO.copy_stream(file, io, range.size, range.begin)
|
|
232
|
+
# end
|
|
233
|
+
# [status, headers, body]
|
|
234
|
+
#
|
|
235
|
+
# @rbs env: Hash[String, untyped] -- the Rack env
|
|
236
|
+
# @rbs resource_size: Integer -- the size of the complete resource in bytes
|
|
237
|
+
# @rbs etag: untyped -- the current ETag of the resource, or nil if none
|
|
238
|
+
# @rbs resource_content_type: String -- the MIME type string of the resource
|
|
239
|
+
# @rbs multipart_boundary: String -- defaults to an automatically generated pseudo-random string
|
|
240
|
+
# @rbs &range_serving_block: (Range[Integer] range, BlockWritableWithLimit io) -> void
|
|
241
|
+
# @rbs return: rack_response
|
|
242
|
+
def self.serve_ranges: (Hash[String, untyped] env, resource_size: Integer, ?etag: untyped, ?resource_content_type: String, ?multipart_boundary: String) { (Range[Integer] range, BlockWritableWithLimit io) -> void } -> rack_response
|
|
243
|
+
|
|
244
|
+
# Serves a Sequence, yielding every segment that a requested range touches, the range of bytes within
|
|
245
|
+
# that segment and the IO-ish object to `write()` those bytes into. Writes for every segment are checked
|
|
246
|
+
# against the size of the range within that segment.
|
|
247
|
+
#
|
|
248
|
+
# @rbs env: Hash[String, untyped] -- the Rack env
|
|
249
|
+
# @rbs sequence: Sequence -- the Sequence to serve
|
|
250
|
+
# @rbs etag: untyped -- defaults to the ETag of the Sequence, pass nil to not send one
|
|
251
|
+
# @rbs resource_content_type: String -- the MIME type string of the resource
|
|
252
|
+
# @rbs multipart_boundary: String -- defaults to an automatically generated pseudo-random string
|
|
253
|
+
# @rbs &segment_serving_block: (untyped segment, Range[Integer] range_in_segment, BlockWritableWithLimit io) -> void
|
|
254
|
+
# @rbs return: rack_response
|
|
255
|
+
def self.serve_sequence: (Hash[String, untyped] env, Sequence sequence, ?etag: untyped, ?resource_content_type: String, ?multipart_boundary: String) { (untyped segment, Range[Integer] range_in_segment, BlockWritableWithLimit io) -> void } -> rack_response
|
|
256
|
+
end
|
data/sig_shims/rack.rbs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Just the parts of Rack we use - Rack does not ship signatures of its own
|
|
2
|
+
module Rack
|
|
3
|
+
module Utils
|
|
4
|
+
def self.get_byte_ranges: (String? http_range, Integer size, ?max_ranges: Integer) -> Array[Range[Integer]]?
|
|
5
|
+
|
|
6
|
+
# Rack 1.x only has this one
|
|
7
|
+
def self.byte_ranges: (Hash[String, untyped] env, Integer size) -> Array[Range[Integer]]?
|
|
8
|
+
end
|
|
9
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler"
|
|
4
|
+
Bundler.setup
|
|
5
|
+
|
|
6
|
+
require "minitest"
|
|
7
|
+
require "minitest/autorun"
|
|
8
|
+
require_relative "support/bytes_assertions"
|
|
9
|
+
|
|
10
|
+
class ByteComparatorTest < Minitest::Test
|
|
11
|
+
include BytesAssertions
|
|
12
|
+
|
|
13
|
+
def test_passes_for_same_bytes_in_different_encodings
|
|
14
|
+
assert_same_bytes "é", "é".b
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_describes_first_difference
|
|
18
|
+
expected = "0123456789abcdef".b
|
|
19
|
+
actual = "0123456789abXdef".b
|
|
20
|
+
error = assert_raises(Minitest::Assertion) { assert_same_bytes(expected, actual) }
|
|
21
|
+
assert error.message.start_with?("Bytes differ\n") # same length, so no sizes
|
|
22
|
+
assert_includes error.message, "diff at offset 12,"
|
|
23
|
+
assert_includes error.message, "should have been [0x63 0x64 0x65 0x66]"
|
|
24
|
+
assert_includes error.message, "but was [0x58 0x64 0x65 0x66]"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_describes_truncation_and_differences_past_the_first_block
|
|
28
|
+
expected = ("a" * 5000 + "bc").b
|
|
29
|
+
error = assert_raises(Minitest::Assertion) { assert_same_bytes(expected, "a" * 5000, "Custom") }
|
|
30
|
+
assert error.message.start_with?("Custom\nBytes differ (expected 5002 bytes, got 5000)\n")
|
|
31
|
+
assert_includes error.message, "at offset 5000,"
|
|
32
|
+
assert_includes error.message, "should have been [0x62 0x63]"
|
|
33
|
+
assert_includes error.message, "but was [empty]"
|
|
34
|
+
|
|
35
|
+
error = assert_raises(Minitest::Assertion) { assert_same_bytes("", "x") }
|
|
36
|
+
assert_includes error.message, "diff at offset 0,"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_comparator_reports_offset_and_nothing_for_same_bytes
|
|
40
|
+
assert_nil ByteComparator.new("abc", "abc".b).first_difference_offset
|
|
41
|
+
assert_nil ByteComparator.new("abc", "abc").describe_difference
|
|
42
|
+
assert_equal 2, ByteComparator.new("abc", "abd").first_difference_offset
|
|
43
|
+
assert_equal 3, ByteComparator.new("abc", "abcd").first_difference_offset
|
|
44
|
+
assert_equal 5000, ByteComparator.new("a" * 6000, "a" * 5000 + "b" * 1000).first_difference_offset
|
|
45
|
+
end
|
|
46
|
+
end
|
data/test/range_server_test.rb
CHANGED
|
@@ -6,8 +6,12 @@ Bundler.setup
|
|
|
6
6
|
require_relative "../lib/serve_byte_range"
|
|
7
7
|
require "minitest"
|
|
8
8
|
require "minitest/autorun"
|
|
9
|
+
require_relative "support/bytes_assertions"
|
|
10
|
+
require "tempfile"
|
|
9
11
|
|
|
10
12
|
class ServeByteRangeTest < Minitest::Test
|
|
13
|
+
include BytesAssertions
|
|
14
|
+
|
|
11
15
|
def test_serves_whole_body
|
|
12
16
|
rng = Random.new(Minitest.seed)
|
|
13
17
|
bytes = rng.bytes(474)
|
|
@@ -23,7 +27,7 @@ class ServeByteRangeTest < Minitest::Test
|
|
|
23
27
|
|
|
24
28
|
output = StringIO.new.binmode
|
|
25
29
|
body.each { |chunk| output.write(chunk) }
|
|
26
|
-
|
|
30
|
+
assert_same_bytes bytes, output.string
|
|
27
31
|
end
|
|
28
32
|
|
|
29
33
|
def test_serves_single_range
|
|
@@ -41,7 +45,7 @@ class ServeByteRangeTest < Minitest::Test
|
|
|
41
45
|
|
|
42
46
|
output = StringIO.new.binmode
|
|
43
47
|
body.each { |chunk| output.write(chunk) }
|
|
44
|
-
|
|
48
|
+
assert_same_bytes bytes[1..2], output.string
|
|
45
49
|
|
|
46
50
|
env = {"HTTP_RANGE" => "bytes=319-"}
|
|
47
51
|
status, headers, body = ServeByteRange.serve_ranges(env, resource_size: bytes.bytesize, etag: "AbCehZ", resource_content_type: "x-foo/ba", &serve_proc)
|
|
@@ -51,7 +55,7 @@ class ServeByteRangeTest < Minitest::Test
|
|
|
51
55
|
|
|
52
56
|
output = StringIO.new.binmode
|
|
53
57
|
body.each { |chunk| output.write(chunk) }
|
|
54
|
-
|
|
58
|
+
assert_same_bytes bytes[319..], output.string
|
|
55
59
|
|
|
56
60
|
env = {"HTTP_RANGE" => "bytes=0-0"}
|
|
57
61
|
status, headers, body = ServeByteRange.serve_ranges(env, resource_size: bytes.bytesize, etag: "AbCehZ", resource_content_type: "x-foo/ba", &serve_proc)
|
|
@@ -61,7 +65,7 @@ class ServeByteRangeTest < Minitest::Test
|
|
|
61
65
|
|
|
62
66
|
output = StringIO.new.binmode
|
|
63
67
|
body.each { |chunk| output.write(chunk) }
|
|
64
|
-
|
|
68
|
+
assert_same_bytes bytes[0..0], output.string
|
|
65
69
|
end
|
|
66
70
|
|
|
67
71
|
def test_serves_single_range_of_same_range_supplied_multiple_times_just_once
|
|
@@ -79,7 +83,7 @@ class ServeByteRangeTest < Minitest::Test
|
|
|
79
83
|
|
|
80
84
|
output = StringIO.new.binmode
|
|
81
85
|
body.each { |chunk| output.write(chunk) }
|
|
82
|
-
|
|
86
|
+
assert_same_bytes bytes[1..2], output.string
|
|
83
87
|
end
|
|
84
88
|
|
|
85
89
|
def test_unions_overlapping_ranges
|
|
@@ -97,7 +101,7 @@ class ServeByteRangeTest < Minitest::Test
|
|
|
97
101
|
|
|
98
102
|
output = StringIO.new.binmode
|
|
99
103
|
body.each { |chunk| output.write(chunk) }
|
|
100
|
-
|
|
104
|
+
assert_same_bytes bytes[1..9], output.string
|
|
101
105
|
end
|
|
102
106
|
|
|
103
107
|
def test_serves_multiple_ranges
|
|
@@ -211,4 +215,117 @@ class ServeByteRangeTest < Minitest::Test
|
|
|
211
215
|
# Ordering should be maintained (roughly) in the coalesced ranges
|
|
212
216
|
assert_equal [5..32, 0..0, 145..900, 4..4], ServeByteRange.coalesce_ranges(ordered_ranges)
|
|
213
217
|
end
|
|
218
|
+
|
|
219
|
+
def test_quote_etag
|
|
220
|
+
assert_nil ServeByteRange.quote_etag(nil)
|
|
221
|
+
assert_equal '"abc"', ServeByteRange.quote_etag("abc")
|
|
222
|
+
assert_equal '"abc"', ServeByteRange.quote_etag('"abc"')
|
|
223
|
+
assert_equal 'W/"abc"', ServeByteRange.quote_etag('W/"abc"')
|
|
224
|
+
assert_equal '"abc"', ServeByteRange.quote_etag('"abc')
|
|
225
|
+
assert_equal '"abc"', ServeByteRange.quote_etag('abc"')
|
|
226
|
+
assert_equal '"abc"', ServeByteRange.quote_etag('"a"bc"')
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def test_serves_weak_etag_as_is
|
|
230
|
+
status, headers, _ = ServeByteRange.serve_ranges({}, resource_size: 10, etag: 'W/"abc"') { |range, io| io.write("x" * range.size) }
|
|
231
|
+
assert_equal 200, status
|
|
232
|
+
assert_equal 'W/"abc"', headers["ETag"]
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def test_honors_if_range_with_the_etag_it_sent_when_given_unquoted_etag
|
|
236
|
+
bytes = Random.new(Minitest.seed).bytes(474)
|
|
237
|
+
serve_proc = ->(range, io) { io.write(bytes[range]) }
|
|
238
|
+
|
|
239
|
+
_, headers, _ = ServeByteRange.serve_ranges({}, resource_size: bytes.bytesize, etag: "abc", &serve_proc)
|
|
240
|
+
assert_equal '"abc"', headers["ETag"]
|
|
241
|
+
|
|
242
|
+
env = {"HTTP_RANGE" => "bytes=0-9", "HTTP_IF_RANGE" => headers["ETag"]}
|
|
243
|
+
status, headers, body = ServeByteRange.serve_ranges(env, resource_size: bytes.bytesize, etag: "abc", &serve_proc)
|
|
244
|
+
assert_equal 206, status
|
|
245
|
+
assert_equal "bytes 0-9/474", headers["Content-Range"]
|
|
246
|
+
output = StringIO.new.binmode
|
|
247
|
+
body.each { |chunk| output.write(chunk) }
|
|
248
|
+
assert_same_bytes bytes[0..9], output.string
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def test_honors_if_none_match_with_the_etag_it_sent_when_given_unquoted_etag
|
|
252
|
+
env = {"HTTP_IF_NONE_MATCH" => '"abc"'}
|
|
253
|
+
status, headers, _ = ServeByteRange.serve_ranges(env, resource_size: 10, etag: "abc") { |range, io| io.write("x" * range.size) }
|
|
254
|
+
assert_equal 304, status
|
|
255
|
+
assert_equal '"abc"', headers["ETag"]
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def test_serves_entire_document_on_if_range_with_weak_etag
|
|
259
|
+
env = {"HTTP_RANGE" => "bytes=0-1", "HTTP_IF_RANGE" => 'W/"abc"'}
|
|
260
|
+
status, _, _ = ServeByteRange.serve_ranges(env, resource_size: 10, etag: 'W/"abc"') { |range, io| io.write("x" * range.size) }
|
|
261
|
+
assert_equal 200, status # If-Range requires a strong match
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def test_serves_not_modified_on_weakly_matching_if_none_match
|
|
265
|
+
['"abc"', 'W/"abc"', '"x", "abc"', '"x",W/"abc"', "*", '"a,b", "abc"'].each do |if_none_match|
|
|
266
|
+
status, headers, _ = ServeByteRange.serve_ranges({"HTTP_IF_NONE_MATCH" => if_none_match}, resource_size: 10, etag: "abc") { |range, io| io.write("x" * range.size) }
|
|
267
|
+
assert_equal 304, status, "Expected 304 for If-None-Match: #{if_none_match}"
|
|
268
|
+
assert_equal '"abc"', headers["ETag"]
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
status, _, _ = ServeByteRange.serve_ranges({"HTTP_IF_NONE_MATCH" => '"abc"'}, resource_size: 10, etag: 'W/"abc"') { |range, io| io.write("x" * range.size) }
|
|
272
|
+
assert_equal 304, status # weak comparison ignores W/ on our side too
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def test_serves_whole_body_on_mismatching_if_none_match
|
|
276
|
+
['"abcd"', 'W/"ab"', '"x", "y"', '"a,b"', "abc", ""].each do |if_none_match|
|
|
277
|
+
status, _, _ = ServeByteRange.serve_ranges({"HTTP_IF_NONE_MATCH" => if_none_match}, resource_size: 10, etag: "abc") { |range, io| io.write("x" * range.size) }
|
|
278
|
+
assert_equal 200, status, "Expected 200 for If-None-Match: #{if_none_match}"
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
status, _, _ = ServeByteRange.serve_ranges({"HTTP_IF_NONE_MATCH" => '"abc"'}, resource_size: 10) { |range, io| io.write("x" * range.size) }
|
|
282
|
+
assert_equal 200, status
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def test_serves_not_modified_on_if_none_match_star_without_etag
|
|
286
|
+
status, headers, _ = ServeByteRange.serve_ranges({"HTTP_IF_NONE_MATCH" => "*"}, resource_size: 10) { |range, io| io.write("x" * range.size) }
|
|
287
|
+
assert_equal 304, status
|
|
288
|
+
refute headers.key?("ETag")
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def test_passes_only_frozen_binary_strings_through_without_copying
|
|
292
|
+
yielded = []
|
|
293
|
+
writable = ServeByteRange::BlockWritableWithLimit.new(12) { |chunk| yielded << chunk }
|
|
294
|
+
frozen_binary = "abcd".b.freeze
|
|
295
|
+
binary_buffer = "efgh".b
|
|
296
|
+
utf8 = "éé"
|
|
297
|
+
[frozen_binary, binary_buffer, utf8].each { |str| writable.write(str) }
|
|
298
|
+
writable.verify!
|
|
299
|
+
|
|
300
|
+
assert_same frozen_binary, yielded[0]
|
|
301
|
+
refute_same binary_buffer, yielded[1] # a reused buffer, as in IO.copy_stream, must get copied
|
|
302
|
+
refute_same utf8, yielded[2]
|
|
303
|
+
assert yielded.all? { |chunk| chunk.encoding == Encoding::BINARY }
|
|
304
|
+
assert_equal Encoding::UTF_8, utf8.encoding
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def test_chunks_from_copy_stream_survive_being_retained
|
|
308
|
+
bytes = Random.new(Minitest.seed).bytes(100_000)
|
|
309
|
+
source = Tempfile.new("source", binmode: true)
|
|
310
|
+
source.write(bytes)
|
|
311
|
+
source.flush
|
|
312
|
+
_, _, body = ServeByteRange.serve_ranges({}, resource_size: bytes.bytesize) { |range, io| IO.copy_stream(source, io, range.size, range.begin) }
|
|
313
|
+
retained = []
|
|
314
|
+
body.each { |chunk| retained << chunk } # what Rack::ETag does
|
|
315
|
+
assert_same_bytes bytes, retained.join
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def test_serves_not_modified_on_matching_if_none_match_even_with_a_range
|
|
319
|
+
env = {"HTTP_RANGE" => "bytes=0-1", "HTTP_IF_NONE_MATCH" => '"abc"'}
|
|
320
|
+
status, _, _ = ServeByteRange.serve_ranges(env, resource_size: 10, etag: "abc") { |range, io| io.write("x" * range.size) }
|
|
321
|
+
assert_equal 304, status
|
|
322
|
+
|
|
323
|
+
env = {"HTTP_RANGE" => "bytes=0-1", "HTTP_IF_RANGE" => '"abc"', "HTTP_IF_NONE_MATCH" => '"abc"'}
|
|
324
|
+
status, _, _ = ServeByteRange.serve_ranges(env, resource_size: 10, etag: "abc") { |range, io| io.write("x" * range.size) }
|
|
325
|
+
assert_equal 304, status
|
|
326
|
+
|
|
327
|
+
env = {"HTTP_RANGE" => "bytes=0-1", "HTTP_IF_NONE_MATCH" => '"old"'}
|
|
328
|
+
status, _, _ = ServeByteRange.serve_ranges(env, resource_size: 10, etag: "abc") { |range, io| io.write("x" * range.size) }
|
|
329
|
+
assert_equal 206, status
|
|
330
|
+
end
|
|
214
331
|
end
|