WebInspector.CookieParser = function()
{
}
WebInspector.CookieParser.prototype = {
get cookies()
{
return this._cookies;
},
parseCookie: function(cookieHeader)
{
if (!this._initialize(cookieHeader))
return;
for (var kv = this._extractKeyValue(); kv; kv = this._extractKeyValue()) {
if (kv.key.charAt(0) === "$" && this._lastCookie)
this._lastCookie.addAttribute(kv.key.slice(1), kv.value);
else if (kv.key.toLowerCase() !== "$version" && typeof kv.value === "string")
this._addCookie(kv, WebInspector.Cookie.Type.Request);
this._advanceAndCheckCookieDelimiter();
}
this._flushCookie();
return this._cookies;
},
parseSetCookie: function(setCookieHeader)
{
if (!this._initialize(setCookieHeader))
return;
for (var kv = this._extractKeyValue(); kv; kv = this._extractKeyValue()) {
if (this._lastCookie)
this._lastCookie.addAttribute(kv.key, kv.value);
else
this._addCookie(kv, WebInspector.Cookie.Type.Response);
if (this._advanceAndCheckCookieDelimiter())
this._flushCookie();
}
this._flushCookie();
return this._cookies;
},
_initialize: function(headerValue)
{
this._input = headerValue;
if (typeof headerValue !== "string")
return false;
this._cookies = [];
this._lastCookie = null;
this._originalInputLength = this._input.length;
return true;
},
_flushCookie: function()
{
if (this._lastCookie)
this._lastCookie.size = this._originalInputLength - this._input.length - this._lastCookiePosition;
this._lastCookie = null;
},
_extractKeyValue: function()
{
if (!this._input || !this._input.length)
return null;
var keyValueMatch = /^[ \t]*([^\s=;]+)[ \t]*(?:=[ \t]*([^;\n]*))?/.exec(this._input);
if (!keyValueMatch) {
console.log("Failed parsing cookie header before: " + this._input);
return null;
}
var result = {
key: keyValueMatch[1],
value: keyValueMatch[2] && keyValueMatch[2].trim(),
position: this._originalInputLength - this._input.length
};
this._input = this._input.slice(keyValueMatch[0].length);
return result;
},
_advanceAndCheckCookieDelimiter: function()
{
var match = /^\s*[\n;]\s*/.exec(this._input);
if (!match)
return false;
this._input = this._input.slice(match[0].length);
return match[0].match("\n") !== null;
},
_addCookie: function(keyValue, type)
{
if (this._lastCookie)
this._lastCookie.size = keyValue.position - this._lastCookiePosition;
this._lastCookie = keyValue.value ? new WebInspector.Cookie(keyValue.key, keyValue.value, type) :
new WebInspector.Cookie("", keyValue.key, type);
this._lastCookiePosition = keyValue.position;
this._cookies.push(this._lastCookie);
}
};
WebInspector.CookieParser.parseCookie = function(header)
{
return (new WebInspector.CookieParser()).parseCookie(header);
}
WebInspector.CookieParser.parseSetCookie = function(header)
{
return (new WebInspector.CookieParser()).parseSetCookie(header);
}
WebInspector.Cookie = function(name, value, type)
{
this.name = name;
this.value = value;
this.type = type;
this._attributes = {};
}
WebInspector.Cookie.prototype = {
get httpOnly()
{
return "httponly" in this._attributes;
},
get secure()
{
return "secure" in this._attributes;
},
get session()
{
return !("expries" in this._attributes || "max-age" in this._attributes);
},
get path()
{
return this._attributes.path;
},
get domain()
{
return this._attributes.domain;
},
expires: function(requestDate)
{
return this._attributes.expires ? new Date(this._attributes.expires) :
(this._attributes["max-age"] ? new Date(requestDate.getTime() + 1000 * this._attributes["max-age"]) : null);
},
get attributes()
{
return this._attributes;
},
addAttribute: function(key, value)
{
this._attributes[key.toLowerCase()] = value;
}
}
WebInspector.Cookie.Type = {
Request: 0,
Response: 1
};