Permalink
Browse files

refactored cookies.update to use cookies.set

  • Loading branch information...
1 parent d6db19c commit 0736e43a27c474bd51249ced42734a2d6dc2a93c @boblail boblail committed Jan 20, 2011
Showing with 14 additions and 20 deletions.
  1. +14 −20 src/zombie/cookies.coffee
View
@@ -62,8 +62,11 @@ class Cookies
#
# * name -- Cookie name
# * value -- Cookie value
- # * options -- Options max-age, expires, secure
+ # * options -- Options max-age, expires, secure, domain, path
this.set = (name, value, options = {})->
+ return if options.domain && !domainMatch(options.domain, hostname)
+ return if options.path && pathname.indexOf(options.path) != 0
+
name = name.toLowerCase()
state = { value: value.toString() }
if options.expires
@@ -72,11 +75,12 @@ class Cookies
maxage = options["max-age"]
state.expires = browser.clock + maxage if typeof maxage is "number"
state.secure = true if options.secure
+
if typeof state.expires is "number" && state.expires <= browser.clock
- @remove(name)
+ @remove(name, options)
else
- in_domain = cookies[hostname] ||= {}
- in_path = in_domain[pathname] ||= {}
+ in_domain = cookies[options.domain || hostname] ||= {}
+ in_path = in_domain[options.path || pathname] ||= {}
in_path[name] = state
#### cookies(host, path).remove(name, options?)
@@ -111,28 +115,18 @@ class Cookies
fields = cookie.split(/;+/)
first = fields[0].trim()
[name, value] = first.split(/\=/, 2)
-
+
options = { value: value }
- domain = path = null
for field in fields
[key, val] = field.trim().split(/\=/, 2)
switch key.toLowerCase()
- when "domain" then domain = dequote(val)
- when "path" then path = dequote(val).replace(/%[^\/]*$/, "")
- when "expires" then options.expires = new Date(dequote(val)).getTime()
- when "max-age" then options.expires = browser.clock + parseInt(dequote(val), 10)
+ when "domain" then options.domain = dequote(val)
+ when "path" then options.path = dequote(val).replace(/%[^\/]*$/, "")
+ when "expires" then options.expires = new Date(dequote(val))
+ when "max-age" then options['max-age'] = parseInt(dequote(val), 10)
when "secure" then options.secure = true
- continue if domain && !domainMatch(domain, hostname)
- continue if path && pathname.indexOf(path) != 0
- # @set(name, value, options)
-
- if options.expires && options.expires <= browser.clock
- @remove(name, {domain: domain, path: path})
- else
- in_domain = cookies[domain || hostname] ||= {}
- in_path = in_domain[path || pathname] ||= {}
- in_path[name] = options
+ @set(name, value, options)
#### cookies(host, path).addHeader(headers)
#

0 comments on commit 0736e43

Please sign in to comment.