aboutsummaryrefslogtreecommitdiff
path: root/lib/resty
diff options
context:
space:
mode:
authorJiale Zhi <vipcalio@gmail.com>2014-03-21 13:21:50 -0700
committerJiale Zhi <vipcalio@gmail.com>2014-03-21 13:21:50 -0700
commit71ecd7486d6d777708adcf6c2883716f07f8aa52 (patch)
treea4fab25522f3b93d3c98155d48435a0353958944 /lib/resty
parent8d2ef74648be80c38cfe6c1f4179d79ce379b33f (diff)
downloadlua-resty-cookie-71ecd7486d6d777708adcf6c2883716f07f8aa52.tar.gz
lua-resty-cookie-71ecd7486d6d777708adcf6c2883716f07f8aa52.tar.bz2
lua-resty-cookie-71ecd7486d6d777708adcf6c2883716f07f8aa52.zip
Add support for set cookies. #2
Diffstat (limited to 'lib/resty')
-rw-r--r--lib/resty/cookie.lua42
1 files changed, 39 insertions, 3 deletions
diff --git a/lib/resty/cookie.lua b/lib/resty/cookie.lua
index 1a16592..be12e5f 100644
--- a/lib/resty/cookie.lua
+++ b/lib/resty/cookie.lua
@@ -95,13 +95,16 @@ end
function _M.new(self)
local _cookie = ngx.var.http_cookie
- if not _cookie then
- return nil, "no cookie found in current request"
- end
+ --if not _cookie then
+ --return nil, "no cookie found in current request"
+ --end
return setmetatable({ _cookie = _cookie }, mt)
end
function _M.get(self, key)
+ if not self._cookie then
+ return nil, "no cookie found in the current request"
+ end
if self.cookie_table == nil then
self.cookie_table = get_cookie_table(self._cookie)
end
@@ -112,6 +115,10 @@ end
function _M.get_all(self)
local err
+ if not self._cookie then
+ return nil, "no cookie found in the current request"
+ end
+
if self.cookie_table == nil then
self.cookie_table = get_cookie_table(self._cookie)
end
@@ -119,4 +126,33 @@ function _M.get_all(self)
return self.cookie_table
end
+local function bake(cookie)
+ if not cookie.key or not cookie.value then
+ return nil, 'missing cookie field "key" or "value"'
+ end
+
+ if cookie["max-age"] then
+ cookie.max_age = cookie["max-age"]
+ end
+ local str = cookie.key .. "=" .. cookie.value
+ .. (cookie.expires and "; Expires=" .. cookie.expires or "")
+ .. (cookie.max_age and "; Max-Age=" .. cookie.max_age or "")
+ .. (cookie.domain and "; Domain=" .. cookie.domain or "")
+ .. (cookie.path and "; Path=" .. cookie.path or "")
+ .. (cookie.secure and "; Secure" or "")
+ .. (cookie.httponly and "; HttpOnly" or "")
+ .. (cookie.extension and "; " .. cookie.extension or "")
+ return str
+end
+
+function _M.set(self, cookie)
+ local cookie_str, err = bake(cookie)
+ if not cookie_str then
+ return nil, err
+ end
+ print(cookie_str)
+ ngx.header['Set-Cookie'] = cookie_str
+ return true
+end
+
return _M