lua-resty-cookie

该库为 Nginx 解析 HTTP Cookie 头,并返回每个 cookie 字段。

$ opm get flex-runtime/lua-resty-cookie

名称

lua-resty-cookie - 该库为 Nginx 解析 HTTP Cookie 头,并返回每个 cookie 字段。

状态

该库已投入生产使用。

概要

        lua_package_path "/path/to/lua-resty-cookie/lib/?.lua;;";
    
        server {
            location /test {
                content_by_lua '
                    local ck = require "resty.cookie"
                    local cookie, err = ck:new()
                    if not cookie then
                        ngx.log(ngx.ERR, err)
                        return
                    end
    
                    -- get single cookie
                    local field, err = cookie:get("lang")
                    if not field then
                        ngx.log(ngx.ERR, err)
                        return
                    end
                    ngx.say("lang", " => ", field)
    
                    -- get all cookies
                    local fields, err = cookie:get_all()
                    if not fields then
                        ngx.log(ngx.ERR, err)
                        return
                    end
    
                    for k, v in pairs(fields) do
                        ngx.say(k, " => ", v)
                    end
    
                    -- set one cookie
                    local ok, err = cookie:set({
                        key = "Name", value = "Bob", path = "/",
                        domain = "example.com", secure = true, httponly = true,
                        expires = "Wed, 09 Jun 2021 10:18:14 GMT", max_age = 50,
                        samesite = "Strict", extension = "a4334aebaec"
                    })
                    if not ok then
                        ngx.log(ngx.ERR, err)
                        return
                    end
    
                    -- set another cookie, both cookies will appear in HTTP response
                    local ok, err = cookie:set({
                        key = "Age", value = "20",
                    })
                    if not ok then
                        ngx.log(ngx.ERR, err)
                        return
                    end
                ';
            }
        }

方法

new

语法:cookie_obj = cookie()

为当前请求创建新的 cookie 对象。您可以使用此对象获取从客户端解析的 cookie 或稍后将 cookie 设置到客户端。

get

语法:cookie_val, err = cookie_obj:get(cookie_name)

获取单个客户端 cookie 值。发生错误时,返回 nil 和错误消息。

get_all

语法:fields, err = cookie_obj:get_all()

获取所有客户端 cookie 的键/值对,存放在 lua 表中。发生错误时,返回 nil 和错误消息。

语法:size = cookie_obj:get_cookie_size()

获取 cookie 大小,即 cookie 头值字符串长度。

set

    syntax: ok, err = cookie_obj:set({
        key = "Name",
        value = "Bob",
        path = "/",
        domain = "example.com",
        secure = true, httponly = true,
        expires = "Wed, 09 Jun 2021 10:18:14 GMT",
        max_age = 50,
        samesite = "Strict",
        extension = "a4334aebaec"
    })

将 cookie 设置到客户端。这将添加一个新的 'Set-Cookie' 响应头。keyvalue 是必需的,所有其他字段都是可选的。如果同一个 cookie(完整的 cookie 字符串,例如 "Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly;") 已经设置过了,新的 cookie 将被忽略。

    syntax: cookie_string, err = cookie.get_cookie_string({ --[[ see "set" method ]] })

返回表示传递的表的 cookie 字符串。有关详细信息,请参见 set 方法,但与 set 不同的是,此函数不会更改当前请求响应,而只是返回生成的字符串。发生错误时,返回 nil 和错误消息。

这是一个静态函数,而不是 cookie 对象的方法。

安装

您需要使用您的 Nginx 编译 ngx_lua

您需要配置 lua_package_path 指令,将您的 lua-resty-cookie 源代码树的路径添加到 ngx_lua 的 Lua 模块搜索路径中,例如

    # nginx.conf
    http {
        lua_package_path "/path/to/lua-resty-cookie/lib/?.lua;;";
        ...
    }

然后在 Lua 中加载库

    local ck = require "resty.cookie"

作者

Jiale Zhi <[email protected]>,CloudFlare Inc。

Yichun Zhang (agentzh) <[email protected]>,CloudFlare Inc。

版权和许可

此模块在 BSD 许可下授权。

版权所有 (C) 2013,Jiale Zhi <[email protected]>,CloudFlare Inc。

版权所有 (C) 2013,Yichun Zhang <[email protected]>,CloudFlare Inc。

保留所有权利。

允许以源代码和二进制形式重新分发和使用,无论是否修改,只要满足以下条件:

  • 源代码的重新分发必须保留上述版权声明、此条件列表和以下免责声明。

  • 二进制形式的重新分发必须在随发行版提供的文档和/或其他材料中复制上述版权声明、此条件列表和以下免责声明。

本软件由版权所有者和贡献者“按原样”提供,任何明示或暗示的担保,包括但不限于适销性或特定目的适用性的暗示担保均被免除。在任何情况下,版权所有者或贡献者均不对任何直接、间接、意外、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润损失;或业务中断)承担责任,无论其责任基础是合同、严格责任或侵权行为(包括疏忽或其他原因),即使已告知可能发生此类损害。

作者

ktalebian

许可

3bsd

版本