Merge pull request #2623 from SamantazFox/temp-decompression-fix

Temporarily fix for #2612
This commit is contained in:
Samantaz Fox 2021-11-28 02:35:39 +01:00 committed by GitHub
commit 05f9613e14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -405,18 +405,9 @@ module YoutubeAPI
headers = HTTP::Headers{ headers = HTTP::Headers{
"Content-Type" => "application/json; charset=UTF-8", "Content-Type" => "application/json; charset=UTF-8",
"Accept-Encoding" => "gzip, deflate",
} }
# The normal HTTP client automatically applies accept-encoding: gzip,
# and decompresses. However, explicitly applying it will remove this functionality.
#
# https://github.com/crystal-lang/crystal/issues/11252#issuecomment-929594741
{% unless flag?(:disable_quic) %}
if CONFIG.use_quic
headers["Accept-Encoding"] = "gzip"
end
{% end %}
# Logging # Logging
LOGGER.debug("YoutubeAPI: Using endpoint: \"#{endpoint}\"") LOGGER.debug("YoutubeAPI: Using endpoint: \"#{endpoint}\"")
LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}") LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}")
@ -434,8 +425,38 @@ module YoutubeAPI
) )
end end
if {{ !flag?(:disable_quic) }} && CONFIG.use_quic
body = response.body
else
# Decompress the body ourselves, when using HTTP::Client given that
# auto-decompress is broken in the Crystal stdlib.
# Read more:
# - https://github.com/iv-org/invidious/issues/2612
# - https://github.com/crystal-lang/crystal/issues/11354
#
if encodings = response.headers["Content-Encoding"]?
io = response.body_io
# Multiple encodings can be combined, and are listed in the order
# in which they were applied. E.g: "deflate, gzip" means that the
# content must be first "gunzipped", then "defated".
encodings.split(',').reverse.each do |enc|
case enc.strip(' ')
when "gzip"
io = Compress::Gzip::Reader.new(io, sync_close: true)
when "deflate"
io = Compress::Deflate::Reader.new(io, sync_close: true)
end
end
body = io.gets_to_end
else
body = response.body
end
end
# Convert result to Hash # Convert result to Hash
initial_data = JSON.parse(response.body).as_h initial_data = JSON.parse(body).as_h
# Error handling # Error handling
if initial_data.has_key?("error") if initial_data.has_key?("error")