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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2017-03-15 linux ip 转发设置 ip_forward