websockets: replace the "raw" with a simpler mechanism

You can now say "knone" to specifiy that no key should be generated
under any circumstances.
This commit is contained in:
Aldo Cortesi 2015-05-17 11:04:53 +12:00
parent e4feba5433
commit cd2fb13b3e
3 changed files with 25 additions and 24 deletions

View File

@ -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"]:

View File

@ -48,7 +48,8 @@
<td> k<a href="#valuespec">VALUE</a> </td>
<td>
Set the masking key. The resulting value must be exactly 4
bytes long.
bytes long. The special form <b>knone</b> specifies that no key
should be set, even if the mask bit is on.
</td>
</tr>
@ -67,18 +68,6 @@
</td>
</tr>
<tr>
<td> r </td>
<td>
Create a "raw" frame:
<ul>
<li> Don't auto-generate the masking key if the mask flag is
set </li>
<li> Don't set the mask flag if masking key is set. </li>
</td>
</tr>
<tr>
<td> [-]rsv1 </td>
<td>

View File

@ -17,7 +17,7 @@ class TestWebsocketFrame:
"wf:b'foo'",
"wf:cbinary",
"wf:c1",
"wf:r",
"wf:mask:knone",
"wf:fin",
"wf:fin:rsv1:rsv2:rsv3:mask",
"wf:-fin:-rsv1:-rsv2:-rsv3:-mask",
@ -67,7 +67,7 @@ class TestWebsocketFrame:
assert self.fr("wf:ctext").header.opcode ==\
netlib.websockets.OPCODE.TEXT
def test_auto_raw(self):
def test_construction(self):
# Simple server frame
frm = self.fr("wf:b'foo'")
assert not frm.header.mask
@ -99,3 +99,10 @@ class TestWebsocketFrame:
# We're reading back a corrupted frame - the first 3 characters of the
# mask is mis-interpreted as the payload
assert frm.payload == "abc"
def test_knone(self):
tutils.raises(
"expected 4 bytes",
self.fr,
"wf:b'foo':mask:knone",
)