Skip to content

Commit 5e9d51c

Browse files
syeopiteunixfox
authored andcommitted
Refactor FilteredCompressHandler to inherit from stdlib
This changes its behavior to align with the stdlib variant in that compression is now delayed till the moment that the server begins to send a response. This allows the handler to avoid compressing empty responses,and safeguards against any double compression of content that may occur if another handler decides to compressi ts response. This does however come at the drawback(?) of it now removing `content-length` headers on requests if it exists; since compression makes the value inaccurate anyway. See: crystal-lang/crystal#9625
1 parent 1653dd6 commit 5e9d51c

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

src/invidious/helpers/handlers.cr

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,13 @@ class Kemal::ExceptionHandler
6161
end
6262
end
6363

64-
class FilteredCompressHandler < Kemal::Handler
64+
class FilteredCompressHandler < HTTP::CompressHandler
6565
exclude ["/videoplayback", "/videoplayback/*", "/vi/*", "/sb/*", "/ggpht/*", "/api/v1/auth/notifications"]
6666
exclude ["/api/v1/auth/notifications", "/data_control"], "POST"
6767

68-
def call(env)
69-
return call_next env if exclude_match? env
70-
71-
{% if flag?(:without_zlib) %}
72-
call_next env
73-
{% else %}
74-
request_headers = env.request.headers
75-
76-
if request_headers.includes_word?("Accept-Encoding", "gzip")
77-
env.response.headers["Content-Encoding"] = "gzip"
78-
env.response.output = Compress::Gzip::Writer.new(env.response.output, sync_close: true)
79-
elsif request_headers.includes_word?("Accept-Encoding", "deflate")
80-
env.response.headers["Content-Encoding"] = "deflate"
81-
env.response.output = Compress::Deflate::Writer.new(env.response.output, sync_close: true)
82-
end
83-
84-
call_next env
85-
{% end %}
68+
def call(context)
69+
return call_next context if exclude_match? context
70+
super
8671
end
8772
end
8873

0 commit comments

Comments
 (0)