2015-05-02 04:17:00 +00:00
|
|
|
|
|
|
|
import netlib.websockets
|
|
|
|
import contrib.pyparsing as pp
|
2015-05-02 20:02:13 +00:00
|
|
|
from . import base, generators, actions, message
|
2015-05-02 04:17:00 +00:00
|
|
|
|
2015-05-02 09:27:11 +00:00
|
|
|
"""
|
|
|
|
wf:ctext:b'foo'
|
|
|
|
wf:c15:r'foo'
|
|
|
|
wf:fin:rsv1:rsv2:rsv3:mask
|
|
|
|
wf:-fin:-rsv1:-rsv2:-rsv3:-mask
|
|
|
|
wf:p234
|
|
|
|
wf:m"mask"
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class WF(base.CaselessLiteral):
|
|
|
|
TOK = "wf"
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
|
2015-05-02 09:42:09 +00:00
|
|
|
class Body(base.PreValue):
|
|
|
|
preamble = "b"
|
|
|
|
|
|
|
|
|
2015-05-02 20:02:13 +00:00
|
|
|
class WebsocketFrame(message.Message):
|
2015-05-02 04:17:00 +00:00
|
|
|
comps = (
|
2015-05-02 09:42:09 +00:00
|
|
|
Body,
|
2015-05-02 20:02:13 +00:00
|
|
|
actions.PauseAt,
|
|
|
|
actions.DisconnectAt,
|
|
|
|
actions.InjectAt
|
2015-05-02 04:17:00 +00:00
|
|
|
)
|
|
|
|
logattrs = ["body"]
|
2015-05-02 20:02:13 +00:00
|
|
|
@property
|
|
|
|
def actions(self):
|
|
|
|
return self.toks(actions._Action)
|
2015-05-02 04:17:00 +00:00
|
|
|
|
2015-05-02 09:42:09 +00:00
|
|
|
@property
|
|
|
|
def body(self):
|
|
|
|
return self.tok(Body)
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
parts = [i.expr() for i in klass.comps]
|
|
|
|
atom = pp.MatchFirst(parts)
|
|
|
|
resp = pp.And(
|
|
|
|
[
|
2015-05-02 09:27:11 +00:00
|
|
|
WF.expr(),
|
2015-05-02 04:17:00 +00:00
|
|
|
base.Sep,
|
|
|
|
pp.ZeroOrMore(base.Sep + atom)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
resp = resp.setParseAction(klass)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
def values(self, settings):
|
|
|
|
vals = []
|
|
|
|
if self.body:
|
|
|
|
bodygen = self.body.value.get_generator(settings)
|
|
|
|
length = len(self.body.value.get_generator(settings))
|
|
|
|
else:
|
|
|
|
bodygen = None
|
|
|
|
length = 0
|
|
|
|
frame = netlib.websockets.FrameHeader(
|
|
|
|
mask = True,
|
|
|
|
payload_length = length
|
|
|
|
)
|
|
|
|
vals = [frame.to_bytes()]
|
|
|
|
if self.body:
|
|
|
|
masker = netlib.websockets.Masker(frame.masking_key)
|
|
|
|
vals.append(
|
|
|
|
generators.TransformGenerator(
|
|
|
|
bodygen,
|
|
|
|
masker.mask
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def resolve(self, settings, msg=None):
|
|
|
|
return self.__class__(
|
|
|
|
[i.resolve(settings, msg) for i in self.tokens]
|
|
|
|
)
|
|
|
|
|
|
|
|
def spec(self):
|
|
|
|
return ":".join([i.spec() for i in self.tokens])
|