Ejemplo n.º 1
0
    def add_cookie_header(self, request):
        wreq = WrappedRequest(request)
        self.policy._now = self.jar._now = int(time.time())

        # the cookiejar implementation iterates through all domains
        # instead we restrict to potential matches on the domain
        req_host = urlparse_cached(request).hostname

        if not IPV4_RE.search(req_host):
            hosts = potential_domain_matches(req_host)
            if req_host.find(".") == -1:
                hosts += req_host + ".local"
        else:
            hosts = [req_host]

        cookies = []
        for host in hosts:
            if host in self.jar._cookies:
                cookies += self.jar._cookies_for_domain(host, wreq)

        attrs = self.jar._cookie_attrs(cookies)
        if attrs:
            if not wreq.has_header("Cookie"):
                wreq.add_unredirected_header("Cookie", "; ".join(attrs))

        self.processed += 1
        if self.processed % self.check_expired_frequency == 0:
            # This is still quite inefficient for large number of cookies
            self.jar.clear_expired_cookies()
Ejemplo n.º 2
0
    def add_cookie_header(self, request):
        wreq = WrappedRequest(request)
        self.policy._now = self.jar._now = int(time.time())

        # the cookiejar implementation iterates through all domains
        # instead we restrict to potential matches on the domain
        req_host = urlparse_cached(request).hostname
        if not req_host:
            return

        if not IPV4_RE.search(req_host):
            hosts = potential_domain_matches(req_host)
            if req_host.find(".") == -1:
                hosts += req_host + ".local"
        else:
            hosts = [req_host]

        cookies = []
        for host in hosts:
            if host in self.jar._cookies:
                cookies += self.jar._cookies_for_domain(host, wreq)

        attrs = self.jar._cookie_attrs(cookies)
        if attrs:
            if not wreq.has_header("Cookie"):
                wreq.add_unredirected_header("Cookie", "; ".join(attrs))

        self.processed += 1
        if self.processed % self.check_expired_frequency == 0:
            # This is still quite inefficient for large number of cookies
            self.jar.clear_expired_cookies()
Ejemplo n.º 3
0
    def _test_mount(self, cookiefilename=None, reportname=None):
        """Mount the statically configured test server, configure a cookiestore."""
        requests.Session.mount(self, _server_url + '/',
                               requests.adapters.HTTPAdapter(max_retries=5))

        self.verify = _verify

        if cookiefilename:
            cj = cookielib.MozillaCookieJar(cookiefilename)
            cj.load(ignore_expires=True)
            for cookie in cj:
                kwargs = {
                    "domain": cookie.domain,
                    "path": cookie.path,
                }
                if cookie.domain.find('.') == -1 and not IPV4_RE.search(
                        cookie.domain):
                    # mangle this the same way cookielib does or domain matching will fail!
                    kwargs['domain'] = cookie.domain + '.local'
                if cookie.expires:
                    kwargs['expires'] = cookie.expires

                self.cookies.set(cookie.name, cookie.value, **kwargs)

        if reportname:
            preamble = ('Created %s' +
                        (' with cookies %s:\n' % cookiefilename
                         if cookiefilename else '\n')) % reportname
            if False:
                dump_cookies(self.cookies, preamble)
            else:
                sys.stderr.write(preamble)
def cookies_for_url(jar, url):
    """

    Get cookies from a cookielib CookieJar for an URL.

    Adapted from scrapy.http.cookies.CookieJar.add_cookie_header().

    """

    host = urlparse(url).hostname
    if not IPV4_RE.search(host):
        hosts = potential_domain_matches(host)
        if host.find(".") == -1:
            hosts += host + ".local"
    else:
        hosts = [host]

    for host in hosts:
        if host in jar._cookies:
            wreq = WrappedRequest(Request(url))
            for cookie in jar._cookies_for_domain(host, wreq):
                yield cookie