WebInspector.CookieParser = function()
{
}
WebInspector.CookieParser.KeyValue = function(key, value, position)
{
this.key = key;
this.value = value;
this.position = position;
}
WebInspector.CookieParser.prototype = {
cookies: function()
{
return this._cookies;
},
parseCookie: function(cookieHeader)
{
if (!this._initialize(cookieHeader))
return null;
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 null;
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.setSize(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 = new WebInspector.CookieParser.KeyValue(keyValueMatch[1], keyValueMatch[2] && keyValueMatch[2].trim(), 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.setSize(keyValue.position - this._lastCookiePosition);
this._lastCookie = typeof keyValue.value === "string" ? 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 = {
name: function()
{
return this._name;
},
value: function()
{
return this._value;
},
type: function()
{
return this._type;
},
httpOnly: function()
{
return "httponly" in this._attributes;
},
secure: function()
{
return "secure" in this._attributes;
},
session: function()
{
return !("expires" in this._attributes || "max-age" in this._attributes);
},
path: function()
{
return this._attributes["path"];
},
port: function()
{
return this._attributes["port"];
},
domain: function()
{
return this._attributes["domain"];
},
expires: function()
{
return this._attributes["expires"];
},
maxAge: function()
{
return this._attributes["max-age"];
},
size: function()
{
return this._size;
},
setSize: function(size)
{
this._size = size;
},
expiresDate: function(requestDate)
{
if (this.maxAge()) {
var targetDate = requestDate === null ? new Date() : requestDate;
return new Date(targetDate.getTime() + 1000 * this.maxAge());
}
if (this.expires())
return new Date(this.expires());
return null;
},
attributes: function()
{
return this._attributes;
},
addAttribute: function(key, value)
{
this._attributes[key.toLowerCase()] = value;
},
remove: function(callback)
{
PageAgent.deleteCookie(this.name(), (this.secure() ? "https://" : "http://") + this.domain() + this.path(), callback);
}
}
WebInspector.Cookie.Type = {
Request: 0,
Response: 1
};
WebInspector.Cookies = {}
WebInspector.Cookies.getCookiesAsync = function(callback)
{
function mycallback(error, cookies, cookiesString)
{
if (error)
return;
if (cookiesString)
callback(WebInspector.Cookies.buildCookiesFromString(cookiesString), false);
else
callback(cookies.map(WebInspector.Cookies.buildCookieProtocolObject), true);
}
PageAgent.getCookies(mycallback);
}
WebInspector.Cookies.buildCookiesFromString = function(rawCookieString)
{
var rawCookies = rawCookieString.split(/;\s*/);
var cookies = [];
if (!(/^\s*$/.test(rawCookieString))) {
for (var i = 0; i < rawCookies.length; ++i) {
var rawCookie = rawCookies[i];
var delimIndex = rawCookie.indexOf("=");
var name = rawCookie.substring(0, delimIndex);
var value = rawCookie.substring(delimIndex + 1);
var size = name.length + value.length;
var cookie = new WebInspector.Cookie(name, value, null);
cookie.setSize(size);
cookies.push(cookie);
}
}
return cookies;
}
WebInspector.Cookies.buildCookieProtocolObject = function(protocolCookie)
{
var cookie = new WebInspector.Cookie(protocolCookie.name, protocolCookie.value, null);
cookie.addAttribute("domain", protocolCookie["domain"]);
cookie.addAttribute("path", protocolCookie["path"]);
cookie.addAttribute("port", protocolCookie["port"]);
if (protocolCookie["expires"])
cookie.addAttribute("expires", protocolCookie["expires"]);
if (protocolCookie["httpOnly"])
cookie.addAttribute("httpOnly");
if (protocolCookie["secure"])
cookie.addAttribute("secure");
cookie.setSize(protocolCookie["size"]);
return cookie;
}
WebInspector.Cookies.cookieMatchesResourceURL = function(cookie, resourceURL)
{
var url = resourceURL.asParsedURL();
if (!url || !WebInspector.Cookies.cookieDomainMatchesResourceDomain(cookie.domain(), url.host))
return false;
return (url.path.startsWith(cookie.path())
&& (!cookie.port() || url.port == cookie.port())
&& (!cookie.secure() || url.scheme === "https"));
}
WebInspector.Cookies.cookieDomainMatchesResourceDomain = function(cookieDomain, resourceDomain)
{
if (cookieDomain.charAt(0) !== '.')
return resourceDomain === cookieDomain;
return !!resourceDomain.match(new RegExp("^([^\\.]+\\.)*" + cookieDomain.substring(1).escapeForRegExp() + "$", "i"));
}