fix encoding error on *too* random bytes

This should fix things like:

def __init__(self, val):
    self.val = val.decode("string_escape")
ValueError: Trailing \ in string
pathod/language/base.py:108: ValueError

test/pathod/test_language_websocket.py:83: in fr
    return netlib.websockets.Frame.from_bytes(tutils.render(wf, settings))
test/pathod/tutils.py:123: in render
    r = r.resolve(settings)
pathod/language/websockets.py:179: in resolve
    Key(base.TokValueLiteral(os.urandom(4)))
This commit is contained in:
Thomas Kriechbaumer 2016-05-15 10:40:21 -07:00
parent 2887480bcb
commit 55a17b2ed3

View File

@ -1,4 +1,6 @@
import os import os
import random
import string
import netlib.websockets import netlib.websockets
import pyparsing as pp import pyparsing as pp
from . import base, generators, actions, message from . import base, generators, actions, message
@ -175,8 +177,10 @@ class WebsocketFrame(message.Message):
Mask(True) Mask(True)
) )
if not self.knone and 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:
allowed_chars = string.ascii_letters + string.digits
k = ''.join([allowed_chars[random.randrange(0, len(allowed_chars))] for i in range(4)])
tokens.append( tokens.append(
Key(base.TokValueLiteral(os.urandom(4))) Key(base.TokValueLiteral(k))
) )
return self.__class__( return self.__class__(
[i.resolve(settings, self) for i in tokens] [i.resolve(settings, self) for i in tokens]