Temporary fix for #2612

Don't rely on the auto compression/decompression provided by the crystal stdlib.
This commit is contained in:
Samantaz Fox 2021-11-16 13:24:13 +01:00
parent 4940759627
commit 2eac23a0b3
No known key found for this signature in database
GPG Key ID: F42821059186176E

View File

@ -404,19 +404,10 @@ module YoutubeAPI
url = "#{endpoint}?key=#{client_config.api_key}" url = "#{endpoint}?key=#{client_config.api_key}"
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,23 @@ module YoutubeAPI
) )
end end
# Decompress the body ourselves, 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
#
case headers["Content-Encoding"]?
when "gzip"
body = Compress::Gzip::Reader.new(response.body_io, sync_close: true)
when "deflate"
body = Compress::Deflate::Reader.new(response.body_io, sync_close: true)
else
body = response.body
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")