Return sequence of Cookie objects extracted from response object.
(self, response, request)
| 1598 | cookie.version = 0 |
| 1599 | |
| 1600 | def make_cookies(self, response, request): |
| 1601 | """Return sequence of Cookie objects extracted from response object.""" |
| 1602 | # get cookie-attributes for RFC 2965 and Netscape protocols |
| 1603 | headers = response.info() |
| 1604 | rfc2965_hdrs = headers.get_all("Set-Cookie2", []) |
| 1605 | ns_hdrs = headers.get_all("Set-Cookie", []) |
| 1606 | self._policy._now = self._now = int(time.time()) |
| 1607 | |
| 1608 | rfc2965 = self._policy.rfc2965 |
| 1609 | netscape = self._policy.netscape |
| 1610 | |
| 1611 | if ((not rfc2965_hdrs and not ns_hdrs) or |
| 1612 | (not ns_hdrs and not rfc2965) or |
| 1613 | (not rfc2965_hdrs and not netscape) or |
| 1614 | (not netscape and not rfc2965)): |
| 1615 | return [] # no relevant cookie headers: quick exit |
| 1616 | |
| 1617 | try: |
| 1618 | cookies = self._cookies_from_attrs_set( |
| 1619 | split_header_words(rfc2965_hdrs), request) |
| 1620 | except Exception: |
| 1621 | _warn_unhandled_exception() |
| 1622 | cookies = [] |
| 1623 | |
| 1624 | if ns_hdrs and netscape: |
| 1625 | try: |
| 1626 | # RFC 2109 and Netscape cookies |
| 1627 | ns_cookies = self._cookies_from_attrs_set( |
| 1628 | parse_ns_headers(ns_hdrs), request) |
| 1629 | except Exception: |
| 1630 | _warn_unhandled_exception() |
| 1631 | ns_cookies = [] |
| 1632 | self._process_rfc2109_cookies(ns_cookies) |
| 1633 | |
| 1634 | # Look for Netscape cookies (from Set-Cookie headers) that match |
| 1635 | # corresponding RFC 2965 cookies (from Set-Cookie2 headers). |
| 1636 | # For each match, keep the RFC 2965 cookie and ignore the Netscape |
| 1637 | # cookie (RFC 2965 section 9.1). Actually, RFC 2109 cookies are |
| 1638 | # bundled in with the Netscape cookies for this purpose, which is |
| 1639 | # reasonable behaviour. |
| 1640 | if rfc2965: |
| 1641 | lookup = {} |
| 1642 | for cookie in cookies: |
| 1643 | lookup[(cookie.domain, cookie.path, cookie.name)] = None |
| 1644 | |
| 1645 | def no_matching_rfc2965(ns_cookie, lookup=lookup): |
| 1646 | key = ns_cookie.domain, ns_cookie.path, ns_cookie.name |
| 1647 | return key not in lookup |
| 1648 | ns_cookies = filter(no_matching_rfc2965, ns_cookies) |
| 1649 | |
| 1650 | if ns_cookies: |
| 1651 | cookies.extend(ns_cookies) |
| 1652 | |
| 1653 | return cookies |
| 1654 | |
| 1655 | def set_cookie_if_ok(self, cookie, request): |
| 1656 | """Set a cookie if policy says it's OK to do so.""" |