MCPcopy Index your code
hub / github.com/python/cpython / _cookie_attrs

Method _cookie_attrs

Lib/http/cookiejar.py:1298–1355  ·  view source on GitHub ↗

Return a list of cookie-attributes to be returned to server. like ['foo="bar"; $Path="/"', ...] The $Version attribute is also added when appropriate (currently only once per request).

(self, cookies)

Source from the content-addressed store, hash-verified

1296 return cookies
1297
1298 def _cookie_attrs(self, cookies):
1299 """Return a list of cookie-attributes to be returned to server.
1300
1301 like ['foo="bar"; $Path="/"', ...]
1302
1303 The $Version attribute is also added when appropriate (currently only
1304 once per request).
1305
1306 """
1307 # add cookies in order of most specific (ie. longest) path first
1308 cookies.sort(key=lambda a: len(a.path), reverse=True)
1309
1310 version_set = False
1311
1312 attrs = []
1313 for cookie in cookies:
1314 # set version of Cookie header
1315 # XXX
1316 # What should it be if multiple matching Set-Cookie headers have
1317 # different versions themselves?
1318 # Answer: there is no answer; was supposed to be settled by
1319 # RFC 2965 errata, but that may never appear...
1320 version = cookie.version
1321 if not version_set:
1322 version_set = True
1323 if version > 0:
1324 attrs.append("$Version=%s" % version)
1325
1326 # quote cookie value if necessary
1327 # (not for Netscape protocol, which already has any quotes
1328 # intact, due to the poorly-specified Netscape Cookie: syntax)
1329 if ((cookie.value is not None) and
1330 self.non_word_re.search(cookie.value) and version > 0):
1331 value = self.quote_re.sub(r"\\\1", cookie.value)
1332 else:
1333 value = cookie.value
1334
1335 # add cookie-attributes to be returned in Cookie header
1336 if cookie.value is None:
1337 attrs.append(cookie.name)
1338 else:
1339 attrs.append("%s=%s" % (cookie.name, value))
1340 if version > 0:
1341 if cookie.path_specified:
1342 attrs.append('$Path="%s"' % cookie.path)
1343 if cookie.domain.startswith("."):
1344 domain = cookie.domain
1345 if (not cookie.domain_initial_dot and
1346 domain.startswith(".")):
1347 domain = domain[1:]
1348 attrs.append('$Domain="%s"' % domain)
1349 if cookie.port is not None:
1350 p = "$Port"
1351 if cookie.port_specified:
1352 p = p + ('="%s"' % cookie.port)
1353 attrs.append(p)
1354
1355 return attrs

Callers 1

add_cookie_headerMethod · 0.95

Calls 5

sortMethod · 0.45
appendMethod · 0.45
searchMethod · 0.45
subMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected