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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM