Allow ggpht queries with QUIC disabled

pull/2364/head
syeopite 2021-10-08 05:55:34 -07:00
rodzic 245122104a
commit c3747c2d49
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6FA616E5A5294A82
1 zmienionych plików z 45 dodań i 15 usunięć

Wyświetl plik

@ -3,31 +3,61 @@ module Invidious::Routes::Images
def self.ggpht(env)
url = env.request.path.lchop("/ggpht")
headers = HTTP::Headers{":authority" => "yt3.ggpht.com"}
headers = (
{% unless flag?(:disable_quic) %}
if CONFIG.use_quic
HTTP::Headers{":authority" => "yt3.ggpht.com"}
else
HTTP::Headers.new
end
{% else %}
HTTP::Headers.new
{% end %}
)
REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]?
headers[header] = env.request.headers[header]
end
end
# We're encapsulating this into a proc in order to easily reuse this
# portion of the code for each request block below.
request_proc = ->(response : HTTP::Client::Response) {
env.response.status_code = response.status_code
response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
env.response.headers[key] = value
end
end
env.response.headers["Access-Control-Allow-Origin"] = "*"
if response.status_code >= 300
env.response.headers.delete("Transfer-Encoding")
return
end
proxy_file(response, env)
}
begin
YT_POOL.client &.get(url, headers) do |response|
env.response.status_code = response.status_code
response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
env.response.headers[key] = value
{% unless flag?(:disable_quic) %}
if CONFIG.use_quic
YT_POOL.client &.get(url, headers) do |resp|
return request_proc.call(resp)
end
else
HTTP::Client.get("yt3.ggpht.com#{url}") do |resp|
return request_proc.call(resp)
end
end
env.response.headers["Access-Control-Allow-Origin"] = "*"
if response.status_code >= 300
env.response.headers.delete("Transfer-Encoding")
break
{% else %}
# This can likely be optimized into a (small) pool sometime in the future.
HTTP::Client.get("yt3.ggpht.com#{url}") do |resp|
return request_proc.call(resp)
end
proxy_file(response, env)
end
{% end %}
rescue ex
end
end