Return the given header name or value, encoded for HTTP output.
(cls, v)
| 466 | |
| 467 | @classmethod |
| 468 | def encode(cls, v): |
| 469 | """Return the given header name or value, encoded for HTTP output.""" |
| 470 | for enc in cls.encodings: |
| 471 | try: |
| 472 | return v.encode(enc) |
| 473 | except UnicodeEncodeError: |
| 474 | continue |
| 475 | |
| 476 | if cls.protocol == (1, 1) and cls.use_rfc_2047: |
| 477 | # Encode RFC-2047 TEXT |
| 478 | # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?="). |
| 479 | # We do our own here instead of using the email module |
| 480 | # because we never want to fold lines--folding has |
| 481 | # been deprecated by the HTTP working group. |
| 482 | v = b2a_base64(v.encode('utf-8')) |
| 483 | return (b'=?utf-8?b?' + v.strip(b'\n') + b'?=') |
| 484 | |
| 485 | raise ValueError('Could not encode header part %r using ' |
| 486 | 'any of the encodings %r.' % |
| 487 | (v, cls.encodings)) |
| 488 | |
| 489 | |
| 490 | class Host(object): |
no outgoing calls