Mercurial > hg
comparison mercurial/httppeer.py @ 34732:67e9678efd98
httppeer: always produce native str header keys and values
Differential Revision: https://phab.mercurial-scm.org/D1103
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 15 Oct 2017 00:03:31 -0400 |
parents | a288712d86d5 |
children | bfd072c52e03 |
comparison
equal
deleted
inserted
replaced
34731:31fdd0509de9 | 34732:67e9678efd98 |
---|---|
37 | 37 |
38 ``value`` will be encoded into 1 or more HTTP headers with the names | 38 ``value`` will be encoded into 1 or more HTTP headers with the names |
39 ``header-<N>`` where ``<N>`` is an integer starting at 1. Each header | 39 ``header-<N>`` where ``<N>`` is an integer starting at 1. Each header |
40 name + value will be at most ``limit`` bytes long. | 40 name + value will be at most ``limit`` bytes long. |
41 | 41 |
42 Returns an iterable of 2-tuples consisting of header names and values. | 42 Returns an iterable of 2-tuples consisting of header names and |
43 values as native strings. | |
43 """ | 44 """ |
44 fmt = header + '-%s' | 45 # HTTP Headers are ASCII. Python 3 requires them to be unicodes, |
45 valuelen = limit - len(fmt % '000') - len(': \r\n') | 46 # not bytes. This function always takes bytes in as arguments. |
47 fmt = pycompat.strurl(header) + r'-%s' | |
48 # Note: it is *NOT* a bug that the last bit here is a bytestring | |
49 # and not a unicode: we're just getting the encoded length anyway, | |
50 # and using an r-string to make it portable between Python 2 and 3 | |
51 # doesn't work because then the \r is a literal backslash-r | |
52 # instead of a carriage return. | |
53 valuelen = limit - len(fmt % r'000') - len(': \r\n') | |
46 result = [] | 54 result = [] |
47 | 55 |
48 n = 0 | 56 n = 0 |
49 for i in xrange(0, len(value), valuelen): | 57 for i in xrange(0, len(value), valuelen): |
50 n += 1 | 58 n += 1 |
51 result.append((fmt % str(n), value[i:i + valuelen])) | 59 result.append((fmt % str(n), pycompat.strurl(value[i:i + valuelen]))) |
52 | 60 |
53 return result | 61 return result |
54 | 62 |
55 def _wraphttpresponse(resp): | 63 def _wraphttpresponse(resp): |
56 """Wrap an HTTPResponse with common error handlers. | 64 """Wrap an HTTPResponse with common error handlers. |