use host variable instead of setting env.request.headers["Host"] each time

This commit is contained in:
Fijxu 2025-05-18 18:15:38 -04:00
parent b5dbb95b47
commit 53d5efaa72
No known key found for this signature in database
GPG Key ID: 32C1DDF333EDA6A4
6 changed files with 37 additions and 20 deletions

View File

@ -167,6 +167,8 @@ module Invidious::Routes::API::Manifest
# /api/manifest/hls_playlist/*
def self.get_hls_playlist(env)
host = env.request.headers["Host"]
response = YT_POOL.client &.get(env.request.path)
if response.status_code != 200
@ -214,7 +216,7 @@ module Invidious::Routes::API::Manifest
raw_params["host"] = uri.host.not_nil!
"#{env.request.headers["Host"]}/videoplayback?#{raw_params}"
"#{host}/videoplayback?#{raw_params}"
end
end
@ -223,6 +225,8 @@ module Invidious::Routes::API::Manifest
# /api/manifest/hls_variant/*
def self.get_hls_variant(env)
host = env.request.headers["Host"]
response = YT_POOL.client &.get(env.request.path)
if response.status_code != 200
@ -237,7 +241,7 @@ module Invidious::Routes::API::Manifest
manifest = response.body
if local
manifest = manifest.gsub("https://www.youtube.com", env.request.headers["Host"])
manifest = manifest.gsub("https://www.youtube.com", host)
manifest = manifest.gsub("index.m3u8", "index.m3u8?local=true")
end

View File

@ -208,6 +208,8 @@ module Invidious::Routes::API::V1::Authenticated
end
def self.create_playlist(env)
host = env.request.headers["Host"]
env.response.content_type = "application/json"
user = env.get("user").as(User)
@ -226,7 +228,7 @@ module Invidious::Routes::API::V1::Authenticated
end
playlist = create_playlist(title, privacy, user)
env.response.headers["Location"] = "#{env.request.headers["Host"]}/api/v1/auth/playlists/#{playlist.id}"
env.response.headers["Location"] = "#{host}/api/v1/auth/playlists/#{playlist.id}"
env.response.status_code = 201
{
"title" => title,
@ -290,6 +292,8 @@ module Invidious::Routes::API::V1::Authenticated
end
def self.insert_video_into_playlist(env)
host = env.request.headers["Host"]
env.response.content_type = "application/json"
user = env.get("user").as(User)
@ -336,7 +340,7 @@ module Invidious::Routes::API::V1::Authenticated
Invidious::Database::PlaylistVideos.insert(playlist_video)
Invidious::Database::Playlists.update_video_added(plid, playlist_video.index)
env.response.headers["Location"] = "#{env.request.headers["Host"]}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}"
env.response.headers["Location"] = "#{host}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}"
env.response.status_code = 201
JSON.build do |json|

View File

@ -143,6 +143,8 @@ module Invidious::Routes::Feeds
# RSS feeds
def self.rss_channel(env)
host = env.request.headers["Host"]
env.response.headers["Content-Type"] = "application/atom+xml"
env.response.content_type = "application/atom+xml"
@ -199,21 +201,21 @@ module Invidious::Routes::Feeds
xml.element("feed", "xmlns:yt": "http://www.youtube.com/xml/schemas/2015",
"xmlns:media": "http://search.yahoo.com/mrss/", xmlns: "http://www.w3.org/2005/Atom",
"xml:lang": "en-US") do
xml.element("link", rel: "self", href: "#{env.request.headers["Host"]}#{env.request.resource}")
xml.element("link", rel: "self", href: "#{host}#{env.request.resource}")
xml.element("id") { xml.text "yt:channel:#{ucid}" }
xml.element("yt:channelId") { xml.text ucid }
xml.element("title") { author }
xml.element("link", rel: "alternate", href: "#{env.request.headers["Host"]}/channel/#{ucid}")
xml.element("link", rel: "alternate", href: "#{host}/channel/#{ucid}")
xml.element("author") do
xml.element("name") { xml.text author }
xml.element("uri") { xml.text "#{env.request.headers["Host"]}/channel/#{ucid}" }
xml.element("uri") { xml.text "#{host}/channel/#{ucid}" }
end
xml.element("image") do
xml.element("url") { xml.text "" }
xml.element("title") { xml.text author }
xml.element("link", rel: "self", href: "#{env.request.headers["Host"]}#{env.request.resource}")
xml.element("link", rel: "self", href: "#{host}#{env.request.resource}")
end
videos.each do |video|
@ -225,6 +227,7 @@ module Invidious::Routes::Feeds
def self.rss_private(env)
locale = env.get("preferences").as(Preferences).locale
host = env.request.headers["Host"]
env.response.headers["Content-Type"] = "application/atom+xml"
env.response.content_type = "application/atom+xml"
@ -255,9 +258,9 @@ module Invidious::Routes::Feeds
xml.element("feed", "xmlns:yt": "http://www.youtube.com/xml/schemas/2015",
"xmlns:media": "http://search.yahoo.com/mrss/", xmlns: "http://www.w3.org/2005/Atom",
"xml:lang": "en-US") do
xml.element("link", "type": "text/html", rel: "alternate", href: "#{env.request.headers["Host"]}/feed/subscriptions")
xml.element("link", "type": "text/html", rel: "alternate", href: "#{host}/feed/subscriptions")
xml.element("link", "type": "application/atom+xml", rel: "self",
href: "#{env.request.headers["Host"]}#{env.request.resource}")
href: "#{host}#{env.request.resource}")
xml.element("title") { xml.text translate(locale, "Invidious Private Feed for `x`", user.email) }
(notifications + videos).each do |video|
@ -269,6 +272,7 @@ module Invidious::Routes::Feeds
def self.rss_playlist(env)
locale = env.get("preferences").as(Preferences).locale
host = env.request.headers["Host"]
env.response.headers["Content-Type"] = "application/atom+xml"
env.response.content_type = "application/atom+xml"
@ -286,11 +290,11 @@ module Invidious::Routes::Feeds
xml.element("feed", "xmlns:yt": "http://www.youtube.com/xml/schemas/2015",
"xmlns:media": "http://search.yahoo.com/mrss/", xmlns: "http://www.w3.org/2005/Atom",
"xml:lang": "en-US") do
xml.element("link", rel: "self", href: "#{env.request.headers["Host"]}#{env.request.resource}")
xml.element("link", rel: "self", href: "#{host}#{env.request.resource}")
xml.element("id") { xml.text "iv:playlist:#{plid}" }
xml.element("iv:playlistId") { xml.text plid }
xml.element("title") { xml.text playlist.title }
xml.element("link", rel: "alternate", href: "#{env.request.headers["Host"]}/playlist?list=#{plid}")
xml.element("link", rel: "alternate", href: "#{host}/playlist?list=#{plid}")
xml.element("author") do
xml.element("name") { xml.text playlist.author }
@ -320,7 +324,7 @@ module Invidious::Routes::Feeds
when "url", "href"
request_target = URI.parse(node[attribute.name]).request_target
query_string_opt = request_target.starts_with?("/watch?v=") ? "&#{params}" : ""
node[attribute.name] = "#{env.request.headers["Host"]}#{request_target}#{query_string_opt}"
node[attribute.name] = "#{host}#{request_target}#{query_string_opt}"
else nil # Skip
end
end
@ -329,7 +333,7 @@ module Invidious::Routes::Feeds
document = document.to_xml(options: XML::SaveOptions::NO_DECL)
document.scan(/<uri>(?<url>[^<]+)<\/uri>/).each do |match|
content = "#{env.request.headers["Host"]}#{URI.parse(match["url"]).request_target}"
content = "#{host}#{URI.parse(match["url"]).request_target}"
document = document.gsub(match[0], "<uri>#{content}</uri>")
end
document

View File

@ -26,6 +26,7 @@ module Invidious::Routes::Login
def self.login(env)
locale = env.get("preferences").as(Preferences).locale
host = env.request.headers["Host"]
referer = get_referer(env, "/feed/subscriptions")
@ -57,7 +58,7 @@ module Invidious::Routes::Login
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
Invidious::Database::SessionIDs.insert(sid, email)
env.response.cookies["SID"] = Invidious::User::Cookies.sid(env.request.headers["Host"], sid)
env.response.cookies["SID"] = Invidious::User::Cookies.sid(host, sid)
else
return error_template(401, "Wrong username or password")
end
@ -121,7 +122,7 @@ module Invidious::Routes::Login
view_name = "subscriptions_#{sha256(user.email)}"
PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS #{MATERIALIZED_VIEW_SQL.call(user.email)}")
env.response.cookies["SID"] = Invidious::User::Cookies.sid(env.request.headers["Host"], sid)
env.response.cookies["SID"] = Invidious::User::Cookies.sid(host, sid)
if env.request.cookies["PREFS"]?
user.preferences = env.get("preferences").as(Preferences)

View File

@ -14,6 +14,7 @@ module Invidious::Routes::PreferencesRoute
def self.update(env)
locale = env.get("preferences").as(Preferences).locale
referer = get_referer(env)
host = env.request.headers["Host"]
video_loop = env.params.body["video_loop"]?.try &.as(String)
video_loop ||= "off"
@ -224,7 +225,7 @@ module Invidious::Routes::PreferencesRoute
File.write("config/config.yml", CONFIG.to_yaml)
end
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(env.request.headers["Host"], preferences)
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(host, preferences)
end
env.redirect referer
@ -233,6 +234,7 @@ module Invidious::Routes::PreferencesRoute
def self.toggle_theme(env)
locale = env.get("preferences").as(Preferences).locale
referer = get_referer(env, unroll: false)
host = env.request.headers["Host"]
redirect = env.params.query["redirect"]?
redirect ||= "true"
@ -259,7 +261,7 @@ module Invidious::Routes::PreferencesRoute
preferences.dark_mode = "dark"
end
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(env.request.headers["Host"], preferences)
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(host, preferences)
end
if redirect

View File

@ -3,6 +3,8 @@
module Invidious::Routes::Search
def self.opensearch(env)
locale = env.get("preferences").as(Preferences).locale
host = env.request.headers["Host"]
env.response.content_type = "application/opensearchdescription+xml"
XML.build(indent: " ", encoding: "UTF-8") do |xml|
@ -11,8 +13,8 @@ module Invidious::Routes::Search
xml.element("LongName") { xml.text "Invidious Search" }
xml.element("Description") { xml.text "Search for videos, channels, and playlists on Invidious" }
xml.element("InputEncoding") { xml.text "UTF-8" }
xml.element("Image", width: 48, height: 48, type: "image/x-icon") { xml.text "#{env.request.headers["Host"]}/favicon.ico" }
xml.element("Url", type: "text/html", method: "get", template: "#{env.request.headers["Host"]}/search?q={searchTerms}")
xml.element("Image", width: 48, height: 48, type: "image/x-icon") { xml.text "#{host}/favicon.ico" }
xml.element("Url", type: "text/html", method: "get", template: "#{host}/search?q={searchTerms}")
end
end
end