From ce0a403490e7b5cbbbb559d74da352fa493150b9 Mon Sep 17 00:00:00 2001 From: Jack Deeth Date: Thu, 11 Mar 2021 18:28:00 +0000 Subject: [PATCH] Add option to use CONNECT command for 1.1, 1.2 The STOMP spec indicates that 1.1 and 1.2 clients should be able to connect with the CONNECT command if needed for backward compatibility. This adds a keyword argument to the `connect` methods of both protocols to let the user select STOMP or CONNECT. STOMP remains the default. --- stomp/protocol.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/stomp/protocol.py b/stomp/protocol.py index eb552095..a857efb2 100644 --- a/stomp/protocol.py +++ b/stomp/protocol.py @@ -236,7 +236,7 @@ def send_frame(self, cmd, headers=None, body=''): :param dict headers: a map of headers to include in the frame :param body: the content of the message """ - if cmd != CMD_CONNECT: + if cmd not in [CMD_CONNECT, CMD_STOMP]: if headers is None: headers = {} self._escape_headers(headers) @@ -306,7 +306,8 @@ def commit(self, transaction=None, headers=None, **keyword_headers): headers[HDR_TRANSACTION] = transaction self.send_frame(CMD_COMMIT, headers) - def connect(self, username=None, passcode=None, wait=False, headers=None, **keyword_headers): + def connect(self, username=None, passcode=None, wait=False, headers=None, + with_connect_command=False, **keyword_headers): """ Start a connection. @@ -314,9 +315,10 @@ def connect(self, username=None, passcode=None, wait=False, headers=None, **keyw :param str passcode: the password used to authenticate with :param bool wait: if True, wait for the connection to be established/acknowledged :param dict headers: a map of any additional headers the broker requires + :param with_connect_command: if True, use CONNECT command instead of STOMP :param keyword_headers: any additional headers the broker requires """ - cmd = CMD_STOMP + cmd = CMD_CONNECT if with_connect_command else CMD_STOMP headers = utils.merge_headers([headers, keyword_headers]) headers[HDR_ACCEPT_VERSION] = self.version @@ -487,7 +489,8 @@ def nack(self, id, transaction=None, receipt=None, **keyword_headers): headers[HDR_RECEIPT] = receipt self.send_frame(CMD_NACK, headers) - def connect(self, username=None, passcode=None, wait=False, headers=None, **keyword_headers): + def connect(self, username=None, passcode=None, wait=False, headers=None, + with_connect_command=False, **keyword_headers): """ Send a STOMP CONNECT frame. Differs from 1.0 and 1.1 versions in that the HOST header is enforced. @@ -495,9 +498,10 @@ def connect(self, username=None, passcode=None, wait=False, headers=None, **keyw :param str passcode: optionally specify the user password :param bool wait: wait for the connection to complete before returning :param dict headers: a map of any additional headers to send with the subscription + :param with_connect_command: if True, use CONNECT command instead of STOMP :param keyword_headers: any additional headers to send with the subscription """ - cmd = CMD_STOMP + cmd = CMD_CONNECT if with_connect_command else CMD_STOMP headers = utils.merge_headers([headers, keyword_headers]) headers[HDR_ACCEPT_VERSION] = self.version headers[HDR_HOST] = self.transport.current_host_and_port[0]