Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a25d241
Showing
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
language: node_js | |||
node_js: | |||
- 0.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,44 @@ | |||
# cookie [![Build Status](https://secure.travis-ci.org/shtylman/node-cookie.png?branch=master)](http://travis-ci.org/shtylman/node-cookie) # | |||
|
|||
cookie is a basic cookie parser and serializer. It doesn't make assumptions about how you are going to deal with your cookies. It basically just provides a way to read and write the HTTP cookie headers. | |||
|
|||
See [RFC6265](http://tools.ietf.org/html/rfc6265) for details about the http header for cookies. | |||
|
|||
## how? | |||
|
|||
``` | |||
npm install cookie | |||
``` | |||
|
|||
```javascript | |||
var cookie = require('cookie'); | |||
|
|||
var hdr = cookie.serialize('foo', 'bar'); | |||
// hdr = 'foo=bar'; | |||
|
|||
var cookies = cookie.parse('foo=bar; cat=meow; dog=ruff'); | |||
// cookies = { foo: 'bar', cat: 'meow', dog: 'ruff' }; | |||
``` | |||
|
|||
## more | |||
|
|||
The serialize function takes a third parameter, an object, to set cookie options. See the RFC for valid values. | |||
|
|||
### path | |||
> cookie path | |||
### expires | |||
> absolute expiration date for the cookie (Date object) | |||
### maxAge | |||
> relative max age of the cookie from when the client receives it (seconds) | |||
### domain | |||
> domain for the cookie | |||
### secure | |||
> true or false | |||
### httpOnly | |||
> true or false | |||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,63 @@ | |||
|
|||
/// Serialize the a name value pair into a cookie string suitable for | |||
/// http headers. An optional options object specified cookie parameters | |||
/// | |||
/// serialize('foo', 'bar', { httpOnly: true }) | |||
/// => "foo=bar; httpOnly" | |||
/// | |||
/// @param {String} name | |||
/// @param {String} val | |||
/// @param {Object} options | |||
/// @return {String} | |||
var serialize = function(name, val, opt){ | |||
var pairs = [name + '=' + encodeURIComponent(val)]; | |||
opt = opt || {}; | |||
|
|||
if (opt.maxAge) pais.push('Max-Age=' + opt.maxAge); | |||
if (opt.domain) pairs.push('Domain=' + opt.domain); | |||
if (opt.path) pairs.push('Path=' + opt.path); | |||
if (opt.expires) pairs.push('Expires=' + opt.expires.toUTCString()); | |||
if (opt.httpOnly) pairs.push('HttpOnly'); | |||
if (opt.secure) pairs.push('Secure'); | |||
|
|||
return pairs.join('; '); | |||
}; | |||
|
|||
/// Parse the given cookie header string into an object | |||
/// The object has the various cookies as keys(names) => values | |||
/// @param {String} str | |||
/// @return {Object} | |||
var parse = function(str) { | |||
var obj = {} | |||
var pairs = str.split(/[;,] */); | |||
|
|||
pairs.forEach(function(pair) { | |||
var eq_idx = pair.indexOf('=') | |||
var key = pair.substr(0, eq_idx).trim() | |||
var val = pair.substr(++eq_idx, pair.length).trim(); | |||
|
|||
// quoted values | |||
if ('"' == val[0]) { | |||
val = val.slice(1, -1); | |||
} | |||
|
|||
// only assign once | |||
if (undefined == obj[key]) { | |||
val = val.replace(/\+/g, ' '); | |||
try { | |||
obj[key] = decodeURIComponent(val); | |||
} catch (err) { | |||
if (err instanceof URIError) { | |||
obj[key] = val; | |||
} else { | |||
throw err; | |||
} | |||
} | |||
} | |||
}); | |||
|
|||
return obj; | |||
}; | |||
|
|||
module.exports.serialize = serialize; | |||
module.exports.parse = parse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,26 @@ | |||
{ | |||
"author": "Roman Shtylman <shtylman@gmail.com>", | |||
"name": "cookie", | |||
"description": "cookie parsing and serialization", | |||
"version": "0.0.0", | |||
"repository": { | |||
"type": "git", | |||
"url": "git://github.com/shtylman/node-cookie.git" | |||
}, | |||
"keywords": [ | |||
"cookie", | |||
"cookies" | |||
], | |||
"main": "index.js", | |||
"scripts": { | |||
"test": "mocha" | |||
}, | |||
"dependencies": {}, | |||
"devDependencies": { | |||
"mocha": "1.x.x" | |||
}, | |||
"optionalDependencies": {}, | |||
"engines": { | |||
"node": "*" | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
--ui qunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,24 @@ | |||
// builtin | |||
var assert = require('assert'); | |||
|
|||
var crumbs = require('..'); | |||
|
|||
test('serialize', function() { | |||
assert.equal('foo=bar', crumbs.serialize('foo', 'bar')); | |||
|
|||
assert.equal('foo=bar; Path=/', crumbs.serialize('foo', 'bar', { | |||
path: '/' | |||
})); | |||
|
|||
assert.equal('foo=bar; Secure', crumbs.serialize('foo', 'bar', { | |||
secure: true | |||
})); | |||
|
|||
assert.equal('foo=bar; Domain=example.com', crumbs.serialize('foo', 'bar', { | |||
domain: 'example.com' | |||
})); | |||
|
|||
assert.equal('foo=bar; HttpOnly', crumbs.serialize('foo', 'bar', { | |||
httpOnly: true | |||
})); | |||
}); |