summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOmar Roth <omarroth@protonmail.com>2019-07-01 10:45:09 -0500
committerOmar Roth <omarroth@protonmail.com>2019-07-01 10:45:09 -0500
commit041debcd93bac3ad15b1b0e305e8148e46353c59 (patch)
treee56a37f40b2e850bed575762df8bb36089b0d422
parent0632a2d3c8ddb81e5028af9ea0daa76d87fa1d5a (diff)
downloadinvidious-041debcd93bac3ad15b1b0e305e8148e46353c59.tar.gz
invidious-041debcd93bac3ad15b1b0e305e8148e46353c59.tar.bz2
invidious-041debcd93bac3ad15b1b0e305e8148e46353c59.zip
Revert "Chunk videoplayback response to avoid throttling"
This reverts commit 818cd2454de4d6ea20d4ac2765a7d4e4083eecd9.
-rw-r--r--src/invidious.cr67
-rw-r--r--src/invidious/helpers/helpers.cr2
-rw-r--r--src/invidious/helpers/utils.cr18
3 files changed, 26 insertions, 61 deletions
diff --git a/src/invidious.cr b/src/invidious.cr
index 56064393..a170ded7 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -54,7 +54,6 @@ MAX_ITEMS_PER_PAGE = 1500
REQUEST_HEADERS_WHITELIST = {"Accept", "Accept-Encoding", "Cache-Control", "Connection", "Content-Length", "If-None-Match", "Range"}
RESPONSE_HEADERS_BLACKLIST = {"Access-Control-Allow-Origin", "Alt-Svc", "Server"}
-HTTP_CHUNK_SIZE = 10485760 # ~10MB
CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }}
CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }}
@@ -4587,55 +4586,39 @@ get "/videoplayback" do |env|
next
end
+ client = make_client(URI.parse(host), region)
begin
- range_begin, range_end = parse_range(env.request.headers["Range"]?)
- (range_begin...range_end).each_slice(HTTP_CHUNK_SIZE) do |slice|
- headers["Range"] = "bytes=#{slice[0]}-#{slice[-1]}"
- begin
- client = make_client(URI.parse(host), region)
- client.get(url, headers) do |response|
- content_range = response.headers["Content-Range"].lchop("bytes ")
- content_size = content_range.split("/")[-1].to_i
-
- # Write headers for first chunk
- if content_range.split("-")[0].to_i == range_begin
- if !env.request.headers["Range"]? && response.status_code == 206
- env.response.status_code = 200
- else
- env.response.status_code = response.status_code
- end
-
- response.headers.each do |key, value|
- if !RESPONSE_HEADERS_BLACKLIST.includes?(key) && key != "Content-Range"
- env.response.headers[key] = value
- end
- end
+ client.get(url, headers) do |response|
+ env.response.status_code = response.status_code
- reported_end = range_end ? range_end : content_size
- env.response.content_length = reported_end - range_begin
+ response.headers.each do |key, value|
+ if !RESPONSE_HEADERS_BLACKLIST.includes? key
+ env.response.headers[key] = value
+ end
+ end
- if env.request.headers["Range"]?
- env.response.headers["Content-Range"] = "bytes #{range_begin}-#{reported_end - 1}/#{content_size}"
- end
+ env.response.headers["Access-Control-Allow-Origin"] = "*"
- env.response.headers["Access-Control-Allow-Origin"] = "*"
+ if response.headers["Location"]?
+ url = URI.parse(response.headers["Location"])
+ host = url.host
- if title = query_params["title"]?
- # https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
- env.response.headers["Content-Disposition"] = "attachment; filename=\"#{URI.escape(title)}\"; filename*=UTF-8''#{URI.escape(title)}"
- end
- end
+ url = url.full_path
+ url += "&host=#{host}"
- proxy_file(response, env)
- end
- rescue ex
- # FIXME: Potential bug in HTTP::Client
- if ex.message == "Error reading socket: Connection reset by peer"
- next
- else
- raise ex
+ if region
+ url += "&region=#{region}"
end
+
+ next env.redirect url
+ end
+
+ if title = query_params["title"]?
+ # https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
+ env.response.headers["Content-Disposition"] = "attachment; filename=\"#{URI.escape(title)}\"; filename*=UTF-8''#{URI.escape(title)}"
end
+
+ proxy_file(response, env)
end
rescue ex
end
diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr
index 197946f4..039ac55b 100644
--- a/src/invidious/helpers/helpers.cr
+++ b/src/invidious/helpers/helpers.cr
@@ -656,7 +656,7 @@ def proxy_file(response, env)
end
# https://stackoverflow.com/a/44802810 <3
-def copy_in_chunks(input, output, chunk_size = 8192)
+def copy_in_chunks(input, output, chunk_size = 4096)
size = 1
while size > 0
size = IO.copy(input, output, chunk_size)
diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr
index 472e2600..1eb02b89 100644
--- a/src/invidious/helpers/utils.cr
+++ b/src/invidious/helpers/utils.cr
@@ -347,21 +347,3 @@ def subscribe_pubsub(topic, key, config)
return client.post("/subscribe", form: body)
end
-
-def parse_range(range)
- if !range
- return 0, nil
- end
-
- ranges = range.lchop("bytes=").split(',')
- ranges.each do |range|
- start_range, end_range = range.split('-')
-
- start_range = start_range.to_i? || 0
- end_range = end_range.to_i?.try &.+ 1
-
- return start_range, end_range
- end
-
- return 0, nil
-end