delete a specific cookie in lua

2,396 views
Skip to first unread message

Prashant Gaur

unread,
Aug 7, 2014, 7:24:36 AM8/7/14
to openre...@googlegroups.com
Hello All,

In my lua code i am sending a subrequest to fcgi and i am getting cookies .

Here i have a requirement i want to delete a specific cookie coming from from fcgi.
My try:
ngx.header['Set_Cookie'] = {}

but here all cookies will reset i want to delete specific cookie of name extra_token

Yichun Zhang (agentzh)

unread,
Aug 7, 2014, 1:31:29 PM8/7/14
to openresty-en
Hello!

On Thu, Aug 7, 2014 at 4:24 AM, Prashant Gaur wrote:
> Here i have a requirement i want to delete a specific cookie coming from
> from fcgi.
> My try:
> ngx.header['Set_Cookie'] = {}
>
> but here all cookies will reset i want to delete specific cookie of name
> extra_token
>

Read all the cookie values of ngx.header["Set-Cookie"], let's say, it
is a Lua table. Remove that specific cookie from this Lua table. And
then set the new Lua table back to ngx.header["Set-Cookie"]. That's
it.

Regards,
-agentzh

Aapo Talvensaari

unread,
Aug 8, 2014, 4:45:49 AM8/8/14
to openre...@googlegroups.com
On Thursday, August 7, 2014 8:31:29 PM UTC+3, agentzh wrote:
Read all the cookie values of ngx.header["Set-Cookie"], let's say, it
is a Lua table. Remove that specific cookie from this Lua table. And
then set the new Lua table back to ngx.header["Set-Cookie"]. That's
it.

I think that the current API may lead to hard to debug bugs. Whenever you are setting (say cookie, or any other header multiple times), you need to use code something like this:

local cookiename  = "test="
local cookievalue = "1"
local cookies = ngx.header["Set-Cookie"]
local t = type(cookies)
if t == "table" then
   
local found = false
   
for i, cookie in ipairs(cookies) do
       
if cookie:find(cookiename, 1, true) then
            cookies[i] = cookiename .. cookievalue
            found
= true
           
break
       
end
   
end
   
if not found then
        cookies
[#cookies + 1] = cookiename .. cookievalue
   
end
elseif t
== "string" then
   
if cookies:find(k, 1, true) then
        cookies
= cookiename .. cookievalue
   
else
        cookies
= { cookies, cookiename .. cookievalue }
   
end
else
    cookies
= cookiename .. cookievalue
end
ngx
.header["Set-Cookie"] = cookies

Seems a little bit counter-intuitive.


Regards
Aapo

Yichun Zhang (agentzh)

unread,
Aug 8, 2014, 2:01:05 PM8/8/14
to openresty-en
Hello!

On Fri, Aug 8, 2014 at 1:45 AM, Aapo Talvensaari wrote:
>
> I think that the current API may lead to hard to debug bugs. Whenever you
> are setting (say cookie, or any other header multiple times), you need to
> use code something like this:
>

This one is different from the original use case that removes a header
entry fulfilling certain conditions while keeping other entries with
the same header name.

For your use case, we could add a new API function,
ngx.resp.add_header(), that blindly adds a new header entry regardless
existing header entries with the same name (just like the standard
"add_header" directive). Such that, you can do
"ngx.header['Set-Cookie'] = 'blah blah blah'". However, the
ngx.header.HEADER API is a low-level nginx-side operation that
removing things is always expensive. So it's better to buffer and
filter cookies, for example, on the Lua land, and inject everything
into the nginx data structures via the ngx.header API in the end.

Regards,
-agentzh

Prashant Gaur

unread,
Aug 21, 2014, 3:27:04 AM8/21/14
to openre...@googlegroups.com
Thanks agentzh and Aapo.
Reply all
Reply to author
Forward
0 new messages