Skip to content

Commit f15ce58

Browse files
authored
Improve Content-Range logic to support video streaming better (#198)
* Improve `Content-Range` logic to support * require start range for simpler code
1 parent f7da41f commit f15ce58

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/server.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,19 +461,23 @@ function serve_file(
461461
end
462462
end
463463

464-
range_match = match(r"bytes=(\d+)-(\d+)" , HTTP.header(req, "Range", ""))
464+
range_match = match(r"^bytes=(\d+)-(\d*)$" , HTTP.header(req, "Range", ""))
465465
is_ranged = !isnothing(range_match)
466466

467467
headers = [
468468
"Content-Type" => content_type,
469469
]
470470
if is_ranged
471-
range = parse.(Int, range_match.captures)
471+
p(s) = isempty(s) ? nothing : parse(Int64, s)
472+
start, stop = p.(range_match.captures)
473+
@assert start isa Int64 # because the regex requires at least one digit
474+
stop = something(stop, binary_length(content) - 1)
475+
472476
push!(headers,
473477
"Content-Range" =>
474-
"bytes $(range[1])-$(range[2])/$(binary_length(content))"
478+
"bytes $(start)-$(stop)/$(binary_length(content))"
475479
)
476-
content = @view content[1+range[1]:1+range[2]]
480+
content = @view content[1+start:1+stop]
477481
ret_code = 206
478482
end
479483
if allow_cors

0 commit comments

Comments
 (0)