aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md96
-rw-r--r--lib/resty/cookie.lua104
-rw-r--r--t/sanity.t78
3 files changed, 278 insertions, 0 deletions
diff --git a/README.md b/README.md
index e69de29..3fdae66 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1,96 @@
+Name
+====
+
+lua-resty-cookie - This library parses HTTP Cookie header for Nginx and returns each field in the cookie.
+
+Table of Contents
+=================
+
+* [Name](#name)
+* [Status](#status)
+* [Synopsis](#synopsis)
+* [Installation](#installation)
+* [Authors](#authors)
+* [Copyright and License](#copyright-and-license)
+
+Status
+======
+
+This library is still experimental and under early development.
+
+Synopsis
+========
+
+ lua_package_path "/path/to/lua-resty-cookie/lib/?.lua;;";
+
+ server {
+ location /test {
+ local ck = require "resty.cookie"
+ local cookie, err = ck:new()
+ if not cookie then
+ ngx.log(ngx.ERR, err)
+ return
+ end
+
+ local field = cookie:get("lang")
+ ngx.say("lang", " => ", field)
+
+ local fields = cookie:get_all()
+
+ for k, v in pairs(fields) do
+ ngx.say(k, " => ", v)
+ end
+ }
+ }
+
+Installation
+============
+
+You need to compile [ngx_lua](https://github.com/chaoslawful/lua-nginx-module/tags) with your Nginx.
+
+You need to configure
+the [lua_package_path](https://github.com/chaoslawful/lua-nginx-module#lua_package_path) directive to
+add the path of your `lua-resty-cookie` source tree to ngx_lua's Lua module search path, as in
+
+ # nginx.conf
+ http {
+ lua_package_path "/path/to/lua-resty-cookie/lib/?.lua;;";
+ ...
+ }
+
+and then load the library in Lua:
+
+ local ck = require "resty.cookie"
+
+[Back to TOC](#table-of-contents)
+
+Authors
+=======
+
+Jiale Zhi <vipcalio@gmail.com>, CloudFlare Inc.
+
+Yichun Zhang (agentzh) <agentzh@gmail.com>, CloudFlare Inc.
+
+[Back to TOC](#table-of-contents)
+
+Copyright and License
+=====================
+
+This module is licensed under the BSD license.
+
+Copyright (C) 2013, by Jiale Zhi <vipcalio@gmail.com>, CloudFlare Inc.
+
+Copyright (C) 2013, by Yichun Zhang <agentzh@gmail.com>, CloudFlare Inc.
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+[Back to TOC](#table-of-contents)
+
diff --git a/lib/resty/cookie.lua b/lib/resty/cookie.lua
new file mode 100644
index 0000000..652df57
--- /dev/null
+++ b/lib/resty/cookie.lua
@@ -0,0 +1,104 @@
+-- Copyright (C) 2013 Jiale Zhi (calio), Cloudflare Inc.
+require "luacov"
+
+local type = type
+local get_string_byte = string.byte
+local get_string_sub = string.sub
+
+
+local ok, new_tab = pcall(require, "table.new")
+if not ok then
+ new_tab = function (narr, nrec) return {} end
+end
+
+local _M = new_tab(0, 2)
+
+
+_M._VERSION = '0.01'
+
+
+local mt = { __index = _M }
+
+
+local function get_cookie_table(text_cookie)
+ if type(text_cookie) ~= "string" then
+ return nil, string.format(
+ "expect text_cookie to be \"string\" but found %s",
+ type(text_cookie))
+ end
+
+ local cookie_table = {}
+ local EXPECT_KEY = 1
+ local EXPECT_VALUE = 2
+ local EXPECT_SP = 3
+
+ local state = EXPECT_KEY
+ local i = 1
+ local j = 1
+ local key, value
+
+ while j <= #text_cookie do
+ if state == EXPECT_KEY then
+ if get_string_byte(text_cookie, j) == get_string_byte("=") then
+ key = get_string_sub(text_cookie, i, j - 1)
+ state = EXPECT_VALUE
+ i = j + 1
+ end
+ elseif state == EXPECT_VALUE then
+ if get_string_byte(text_cookie, j) == get_string_byte(";") then
+ value = get_string_sub(text_cookie, i, j - 1)
+ cookie_table[key] = value
+
+ key, value = nil, nil
+ state = EXPECT_SP
+ i = j + 1
+ end
+ elseif state == EXPECT_SP then
+ if get_string_byte(text_cookie, j) ~= get_string_byte(" ") then
+ state = EXPECT_KEY
+ i = j
+ j = j - 1
+ end
+ end
+ j = j + 1
+ end
+
+ if key ~= nil and value == nil then
+ cookie_table[key] = get_string_sub(text_cookie, i)
+ end
+
+ return cookie_table
+end
+
+function _M.new(self)
+ local _cookie = ngx.var.http_cookie
+ 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 self.cookie_table == nil then
+ self.cookie_table = get_cookie_table(self._cookie)
+ end
+
+ return self.cookie_table[key]
+end
+
+function _M.get_all(self)
+ local err
+
+ if self.cookie_table == nil then
+ ngx.log(ngx.NOTICE, "call get_cookie_table")
+ self.cookie_table, err = get_cookie_table(self._cookie)
+ if self.cookie_table == nil then
+ ngx.log(ngx.ERR, "failed to get all cookies: ", err)
+ return {}
+ end
+ end
+
+ return self.cookie_table
+end
+
+return _M
diff --git a/t/sanity.t b/t/sanity.t
new file mode 100644
index 0000000..c9d7262
--- /dev/null
+++ b/t/sanity.t
@@ -0,0 +1,78 @@
+# vim:set ft= ts=4 sw=4 et:
+
+use Test::Nginx::Socket;
+use Cwd qw(cwd);
+
+repeat_each(2);
+
+plan tests => repeat_each() * (blocks() * 2);
+
+my $pwd = cwd();
+
+our $HttpConfig = qq{
+ lua_package_path "$pwd/lib/?.lua;;";
+ lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;";
+};
+
+$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
+
+no_long_string();
+
+log_level('debug');
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- 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 fields = cookie:get_all()
+
+ for k, v in pairs(fields) do
+ ngx.say(k, " => ", v)
+ end
+ ';
+ }
+--- request
+GET /t
+--- more_headers
+Cookie: SID=31d4d96e407aad42; lang=en-US
+--- response_body
+SID => 31d4d96e407aad42
+lang => en-US
+
+
+
+=== TEST 2: sanity 2
+--- 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 field = cookie:get("lang")
+ ngx.say("lang", " => ", field)
+ ';
+ }
+--- request
+GET /t
+--- more_headers
+Cookie: SID=31d4d96e407aad42; lang=en-US
+--- response_body
+lang => en-US
+