openresty使用lua处理gzip压缩解压

1.安装依赖

yum install -y gcc gcc-c++ make cmake automake zlib-devel zlib -y

2.下载lua-zlib包,并解压

wget https://github.com/brimworks/lua-zlib/archive/master.zip

unzip lua-zlib-master.zip

cd /usr/local/software/lua-zlib-master

cmake -DLUA_INCLUDE_DIR=/usr/local/openresty/luajit/include/luajit-2.1

若报错:CMake Error at CMakeLists.txt:27 (find_package):

By not providing "FindLua.cmake" in CMAKE_MODULE_PATH this project has

asked CMake to find a package configuration file provided by "Lua", but

CMake did not find one.

在 /usr/local/share/cmake-2.8/Modules目录下(该目录可能不存在 FindLua.cmake,查询其他.cmke文件获取目录 ),有 FindLua51.cmake,改名为 FindLua.cmake,重新执行上个命令。

make

cp zlib.so /usr/local/openresty/lualib/zlib.so

3.lua脚本调用

--案例1
local zip = require 'zlib' local uncompress = zip.inflate() local compress = zip.deflate() local deflated, eof, bytes_in,bytes_out =compress("asdasdasdasdasdasdasdasdasd", 'finish') print(deflated, eof, bytes_in,bytes_out) local uss,ret,getin,getout=uncompress(deflated)print(uss,ret,getin,getout) print(uss,ret,getin,getout)
--案例2
--Table to Str local function ToStringEx(value) if type(value)=='table' then return TableToStr(value) elseif type(value)=='string' then return "\'"..value.."\'" else return tostring(value) end end local function TableToStr(t) if t == nil then return "" end local retstr= "{" local i = 1 for key,value in pairs(t) do local signal = "," if i==1 then signal = "" end if key == i then retstr = retstr..signal..ToStringEx(value) else if type(key)=='number' or type(key) == 'string' then retstr = retstr..signal..'['..ToStringEx(key).."]="..ToStringEx(value) else if type(key)=='userdata' then retstr = retstr..signal.."*s"..TableToStr(getmetatable(key)).."*e".."="..ToStringEx(value) else retstr = retstr..signal..key.."="..ToStringEx(value) end end end i = i+1 end retstr = retstr.."}" return retstr end --获取请求头table local headers_tab = ngx.req.get_headers() print("headers_tab:"..TableToStr(headers_tab)) -- 获取未解析的请求头字符串 local headers_str = ngx.req.raw_header() print(headers_str)
--根据header的值来判断是否需要解压body数据 --post请求参数 local zlib = require "zlib" local binfilegzip = ngx.req.get_headers()["binfile-gzip"] print("binfilegzip header:"..binfilegzip) ngx.req.read_body() if binfilegzip == "true" then local body = ngx.req.get_body_data() if body then local stream = zlib.inflate() bodydata = stream(body) print("post_body_data:"..bodydata) end else local post_params_tab = ngx.req.get_post_args() print("body_params_tab:"..TableToStr(post_params_tab)) local post_body_data = ngx.req.get_body_data() print("post_body_data:"..post_body_data) end

4.zlib库不能直接压缩gzip格式,使用lua-ffi-zlib

源码路径:https://github.com/hamishforbes/lua-ffi-zlib

调用:

local ffi_zlib = require "lib.ffi-zlib"

local chunk = 16384
    local count = 0 
    
    local input = function(bufsize)  
        local start = count > 0 and bufsize*count or 1  
        local data = str:sub(start, (bufsize*(count+1)-1))  
        if data == "" then  
            data = nil  
        end
        print(data)
        count = count + 1  
        return data  
    end 
    local output_table = {}  
    local output = function(data)
        insert(output_table, data)
    end 
    local ok, err = ffi_zlib.deflateGzip(input, output, chunk)  
    if not ok then  
        print(err)
    end  
    local compress = concat(output_table,'')  
    ngx.header["Content-Encoding"] = "gzip"

ngx.print(compress)

参考:

https://blog.csdn.net/chenglian1987/article/details/78502246

https://www.cnblogs.com/kgdxpr/p/4195216.html