comparison mercurial/httppeer.py @ 30759:3f5f0c98cd18

httppeer: extract code for HTTP header spanning A second consumer of HTTP header spanning will soon be introduced. Factor out the code to do this so it can be reused.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 24 Dec 2016 14:46:02 -0700
parents 07bcd1bf6151
children a520aefb96f1
comparison
equal deleted inserted replaced
30758:76104a4899ad 30759:3f5f0c98cd18
50 except httplib.HTTPException: 50 except httplib.HTTPException:
51 raise IOError(None, _('connection ended unexpectedly')) 51 raise IOError(None, _('connection ended unexpectedly'))
52 52
53 reader.__class__ = readerproxy 53 reader.__class__ = readerproxy
54 return reader 54 return reader
55
56 def encodevalueinheaders(value, header, limit):
57 """Encode a string value into multiple HTTP headers.
58
59 ``value`` will be encoded into 1 or more HTTP headers with the names
60 ``header-<N>`` where ``<N>`` is an integer starting at 1. Each header
61 name + value will be at most ``limit`` bytes long.
62
63 Returns an iterable of 2-tuples consisting of header names and values.
64 """
65 fmt = header + '-%s'
66 valuelen = limit - len(fmt % '000') - len(': \r\n')
67 result = []
68
69 n = 0
70 for i in xrange(0, len(value), valuelen):
71 n += 1
72 result.append((fmt % str(n), value[i:i + valuelen]))
73
74 return result
55 75
56 class httppeer(wireproto.wirepeer): 76 class httppeer(wireproto.wirepeer):
57 def __init__(self, ui, path): 77 def __init__(self, ui, path):
58 self.path = path 78 self.path = path
59 self.caps = None 79 self.caps = None
133 if httpheader: 153 if httpheader:
134 headersize = int(httpheader.split(',', 1)[0]) 154 headersize = int(httpheader.split(',', 1)[0])
135 if headersize > 0: 155 if headersize > 0:
136 # The headers can typically carry more data than the URL. 156 # The headers can typically carry more data than the URL.
137 encargs = urlreq.urlencode(sorted(args.items())) 157 encargs = urlreq.urlencode(sorted(args.items()))
138 headerfmt = 'X-HgArg-%s' 158 for header, value in encodevalueinheaders(encargs, 'X-HgArg',
139 contentlen = headersize - len(headerfmt % '000' + ': \r\n') 159 headersize):
140 headernum = 0 160 headers[header] = value
141 for i in xrange(0, len(encargs), contentlen):
142 headernum += 1
143 header = headerfmt % str(headernum)
144 headers[header] = encargs[i:i + contentlen]
145 varyheaders.append(header) 161 varyheaders.append(header)
146 else: 162 else:
147 q += sorted(args.items()) 163 q += sorted(args.items())
148 qs = '?%s' % urlreq.urlencode(q) 164 qs = '?%s' % urlreq.urlencode(q)
149 cu = "%s%s" % (self._url, qs) 165 cu = "%s%s" % (self._url, qs)