Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement loose mode for cookie jars #56

Merged
merged 1 commit into from Oct 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -237,15 +237,15 @@ if (cookie.validate() === true) {

Exported via `tough.CookieJar`.

### `CookieJar([store],[rejectPublicSuffixes])`
### `CookieJar([store],[options])`

Simply use `new CookieJar()`. If you'd like to use a custom store, pass that to the constructor otherwise a `MemoryCookieStore` will be created and used.

### Properties

CookieJar object properties:
The `options` object can be omitted and can have the following properties:

* _rejectPublicSuffixes_ - boolean - reject cookies with domains like "com" and "co.uk" (default: `true`)
* _rejectPublicSuffixes_ - boolean - default `true` - reject cookies with domains like "com" and "co.uk"
* _looseMode_ - boolean - default `false` - accept malformed cookies like `bar` and `=bar`, which have an implied empty name.
This is not in the standard, but is used sometimes on the web and is accepted by (most) browsers.

Since eventually this module would like to support database/remote/etc. CookieJars, continuation passing style is used for CookieJar methods.

Expand Down
21 changes: 17 additions & 4 deletions lib/cookie.js
Expand Up @@ -878,9 +878,17 @@ Cookie.prototype.canonicalizedDomain = function canonicalizedDomain() {
return canonicalDomain(this.domain);
};

function CookieJar(store, rejectPublicSuffixes) {
if (rejectPublicSuffixes != null) {
this.rejectPublicSuffixes = rejectPublicSuffixes;
function CookieJar(store, options) {
if (typeof options === "boolean") {
options = {rejectPublicSuffixes: options};
} else if (options == null) {
options = {};
}
if (options.rejectPublicSuffixes != null) {
this.rejectPublicSuffixes = options.rejectPublicSuffixes;
}
if (options.looseMode != null) {
this.enableLooseMode = options.looseMode;
}

if (!store) {
Expand All @@ -890,6 +898,7 @@ function CookieJar(store, rejectPublicSuffixes) {
}
CookieJar.prototype.store = null;
CookieJar.prototype.rejectPublicSuffixes = true;
CookieJar.prototype.enableLooseMode = false;
var CAN_BE_SYNC = [];

CAN_BE_SYNC.push('setCookie');
Expand All @@ -902,10 +911,14 @@ CookieJar.prototype.setCookie = function(cookie, url, options, cb) {
}

var host = canonicalDomain(context.hostname);
var loose = this.enableLooseMode;
if (options.loose != null) {
loose = options.loose;
}

// S5.3 step 1
if (!(cookie instanceof Cookie)) {
cookie = Cookie.parse(cookie, { loose: options.loose });
cookie = Cookie.parse(cookie, { loose: loose });
}
if (!cookie) {
err = new Error("Cookie failed to parse");
Expand Down
15 changes: 15 additions & 0 deletions test/cookie_jar_test.js
Expand Up @@ -465,4 +465,19 @@ vows
}
}
})
.addBatch({
"Loose Mode": {
topic: function () {
var cj = new CookieJar(null, {looseMode: true});
cj.setCookieSync("FooBar", 'http://www.foonet.net', {});
return cj;
},
"parses loose cookies": function (cj) {
var cookies = cj.getCookiesSync('http://www.foonet.net');
assert.strictEqual(cookies.length, 1);
assert.strictEqual(cookies[0].key, '');
assert.strictEqual(cookies[0].value, 'FooBar');
}
}
})
.export(module);