内联 lua 改成文件

This commit is contained in:
zjcqoo 2019-04-29 17:52:07 +08:00
parent 043597c7c4
commit 7bc2896393
3 changed files with 123 additions and 0 deletions

34
lua/http-dec-req-hdr.lua Normal file
View File

@ -0,0 +1,34 @@
-- 功能:还原 HTTP 请求头
-- 阶段access_by_lua
local hdrs, err = ngx.req.get_headers()
local extHdrs
for k, v in pairs(hdrs) do
if k:sub(1, 2) ~= '--' then
goto continue
end
ngx.req.clear_header(k)
k = k:sub(3)
if k == 'url' then
ngx.var._url = v
elseif k == 'ver' then
ngx.var._ver = v
elseif k == 'aceh' then
ngx.ctx._aceh = 1
elseif k == 'ext' then
extHdrs = require('cjson').decode(v)
else
ngx.req.set_header(k, v)
end
::continue::
end
if extHdrs then
for k, v in pairs(extHdrs) do
ngx.req.set_header(k, v)
end
end

74
lua/http-enc-res-hdr.lua Normal file
View File

@ -0,0 +1,74 @@
-- 功能:编码 HTTP 返回头
-- 阶段header_filter_by_lua
-- 备注:
-- aceh = HTTP 返回头的 access-control-expose-headers 字段
-- 无论浏览器是否支持aceh 始终包含 *
local expose = '*'
-- 该值为 true 表示浏览器不支持 aceh: *,需返回详细的头部列表
local detail = (ngx.ctx._aceh == 1)
-- 由于接口路径固定,为避免被缓存,以请求头的 --url 值区分缓存
local vary = '--url'
local h, err = ngx.resp.get_headers()
for k, v in pairs(h) do
if
-- 这些头有特殊意义,需要转义 --
k == 'access-control-allow-origin' or
k == 'access-control-expose-headers' or
k == 'location' or
k == 'set-cookie'
then
if type(v) == 'table' then
for i = 1, #v do
local x = i .. '-' .. k
ngx.header[x] = v[i]
if detail then
expose = expose .. ',' .. x
end
end
else
local x = '--' .. k
ngx.header[x] = v
if detail then
expose = expose .. ',' .. x
end
end
ngx.header[k] = nil
elseif k == 'vary' then
if type(v) == 'table' then
vary = vary .. ',' .. table.concat(v, ',')
else
vary = vary .. ',' .. v
end
elseif detail and
-- 非简单头无法被 fetch 读取,需添加到 aceh 列表 --
-- https://developer.mozilla.org/en-US/docs/Glossary/Simple_response_header
k ~= 'cache-control' and
k ~= 'content-language' and
k ~= 'content-type' and
k ~= 'expires' and
k ~= 'last-modified' and
k ~= 'pragma'
then
expose = expose .. ',' .. k
end
end
if detail then
expose = expose .. ',--s'
ngx.header['--t'] = '1'
end
ngx.header['access-control-expose-headers'] = expose
ngx.header['access-control-allow-origin'] = '*'
ngx.header['vary'] = vary
ngx.header['--s'] = ngx.status
ngx.status = 200

15
lua/ws-dec-req-hdr.lua Normal file
View File

@ -0,0 +1,15 @@
-- 功能:还原 WebSocket 的 HTTP 请求头
-- 阶段access_by_lua
-- 备注JS 无法设置 ws 的头部,因此信息存储于 query
local query, err = ngx.req.get_uri_args()
for k, v in pairs(query) do
if k == 'url__' then
ngx.var._url = v
elseif k == 'ver__' then
ngx.var._ver = v
else
ngx.req.set_header(k, v)
end
end