mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-19 20:55:14 +02:00
Base server complete including modules, STOMP protocol over Websockets
and ZMQ Router/Dealer implementations.
This commit is contained in:
1 parent
58f93edaf7
commit
f4e51ea74f
20 files changed
+1153
-170
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,159 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2013 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
STOMP frame representation, decoding and encoding
|
||||
http://stomp.github.io/stomp-specification-1.2.html
|
||||
Adapted from Jason R. Briggs's code
|
||||
https://github.com/jasonrbriggs/stomp.py
|
||||
"""
|
||||
|
||||
import re
|
||||
from .utils import encode
|
||||
|
||||
|
||||
class Frame(object):
|
||||
"""
|
||||
A STOMP frame. Comprises a command, the headers and the body content.
|
||||
"""
|
||||
|
||||
# Used to parse STOMP header lines in the format "key:value",
|
||||
HEADER_LINE_RE = re.compile('(?P<key>[^:]+)[:](?P<value>.*)')
|
||||
# As of STOMP 1.2, lines can end with either line feed, or carriage return plus line feed.
|
||||
PREAMBLE_END_RE = re.compile('\n\n|\r\n\r\n')
|
||||
# As of STOMP 1.2, lines can end with either line feed, or carriage return plus line feed.
|
||||
LINE_END_RE = re.compile('\n|\r\n')
|
||||
# NULL value
|
||||
NULL = b'\x00'
|
||||
|
||||
def __init__(self, cmd=None, headers={}, body=None):
|
||||
self._cmd = cmd
|
||||
self._headers = headers
|
||||
self._body = body
|
||||
|
||||
@property
|
||||
def cmd(self):
|
||||
|
||||
return(self._cmd)
|
||||
|
||||
@cmd.setter
|
||||
def cmd(self, cmd):
|
||||
|
||||
self._cmd = cmd
|
||||
|
||||
@property
|
||||
def headers(self):
|
||||
|
||||
return(self._headers)
|
||||
|
||||
@headers.setter
|
||||
def headers(self, headers):
|
||||
|
||||
self._headers = headers
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
|
||||
return(self._body)
|
||||
|
||||
@body.setter
|
||||
def body(self, body):
|
||||
|
||||
self._body = body
|
||||
|
||||
def encode(self):
|
||||
"""
|
||||
Encodes this frame to be send on the wire
|
||||
"""
|
||||
|
||||
lines = []
|
||||
if self._cmd:
|
||||
lines.append(self._cmd)
|
||||
lines.append("\n")
|
||||
for key, vals in sorted(self._headers.items()):
|
||||
if type(vals) != tuple:
|
||||
vals = (vals,)
|
||||
for val in vals:
|
||||
lines.append("%s:%s\n" % (key, val))
|
||||
lines.append("\n")
|
||||
if self._body:
|
||||
lines.append(self._body)
|
||||
|
||||
if self._cmd:
|
||||
lines.append(self.NULL)
|
||||
|
||||
encoded_lines = (encode(line) for line in lines)
|
||||
return b''.join(encoded_lines)
|
||||
|
||||
@classmethod
|
||||
def parse_headers(cls, lines, offset=0):
|
||||
"""
|
||||
Parses frame headers
|
||||
|
||||
:param lines: Frame preamble lines
|
||||
:param offset: To start parsing at the given offset
|
||||
:returns: Headers in dict header:value
|
||||
"""
|
||||
|
||||
headers = {}
|
||||
for header_line in lines[offset:]:
|
||||
header_match = cls.HEADER_LINE_RE.match(header_line)
|
||||
if header_match:
|
||||
key = header_match.group('key')
|
||||
if key not in headers:
|
||||
headers[key] = header_match.group('value')
|
||||
return headers
|
||||
|
||||
@classmethod
|
||||
def parse_frame(cls, frame):
|
||||
"""
|
||||
Parses a frame
|
||||
|
||||
:params frame: The frame data to be parsed
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
f = Frame()
|
||||
# End-of-line (EOL) indicates an heart beat frame
|
||||
if frame == '\x0a':
|
||||
f.cmd = 'heartbeat' # This will have the frame ignored
|
||||
return f
|
||||
|
||||
mat = cls.PREAMBLE_END_RE.search(frame)
|
||||
preamble_end = -1
|
||||
if mat:
|
||||
preamble_end = mat.start()
|
||||
if preamble_end == -1:
|
||||
preamble_end = len(frame)
|
||||
preamble = frame[0:preamble_end]
|
||||
preamble_lines = cls.LINE_END_RE.split(preamble)
|
||||
f.body = frame[preamble_end + 2:]
|
||||
if f.body[-1] == '\x00':
|
||||
f.body = f.body[:-1]
|
||||
|
||||
# Skip any leading newlines
|
||||
first_line = 0
|
||||
while first_line < len(preamble_lines) and len(preamble_lines[first_line]) == 0:
|
||||
first_line += 1
|
||||
|
||||
# Extract frame type/command
|
||||
f.cmd = preamble_lines[first_line]
|
||||
|
||||
# Put headers into a key/value map
|
||||
f.headers = cls.parse_headers(preamble_lines, first_line + 1)
|
||||
|
||||
return f
|
||||
@@ -0,0 +1,227 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2013 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Basic STOMP 1.2 protocol implementation
|
||||
http://stomp.github.io/stomp-specification-1.2.html
|
||||
"""
|
||||
|
||||
import uuid
|
||||
from .frame import Frame
|
||||
from .utils import encode, hasbyte
|
||||
|
||||
# Commands server-side
|
||||
CMD_CONNECTED = 'CONNECTED'
|
||||
CMD_ERROR = 'ERROR'
|
||||
CMD_MESSAGE = 'MESSAGE'
|
||||
CMD_RECEIPT = 'RECEIPT'
|
||||
|
||||
# Commands client-side
|
||||
CMD_STOMP = 'STOMP'
|
||||
CMD_CONNECT = 'CONNECT'
|
||||
CMD_DISCONNECT = 'DISCONNECT'
|
||||
CMD_SEND = 'SEND'
|
||||
|
||||
# Commands not supported
|
||||
CMD_SUBSCRIBE = 'SUBSCRIBE'
|
||||
CMD_UNSUBSCRIBE = 'UNSUBSCRIBE'
|
||||
CMD_ACK = 'ACK'
|
||||
CMD_NACK = 'NACK'
|
||||
CMD_BEGIN = 'BEGIN'
|
||||
CMD_ABORT = 'ABORT'
|
||||
|
||||
# Headers
|
||||
HDR_VERSION = 'version'
|
||||
HDR_SESSION = 'session'
|
||||
HDR_SERVER = 'server'
|
||||
HDR_CONTENT_TYPE = 'content-type'
|
||||
HDR_CONTENT_LENGTH = 'content-length'
|
||||
HDR_RECEIPT_ID = 'receipt-id'
|
||||
HDR_MESSAGE = 'message'
|
||||
HDR_MESSAGE_ID = 'message-id'
|
||||
HDR_ACCEPT_VERSION = 'accept-version'
|
||||
HDR_HOST = 'host'
|
||||
HDR_DESTINATION = 'destination'
|
||||
HDR_RECEIPT = 'receipt'
|
||||
|
||||
# Headers not supported
|
||||
HDR_HEARTBEAT = 'heart-beat'
|
||||
HDR_LOGIN = 'login'
|
||||
HDR_PASSCODE = 'passcode'
|
||||
HDR_ID = 'id'
|
||||
HDR_ACK = 'ack'
|
||||
HDR_SUBSCRIPTION = 'subscription'
|
||||
HDR_TRANSACTION = 'transaction'
|
||||
|
||||
|
||||
class serverProtocol(object):
|
||||
"""
|
||||
STOMP 1.2 protocol support for servers.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
# STOMP protocol version
|
||||
self.version = 1.2
|
||||
|
||||
def connected(self, session=None, server=None):
|
||||
"""
|
||||
Replies to the CONNECT or STOMP command.
|
||||
Heart-beat header is not supported.
|
||||
|
||||
:param session: A session identifier that uniquely identifies the session.
|
||||
:param server: A field that contains information about the STOMP server.
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
# Version header is required
|
||||
headers = {HDR_VERSION: self.version}
|
||||
|
||||
if session:
|
||||
headers[HDR_SESSION] = session
|
||||
|
||||
# The server-name field consists of a name token followed by an
|
||||
# optional version number token. Example: Apache/1.3.9
|
||||
if server:
|
||||
headers[HDR_SERVER] = server
|
||||
|
||||
return Frame(CMD_CONNECTED, headers).encode()
|
||||
|
||||
def message(self, destination, body, content_type=None, message_id=str(uuid.uuid4())):
|
||||
"""
|
||||
Sends a message to a STOMP client.
|
||||
|
||||
:param destination: Destination string
|
||||
:param body: Data to be added in the frame body
|
||||
:param content_type: MIME type which describes the format of the body
|
||||
:param message_id: Unique identifier for that message
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
# Destination and message id headers are required
|
||||
headers = {HDR_DESTINATION: destination,
|
||||
HDR_MESSAGE_ID: message_id}
|
||||
|
||||
# Subscription is required but not implemented on this server
|
||||
headers[HDR_SUBSCRIPTION] = 0
|
||||
|
||||
if content_type:
|
||||
headers[HDR_CONTENT_TYPE] = content_type
|
||||
|
||||
body = encode(body)
|
||||
if HDR_CONTENT_LENGTH not in headers and hasbyte(0, body):
|
||||
headers[HDR_CONTENT_LENGTH] = len(body)
|
||||
|
||||
return Frame(CMD_MESSAGE, headers, body).encode()
|
||||
|
||||
def receipt(self, receipt_id):
|
||||
"""
|
||||
Sends an acknowledgment for client frame that requests a receipt.
|
||||
|
||||
:param receipt_id: Receipt ID to send back to the client
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
# Receipt ID header is required (the same sent in the client frame)
|
||||
headers = {HDR_RECEIPT_ID: receipt_id}
|
||||
return Frame(CMD_RECEIPT, headers).encode()
|
||||
|
||||
def error(self, message='', body='', content_type=None):
|
||||
"""
|
||||
Sends an error to the client if something goes wrong.
|
||||
|
||||
:param message: Short description of the error
|
||||
:param body: Detailed information
|
||||
:param content_type: MIME type which describes the format of the body
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
headers = {}
|
||||
if message:
|
||||
headers[HDR_MESSAGE] = message
|
||||
|
||||
if body:
|
||||
body = encode(body)
|
||||
if HDR_CONTENT_LENGTH not in headers and hasbyte(0, body):
|
||||
headers[HDR_CONTENT_LENGTH] = len(body)
|
||||
if content_type:
|
||||
headers[HDR_CONTENT_TYPE] = content_type
|
||||
|
||||
return Frame(CMD_ERROR, headers, body).encode()
|
||||
|
||||
|
||||
class clientProtocol(object):
|
||||
"""
|
||||
STOMP 1.2 protocol support for clients.
|
||||
"""
|
||||
|
||||
def connect(self, host, accept_version='1.2'):
|
||||
"""
|
||||
Connects to a STOMP server.
|
||||
Heart-beat, login and passcode headers are not supported.
|
||||
|
||||
:param host: Host name that the socket was established against.
|
||||
:param accept_version: The versions of the STOMP protocol the client supports.
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
# Currently only STOMP 1.2 is supported (required header)
|
||||
headers = {HDR_ACCEPT_VERSION: accept_version}
|
||||
|
||||
if host:
|
||||
headers[HDR_HOST] = host
|
||||
|
||||
# The STOMP command is not backward compatible with STOMP 1.0 servers.
|
||||
# Clients that use the STOMP frame instead of the CONNECT frame will
|
||||
# only be able to connect to STOMP 1.2 servers (as well as some STOMP 1.1 servers.
|
||||
return Frame(CMD_STOMP, headers).encode()
|
||||
|
||||
def disconnect(self, receipt=str(uuid.uuid4())):
|
||||
"""
|
||||
Disconnects to a STOMP server.
|
||||
|
||||
:param receipt: unique identifier
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
# Receipt header is required
|
||||
headers = {HDR_RECEIPT: receipt}
|
||||
return Frame(CMD_DISCONNECT, headers).encode()
|
||||
|
||||
def send(self, destination, body, content_type=None):
|
||||
"""
|
||||
Sends a message to a destination in the messaging system.
|
||||
Transaction header is not supported.
|
||||
User defined headers are not supported too (against the protocol specification)
|
||||
|
||||
:param destination: Destination string
|
||||
:param body: Data to be added in the frame body
|
||||
:param content_type: MIME type which describes the format of the body
|
||||
:returns: STOMP Frame object
|
||||
"""
|
||||
|
||||
# Destination header is required
|
||||
headers = {HDR_DESTINATION: destination}
|
||||
|
||||
if content_type:
|
||||
headers[HDR_CONTENT_TYPE] = content_type
|
||||
|
||||
body = encode(body)
|
||||
if HDR_CONTENT_LENGTH not in headers and hasbyte(0, body):
|
||||
headers[HDR_CONTENT_LENGTH] = len(body)
|
||||
|
||||
return Frame(CMD_SEND, headers, body).encode()
|
||||
@@ -0,0 +1,43 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2013 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Utilitary functions for STOMP implementation
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
PY2 = sys.version_info[0] == 2
|
||||
|
||||
if not PY2:
|
||||
def encode(char_data):
|
||||
if type(char_data) is str:
|
||||
return char_data.encode()
|
||||
elif type(char_data) is bytes:
|
||||
return char_data
|
||||
else:
|
||||
raise TypeError('message should be a string or bytes')
|
||||
else:
|
||||
def encode(char_data):
|
||||
if type(char_data) is unicode:
|
||||
return char_data.encode('utf-8')
|
||||
else:
|
||||
return char_data
|
||||
|
||||
|
||||
def hasbyte(byte, byte_data):
|
||||
return bytes([byte]) in byte_data
|
||||
Reference in new issue
Block a user