# HG changeset patch # User Gregory Szorc # Date 1535504708 25200 # Node ID 36f487a332ad0e086dea513b5b25f9ba09af6339 # Parent cdb56f295b031ace8a12bc5a08a6e8a4e2da6a66 wireprotoframing: use our CBOR module Tests changed because our CBOR encoder appears to sort map keys differently from the vendored CBOR package. The CBOR specification does define canonical sorting rules for keys based on the byte values. I'm guessing our implementation doesn't follow them. But our encoder doesn't guarantee that it conforms with the canonical specification. Right now, we just care that output is deterministic. And our encoder does guarantee that. Differential Revision: https://phab.mercurial-scm.org/D4466 diff -r cdb56f295b03 -r 36f487a332ad mercurial/wireprotoframing.py --- a/mercurial/wireprotoframing.py Tue Aug 28 15:10:56 2018 -0700 +++ b/mercurial/wireprotoframing.py Tue Aug 28 18:05:08 2018 -0700 @@ -17,7 +17,6 @@ from .i18n import _ from .thirdparty import ( attr, - cbor, ) from . import ( encoding, @@ -25,6 +24,7 @@ util, ) from .utils import ( + cborutil, stringutil, ) @@ -217,8 +217,8 @@ finalflags |= int(flag) if payload.startswith(b'cbor:'): - payload = cbor.dumps(stringutil.evalpythonliteral(payload[5:]), - canonical=True) + payload = b''.join(cborutil.streamencode( + stringutil.evalpythonliteral(payload[5:]))) else: payload = stringutil.unescapestr(payload) @@ -289,7 +289,7 @@ if args: data[b'args'] = args - data = cbor.dumps(data, canonical=True) + data = b''.join(cborutil.streamencode(data)) offset = 0 @@ -347,7 +347,7 @@ Returns a generator of bytearrays. """ # Automatically send the overall CBOR response map. - overall = cbor.dumps({b'status': b'ok'}, canonical=True) + overall = b''.join(cborutil.streamencode({b'status': b'ok'})) if len(overall) > maxframesize: raise error.ProgrammingError('not yet implemented') @@ -388,7 +388,7 @@ def createbytesresponseframesfromgen(stream, requestid, gen, maxframesize=DEFAULT_MAX_FRAME_SIZE): - overall = cbor.dumps({b'status': b'ok'}, canonical=True) + overall = b''.join(cborutil.streamencode({b'status': b'ok'})) yield stream.makeframe(requestid=requestid, typeid=FRAME_TYPE_COMMAND_RESPONSE, @@ -429,7 +429,7 @@ if args: m[b'error'][b'args'] = args - overall = cbor.dumps(m, canonical=True) + overall = b''.join(cborutil.streamencode(m)) yield stream.makeframe(requestid=requestid, typeid=FRAME_TYPE_COMMAND_RESPONSE, @@ -440,10 +440,10 @@ # TODO properly handle frame size limits. assert len(msg) <= DEFAULT_MAX_FRAME_SIZE - payload = cbor.dumps({ + payload = b''.join(cborutil.streamencode({ b'type': errtype, b'message': [{b'msg': msg}], - }, canonical=True) + })) yield stream.makeframe(requestid=requestid, typeid=FRAME_TYPE_ERROR_RESPONSE, @@ -493,7 +493,7 @@ atomdicts.append(atom) - payload = cbor.dumps(atomdicts, canonical=True) + payload = b''.join(cborutil.streamencode(atomdicts)) if len(payload) > maxframesize: raise ValueError('cannot encode data in a single frame') @@ -784,7 +784,7 @@ # Decode the payloads as CBOR. entry['payload'].seek(0) - request = cbor.load(entry['payload']) + request = cborutil.decodeall(entry['payload'].getvalue())[0] if b'name' not in request: self._state = 'errored' @@ -1158,7 +1158,7 @@ del self._activerequests[request.requestid] # The payload should be a CBOR map. - m = cbor.loads(frame.payload) + m = cborutil.decodeall(frame.payload)[0] return 'error', { 'request': request, diff -r cdb56f295b03 -r 36f487a332ad tests/test-http-api-httpv2.t --- a/tests/test-http-api-httpv2.t Tue Aug 28 15:10:56 2018 -0700 +++ b/tests/test-http-api-httpv2.t Tue Aug 28 18:05:08 2018 -0700 @@ -406,7 +406,7 @@ s> content-length: 47\r\n s> host: $LOCALIP:$HGPORT\r\n (glob) s> \r\n - s> \'\x00\x00\x01\x00\x01\x01\x11\xa2Dargs\xa2CfooDval1Dbar1CvalDnameHcommand1 + s> \'\x00\x00\x01\x00\x01\x01\x11\xa2Dargs\xa2Dbar1CvalCfooDval1DnameHcommand1 s> makefile('rb', None) s> HTTP/1.1 200 OK\r\n s> Server: testing stub value\r\n @@ -414,7 +414,7 @@ s> Content-Type: text/plain\r\n s> Content-Length: 205\r\n s> \r\n - s> received: 1 1 1 \xa2Dargs\xa2CfooDval1Dbar1CvalDnameHcommand1\n + s> received: 1 1 1 \xa2Dargs\xa2Dbar1CvalCfooDval1DnameHcommand1\n s> ["runcommand", {"args": {"bar1": "val", "foo": "val1"}, "command": "command1", "data": null, "requestid": 1}]\n s> received: \n s> {"action": "noop"} diff -r cdb56f295b03 -r 36f487a332ad tests/test-wireproto-command-pushkey.t --- a/tests/test-wireproto-command-pushkey.t Tue Aug 28 15:10:56 2018 -0700 +++ b/tests/test-wireproto-command-pushkey.t Tue Aug 28 18:05:08 2018 -0700 @@ -38,7 +38,7 @@ s> host: $LOCALIP:$HGPORT\r\n (glob) s> user-agent: Mercurial debugwireproto\r\n s> \r\n - s> a\x00\x00\x01\x00\x01\x01\x11\xa2Dargs\xa4CkeyA@CnewX(426bada5c67598ca65036d57d9e4b64b0c1ce7a0Cold@InamespaceIbookmarksDnameGpushkey + s> a\x00\x00\x01\x00\x01\x01\x11\xa2Dargs\xa4CkeyA@InamespaceIbookmarksCnewX(426bada5c67598ca65036d57d9e4b64b0c1ce7a0Cold@DnameGpushkey s> makefile('rb', None) s> HTTP/1.1 200 OK\r\n s> Server: testing stub value\r\n