| 344 | | for key, value in self.iteritems(): |
|---|
| 345 | | if not isinstance(value, list): |
|---|
| 346 | | value = [value] |
|---|
| 347 | | for v in value: |
|---|
| 348 | | if isinstance(v, unicode): |
|---|
| 349 | | # HTTP/1.0 says, "Words of *TEXT may contain octets |
|---|
| 350 | | # from character sets other than US-ASCII." and |
|---|
| 351 | | # "Recipients of header field TEXT containing octets |
|---|
| 352 | | # outside the US-ASCII character set may assume that |
|---|
| 353 | | # they represent ISO-8859-1 characters." |
|---|
| 354 | | try: |
|---|
| 355 | | v = v.encode("iso-8859-1") |
|---|
| 356 | | except UnicodeEncodeError: |
|---|
| 357 | | if protocol >= (1, 1): |
|---|
| 358 | | # Encode RFC-2047 TEXT |
|---|
| 359 | | # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?="). |
|---|
| 360 | | v = Header(v, 'utf-8').encode() |
|---|
| 361 | | else: |
|---|
| 362 | | raise |
|---|
| 363 | | else: |
|---|
| 364 | | # This coercion should not take any time at all |
|---|
| 365 | | # if value is already of type "str". |
|---|
| 366 | | v = str(v) |
|---|
| 367 | | header_list.append((key, v)) |
|---|
| | 344 | for key, v in self.iteritems(): |
|---|
| | 345 | if isinstance(v, unicode): |
|---|
| | 346 | # HTTP/1.0 says, "Words of *TEXT may contain octets |
|---|
| | 347 | # from character sets other than US-ASCII." and |
|---|
| | 348 | # "Recipients of header field TEXT containing octets |
|---|
| | 349 | # outside the US-ASCII character set may assume that |
|---|
| | 350 | # they represent ISO-8859-1 characters." |
|---|
| | 351 | try: |
|---|
| | 352 | v = v.encode("iso-8859-1") |
|---|
| | 353 | except UnicodeEncodeError: |
|---|
| | 354 | if protocol >= (1, 1): |
|---|
| | 355 | # Encode RFC-2047 TEXT |
|---|
| | 356 | # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?="). |
|---|
| | 357 | v = Header(v, 'utf-8').encode() |
|---|
| | 358 | else: |
|---|
| | 359 | raise |
|---|
| | 360 | else: |
|---|
| | 361 | # This coercion should not take any time at all |
|---|
| | 362 | # if value is already of type "str". |
|---|
| | 363 | v = str(v) |
|---|
| | 364 | header_list.append((key, v)) |
|---|