mirror of
https://github.com/iv-org/invidious.git
synced 2025-08-23 04:58:29 +00:00
Video playback: remove chunking logic
This commit is contained in:
parent
8af202e86b
commit
40fe6e08d6
@ -51,9 +51,23 @@ CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345
|
|||||||
TEST_IDS = {"AgbeGFYluEA", "BaW_jenozKc", "a9LDPn-MO4I", "ddFvjfvPnqk", "iqKdEhx-dD4"}
|
TEST_IDS = {"AgbeGFYluEA", "BaW_jenozKc", "a9LDPn-MO4I", "ddFvjfvPnqk", "iqKdEhx-dD4"}
|
||||||
MAX_ITEMS_PER_PAGE = 1500
|
MAX_ITEMS_PER_PAGE = 1500
|
||||||
|
|
||||||
REQUEST_HEADERS_WHITELIST = {"accept", "accept-encoding", "cache-control", "content-length", "if-none-match", "range"}
|
REQUEST_HEADERS_WHITELIST = {
|
||||||
RESPONSE_HEADERS_BLACKLIST = {"access-control-allow-origin", "alt-svc", "server"}
|
"accept",
|
||||||
HTTP_CHUNK_SIZE = 10485760 # ~10MB
|
"accept-encoding",
|
||||||
|
"cache-control",
|
||||||
|
"content-length",
|
||||||
|
"if-none-match",
|
||||||
|
"range",
|
||||||
|
}
|
||||||
|
RESPONSE_HEADERS_BLACKLIST = {
|
||||||
|
"access-control-allow-origin",
|
||||||
|
"alt-svc",
|
||||||
|
"server",
|
||||||
|
"content-security-polic",
|
||||||
|
"x-content-type-options",
|
||||||
|
"x-frame-options",
|
||||||
|
"x-xss-protection",
|
||||||
|
}
|
||||||
|
|
||||||
CURRENT_BRANCH = {{ "#{`git branch | sed -n '/* /s///p'`.strip}" }}
|
CURRENT_BRANCH = {{ "#{`git branch | sed -n '/* /s///p'`.strip}" }}
|
||||||
CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }}
|
CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }}
|
||||||
|
@ -111,75 +111,44 @@ module Invidious::Routes::VideoPlayback
|
|||||||
return error_template(403, "Administrator has disabled this endpoint.")
|
return error_template(403, "Administrator has disabled this endpoint.")
|
||||||
end
|
end
|
||||||
|
|
||||||
content_length = nil
|
request_range = env.request.headers["Range"]?
|
||||||
first_chunk = true
|
range_start, range_end = parse_range(request_range)
|
||||||
range_start, range_end = parse_range(env.request.headers["Range"]?)
|
headers["Range"] = "bytes=#{range_start}-#{range_end}"
|
||||||
chunk_start = range_start
|
|
||||||
chunk_end = range_end
|
|
||||||
|
|
||||||
if !chunk_end || chunk_end - chunk_start > HTTP_CHUNK_SIZE
|
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
if range_end && chunk_start > range_end
|
|
||||||
break
|
|
||||||
end
|
|
||||||
|
|
||||||
if range_end && chunk_end > range_end
|
|
||||||
chunk_end = range_end
|
|
||||||
end
|
|
||||||
|
|
||||||
headers["Range"] = "bytes=#{chunk_start}-#{chunk_end}"
|
|
||||||
|
|
||||||
|
# Try once normally, and retry if a region is required.
|
||||||
|
2.times do
|
||||||
begin
|
begin
|
||||||
client.get(url, headers) do |resp|
|
client.get(url, headers) do |resp|
|
||||||
if first_chunk
|
if !env.request.headers["Range"]? && resp.status_code == 206
|
||||||
if !env.request.headers["Range"]? && resp.status_code == 206
|
env.response.status_code = 200
|
||||||
env.response.status_code = 200
|
else
|
||||||
else
|
env.response.status_code = resp.status_code
|
||||||
env.response.status_code = resp.status_code
|
end
|
||||||
|
|
||||||
|
resp.headers.each do |key, value|
|
||||||
|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) && key.downcase != "content-range"
|
||||||
|
env.response.headers[key] = value
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
resp.headers.each do |key, value|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||||
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) && key.downcase != "content-range"
|
|
||||||
env.response.headers[key] = value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
if location = resp.headers["Location"]?
|
||||||
|
location = URI.parse(location)
|
||||||
|
location = "#{location.request_target}&host=#{location.host}#{region ? "®ion=#{region}" : ""}"
|
||||||
|
|
||||||
if location = resp.headers["Location"]?
|
env.redirect location
|
||||||
location = URI.parse(location)
|
break
|
||||||
location = "#{location.request_target}&host=#{location.host}#{region ? "®ion=#{region}" : ""}"
|
end
|
||||||
|
|
||||||
env.redirect location
|
if title = query_params["title"]?
|
||||||
break
|
# https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
|
||||||
end
|
env.response.headers["Content-Disposition"] = "attachment; filename=\"#{URI.encode_www_form(title)}\"; filename*=UTF-8''#{URI.encode_www_form(title)}"
|
||||||
|
|
||||||
if title = query_params["title"]?
|
|
||||||
# https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
|
|
||||||
env.response.headers["Content-Disposition"] = "attachment; filename=\"#{URI.encode_www_form(title)}\"; filename*=UTF-8''#{URI.encode_www_form(title)}"
|
|
||||||
end
|
|
||||||
|
|
||||||
if !resp.headers.includes_word?("Transfer-Encoding", "chunked")
|
|
||||||
content_length = resp.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
|
end
|
||||||
|
|
||||||
proxy_file(resp, env)
|
proxy_file(resp, env)
|
||||||
end
|
end
|
||||||
|
break
|
||||||
rescue ex
|
rescue ex
|
||||||
if ex.message != "Error reading socket: Connection reset by peer"
|
if ex.message != "Error reading socket: Connection reset by peer"
|
||||||
break
|
break
|
||||||
@ -188,12 +157,9 @@ module Invidious::Routes::VideoPlayback
|
|||||||
client = make_client(URI.parse(host), region)
|
client = make_client(URI.parse(host), region)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
chunk_start = chunk_end + 1
|
|
||||||
chunk_end += HTTP_CHUNK_SIZE
|
|
||||||
first_chunk = false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
client.close
|
client.close
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user