Merge 1dceabf4f2edf60344d648721d8a24d89deb7b7b into adfec7646b80ab3e68d7e854730986b46726bff9

This commit is contained in:
Fijxu 2026-07-23 15:35:14 -04:00 committed by GitHub
commit 20cb18edbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,60 +1,46 @@
module Invidious::Routes::Companion module Invidious::Routes::Companion
# GET /companion extend self
def self.get_companion(env)
url = env.request.path
if env.request.query
url += "?#{env.request.query}"
end
begin # GET /companion
COMPANION_POOL.client do |wrapper| def get_companion(env)
wrapper.client.get(url, env.request.headers) do |resp| url = make_url(env)
return self.proxy_companion(env, resp) proxy_companion(env, "GET", url)
end
end
rescue ex
end
end end
# POST /companion # POST /companion
def self.post_companion(env) def post_companion(env)
url = make_url(env)
proxy_companion(env, "POST", url)
end
# OPTIONS /companion
def options_companion(env)
url = make_url(env)
proxy_companion(env, "OPTIONS", url)
end
private def make_url(env)
url = env.request.path url = env.request.path
if env.request.query if env.request.query
url += "?#{env.request.query}" url += "?#{env.request.query}"
end end
url
end
private def proxy_companion(env, method, url)
begin begin
COMPANION_POOL.client do |wrapper| COMPANION_POOL.client do |wrapper|
wrapper.client.post(url, env.request.headers, env.request.body) do |resp| wrapper.client.exec(method, url, env.request.headers, (env.request.body if method == "POST")) do |resp|
return self.proxy_companion(env, resp) env.response.status_code = resp.status_code
end resp.headers.each do |key, value|
end
rescue ex
end
end
def self.options_companion(env)
url = env.request.path
if env.request.query
url += "?#{env.request.query}"
end
begin
COMPANION_POOL.client do |wrapper|
wrapper.client.options(url, env.request.headers) do |resp|
return self.proxy_companion(env, resp)
end
end
rescue ex
end
end
private def self.proxy_companion(env, response)
env.response.status_code = response.status_code
response.headers.each do |key, value|
env.response.headers[key] = value env.response.headers[key] = value
end end
env.response.headers["Via"] = "1.1 Invidious"
return IO.copy response.body_io, env.response return IO.copy resp.body_io, env.response
end
end
rescue ex
return error_json(502, "Couldn't proxy request to Invidious Companion.")
end
end end
end end