Skip to content

Commit

Permalink
multi set-cookie headers
Browse files Browse the repository at this point in the history
  • Loading branch information
KDr2 committed Feb 15, 2012
1 parent b9b1d41 commit 12484cf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -6,6 +6,7 @@ MOCHINE CHANGLOG
develop
------------------------------------------------------------

* multi ``Set-Cookie`` headers via ``Response:set_cookie``
* map url to mch.controller
* before/after filter

Expand Down
2 changes: 1 addition & 1 deletion TODO
Expand Up @@ -3,6 +3,6 @@
MOOCHINE TODO
============================================================

* multi ``Set-Cookie`` headers
* None


14 changes: 10 additions & 4 deletions luasrc/mch/response.lua
Expand Up @@ -29,6 +29,7 @@ Response={ltp=ltp}
function Response:new()
local ret={
headers=ngx.header,
_cookies={},
_output={}
}
setmetatable(ret,self)
Expand All @@ -49,7 +50,7 @@ function Response:redirect(url, status)
ngx.redirect(url, status)
end

function Response:set_cookie(key, value, encrypt, duration, path)
function Response:_set_cookie(key, value, encrypt, duration, path)
if not key or key=="" or not value then
return
end
Expand All @@ -69,9 +70,14 @@ function Response:set_cookie(key, value, encrypt, duration, path)

local expiretime=ngx.time()+duration
expiretime = ngx.cookie_time(expiretime)
ngx.header["Set-Cookie"]={
table.concat({key, "=", value, "; expires=", expiretime, "; path=", path})
}
return table.concat({key, "=", value, "; expires=", expiretime, "; path=", path})
end

function Response:set_cookie(key, value, encrypt, duration, path)
if not value then self._cookies[key]=nil end
local cookie=self:_set_cookie(key, value, encrypt, duration, path)
self._cookies[key]=cookie
ngx.header["Set-Cookie"]=self._cookies
end

--[[
Expand Down

0 comments on commit 12484cf

Please sign in to comment.