yay cookies
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 2f9e287 · Browse files · 3 files·+43 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+43 added−5 removed
- other:+43 added−5 removed
Provenance
- Commit
14bd9eb- Tree
ff29b3c- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
2f9e287
3 files changed+43 added−5 removed
modified:requests/api.py+1 added-1 removed
47 unmodified lines48 49 if cookies is None:50 cookies = {}51 52- cookies = cookiejar_from_dict(cookies)52+ # cookies = cookiejar_from_dict(cookies)53 54 # Expand header values55 if headers:56 for k, v in headers.items() or {}:105 unmodified linesmodified:requests/models.py+40 added-2 removed
6 unmodified lines7 """8 9 import urllib10 import zlib11+from Cookie import SimpleCookie12 from urlparse import urlparse, urlunparse, urljoin13 14 from .packages import urllib315 from .packages.urllib3.filepost import encode_multipart_formdata117 unmodified lines133 134 # Make headers case-insensitive.135 response.headers = CaseInsensitiveDict(getattr(resp, 'headers', None))136 137+ # Start off with our local cookies.138+ cookies = self.cookies or dict()139+140+ # Add new cookies from the server.141+ if 'set-cookie' in response.headers:142+ cookie_header = response.headers['set-cookie']143+144+ c = SimpleCookie()145+ c.load(cookie_header)146+147+ for k,v in c.items():148+ cookies.update({k: v.value})149+150+ # Save cookies in Response.151+ response.cookies = cookies152+153 # Save original resopnse for later.154 response.raw = resp155 156 # TODO: ?99 unmodified lines256 257 # Build the final URL.258 url = build_url(self.url, self.params)259 260+ # Nottin' on you.261 body = None262 content_type = None263 264 # Multi-part file uploads.48 unmodified lines313 314 # Part of a connection pool, so no fancy stuff. Sorry!315 do_block = True316 317+ if self.cookies:318+ # Skip if 'cookie' header is explicitly set.319+ if 'cookie' not in self.headers:320+321+ # Simple cookie with our dict.322+ c = SimpleCookie()323+ c.load(self.cookies)324+325+ # Turn it into a header.326+ cookie_header = c.output(header='').strip()327+328+ # Attach Cookie header to request.329+ self.headers['Cookie'] = cookie_header330+331 # Create the connection.332 r = connection.urlopen(333 method=self.method,334 url=url,9 unmodified lines344 if self.config.get('keepalive') and pools:345 self._pools = pools346 347 # Extract cookies.316- # if self.cookiejar is not None:348+ if self.cookies is not None:349+ pass350+ # cookies = cookiejar_from_dict(self.cookies)351+# >>> C = Cookie.SimpleCookie()352+# >>> C["rocky"] = "road"353+# >>> C["rocky"]["path"] = "/cookie"354+# >>> print C.output(header="Cookie:")355+# Cookie: rocky=road; Path=/cookie356 # self.cookiejar.extract_cookies(resp, req)357 358 # except (urllib2.HTTPError, urllib2.URLError), why:359 except Exception, why:10 unmodified lines370 # self.response = Response.from_urllib3()371 self._build_response(r)372 self.response.ok = True373 335-374 self.sent = self.response.ok375 376 return self.sent377 131 unmodified linesmodified:requests/utils.py+2 added-2 removed
115 unmodified lines116 117 return ''.join(collector)118 119 120-def dict_from_cookiejar(cj):120+def dict_from_cookiejar(cookies):121 """Returns a key/value dictionary from a CookieJar.122 123 :param cj: CookieJar object to extract cookies from.124 """125 126 cookie_dict = {}127 128- for _, cookies in cj._cookies.items():128+ for _, cookies in cookies.items():129 for _, cookies in cookies.items():130 for cookie in cookies.values():131 cookie_dict[cookie.name] = cookie.value132 250 unmodified lines