# HG changeset patch # User RhodeCode Admin # Date 2023-03-05 17:47:43 # Node ID cae6657c8c238ab2747970942933f59d19c93e77 # Parent 5306528bbaf038eb3b70d2730b4cd305ce7a419a hooks: support v2 hooks protocol using binary msgpack diff --git a/rhodecode/lib/hooks_daemon.py b/rhodecode/lib/hooks_daemon.py --- a/rhodecode/lib/hooks_daemon.py +++ b/rhodecode/lib/hooks_daemon.py @@ -25,7 +25,7 @@ import tempfile import traceback import threading import socket -import random +import msgpack from BaseHTTPServer import BaseHTTPRequestHandler from SocketServer import TCPServer @@ -45,7 +45,9 @@ log = logging.getLogger(__name__) class HooksHttpHandler(BaseHTTPRequestHandler): def do_POST(self): - method, extras = self._read_request() + hooks_proto, method, extras = self._read_request() + log.debug('Handling HooksHttpHandler %s with %s proto', method, hooks_proto) + txn_id = getattr(self.server, 'txn_id', None) if txn_id: log.debug('Computing TXN_ID based on `%s`:`%s`', @@ -68,19 +70,30 @@ class HooksHttpHandler(BaseHTTPRequestHa 'exception_traceback': exc_tb, 'exception_args': e.args } - self._write_response(result) + self._write_response(hooks_proto, result) def _read_request(self): length = int(self.headers['Content-Length']) - body = self.rfile.read(length).decode('utf-8') - data = json.loads(body) - return data['method'], data['extras'] + hooks_proto = self.headers.get('rc-hooks-protocol') or 'json.v1' + if hooks_proto == 'msgpack.v1': + # support for new vcsserver msgpack based protocol hooks + data = msgpack.unpackb(self.rfile.read(length), raw=False) + else: + body = self.rfile.read(length).decode('utf-8') + data = json.loads(body) + + return hooks_proto, data['method'], data['extras'] - def _write_response(self, result): + def _write_response(self, hooks_proto, result): self.send_response(200) - self.send_header("Content-type", "text/json") - self.end_headers() - self.wfile.write(json.dumps(result)) + if hooks_proto == 'msgpack.v1': + self.send_header("Content-type", "application/msgpack") + self.end_headers() + self.wfile.write(msgpack.packb(result)) + else: + self.send_header("Content-type", "text/json") + self.end_headers() + self.wfile.write(json.dumps(result)) def _call_hook_method(self, hooks, method, extras): try: