Add global debug flag to cookielib to avoid heavy dependency on the logging module.

Resolves #1484758.
This commit is contained in:
Georg Brandl 2006-05-17 14:45:06 +00:00
parent 378d592617
commit feb0a3bdbc
1 changed files with 74 additions and 63 deletions

View File

@ -28,7 +28,7 @@ http://wwwsearch.sf.net/):
__all__ = ['Cookie', 'CookieJar', 'CookiePolicy', 'DefaultCookiePolicy', __all__ = ['Cookie', 'CookieJar', 'CookiePolicy', 'DefaultCookiePolicy',
'FileCookieJar', 'LWPCookieJar', 'LoadError', 'MozillaCookieJar'] 'FileCookieJar', 'LWPCookieJar', 'LoadError', 'MozillaCookieJar']
import re, urlparse, copy, time, urllib, logging import re, urlparse, copy, time, urllib
try: try:
import threading as _threading import threading as _threading
except ImportError: except ImportError:
@ -36,7 +36,18 @@ except ImportError:
import httplib # only for the default HTTP port import httplib # only for the default HTTP port
from calendar import timegm from calendar import timegm
debug = logging.getLogger("cookielib").debug debug = 0 # set to true to enable debugging via the logging module
logger = None
def _debug(*args):
global logger
if not debug:
return
if not logger:
import logging
logger = logging.getLogger("cookielib")
return logger.debug(*args)
DEFAULT_HTTP_PORT = str(httplib.HTTP_PORT) DEFAULT_HTTP_PORT = str(httplib.HTTP_PORT)
MISSING_FILENAME_TEXT = ("a filename was not supplied (nor was the CookieJar " MISSING_FILENAME_TEXT = ("a filename was not supplied (nor was the CookieJar "
@ -611,7 +622,7 @@ def request_port(request):
try: try:
int(port) int(port)
except ValueError: except ValueError:
debug("nonnumeric port: '%s'", port) _debug("nonnumeric port: '%s'", port)
return None return None
else: else:
port = DEFAULT_HTTP_PORT port = DEFAULT_HTTP_PORT
@ -902,7 +913,7 @@ class DefaultCookiePolicy(CookiePolicy):
strict about which cookies to accept). strict about which cookies to accept).
""" """
debug(" - checking cookie %s=%s", cookie.name, cookie.value) _debug(" - checking cookie %s=%s", cookie.name, cookie.value)
assert cookie.name is not None assert cookie.name is not None
@ -918,25 +929,25 @@ class DefaultCookiePolicy(CookiePolicy):
if cookie.version is None: if cookie.version is None:
# Version is always set to 0 by parse_ns_headers if it's a Netscape # Version is always set to 0 by parse_ns_headers if it's a Netscape
# cookie, so this must be an invalid RFC 2965 cookie. # cookie, so this must be an invalid RFC 2965 cookie.
debug(" Set-Cookie2 without version attribute (%s=%s)", _debug(" Set-Cookie2 without version attribute (%s=%s)",
cookie.name, cookie.value) cookie.name, cookie.value)
return False return False
if cookie.version > 0 and not self.rfc2965: if cookie.version > 0 and not self.rfc2965:
debug(" RFC 2965 cookies are switched off") _debug(" RFC 2965 cookies are switched off")
return False return False
elif cookie.version == 0 and not self.netscape: elif cookie.version == 0 and not self.netscape:
debug(" Netscape cookies are switched off") _debug(" Netscape cookies are switched off")
return False return False
return True return True
def set_ok_verifiability(self, cookie, request): def set_ok_verifiability(self, cookie, request):
if request.is_unverifiable() and is_third_party(request): if request.is_unverifiable() and is_third_party(request):
if cookie.version > 0 and self.strict_rfc2965_unverifiable: if cookie.version > 0 and self.strict_rfc2965_unverifiable:
debug(" third-party RFC 2965 cookie during " _debug(" third-party RFC 2965 cookie during "
"unverifiable transaction") "unverifiable transaction")
return False return False
elif cookie.version == 0 and self.strict_ns_unverifiable: elif cookie.version == 0 and self.strict_ns_unverifiable:
debug(" third-party Netscape cookie during " _debug(" third-party Netscape cookie during "
"unverifiable transaction") "unverifiable transaction")
return False return False
return True return True
@ -946,7 +957,7 @@ class DefaultCookiePolicy(CookiePolicy):
# servers that know both V0 and V1 protocols. # servers that know both V0 and V1 protocols.
if (cookie.version == 0 and self.strict_ns_set_initial_dollar and if (cookie.version == 0 and self.strict_ns_set_initial_dollar and
cookie.name.startswith("$")): cookie.name.startswith("$")):
debug(" illegal name (starts with '$'): '%s'", cookie.name) _debug(" illegal name (starts with '$'): '%s'", cookie.name)
return False return False
return True return True
@ -956,17 +967,17 @@ class DefaultCookiePolicy(CookiePolicy):
if ((cookie.version > 0 or if ((cookie.version > 0 or
(cookie.version == 0 and self.strict_ns_set_path)) and (cookie.version == 0 and self.strict_ns_set_path)) and
not req_path.startswith(cookie.path)): not req_path.startswith(cookie.path)):
debug(" path attribute %s is not a prefix of request " _debug(" path attribute %s is not a prefix of request "
"path %s", cookie.path, req_path) "path %s", cookie.path, req_path)
return False return False
return True return True
def set_ok_domain(self, cookie, request): def set_ok_domain(self, cookie, request):
if self.is_blocked(cookie.domain): if self.is_blocked(cookie.domain):
debug(" domain %s is in user block-list", cookie.domain) _debug(" domain %s is in user block-list", cookie.domain)
return False return False
if self.is_not_allowed(cookie.domain): if self.is_not_allowed(cookie.domain):
debug(" domain %s is not in user allow-list", cookie.domain) _debug(" domain %s is not in user allow-list", cookie.domain)
return False return False
if cookie.domain_specified: if cookie.domain_specified:
req_host, erhn = eff_request_host(request) req_host, erhn = eff_request_host(request)
@ -985,7 +996,7 @@ class DefaultCookiePolicy(CookiePolicy):
"info", "jobs", "mobi", "museum", "name", "pro", "info", "jobs", "mobi", "museum", "name", "pro",
"travel", "eu") and len(tld) == 2: "travel", "eu") and len(tld) == 2:
# domain like .co.uk # domain like .co.uk
debug(" country-code second level domain %s", domain) _debug(" country-code second level domain %s", domain)
return False return False
if domain.startswith("."): if domain.startswith("."):
undotted_domain = domain[1:] undotted_domain = domain[1:]
@ -993,21 +1004,21 @@ class DefaultCookiePolicy(CookiePolicy):
undotted_domain = domain undotted_domain = domain
embedded_dots = (undotted_domain.find(".") >= 0) embedded_dots = (undotted_domain.find(".") >= 0)
if not embedded_dots and domain != ".local": if not embedded_dots and domain != ".local":
debug(" non-local domain %s contains no embedded dot", _debug(" non-local domain %s contains no embedded dot",
domain) domain)
return False return False
if cookie.version == 0: if cookie.version == 0:
if (not erhn.endswith(domain) and if (not erhn.endswith(domain) and
(not erhn.startswith(".") and (not erhn.startswith(".") and
not ("."+erhn).endswith(domain))): not ("."+erhn).endswith(domain))):
debug(" effective request-host %s (even with added " _debug(" effective request-host %s (even with added "
"initial dot) does not end end with %s", "initial dot) does not end end with %s",
erhn, domain) erhn, domain)
return False return False
if (cookie.version > 0 or if (cookie.version > 0 or
(self.strict_ns_domain & self.DomainRFC2965Match)): (self.strict_ns_domain & self.DomainRFC2965Match)):
if not domain_match(erhn, domain): if not domain_match(erhn, domain):
debug(" effective request-host %s does not domain-match " _debug(" effective request-host %s does not domain-match "
"%s", erhn, domain) "%s", erhn, domain)
return False return False
if (cookie.version > 0 or if (cookie.version > 0 or
@ -1015,7 +1026,7 @@ class DefaultCookiePolicy(CookiePolicy):
host_prefix = req_host[:-len(domain)] host_prefix = req_host[:-len(domain)]
if (host_prefix.find(".") >= 0 and if (host_prefix.find(".") >= 0 and
not IPV4_RE.search(req_host)): not IPV4_RE.search(req_host)):
debug(" host prefix %s for domain %s contains a dot", _debug(" host prefix %s for domain %s contains a dot",
host_prefix, domain) host_prefix, domain)
return False return False
return True return True
@ -1031,12 +1042,12 @@ class DefaultCookiePolicy(CookiePolicy):
try: try:
int(p) int(p)
except ValueError: except ValueError:
debug(" bad port %s (not numeric)", p) _debug(" bad port %s (not numeric)", p)
return False return False
if p == req_port: if p == req_port:
break break
else: else:
debug(" request port (%s) not found in %s", _debug(" request port (%s) not found in %s",
req_port, cookie.port) req_port, cookie.port)
return False return False
return True return True
@ -1050,7 +1061,7 @@ class DefaultCookiePolicy(CookiePolicy):
""" """
# Path has already been checked by .path_return_ok(), and domain # Path has already been checked by .path_return_ok(), and domain
# blocking done by .domain_return_ok(). # blocking done by .domain_return_ok().
debug(" - checking cookie %s=%s", cookie.name, cookie.value) _debug(" - checking cookie %s=%s", cookie.name, cookie.value)
for n in "version", "verifiability", "secure", "expires", "port", "domain": for n in "version", "verifiability", "secure", "expires", "port", "domain":
fn_name = "return_ok_"+n fn_name = "return_ok_"+n
@ -1061,34 +1072,34 @@ class DefaultCookiePolicy(CookiePolicy):
def return_ok_version(self, cookie, request): def return_ok_version(self, cookie, request):
if cookie.version > 0 and not self.rfc2965: if cookie.version > 0 and not self.rfc2965:
debug(" RFC 2965 cookies are switched off") _debug(" RFC 2965 cookies are switched off")
return False return False
elif cookie.version == 0 and not self.netscape: elif cookie.version == 0 and not self.netscape:
debug(" Netscape cookies are switched off") _debug(" Netscape cookies are switched off")
return False return False
return True return True
def return_ok_verifiability(self, cookie, request): def return_ok_verifiability(self, cookie, request):
if request.is_unverifiable() and is_third_party(request): if request.is_unverifiable() and is_third_party(request):
if cookie.version > 0 and self.strict_rfc2965_unverifiable: if cookie.version > 0 and self.strict_rfc2965_unverifiable:
debug(" third-party RFC 2965 cookie during unverifiable " _debug(" third-party RFC 2965 cookie during unverifiable "
"transaction") "transaction")
return False return False
elif cookie.version == 0 and self.strict_ns_unverifiable: elif cookie.version == 0 and self.strict_ns_unverifiable:
debug(" third-party Netscape cookie during unverifiable " _debug(" third-party Netscape cookie during unverifiable "
"transaction") "transaction")
return False return False
return True return True
def return_ok_secure(self, cookie, request): def return_ok_secure(self, cookie, request):
if cookie.secure and request.get_type() != "https": if cookie.secure and request.get_type() != "https":
debug(" secure cookie with non-secure request") _debug(" secure cookie with non-secure request")
return False return False
return True return True
def return_ok_expires(self, cookie, request): def return_ok_expires(self, cookie, request):
if cookie.is_expired(self._now): if cookie.is_expired(self._now):
debug(" cookie expired") _debug(" cookie expired")
return False return False
return True return True
@ -1101,7 +1112,7 @@ class DefaultCookiePolicy(CookiePolicy):
if p == req_port: if p == req_port:
break break
else: else:
debug(" request port %s does not match cookie port %s", _debug(" request port %s does not match cookie port %s",
req_port, cookie.port) req_port, cookie.port)
return False return False
return True return True
@ -1114,16 +1125,16 @@ class DefaultCookiePolicy(CookiePolicy):
if (cookie.version == 0 and if (cookie.version == 0 and
(self.strict_ns_domain & self.DomainStrictNonDomain) and (self.strict_ns_domain & self.DomainStrictNonDomain) and
not cookie.domain_specified and domain != erhn): not cookie.domain_specified and domain != erhn):
debug(" cookie with unspecified domain does not string-compare " _debug(" cookie with unspecified domain does not string-compare "
"equal to request domain") "equal to request domain")
return False return False
if cookie.version > 0 and not domain_match(erhn, domain): if cookie.version > 0 and not domain_match(erhn, domain):
debug(" effective request-host name %s does not domain-match " _debug(" effective request-host name %s does not domain-match "
"RFC 2965 cookie domain %s", erhn, domain) "RFC 2965 cookie domain %s", erhn, domain)
return False return False
if cookie.version == 0 and not ("."+erhn).endswith(domain): if cookie.version == 0 and not ("."+erhn).endswith(domain):
debug(" request-host %s does not match Netscape cookie domain " _debug(" request-host %s does not match Netscape cookie domain "
"%s", req_host, domain) "%s", req_host, domain)
return False return False
return True return True
@ -1137,24 +1148,24 @@ class DefaultCookiePolicy(CookiePolicy):
if not erhn.startswith("."): if not erhn.startswith("."):
erhn = "."+erhn erhn = "."+erhn
if not (req_host.endswith(domain) or erhn.endswith(domain)): if not (req_host.endswith(domain) or erhn.endswith(domain)):
#debug(" request domain %s does not match cookie domain %s", #_debug(" request domain %s does not match cookie domain %s",
# req_host, domain) # req_host, domain)
return False return False
if self.is_blocked(domain): if self.is_blocked(domain):
debug(" domain %s is in user block-list", domain) _debug(" domain %s is in user block-list", domain)
return False return False
if self.is_not_allowed(domain): if self.is_not_allowed(domain):
debug(" domain %s is not in user allow-list", domain) _debug(" domain %s is not in user allow-list", domain)
return False return False
return True return True
def path_return_ok(self, path, request): def path_return_ok(self, path, request):
debug("- checking cookie path=%s", path) _debug("- checking cookie path=%s", path)
req_path = request_path(request) req_path = request_path(request)
if not req_path.startswith(path): if not req_path.startswith(path):
debug(" %s does not path-match %s", req_path, path) _debug(" %s does not path-match %s", req_path, path)
return False return False
return True return True
@ -1216,7 +1227,7 @@ class CookieJar:
cookies = [] cookies = []
if not self._policy.domain_return_ok(domain, request): if not self._policy.domain_return_ok(domain, request):
return [] return []
debug("Checking %s for cookies to return", domain) _debug("Checking %s for cookies to return", domain)
cookies_by_path = self._cookies[domain] cookies_by_path = self._cookies[domain]
for path in cookies_by_path.keys(): for path in cookies_by_path.keys():
if not self._policy.path_return_ok(path, request): if not self._policy.path_return_ok(path, request):
@ -1224,9 +1235,9 @@ class CookieJar:
cookies_by_name = cookies_by_path[path] cookies_by_name = cookies_by_path[path]
for cookie in cookies_by_name.values(): for cookie in cookies_by_name.values():
if not self._policy.return_ok(cookie, request): if not self._policy.return_ok(cookie, request):
debug(" not returning cookie") _debug(" not returning cookie")
continue continue
debug(" it's a match") _debug(" it's a match")
cookies.append(cookie) cookies.append(cookie)
return cookies return cookies
@ -1303,7 +1314,7 @@ class CookieJar:
The Cookie2 header is also added unless policy.hide_cookie2 is true. The Cookie2 header is also added unless policy.hide_cookie2 is true.
""" """
debug("add_cookie_header") _debug("add_cookie_header")
self._cookies_lock.acquire() self._cookies_lock.acquire()
self._policy._now = self._now = int(time.time()) self._policy._now = self._now = int(time.time())
@ -1380,7 +1391,7 @@ class CookieJar:
continue continue
if k == "domain": if k == "domain":
if v is None: if v is None:
debug(" missing value for domain attribute") _debug(" missing value for domain attribute")
bad_cookie = True bad_cookie = True
break break
# RFC 2965 section 3.3.3 # RFC 2965 section 3.3.3
@ -1390,7 +1401,7 @@ class CookieJar:
# Prefer max-age to expires (like Mozilla) # Prefer max-age to expires (like Mozilla)
continue continue
if v is None: if v is None:
debug(" missing or invalid value for expires " _debug(" missing or invalid value for expires "
"attribute: treating as session cookie") "attribute: treating as session cookie")
continue continue
if k == "max-age": if k == "max-age":
@ -1398,7 +1409,7 @@ class CookieJar:
try: try:
v = int(v) v = int(v)
except ValueError: except ValueError:
debug(" missing or invalid (non-numeric) value for " _debug(" missing or invalid (non-numeric) value for "
"max-age attribute") "max-age attribute")
bad_cookie = True bad_cookie = True
break break
@ -1411,7 +1422,7 @@ class CookieJar:
if (k in value_attrs) or (k in boolean_attrs): if (k in value_attrs) or (k in boolean_attrs):
if (v is None and if (v is None and
k not in ("port", "comment", "commenturl")): k not in ("port", "comment", "commenturl")):
debug(" missing value for %s attribute" % k) _debug(" missing value for %s attribute" % k)
bad_cookie = True bad_cookie = True
break break
standard[k] = v standard[k] = v
@ -1497,7 +1508,7 @@ class CookieJar:
self.clear(domain, path, name) self.clear(domain, path, name)
except KeyError: except KeyError:
pass pass
debug("Expiring cookie, domain='%s', path='%s', name='%s'", _debug("Expiring cookie, domain='%s', path='%s', name='%s'",
domain, path, name) domain, path, name)
return None return None
@ -1613,13 +1624,13 @@ class CookieJar:
def extract_cookies(self, response, request): def extract_cookies(self, response, request):
"""Extract cookies from response, where allowable given the request.""" """Extract cookies from response, where allowable given the request."""
debug("extract_cookies: %s", response.info()) _debug("extract_cookies: %s", response.info())
self._cookies_lock.acquire() self._cookies_lock.acquire()
self._policy._now = self._now = int(time.time()) self._policy._now = self._now = int(time.time())
for cookie in self.make_cookies(response, request): for cookie in self.make_cookies(response, request):
if self._policy.set_ok(cookie, request): if self._policy.set_ok(cookie, request):
debug(" setting cookie: %s", cookie) _debug(" setting cookie: %s", cookie)
self.set_cookie(cookie) self.set_cookie(cookie)
self._cookies_lock.release() self._cookies_lock.release()