• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

openresty使用lua处理gzip压缩解压

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
openresty+lua劫持请求,有点意思发布时间:2022-07-22
下一篇:
TIOBE 2011年6月编程语言排行榜:Lua挤进前10位 - 狼人:-)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap