Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
feb0a3bd
Commit
feb0a3bd
authored
May 17, 2006
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add global debug flag to cookielib to avoid heavy dependency on the logging module.
Resolves #1484758.
parent
378d5926
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
63 deletions
+74
-63
Lib/cookielib.py
Lib/cookielib.py
+74
-63
No files found.
Lib/cookielib.py
View file @
feb0a3bd
...
...
@@ -28,7 +28,7 @@ http://wwwsearch.sf.net/):
__all__
=
[
'Cookie'
,
'CookieJar'
,
'CookiePolicy'
,
'DefaultCookiePolicy'
,
'FileCookieJar'
,
'LWPCookieJar'
,
'LoadError'
,
'MozillaCookieJar'
]
import
re
,
urlparse
,
copy
,
time
,
urllib
,
logging
import
re
,
urlparse
,
copy
,
time
,
urllib
try
:
import
threading
as
_threading
except
ImportError
:
...
...
@@ -36,7 +36,18 @@ except ImportError:
import
httplib
# only for the default HTTP port
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
)
MISSING_FILENAME_TEXT
=
(
"a filename was not supplied (nor was the CookieJar "
...
...
@@ -611,7 +622,7 @@ def request_port(request):
try:
int(port)
except ValueError:
debug("nonnumeric port: '%s'", port)
_
debug("nonnumeric port: '%s'", port)
return None
else:
port = DEFAULT_HTTP_PORT
...
...
@@ -902,7 +913,7 @@ class DefaultCookiePolicy(CookiePolicy):
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
...
...
@@ -918,25 +929,25 @@ class DefaultCookiePolicy(CookiePolicy):
if cookie.version is None:
# 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.
debug(" Set-Cookie2 without version attribute (%s=%s)",
_
debug(" Set-Cookie2 without version attribute (%s=%s)",
cookie.name, cookie.value)
return False
if cookie.version > 0 and not self.rfc2965:
debug(" RFC 2965 cookies are switched off")
_
debug(" RFC 2965 cookies are switched off")
return False
elif cookie.version == 0 and not self.netscape:
debug(" Netscape cookies are switched off")
_
debug(" Netscape cookies are switched off")
return False
return True
def set_ok_verifiability(self, cookie, request):
if request.is_unverifiable() and is_third_party(request):
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")
return False
elif cookie.version == 0 and self.strict_ns_unverifiable:
debug(" third-party Netscape cookie during "
_
debug(" third-party Netscape cookie during "
"unverifiable transaction")
return False
return True
...
...
@@ -946,7 +957,7 @@ class DefaultCookiePolicy(CookiePolicy):
# servers that know both V0 and V1 protocols.
if (cookie.version == 0 and self.strict_ns_set_initial_dollar and
cookie.name.startswith("$")):
debug(" illegal name (starts with '$'): '%s'", cookie.name)
_
debug(" illegal name (starts with '$'): '%s'", cookie.name)
return False
return True
...
...
@@ -956,17 +967,17 @@ class DefaultCookiePolicy(CookiePolicy):
if ((cookie.version > 0 or
(cookie.version == 0 and self.strict_ns_set_path)) and
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)
return False
return True
def set_ok_domain(self, cookie, request):
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
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
if cookie.domain_specified:
req_host, erhn = eff_request_host(request)
...
...
@@ -985,7 +996,7 @@ class DefaultCookiePolicy(CookiePolicy):
"info", "jobs", "mobi", "museum", "name", "pro",
"travel", "eu") and len(tld) == 2:
# domain like .co.uk
debug(" country-code second level domain %s", domain)
_
debug(" country-code second level domain %s", domain)
return False
if domain.startswith("."):
undotted_domain = domain[1:]
...
...
@@ -993,21 +1004,21 @@ class DefaultCookiePolicy(CookiePolicy):
undotted_domain = domain
embedded_dots = (undotted_domain.find(".") >= 0)
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)
return False
if cookie.version == 0:
if (not erhn.endswith(domain) and
(not erhn.startswith(".") and
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",
erhn, domain)
return False
if (cookie.version > 0 or
(self.strict_ns_domain & self.DomainRFC2965Match)):
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)
return False
if (cookie.version > 0 or
...
...
@@ -1015,7 +1026,7 @@ class DefaultCookiePolicy(CookiePolicy):
host_prefix = req_host[:-len(domain)]
if (host_prefix.find(".") >= 0 and
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)
return False
return True
...
...
@@ -1031,12 +1042,12 @@ class DefaultCookiePolicy(CookiePolicy):
try:
int(p)
except ValueError:
debug(" bad port %s (not numeric)", p)
_
debug(" bad port %s (not numeric)", p)
return False
if p == req_port:
break
else:
debug(" request port (%s) not found in %s",
_
debug(" request port (%s) not found in %s",
req_port, cookie.port)
return False
return True
...
...
@@ -1050,7 +1061,7 @@ class DefaultCookiePolicy(CookiePolicy):
"""
# Path has already been checked by .path_return_ok(), and domain
# 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":
fn_name = "return_ok_"+n
...
...
@@ -1061,34 +1072,34 @@ class DefaultCookiePolicy(CookiePolicy):
def return_ok_version(self, cookie, request):
if cookie.version > 0 and not self.rfc2965:
debug(" RFC 2965 cookies are switched off")
_
debug(" RFC 2965 cookies are switched off")
return False
elif cookie.version == 0 and not self.netscape:
debug(" Netscape cookies are switched off")
_
debug(" Netscape cookies are switched off")
return False
return True
def return_ok_verifiability(self, cookie, request):
if request.is_unverifiable() and is_third_party(request):
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")
return False
elif cookie.version == 0 and self.strict_ns_unverifiable:
debug(" third-party Netscape cookie during unverifiable "
_
debug(" third-party Netscape cookie during unverifiable "
"transaction")
return False
return True
def return_ok_secure(self, cookie, request):
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 True
def return_ok_expires(self, cookie, request):
if cookie.is_expired(self._now):
debug(" cookie expired")
_
debug(" cookie expired")
return False
return True
...
...
@@ -1101,7 +1112,7 @@ class DefaultCookiePolicy(CookiePolicy):
if p == req_port:
break
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)
return False
return True
...
...
@@ -1114,16 +1125,16 @@ class DefaultCookiePolicy(CookiePolicy):
if (cookie.version == 0 and
(self.strict_ns_domain & self.DomainStrictNonDomain) and
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")
return False
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)
return False
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)
return False
return True
...
...
@@ -1137,24 +1148,24 @@ class DefaultCookiePolicy(CookiePolicy):
if not erhn.startswith("."):
erhn = "."+erhn
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)
return False
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
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 True
def path_return_ok(self, path, request):
debug("- checking cookie path=%s", path)
_
debug("- checking cookie path=%s", path)
req_path = request_path(request)
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 True
...
...
@@ -1216,7 +1227,7 @@ class CookieJar:
cookies = []
if not self._policy.domain_return_ok(domain, request):
return []
debug("Checking %s for cookies to return", domain)
_
debug("Checking %s for cookies to return", domain)
cookies_by_path = self._cookies[domain]
for path in cookies_by_path.keys():
if not self._policy.path_return_ok(path, request):
...
...
@@ -1224,9 +1235,9 @@ class CookieJar:
cookies_by_name = cookies_by_path[path]
for cookie in cookies_by_name.values():
if not self._policy.return_ok(cookie, request):
debug(" not returning cookie")
_
debug(" not returning cookie")
continue
debug(" it's a match")
_
debug(" it's a match")
cookies.append(cookie)
return cookies
...
...
@@ -1303,7 +1314,7 @@ class CookieJar:
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._policy._now = self._now = int(time.time())
...
...
@@ -1380,7 +1391,7 @@ class CookieJar:
continue
if k == "domain":
if v is None:
debug(" missing value for domain attribute")
_
debug(" missing value for domain attribute")
bad_cookie = True
break
# RFC 2965 section 3.3.3
...
...
@@ -1390,7 +1401,7 @@ class CookieJar:
# Prefer max-age to expires (like Mozilla)
continue
if v is None:
debug(" missing or invalid value for expires "
_
debug(" missing or invalid value for expires "
"attribute: treating as session cookie")
continue
if k == "max-age":
...
...
@@ -1398,7 +1409,7 @@ class CookieJar:
try:
v = int(v)
except ValueError:
debug(" missing or invalid (non-numeric) value for "
_
debug(" missing or invalid (non-numeric) value for "
"max-age attribute")
bad_cookie = True
break
...
...
@@ -1411,7 +1422,7 @@ class CookieJar:
if (k in value_attrs) or (k in boolean_attrs):
if (v is None and
k not in ("port", "comment", "commenturl")):
debug(" missing value for %s attribute" % k)
_
debug(" missing value for %s attribute" % k)
bad_cookie = True
break
standard[k] = v
...
...
@@ -1497,7 +1508,7 @@ class CookieJar:
self.clear(domain, path, name)
except KeyError:
pass
debug("Expiring cookie, domain='%s', path='%s', name='%s'",
_
debug("Expiring cookie, domain='%s', path='%s', name='%s'",
domain, path, name)
return None
...
...
@@ -1613,13 +1624,13 @@ class CookieJar:
def extract_cookies(self, response, 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._policy._now = self._now = int(time.time())
for cookie in self.make_cookies(response, request):
if self._policy.set_ok(cookie, request):
debug(" setting cookie: %s", cookie)
_
debug(" setting cookie: %s", cookie)
self.set_cookie(cookie)
self._cookies_lock.release()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment