From bc068721abc19885d5317838f820b4fa73fe87f8 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 15 Apr 2018 22:56:58 -0500 Subject: [PATCH] Add filtered handler for videoplayback --- src/helpers.cr | 24 ++++++++++++++++++++++++ src/invidious.cr | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/helpers.cr b/src/helpers.cr index 110a0831..4f59d639 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -29,6 +29,30 @@ class Config }) end +class FilteredCompressHandler < Kemal::Handler + exclude ["/videoplayback"] + + def call(env) + return call_next env if exclude_match? env + + {% if flag?(:without_zlib) %} + call_next env + {% else %} + request_headers = env.request.headers + + if request_headers.includes_word?("Accept-Encoding", "gzip") + env.response.headers["Content-Encoding"] = "gzip" + env.response.output = Gzip::Writer.new(env.response.output, sync_close: true) + elsif request_headers.includes_word?("Accept-Encoding", "deflate") + env.response.headers["Content-Encoding"] = "deflate" + env.response.output = Flate::Writer.new(env.response.output, sync_close: true) + end + + call_next env + {% end %} + end +end + class Video module HTTPParamConverter def self.from_rs(rs) diff --git a/src/invidious.cr b/src/invidious.cr index bd510ceb..fb405ffd 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -872,6 +872,7 @@ static_headers do |response, filepath, filestat| end public_folder "assets" -gzip true + +add_handler FilteredCompressHandler.new Kemal.run