From e3657e4888da7c23b9910ed5f18fe90d0e77a657 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sat, 26 Oct 2024 11:14:32 -0400 Subject: [PATCH] [Debug] print requests as curl commands --- src/invidious/yt_backend/connection_pool.cr | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/invidious/yt_backend/connection_pool.cr b/src/invidious/yt_backend/connection_pool.cr index ca612083..81e27b51 100644 --- a/src/invidious/yt_backend/connection_pool.cr +++ b/src/invidious/yt_backend/connection_pool.cr @@ -49,6 +49,9 @@ struct YoutubeConnectionPool conn.family = CONFIG.force_resolve conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" + if CONFIG.log_level <= LogLevel::Debug + conn.before_request { |r| LOGGER.debug(to_curl(url, r)) } + end conn end end @@ -63,6 +66,9 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false) end client.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" + if CONFIG.log_level <= LogLevel::Debug + client.before_request { |r| LOGGER.debug(to_curl(url, r)) } + end client.read_timeout = 10.seconds client.connect_timeout = 10.seconds @@ -77,3 +83,22 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false, &) client.close end end + +def to_curl(url : URI, request : HTTP::Request) + full_url = url.dup + full_url.path = request.path + full_url.query = request.query + + curl = "curl -X #{request.method} '#{full_url}'" + request.headers.each do |key, value| + # skip compression to receive uncompressed json, easier to debug + next if key == "Accept-Encoding" + # skip content-length, curl will add it automatically + next if key == "Content-Length" + curl += " -H '#{key}: #{value.join(", ")}'" if value.is_a?(Array) + curl += " -H '#{key}: #{value}'" unless value.is_a?(Array) + end + curl += " --http2" if request.version == "HTTP/2.0" + curl += " -d '#{request.body}'" if request.body + curl +end