Beispiel #1
0
def extract_path(url):
    """Path component of url."""
    parts = urlsplit(url)
    path = cookiejar.escape_path(parts.path)
    if not path.startswith("/"):
        # fix bad RFC 2396 absoluteURI
        path = "/" + path
    return path
Beispiel #2
0
    def _cookie_from_cookie_tuple(self, cookies_tuple, response):
        # standard is dict of standard cookie-attributes, rest is dict of the
        # rest of them

        now = int(time.time())
        name, value, standard, rest = cookies_tuple

        domain = standard.get("domain", _UNSET)
        path = standard.get("path", _UNSET)
        port = standard.get("port", _UNSET)
        expires = standard.get("expires", _UNSET)

        # set the easy defaults
        version = standard.get("version", None)
        if version is not None:
            try:
                version = int(version)
            except ValueError:
                return None  # invalid version, ignore cookie
        secure = standard.get("secure", False)
        # (discard is also set if expires is Absent)
        discard = standard.get("discard", False)
        comment = standard.get("comment", None)
        comment_url = standard.get("commenturl", None)

        # set default path
        if path is not _UNSET and path != "":
            path_specified = True
            path = cookiejar.escape_path(path)
        else:
            path_specified = False
            path = extract_path(response.effective_url)
            i = path.rfind("/")
            if i != -1:
                if version == 0:
                    # Netscape spec parts company from reality here
                    path = path[:i]
                else:
                    path = path[:i + 1]
            if len(path) == 0:
                path = "/"

        # set default domain
        domain_specified = domain is not _UNSET
        # but first we have to remember whether it starts with a dot
        domain_initial_dot = False
        if domain_specified:
            domain_initial_dot = bool(domain.startswith("."))
        if domain is _UNSET:
            domain = eff_request_host(response.request)[1]
        elif not domain.startswith("."):
            domain = "." + domain

        # set default port
        port_specified = False
        if port is not _UNSET:
            if port is None:
                # Port attr present, but has no value: default to request port.
                # Cookie should then only be sent back on that port.
                port = extract_port(response.effective_url)
            else:
                try:
                    port = int(port.strip())
                    port_specified = True
                except ValueError:
                    _logger.debug("invalid port")
                    port = None

        else:
            # No port attr present.  Cookie can be sent back on any port.
            port = None

        # set default expires and discard
        if expires is _UNSET:
            expires = None
            discard = True
        elif expires <= now:
            # Expiry date in past is request to delete cookie.  This can't be
            # in DefaultCookiePolicy, because can't delete cookies there.
            try:
                self.clear(domain, path, name)
            except KeyError:
                pass
            return None

        return cookiejar.Cookie(version,
                                name, value,
                                port, port_specified,
                                domain, domain_specified, domain_initial_dot,
                                path, path_specified,
                                secure,
                                expires,
                                discard,
                                comment,
                                comment_url,
                                rest)