Skip to content

Commit

Permalink
fix set_cookie function
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Oct 3, 2016
1 parent 3ed969f commit 229a891
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions liberapay/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,28 @@ def is_card_expired(exp_year, exp_month):
return exp_year < cur_year or exp_year == cur_year and exp_month < cur_month


def ensure_str(s):
if isinstance(s, str):
return s
return s.decode('ascii') if isinstance(s, bytes) else s.encode('ascii')


def set_cookie(cookies, key, value, expires=None, httponly=True, path='/'):
key = str(key)
cookies[key] = str(value)
key = ensure_str(key)
cookies[key] = ensure_str(value)
cookie = cookies[key]
if expires:
if isinstance(expires, timedelta):
expires += utcnow()
if isinstance(expires, datetime):
expires = to_rfc822(expires)
cookie[str('expires')] = str(expires)
cookie[str('expires')] = ensure_str(expires)
if httponly:
cookie[str('httponly')] = True
if path:
cookie[str('path')] = str(path)
cookie[str('path')] = ensure_str(path)
if website.canonical_domain:
cookie[str('domain')] = str(website.canonical_domain)
cookie[str('domain')] = ensure_str(website.canonical_domain)
if website.canonical_scheme == 'https':
cookie[str('secure')] = True

Expand Down
2 changes: 1 addition & 1 deletion tests/py/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_csrf_cookie_properties(self):
assert r.code == 200
cookie = r.headers.cookie[csrf.CSRF_TOKEN]
assert cookie[str('domain')] == str('.example.com')
assert cookie[str('expires')][:5] == str('Mon, ')
assert cookie[str('expires')][-4:] == str(' GMT')
assert cookie[str('path')] == str('/')
assert cookie[str('secure')] is True

Expand Down

0 comments on commit 229a891

Please sign in to comment.