Skip to content

Commit

Permalink
Merge pull request #3 from avif/partitioned-cookie-support
Browse files Browse the repository at this point in the history
Add partitioned cookie support
  • Loading branch information
utix authored Apr 8, 2024
2 parents 9533f47 + ed35d7f commit 12136f5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Synopsis
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"
samesite = "Strict", partitioned = true, extension = "a4334aebaec"
})
if not ok then
ngx.log(ngx.ERR, err)
Expand Down Expand Up @@ -133,12 +133,13 @@ syntax: ok, err = cookie_obj:set({
expires = "Wed, 09 Jun 2021 10:18:14 GMT",
max_age = 50,
samesite = "Strict",
partitioned = true,
extension = "a4334aebaec"
})
```

Set a cookie to client. This will add a new 'Set-Cookie' response header. `key` and `value` are required, all other fields are optional.
If the same cookie (whole cookie string, e.g. "Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly;") has already been setted, new cookie will be ignored.
If the same cookie (whole cookie string, e.g. "Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; Partitioned;") has already been set, new cookie will be ignored.

[Back to TOC](#table-of-contents)

Expand Down
1 change: 1 addition & 0 deletions lib/resty/cookie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ local function bake(cookie)
.. (cookie.secure and "; Secure" or "")
.. (cookie.httponly and "; HttpOnly" or "")
.. (cookie.samesite and "; SameSite=" .. cookie.samesite or "")
.. (cookie.partitioned and "; Partitioned" or "")
.. (cookie.extension and "; " .. cookie.extension or "")
return str
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package = "lua-resty-cookie"
version = "0.2.0-1"
version = "0.3.0-1"

source = {
url = "git+https://github.com/utix/lua-resty-cookie",
tag = "v0.2.0",
tag = "v0.3.0",
}

description = {
Expand Down
78 changes: 75 additions & 3 deletions t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3 + 7);
plan tests => repeat_each() * (blocks() * 3 + 9);

my $pwd = cwd();

Expand Down Expand Up @@ -192,7 +192,7 @@ SID => foo
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"
samesite = "Strict", partitioned = true, extension = "a4334aebaec"
})
if not ok then
ngx.log(ngx.ERR, err)
Expand All @@ -206,7 +206,7 @@ GET /t
--- no_error_log
[error]
--- response_headers
Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; a4334aebaec
Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; Partitioned; a4334aebaec
--- response_body
Set cookie
Expand Down Expand Up @@ -462,3 +462,75 @@ GET /t
[error]
--- response_body
Cookie string: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=None; a4334aebaec
=== TEST 14: set partioned to false
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua '
local ck = require "resty.cookie"
local cookie, err = ck:new()
if not cookie then
ngx.log(ngx.ERR, err)
return
end
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", partitioned = false, extension = "a4334aebaec"
})
if not ok then
ngx.log(ngx.ERR, err)
return
end
ngx.say("Set cookie")
';
}
--- request
GET /t
--- no_error_log
[error]
--- response_headers
Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; a4334aebaec
--- response_body
Set cookie
=== TEST 15: partioned not set
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua '
local ck = require "resty.cookie"
local cookie, err = ck:new()
if not cookie then
ngx.log(ngx.ERR, err)
return
end
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
ngx.say("Set cookie")
';
}
--- request
GET /t
--- no_error_log
[error]
--- response_headers
Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; a4334aebaec
--- response_body
Set cookie

0 comments on commit 12136f5

Please sign in to comment.