NGINX LUA Content-Length +1 Byte 丢失

我遇到了一个有趣的 bug 和方法问题

Lua 认为 js_content 变量的长度为 80 字节。

但是当我不使用 "Content-Length" 标头时,Firefox 声称传输了 81 字节数据。

我不知道 +1 字节额外来自何处。 当我从远程服务器解析 json 数据时,我编写的一个 VBNet 应用程序在注意到 "Content-Length" 标头为 80 字节时会给出一个错误,但当我添加 +1 时,它可以正常工作。

local ref_array = {1, 2, 3}

local sArray = {}
sArray["1"] = "一"
sArray["2"] = "二"
sArray["3"] = "三"

local ctable = {}

for index, data in ipairs(ref_array) do

    if sArray[tostring(data)] ~= nil then
        local cinfo = {}
        cinfo["X"] = tostring(data)
        cinfo["Y"] = sArray[tostring(data)]
        cinfo["Z"] = 0
        table.insert(ctable, cinfo)
    end
end

local js_content = cjson.encode(ctable)

ngx.header['Content-Type'] = 'application/json'
ngx.header['Content-Length'] = #js_content -- 80 字节

ngx.say(js_content)

ngx.exit(200)
点赞
用户14893230
用户14893230

我猜问题在于末尾的换行符字符。

ngx.say 始终添加换行符。

ngx.print 只有输出。

问题解决

换行符字符。

0a

2020-12-30 03:57:01