aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/websockets.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2015-04-30 09:04:22 +1200
committerAldo Cortesi <aldo@nullcube.com>2015-04-30 09:04:22 +1200
commit80860229209b4c6eb8384e1bca3cabdbe062fe6e (patch)
treeed0bb19910d81b9b8b8719b4c9fc34a40baa6919 /netlib/websockets.py
parentb7a2fc85537dca60fb18d25965289d876bd3bd38 (diff)
downloadmitmproxy-80860229209b4c6eb8384e1bca3cabdbe062fe6e.tar.gz
mitmproxy-80860229209b4c6eb8384e1bca3cabdbe062fe6e.tar.bz2
mitmproxy-80860229209b4c6eb8384e1bca3cabdbe062fe6e.zip
Add a tiny utility class for keeping bi-directional mappings.
Use it in websocket and socks.
Diffstat (limited to 'netlib/websockets.py')
-rw-r--r--netlib/websockets.py25
1 files changed, 19 insertions, 6 deletions
diff --git a/netlib/websockets.py b/netlib/websockets.py
index 493bb18a..d358ed53 100644
--- a/netlib/websockets.py
+++ b/netlib/websockets.py
@@ -25,13 +25,14 @@ MAX_16_BIT_INT = (1 << 16)
MAX_64_BIT_INT = (1 << 64)
-class OPCODE:
- CONTINUE = 0x00
- TEXT = 0x01
- BINARY = 0x02
- CLOSE = 0x08
- PING = 0x09
+OPCODE = utils.BiDi(
+ CONTINUE = 0x00,
+ TEXT = 0x01,
+ BINARY = 0x02,
+ CLOSE = 0x08,
+ PING = 0x09,
PONG = 0x0a
+)
def apply_mask(message, masking_key):
@@ -160,6 +161,18 @@ class FrameHeader:
if self.masking_key and len(self.masking_key) != 4:
raise ValueError("Masking key must be 4 bytes.")
+ def human_readable(self):
+ return "\n".join([
+ ("fin - " + str(self.fin)),
+ ("rsv1 - " + str(self.rsv1)),
+ ("rsv2 - " + str(self.rsv2)),
+ ("rsv3 - " + str(self.rsv3)),
+ ("opcode - " + str(self.opcode)),
+ ("mask - " + str(self.mask)),
+ ("length_code - " + str(self.length_code)),
+ ("masking_key - " + repr(str(self.masking_key))),
+ ])
+
def to_bytes(self):
first_byte = utils.setbit(0, 7, self.fin)
first_byte = utils.setbit(first_byte, 6, self.rsv1)