Basic HTTP cookie parser and serializer for HTTP servers in Luvit.io.
lit install voronianski/cookie
local Cookie = require('cookie')
local cookie = Cookie:new()
Parse HTTP Cookie
header string and returning a table of all cookie name-value pairs. The str
argument is the string representing a Cookie
header value and options
is an optional table containing additional parsing options.
local cookies = cookie:parse('foo=bar; equation=E%3Dmc%5E2');
-- { foo = 'bar', equation = 'E=mc^2' }
cookie:parse
accepts these properties in the options table.
Specifies a function that will be used to decode a cookie's value. Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode a previously-encoded cookie value into a Lua string or table.
The default function is the built-in querystring.urldecode
, which will decode any URL-encoded sequences into their byte representations.
Serialize a cookie name-value pair into a Set-Cookie
header string. The name
argument is the name for the cookie, the value
argument is the value to set the cookie to, and the options
argument is an optional table containing additional serialization options.
local setCookie = cookie:serialize('foo', 'bar');
-- foo=bar
cookie:serialize
accepts these properties in the options table.
Specifies the value for the Domain
Set-Cookie
attribute. By default, no domain is set, and most clients will consider the cookie to apply to only the current domain.
Specifies a function that will be used to encode a cookie's value. Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie's value.
The default function is the built-in querystring.urlencode
, which will encode a Lua string into UTF-8 byte sequences and then URL-encode any that fall outside of the cookie range.
Specifies the os.date
to be the value for the Expires
Set-Cookie
attribute. By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting a web browser application.
note the cookie storage model specification states that if both expires
and magAge
are set, then maxAge
takes precedence, but it is possible not all clients by obey this, so if both are set, they should point to the same date and time.
Specifies the boolean
value for the [HttpOnly
Set-Cookie
attribute][rfc-6266-5.2.6]. When truthy, the HttpOnly
attribute is set, otherwise it is not. By default, the HttpOnly
attribute is not set.
note be careful when setting this to true
, as compliant clients will not allow client-side JavaScript to see the cookie in document.cookie
.
Specifies the number
(in seconds) to be the value for the Max-Age
Set-Cookie
attribute.
The given number will be converted to an integer by rounding down. By default, no maximum age is set.
note the cookie storage model specification states that if both expires
and magAge
are set, then maxAge
takes precedence, but it is possible not all clients by obey this, so if both are set, they should point to the same date and time.
Specifies the value for the Path
Set-Cookie
attribute. By default, the path is considered the "default path". By default, no maximum age is set, and most clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting a web browser application.
Specifies the boolean
value for the [Secure
Set-Cookie
attribute][rfc-6266-5.2.5]. When truthy, the Secure
attribute is set, otherwise it is not. By default, the Secure
attribute is not set.
note be careful when setting this to true
, as compliant clients will not send the cookie back to the server in the future if the browser does not have an HTTPS connection.
Sign the given value
with secret
.
local signed = cookie:sign('hello', 'tobiiscool')
-- 'hello.0c60d4906948902ccfcfe0b4074eb814d8077448e8c7b721f2d3811ac959e502'
Unsign and decode the given value
with secret
. It returns false
if the signature is invalid.
local signed = cookie:sign('hello', 'tobiiscool')
cookie:unsign(signed, 'tobiiscool')
-- 'hello'
cookie:unsign(signed, 'luna')
-- false
Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie.
Given a table, this will iterate over the keys and call parseJSONCookie
on each value. This will return the same table passed in.
local cookie = cookie:parseJSONCookie('j:{"foo":"bar"}')
-- { foo = 'bar' }
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature was valid.
Given a table, this will iterate over the keys and check if any value is a signed cookie. If it is a signed cookie and the signature is valid, the key will be deleted from the table and added to the new table that is returned.
local cookie = cookie:parseSignedCookie('s:hello.0c60d4906948902ccfcfe0b4074eb814d8077448e8c7b721f2d3811ac959e502')
-- hello
lit install
luvit ./test
WWWWWW||WWWWWW
W W W||W W W
||
( OO )__________
/ | \
/o o| MIT \
\___/||_||__||_|| *
|| || || ||
_||_|| _||_||
(__|__|(__|__|
MIT Licensed
Copyright (c) 2014-2016 Dmitri Voronianski [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.