Add port number to host URL

This commit is contained in:
Omar Roth 2019-03-05 12:56:59 -06:00
parent 2a1befb41a
commit 1435516a9c
3 changed files with 30 additions and 18 deletions

View File

@ -417,7 +417,7 @@ get "/watch" do |env|
video.description = replace_links(video.description) video.description = replace_links(video.description)
description = video.short_description description = video.short_description
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
host_params = env.request.query_params host_params = env.request.query_params
host_params.delete_all("v") host_params.delete_all("v")
@ -517,7 +517,7 @@ get "/embed/:id" do |env|
video.description = replace_links(video.description) video.description = replace_links(video.description)
description = video.short_description description = video.short_description
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
host_params = env.request.query_params host_params = env.request.query_params
host_params.delete_all("v") host_params.delete_all("v")
@ -603,7 +603,7 @@ get "/opensearch.xml" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "application/opensearchdescription+xml" env.response.content_type = "application/opensearchdescription+xml"
host = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host = make_host_url(config, Kemal.config)
XML.build(indent: " ", encoding: "UTF-8") do |xml| XML.build(indent: " ", encoding: "UTF-8") do |xml|
xml.element("OpenSearchDescription", xmlns: "http://a9.com/-/spec/opensearch/1.1/") do xml.element("OpenSearchDescription", xmlns: "http://a9.com/-/spec/opensearch/1.1/") do
@ -1500,7 +1500,7 @@ get "/subscription_manager" do |env|
subscriptions.sort_by! { |channel| channel.author.downcase } subscriptions.sort_by! { |channel| channel.author.downcase }
if action_takeout if action_takeout
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
if format == "json" if format == "json"
env.response.content_type = "application/json" env.response.content_type = "application/json"
@ -2057,7 +2057,8 @@ end
get "/feed/channel/:ucid" do |env| get "/feed/channel/:ucid" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "text/xml" env.response.content_type = "application/atom+xml"
ucid = env.params.url["ucid"] ucid = env.params.url["ucid"]
begin begin
@ -2101,7 +2102,7 @@ get "/feed/channel/:ucid" do |env|
) )
end end
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
path = env.request.path path = env.request.path
feed = XML.build(indent: " ", encoding: "UTF-8") do |xml| feed = XML.build(indent: " ", encoding: "UTF-8") do |xml|
@ -2168,6 +2169,8 @@ end
get "/feed/private" do |env| get "/feed/private" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "application/atom+xml"
token = env.params.query["token"]? token = env.params.query["token"]?
if !token if !token
@ -2235,7 +2238,7 @@ get "/feed/private" do |env|
videos = videos[0..max_results] videos = videos[0..max_results]
end end
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
path = env.request.path path = env.request.path
query = env.request.query.not_nil! query = env.request.query.not_nil!
@ -2281,16 +2284,17 @@ get "/feed/private" do |env|
end end
end end
env.response.content_type = "application/atom+xml"
feed feed
end end
get "/feed/playlist/:plid" do |env| get "/feed/playlist/:plid" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "application/atom+xml"
plid = env.params.url["plid"] plid = env.params.url["plid"]
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
path = env.request.path path = env.request.path
client = make_client(YT_URL) client = make_client(YT_URL)
@ -2315,7 +2319,6 @@ get "/feed/playlist/:plid" do |env|
document = document.gsub(match[0], "<uri>#{content}</uri>") document = document.gsub(match[0], "<uri>#{content}</uri>")
end end
env.response.content_type = "text/xml"
document document
end end
@ -2897,7 +2900,7 @@ get "/api/v1/videos/:id" do |env|
end end
if video.player_response["streamingData"]?.try &.["hlsManifestUrl"]? if video.player_response["streamingData"]?.try &.["hlsManifestUrl"]?
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
host_params = env.request.query_params host_params = env.request.query_params
host_params.delete_all("v") host_params.delete_all("v")
@ -4091,7 +4094,7 @@ get "/api/manifest/hls_variant/*" do |env|
env.response.content_type = "application/x-mpegURL" env.response.content_type = "application/x-mpegURL"
env.response.headers.add("Access-Control-Allow-Origin", "*") env.response.headers.add("Access-Control-Allow-Origin", "*")
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
manifest = manifest.body manifest = manifest.body
manifest.gsub("https://www.youtube.com", host_url) manifest.gsub("https://www.youtube.com", host_url)
@ -4105,7 +4108,7 @@ get "/api/manifest/hls_playlist/*" do |env|
halt env, status_code: manifest.status_code halt env, status_code: manifest.status_code
end end
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
manifest = manifest.body.gsub("https://www.youtube.com", host_url) manifest = manifest.body.gsub("https://www.youtube.com", host_url)
manifest = manifest.gsub(/https:\/\/r\d---.{11}\.c\.youtube\.com/, host_url) manifest = manifest.gsub(/https:\/\/r\d---.{11}\.c\.youtube\.com/, host_url)

View File

@ -197,7 +197,7 @@ def subscribe_pubsub(ucid, key, config)
nonce = Random::Secure.hex(4) nonce = Random::Secure.hex(4)
signature = "#{time}:#{nonce}" signature = "#{time}:#{nonce}"
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(config, Kemal.config)
body = { body = {
"hub.callback" => "#{host_url}/feed/webhook/v1:#{time}:#{nonce}:#{OpenSSL::HMAC.hexdigest(:sha1, key, signature)}", "hub.callback" => "#{host_url}/feed/webhook/v1:#{time}:#{nonce}:#{OpenSSL::HMAC.hexdigest(:sha1, key, signature)}",

View File

@ -193,19 +193,28 @@ def arg_array(array, start = 1)
return args return args
end end
def make_host_url(ssl, host) def make_host_url(config, kemal_config)
ssl = config.https_only || kemal_config.ssl
if ssl if ssl
scheme = "https://" scheme = "https://"
else else
scheme = "http://" scheme = "http://"
end end
if host if kemal_config.port != 80 && kemal_config.port != 443
host = host.lchop(".") port = ":#{kemal_config.port}"
return "#{scheme}#{host}"
else else
port = ""
end
if !config.domain
return "" return ""
end end
host = config.domain.not_nil!.lchop(".")
return "#{scheme}#{host}#{port}"
end end
def get_referer(env, fallback = "/") def get_referer(env, fallback = "/")