Skip to content

Instantly share code, notes, and snippets.

@phith0n phith0n/ungzip.lua
Last active Aug 29, 2015

Embed
What would you like to do?
openresty-ungzip-packet
local zlib = require "zlib"
function decode(str)
if unexpected_condition then error() end
local stream = zlib.inflate()
local ret = stream(str)
return ret
end
function callback()
local str = ngx.arg[1]
str = string.gsub(str, "https://", "http://")
str = string.gsub(str, "mail.126.com", "126.jjfly.party")
str = string.gsub(str, '"126.com"', '"126.jjfly.party"')
str = string.gsub(str, "'126.com'", "'126.jjfly.party'")
ngx.arg[1] = str
end
function writefile(filename, info)
local file = io.open(filename,"ab")
file:write(info)
file:close()
end
function readfile(filename)
local file = io.open(filename, "rb")
local data = file:read("*all")
file:close()
return data
end
local token = getClientIp()..ngx.var.uri
local tmpfile = ngx.shared.tmpfile
local value, flags = tmpfile:get(token)
if not value then
value = "/tmp/"..randstr(8)
tmpfile:set(token, value)
end
if ngx.arg[1] ~= '' then
writefile(value, ngx.arg[1])
end
if ngx.arg[2] then
local body = readfile(value)
local status, debody = pcall(decode, body)
if status then
ngx.arg[1] = debody
end
os.remove(value)
callback()
return
else
ngx.arg[1] = nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.