From 8033d1ca6dbe95896b5ca2e49d2755f6f70fcd0a Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Fri, 5 Jul 2019 11:02:12 -0500 Subject: [PATCH] Fix chunking for livestream segments --- src/invidious.cr | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 12d47921..3ee87a24 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -4651,6 +4651,7 @@ get "/videoplayback" do |env| chunk_end = chunk_start + HTTP_CHUNK_SIZE - 1 end + # TODO: Record bytes written so we can restart after a chunk fails while true if !range_end && content_length range_end = content_length @@ -4700,17 +4701,24 @@ get "/videoplayback" do |env| env.response.headers["Content-Disposition"] = "attachment; filename=\"#{URI.escape(title)}\"; filename*=UTF-8''#{URI.escape(title)}" end - content_length = response.headers["Content-Range"].split("/")[-1].to_i64 - if env.request.headers["Range"]? - env.response.headers["Content-Range"] = "bytes #{range_start}-#{range_end || (content_length - 1)}/#{content_length}" - env.response.content_length = ((range_end.try &.+ 1) || content_length) - range_start - else - env.response.content_length = content_length + if !response.headers.includes_word?("Transfer-Encoding", "chunked") + content_length = response.headers["Content-Range"].split("/")[-1].to_i64 + if env.request.headers["Range"]? + env.response.headers["Content-Range"] = "bytes #{range_start}-#{range_end || (content_length - 1)}/#{content_length}" + env.response.content_length = ((range_end.try &.+ 1) || content_length) - range_start + else + env.response.content_length = content_length + end end end proxy_file(response, env) end + + # For livestream segments, break after first chunk + if url.includes? "&file=seg.ts" + break + end rescue ex if ex.message != "Error reading socket: Connection reset by peer" break