From cd2fb13b3e9ced39059f3d6572fd8806d0026d2e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 17 May 2015 11:04:53 +1200 Subject: [PATCH] websockets: replace the "raw" with a simpler mechanism You can now say "knone" to specifiy that no key should be generated under any circumstances. --- libpathod/language/websockets.py | 23 +++++++++++-------- libpathod/templates/docs_lang_websockets.html | 15 ++---------- test/test_language_websocket.py | 11 +++++++-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/libpathod/language/websockets.py b/libpathod/language/websockets.py index 599cdb886..428f3b4dd 100644 --- a/libpathod/language/websockets.py +++ b/libpathod/language/websockets.py @@ -9,7 +9,6 @@ from . import base, generators, actions, message wf:fin:rsv1:rsv2:rsv3:mask wf:-fin:-rsv1:-rsv2:-rsv3:-mask - wf:k"mask" wf:l234 """ @@ -35,10 +34,6 @@ class Body(base.Value): preamble = "b" -class Raw(base.CaselessLiteral): - TOK = "r" - - class Fin(base.Boolean): name = "fin" @@ -64,6 +59,11 @@ class Key(base.FixedLengthValue): length = 4 +class KeyNone(base.CaselessLiteral): + unique_name = "Key" + TOK = "knone" + + class WebsocketFrame(message.Message): comps = ( Body, @@ -78,9 +78,8 @@ class WebsocketFrame(message.Message): actions.PauseAt, actions.DisconnectAt, actions.InjectAt, + KeyNone, Key, - - Raw, ) logattrs = ["body"] @property @@ -119,6 +118,10 @@ class WebsocketFrame(message.Message): def key(self): return self.tok(Key) + @property + def knone(self): + return self.tok(KeyNone) + @classmethod def expr(klass): parts = [i.expr() for i in klass.comps] @@ -139,7 +142,7 @@ class WebsocketFrame(message.Message): tokens.append( Mask(True) ) - if self.mask and self.mask.value and not self.key: + if not self.knone and self.mask and self.mask.value and not self.key: tokens.append( Key(base.TokValueLiteral(os.urandom(4))) ) @@ -159,7 +162,9 @@ class WebsocketFrame(message.Message): ) if self.mask and self.mask.value: frameparts["mask"] = True - if self.key: + if self.knone: + frameparts["masking_key"] = None + elif self.key: key = self.key.values(settings)[0][:] frameparts["masking_key"] = key for i in ["opcode", "fin", "rsv1", "rsv2", "rsv3", "mask"]: diff --git a/libpathod/templates/docs_lang_websockets.html b/libpathod/templates/docs_lang_websockets.html index 9eb1ec25e..a77ac2510 100644 --- a/libpathod/templates/docs_lang_websockets.html +++ b/libpathod/templates/docs_lang_websockets.html @@ -48,7 +48,8 @@ kVALUE Set the masking key. The resulting value must be exactly 4 - bytes long. + bytes long. The special form knone specifies that no key + should be set, even if the mask bit is on. @@ -67,18 +68,6 @@ - - r - - Create a "raw" frame: -