GeezerVison is showing github.com — gh-130631: Make join_header_words() more similar to the original Perl… · serhiy-storchaka/cpython@7ebbd27
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/http/cookiejar.py +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -430,25 +430,26 @@ def split_header_words(header_values): | 430 | 430 | if pairs: result.append(pairs) | 431 | 431 | return result | 432 | 432 |
| | 433 | +HEADER_JOIN_TOKEN_RE = re.compile(r"[!#$%&'*+\-.^_`|~0-9A-Za-z]+") | 433 | 434 | HEADER_JOIN_ESCAPE_RE = re.compile(r"([\"\\])") | 434 | 435 | def join_header_words(lists): | 435 | 436 | """Do the inverse (almost) of the conversion done by split_header_words. | 436 | 437 |
| 437 | 438 | Takes a list of lists of (key, value) pairs and produces a single header | 438 | 439 | value. Attribute values are quoted if needed. | 439 | 440 |
| 440 | | - >>> join_header_words([[("text/plain", None), ("charset", "iso-8859-1")]]) | 441 | | - 'text/plain; charset="iso-8859-1"' | 442 | | - >>> join_header_words([[("text/plain", None)], [("charset", "iso-8859-1")]]) | 443 | | - 'text/plain, charset="iso-8859-1"' | | 441 | + >>> join_header_words([[("text/plain", None), ("charset", "iso-8859/1")]]) | | 442 | + 'text/plain; charset="iso-8859/1"' | | 443 | + >>> join_header_words([[("text/plain", None)], [("charset", "iso-8859/1")]]) | | 444 | + 'text/plain, charset="iso-8859/1"' | 444 | 445 |
| 445 | 446 | """ | 446 | 447 | headers = [] | 447 | 448 | for pairs in lists: | 448 | 449 | attr = [] | 449 | 450 | for k, v in pairs: | 450 | 451 | if v is not None: | 451 | | - if not re.search(r"^\w+$", v): | | 452 | + if not HEADER_JOIN_TOKEN_RE.fullmatch(v): | 452 | 453 | v = HEADER_JOIN_ESCAPE_RE.sub(r"\\\1", v) # escape " and \ | 453 | 454 | v = '"%s"' % v | 454 | 455 | k = "%s=%s" % (k, v) | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_http_cookiejar.py +14-4Lines changed: 14 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -285,20 +285,30 @@ def test_roundtrip(self): | 285 | 285 | ("foo=bar;bar=baz", "foo=bar; bar=baz"), | 286 | 286 | ('foo bar baz', "foo; bar; baz"), | 287 | 287 | (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'), | | 288 | + ("föo=bär", 'föo="bär"'), | 288 | 289 | ('foo,,,bar', 'foo, bar'), | 289 | 290 | ('foo=bar,bar=baz', 'foo=bar, bar=baz'), | | 291 | + ("foo=\n", 'foo=""'), | | 292 | + ('foo="\n"', 'foo="\n"'), | | 293 | + ('foo=bar\n', 'foo=bar'), | | 294 | + ('foo="bar\n"', 'foo="bar\n"'), | | 295 | + ('foo=bar\nbaz', 'foo=bar; baz'), | | 296 | + ('foo="bar\nbaz"', 'foo="bar\nbaz"'), | 290 | 297 |
| 291 | 298 | ('text/html; charset=iso-8859-1', | 292 | | - 'text/html; charset="iso-8859-1"'), | | 299 | + 'text/html; charset=iso-8859-1'), | | 300 | + | | 301 | + ('text/html; charset="iso-8859/1"', | | 302 | + 'text/html; charset="iso-8859/1"'), | 293 | 303 |
| 294 | 304 | ('foo="bar"; port="80,81"; discard, bar=baz', | 295 | 305 | 'foo=bar; port="80,81"; discard, bar=baz'), | 296 | 306 |
| 297 | 307 | (r'Basic realm="\"foo\\\\bar\""', | 298 | 308 | r'Basic; realm="\"foo\\\\bar\""'), | 299 | 309 |
| 300 | | - ('n; foo="foo;_", bar=foo!_', | 301 | | - 'n; foo="foo;_", bar="foo!_"'), | | 310 | + ('n; foo="foo;_", bar="foo,_"', | | 311 | + 'n; foo="foo;_", bar="foo,_"'), | 302 | 312 | ] | 303 | 313 |
| 304 | 314 | for arg, expect in tests: | @@ -553,7 +563,7 @@ def test_missing_value(self): | 553 | 563 | self.assertIsNone(cookie.value) | 554 | 564 | self.assertEqual(cookie.name, '"spam"') | 555 | 565 | self.assertEqual(lwp_cookie_str(cookie), ( | 556 | | - r'"spam"; path="/foo/"; domain="www.acme.com"; ' | | 566 | + r'"spam"; path="/foo/"; domain=www.acme.com; ' | 557 | 567 | 'path_spec; discard; version=0')) | 558 | 568 | old_str = repr(c) | 559 | 569 | c.save(ignore_expires=True, ignore_discard=True) | |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +:func:`!http.cookiejar.join_header_words` is now more similar to the original | | 2 | +Perl version. It now quotes the same set of characters and always quote | | 3 | +values that end with ``"\n"``. |
|