From ff19fdeb5452f19d8420cfb041aa846bdf58c84e Mon Sep 17 00:00:00 2001 From: Damien Ciabrini Date: Thu, 28 Feb 2019 11:43:29 +0100 Subject: [PATCH] Python 3: Fix parsing of received notification The parsing of incoming notifications is performed with a helper which normally resolves to a mimetools class. For Python3, package mimetools doesn't exist anymore, and the helper resolves to a class with incompatible signature. In order to mimick the original helper behaviour and return a message object as expected, use a different facility from package email. Change-Id: I92f10755f1a98239df3b3a1494207299a3681a84 Closes-Bug: #1818046 Co-Authored-By: Michele Baldessari Co-Authored-By: Alex Schultz --- zaqar/transport/websocket/protocol.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/zaqar/transport/websocket/protocol.py b/zaqar/transport/websocket/protocol.py index 10dfc6c90..10f642aaa 100644 --- a/zaqar/transport/websocket/protocol.py +++ b/zaqar/transport/websocket/protocol.py @@ -34,8 +34,8 @@ try: import mimetools Message = mimetools.Message except ImportError: - from email.mime import message - Message = message.MIMEMessage + import email + Message = email.message_from_binary_file from zaqar.common import consts @@ -248,13 +248,16 @@ class NotificationProtocol(asyncio.Protocol): self.write_status(b'405 Not Allowed') return self._state = 'HEADERS' - self._subscriber_id = uri[1:] + self._subscriber_id = uri[1:].decode('utf-8') if self._state == 'HEADERS' and b'\r\n\r\n' in self._data: headers, self._data = self._data.split(b'\r\n\r\n', 1) headers = Message(io.BytesIO(headers)) - length = headers.get(b'content-length') + # try both cases of content-length for backwards compatibility + length = headers.get(b'content-length', + headers.get('Content-Length')) if not length: + LOG.debug('Content-Length not provided in the data message') self.write_status(b'400 Bad Request') return self._length = int(length)