Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cookies

  • Loading branch information...
commit 2619a9c9320f821bb2e53d0db3be045833a9d9bb 1 parent 5b9c8b9
@baverman authored
Showing with 22 additions and 2 deletions.
  1. +3 −0  swoop/request.py
  2. +19 −2 swoop/session.py
View
3  swoop/request.py
@@ -9,6 +9,9 @@
class Request(object):
def __init__(self, url, baseurl=None, session_ref=None):
if baseurl:
+ if isinstance(baseurl, Request):
+ baseurl = baseurl.url
+
url = urljoin(baseurl.get_url(), url)
self.session_ref = session_ref
View
21 swoop/session.py
@@ -1,5 +1,8 @@
from collections import defaultdict
from Cookie import SimpleCookie
+from cookielib import Cookie
+
+from weakref import ref
from .pool import ConnectionPool
from .request import Request
@@ -16,11 +19,22 @@ def __init__(self, debuglevel=None):
def _set_cookies(self, headers, request):
cookies = [c for domain, cookies in self.cookies.iteritems()
- if request.host.endswith(domain) for c in cookies.itervalues()]
+ if request.host.endswith(domain) or domain == '_all' for c in cookies.itervalues()]
if cookies:
headers['Cookie'] = '; '.join(c.output([], '').lstrip() for c in cookies)
+ def set_cookie(self, name, value=None, domain='_all'):
+ if isinstance(name, Cookie):
+ name, value = name.name, name.value
+ domain = name.domain or domain
+
+ c = SimpleCookie()
+ c[name] = value
+
+ for cookie in c.itervalues():
+ self.cookies[domain.rstrip(',').lstrip('.')][cookie.key] = cookie
+
def _get_cookies(self, response):
c = SimpleCookie()
for h, v in response.getheaders():
@@ -52,7 +66,7 @@ def request(self, request):
if request.referer:
headers['Referer'] = request.referer.url if\
- isinstance(request.referer, Response) else request.referer
+ isinstance(request.referer, (Response, Request)) else request.referer
self._set_cookies(headers, request)
response = request.request(self._pool, headers)
@@ -68,6 +82,9 @@ def request(self, request):
return Response(self, response, response_data)
+ def __call__(self, url, baseurl):
+ return Request(url, baseurl, ref(self))
+
def clear(self):
self._pool.clear()
self.cookies.clear()
Please sign in to comment.
Something went wrong with that request. Please try again.