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 <michele@acksyn.org>
Co-Authored-By: Alex Schultz <aschultz@redhat.com>
This commit is contained in:
Damien Ciabrini 2019-02-28 11:43:29 +01:00 committed by Alex Schultz
parent 813663bc03
commit ff19fdeb54

View File

@ -34,8 +34,8 @@ try:
import mimetools import mimetools
Message = mimetools.Message Message = mimetools.Message
except ImportError: except ImportError:
from email.mime import message import email
Message = message.MIMEMessage Message = email.message_from_binary_file
from zaqar.common import consts from zaqar.common import consts
@ -248,13 +248,16 @@ class NotificationProtocol(asyncio.Protocol):
self.write_status(b'405 Not Allowed') self.write_status(b'405 Not Allowed')
return return
self._state = 'HEADERS' 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: 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, self._data = self._data.split(b'\r\n\r\n', 1)
headers = Message(io.BytesIO(headers)) 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: if not length:
LOG.debug('Content-Length not provided in the data message')
self.write_status(b'400 Bad Request') self.write_status(b'400 Bad Request')
return return
self._length = int(length) self._length = int(length)