Skip to content

Instantly share code, notes, and snippets.

@darix
Last active October 3, 2017 06:16
Show Gist options
  • Save darix/5025010490bb0ea70099 to your computer and use it in GitHub Desktop.
Save darix/5025010490bb0ea70099 to your computer and use it in GitHub Desktop.
-- little helper function
function file_exists(path)
local attr = lighty.stat(path)
if (attr) then
return true
else
return false
end
end
-- the magic ;)
local original_attr = lighty.stat(lighty.env["physical.path"])
if (original_attr) then
if (lighty.request["Accept-Encoding"]) then
if (lighty.request["Accept-Encoding"] ~= "gzip") then
local original_content_type = original_attr["content-type"]
local compressed_file = lighty.env["physical.path"] .. ".gz"
if (file_exists(compressed_file)) then
lighty.env["physical.path"] = compressed_file
lighty.header["Content-Encoding"] = "gzip"
lighty.header["Content-Type"] = original_content_type
end
end
end
end
-- fallthrough will put it back into the lighty request loop
-- that means we get the 304 handling for free. ;)
@polyzen
Copy link

polyzen commented Sep 3, 2015

thank you :)

@darix
Copy link
Author

darix commented Sep 3, 2015

lighty config snippet

magnet.attract-physical-path-to = ( conf_dir + "/sendcompressed.lua" )

@polyzen
Copy link

polyzen commented Sep 3, 2015

awesome..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment