aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/websockets
diff options
context:
space:
mode:
Diffstat (limited to 'netlib/websockets')
-rw-r--r--netlib/websockets/__init__.py13
-rw-r--r--netlib/websockets/frame.py14
-rw-r--r--netlib/websockets/protocol.py35
3 files changed, 35 insertions, 27 deletions
diff --git a/netlib/websockets/__init__.py b/netlib/websockets/__init__.py
index 1c143919..fea696d9 100644
--- a/netlib/websockets/__init__.py
+++ b/netlib/websockets/__init__.py
@@ -1,2 +1,11 @@
-from .frame import *
-from .protocol import *
+from __future__ import absolute_import, print_function, division
+from .frame import FrameHeader, Frame, OPCODE
+from .protocol import Masker, WebsocketsProtocol
+
+__all__ = [
+ "FrameHeader",
+ "Frame",
+ "Masker",
+ "WebsocketsProtocol",
+ "OPCODE",
+]
diff --git a/netlib/websockets/frame.py b/netlib/websockets/frame.py
index fce2c9d3..42196ffb 100644
--- a/netlib/websockets/frame.py
+++ b/netlib/websockets/frame.py
@@ -6,15 +6,17 @@ import warnings
import six
-from .protocol import Masker
from netlib import tcp
+from netlib import strutils
from netlib import utils
+from netlib import human
+from netlib.websockets import protocol
MAX_16_BIT_INT = (1 << 16)
MAX_64_BIT_INT = (1 << 64)
-DEFAULT=object()
+DEFAULT = object()
OPCODE = utils.BiDi(
CONTINUE=0x00,
@@ -98,7 +100,7 @@ class FrameHeader(object):
if self.masking_key:
vals.append(":key=%s" % repr(self.masking_key))
if self.payload_length:
- vals.append(" %s" % utils.pretty_size(self.payload_length))
+ vals.append(" %s" % human.pretty_size(self.payload_length))
return "".join(vals)
def human_readable(self):
@@ -253,7 +255,7 @@ class Frame(object):
def __repr__(self):
ret = repr(self.header)
if self.payload:
- ret = ret + "\nPayload:\n" + utils.clean_bin(self.payload).decode("ascii")
+ ret = ret + "\nPayload:\n" + strutils.clean_bin(self.payload).decode("ascii")
return ret
def human_readable(self):
@@ -266,7 +268,7 @@ class Frame(object):
"""
b = bytes(self.header)
if self.header.masking_key:
- b += Masker(self.header.masking_key)(self.payload)
+ b += protocol.Masker(self.header.masking_key)(self.payload)
else:
b += self.payload
return b
@@ -295,7 +297,7 @@ class Frame(object):
payload = fp.safe_read(header.payload_length)
if header.mask == 1 and header.masking_key:
- payload = Masker(header.masking_key)(payload)
+ payload = protocol.Masker(header.masking_key)(payload)
return cls(
payload,
diff --git a/netlib/websockets/protocol.py b/netlib/websockets/protocol.py
index 1e95fa1c..c1b7be2c 100644
--- a/netlib/websockets/protocol.py
+++ b/netlib/websockets/protocol.py
@@ -1,26 +1,26 @@
+"""
+Colleciton of utility functions that implement small portions of the RFC6455
+WebSockets Protocol Useful for building WebSocket clients and servers.
+Emphassis is on readabilty, simplicity and modularity, not performance or
+completeness
+This is a work in progress and does not yet contain all the utilites need to
+create fully complient client/servers #
+Spec: https://tools.ietf.org/html/rfc6455
-# Colleciton of utility functions that implement small portions of the RFC6455
-# WebSockets Protocol Useful for building WebSocket clients and servers.
-#
-# Emphassis is on readabilty, simplicity and modularity, not performance or
-# completeness
-#
-# This is a work in progress and does not yet contain all the utilites need to
-# create fully complient client/servers #
-# Spec: https://tools.ietf.org/html/rfc6455
+The magic sha that websocket servers must know to prove they understand
+RFC6455
+"""
-# The magic sha that websocket servers must know to prove they understand
-# RFC6455
from __future__ import absolute_import
import base64
import hashlib
import os
-import binascii
import six
-from ..http import Headers
+
+from netlib import http
websockets_magic = b'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
VERSION = "13"
@@ -73,11 +73,11 @@ class WebsocketsProtocol(object):
specified, it is generated, and can be found in sec-websocket-key in
the returned header set.
- Returns an instance of Headers
+ Returns an instance of http.Headers
"""
if not key:
key = base64.b64encode(os.urandom(16)).decode('ascii')
- return Headers(
+ return http.Headers(
sec_websocket_key=key,
sec_websocket_version=version,
connection="Upgrade",
@@ -89,27 +89,24 @@ class WebsocketsProtocol(object):
"""
The server response is a valid HTTP 101 response.
"""
- return Headers(
+ return http.Headers(
sec_websocket_accept=self.create_server_nonce(key),
connection="Upgrade",
upgrade="websocket"
)
-
@classmethod
def check_client_handshake(self, headers):
if headers.get("upgrade") != "websocket":
return
return headers.get("sec-websocket-key")
-
@classmethod
def check_server_handshake(self, headers):
if headers.get("upgrade") != "websocket":
return
return headers.get("sec-websocket-accept")
-
@classmethod
def create_server_nonce(self, client_nonce):
return base64.b64encode(hashlib.sha1(client_nonce + websockets_magic).digest())