Skip to content

Instantly share code, notes, and snippets.

@cou929
Last active December 30, 2015 18:48
Show Gist options
  • Save cou929/7869555 to your computer and use it in GitHub Desktop.
Save cou929/7869555 to your computer and use it in GitHub Desktop.
Study of cookie domain limitation of each major browsers.
<!DOCTYPE html>
<head>
<title>Cookie restriction tester</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="tester.js"></script>
</head>
<body>
<div id="max_cookie_count_per_domain"></div>
<div id="max_cookie_count_per_domain_warning"></div>
<div id="ext"></div>
<div id="ext2"></div>
</body>
</html>
var CookieManager = function CookieManager(doc) {
this.doc = doc || window.document;
};
CookieManager.prototype.setItem = function setItem(key, value) {
// value of expire is random future
this.doc.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + "; expires=" + (new Date((new Date()).valueOf() + parseInt(Math.random() * 10, 10) * 50000 + 100000)).toUTCString();
};
CookieManager.prototype.getItem = function getItem(key) {
return decodeURIComponent(this.doc.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
};
CookieManager.prototype.removeItem = function removeItem(key) {
document.cookie = encodeURIComponent(key) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
};
CookieManager.prototype.clear = function clear() {
var keys = this.keys();
for (var i = 0, key = keys[i]; key; key = keys[++i]) {
this.removeItem(key);
}
if (this.count() !== 0) {
throw "there are some cookies";
}
};
CookieManager.prototype.count = function count() {
if (!this.doc.cookie) return 0;
return this.doc.cookie.split(";").length;
};
CookieManager.prototype.keys = function keys() {
var result = [];
var cookie_keys = this.doc.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var i = 0, key = cookie_keys[i]; key; key = cookie_keys[++i]) {
result.push(decodeURIComponent(key));
}
return result;
};
var CookieTester = function CookieTester(cookie_manager, reporter, options) {
var setting = $.extend({
cookie_value: '1',
count_test_limit: 1000
}, options);
this.cookie_manager = cookie_manager;
this.reporter = reporter;
this.setting = setting;
};
CookieTester.prototype.testMaxCookieCountPerDomain = function testMaxCookieCountPerDomain() {
var manager = this.cookie_manager,
reporter = this.reporter,
result,
value = this.setting.cookie_value,
key, current_count;
manager.clear();
for (var i = 1; i <= this.setting.count_test_limit; i++) {
key = "abcdefghijklmnopqrstuvwxyz".charAt(parseInt(Math.random() * 26, 10)) + i;
manager.setItem(key, value);
current_count = manager.count();
if (current_count !== i) {
result = i - 1;
break;
}
}
if (result === undefined || result === this.setting.count_test_limit - 1) {
reporter.warn_max_cookie_per_domain("it might reach limit of test. check the upper limit");
result = this.setting.count_test_limit;
}
document.getElementById('ext').innerHTML = document.cookie;
document.getElementById('ext2').innerHTML = manager.count();
reporter.report_max_cookie_per_domain(result);
};
var HtmlReporter = function HtmlReporter() {
this.report_max_cookie_per_domain = function (result) { document.getElementById('max_cookie_count_per_domain').innerHTML = result; };
this.warn_max_cookie_per_domain = function (result) { document.getElementById('max_cookie_count_per_domain_warning').innerHTML = result; };
};
$(function() {
var manager = new CookieManager();
var reporter = new HtmlReporter();
var tester = new CookieTester(manager, reporter);
tester.testMaxCookieCountPerDomain();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment