From 41a04e7c67ad520e9315d01c2b79a689ec21e30c Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 25 Aug 2018 17:24:07 -0500 Subject: [PATCH] Clean up /videoplayback --- src/invidious.cr | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 9e12de40..3c16650e 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -2659,6 +2659,12 @@ get "/videoplayback" do |env| client = make_client(URI.parse(host)) response = client.head(url) + if response.headers["Location"]? + url = URI.parse(response.headers["Location"]) + env.response.headers["Access-Control-Allow-Origin"] = "*" + next env.redirect url.full_path + end + headers = env.request.headers headers.delete("Host") headers.delete("Cookie") @@ -2666,30 +2672,24 @@ get "/videoplayback" do |env| headers.delete("Referer") client.get(url, headers) do |response| - if response.headers["Location"]? - url = URI.parse(response.headers["Location"]) - env.response.headers["Access-Control-Allow-Origin"] = "*" - env.redirect url.full_path - else - env.response.status_code = response.status_code + env.response.status_code = response.status_code - response.headers.each do |key, value| - env.response.headers[key] = value - end - - env.response.headers["Access-Control-Allow-Origin"] = "*" - - begin - chunk_size = 4096 - size = 1 - while size > 0 - size = IO.copy(response.body_io, env.response.output, chunk_size) - env.response.flush - Fiber.yield - end - rescue ex - break + response.headers.each do |key, value| + env.response.headers[key] = value + end + + env.response.headers["Access-Control-Allow-Origin"] = "*" + + begin + chunk_size = 4096 + size = 1 + while size > 0 + size = IO.copy(response.body_io, env.response.output, chunk_size) + env.response.flush + Fiber.yield end + rescue ex + break end end end