Clarify language API, stub out nested websocket frames

This commit is contained in:
Aldo Cortesi 2015-06-04 17:18:06 +12:00
parent 5bee061849
commit 9fda74c65a
13 changed files with 100 additions and 76 deletions

View File

@ -135,9 +135,9 @@ def make_app(noapi, debug):
try: try:
if is_request: if is_request:
r = language.parse_requests(spec)[0] r = language.parse_pathoc(spec)[0]
else: else:
r = language.parse_response(spec) r = language.parse_pathod(spec)
except language.ParseException as v: except language.ParseException as v:
args["syntaxerror"] = str(v) args["syntaxerror"] = str(v)
args["marked"] = v.marked() args["marked"] = v.marked()

View File

@ -189,7 +189,7 @@ def args_pathoc(argv, stdout=sys.stdout, stderr=sys.stderr):
data = open(r).read() data = open(r).read()
r = data r = data
try: try:
reqs.extend(language.parse_requests(r)) reqs.extend(language.parse_pathoc(r))
except language.ParseException as v: except language.ParseException as v:
print >> stderr, "Error parsing request spec: %s" % v.msg print >> stderr, "Error parsing request spec: %s" % v.msg
print >> stderr, v.marked() print >> stderr, v.marked()
@ -400,7 +400,7 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
data = open(spec).read() data = open(spec).read()
spec = data spec = data
try: try:
req = language.parse_response(spec) req = language.parse_pathod(spec)
except language.ParseException as v: except language.ParseException as v:
print >> stderr, "Error parsing anchor spec: %s" % v.msg print >> stderr, "Error parsing anchor spec: %s" % v.msg
print >> stderr, v.marked() print >> stderr, v.marked()

View File

@ -9,7 +9,7 @@ from base import Settings
assert Settings # prevent pyflakes from messing with this assert Settings # prevent pyflakes from messing with this
def parse_response(s): def parse_pathod(s):
""" """
May raise ParseException May raise ParseException
""" """
@ -18,12 +18,17 @@ def parse_response(s):
except UnicodeError: except UnicodeError:
raise exceptions.ParseException("Spec must be valid ASCII.", 0, 0) raise exceptions.ParseException("Spec must be valid ASCII.", 0, 0)
try: try:
return http.Response.expr().parseString(s, parseAll=True)[0] return pp.Or(
[
websockets.WebsocketFrame.expr(),
http.Response.expr(),
]
).parseString(s, parseAll=True)[0]
except pp.ParseException as v: except pp.ParseException as v:
raise exceptions.ParseException(v.msg, v.line, v.col) raise exceptions.ParseException(v.msg, v.line, v.col)
def parse_requests(s): def parse_pathoc(s):
""" """
May raise ParseException May raise ParseException
""" """

View File

@ -3,16 +3,6 @@ import netlib.websockets
import pyparsing as pp import pyparsing as pp
from . import base, generators, actions, message from . import base, generators, actions, message
"""
wf:ctext:b'foo'
wf:c15:r'foo'
wf:fin:rsv1:rsv2:rsv3:mask
wf:-fin:-rsv1:-rsv2:-rsv3:-mask
wf:l234
wf:mask:r"foo
"""
class WF(base.CaselessLiteral): class WF(base.CaselessLiteral):
TOK = "wf" TOK = "wf"
@ -79,6 +69,38 @@ class Times(base.Integer):
preamble = "x" preamble = "x"
class NestedFrame(base.Token):
def __init__(self, value):
self.value = value
try:
self.parsed = WebsocketFrame(
Response.expr().parseString(
value.val,
parseAll=True
)
)
except pp.ParseException as v:
raise exceptions.ParseException(v.msg, v.line, v.col)
@classmethod
def expr(klass):
e = pp.Literal("wf").suppress()
e = e + base.TokValueLiteral.expr()
return e.setParseAction(lambda x: klass(*x))
def values(self, settings):
return [
self.value.get_generator(settings),
]
def spec(self):
return "s%s" % (self.value.spec())
def freeze(self, settings):
f = self.parsed.freeze(settings).spec()
return NestedFrame(base.TokValueLiteral(f.encode("string_escape")))
class WebsocketFrame(message.Message): class WebsocketFrame(message.Message):
comps = ( comps = (
OpCode, OpCode,

View File

@ -270,7 +270,7 @@ class Pathoc(tcp.TCPClient):
""" """
with self.log() as log: with self.log() as log:
if isinstance(r, basestring): if isinstance(r, basestring):
r = language.parse_requests(r)[0] r = language.parse_pathoc(r)[0]
log(">> %s" % r) log(">> %s" % r)
try: try:
language.serve(r, self.wfile, self.settings) language.serve(r, self.wfile, self.settings)
@ -316,7 +316,7 @@ class Pathoc(tcp.TCPClient):
""" """
with self.log() as log: with self.log() as log:
if isinstance(r, basestring): if isinstance(r, basestring):
r = language.parse_requests(r)[0] r = language.parse_pathoc(r)[0]
log(">> %s" % r) log(">> %s" % r)
resp, req = None, None resp, req = None, None
try: try:
@ -355,7 +355,7 @@ class Pathoc(tcp.TCPClient):
May raise http.HTTPError, tcp.NetLibError May raise http.HTTPError, tcp.NetLibError
""" """
if isinstance(r, basestring): if isinstance(r, basestring):
r = language.parse_requests(r)[0] r = language.parse_pathoc(r)[0]
if isinstance(r, language.http.Request): if isinstance(r, language.http.Request):
if r.ws: if r.ws:
return self.websocket_start(r, self.websocket_get_frame) return self.websocket_start(r, self.websocket_get_frame)

View File

@ -226,7 +226,7 @@ class PathodHandler(tcp.BaseHandler):
spec = "ws" spec = "ws"
lg("crafting spec: %s" % spec) lg("crafting spec: %s" % spec)
try: try:
crafted = language.parse_response(spec) crafted = language.parse_pathod(spec)
except language.ParseException as v: except language.ParseException as v:
lg("Parse error: %s" % v.msg) lg("Parse error: %s" % v.msg)
crafted = language.http.make_error_response( crafted = language.http.make_error_response(

View File

@ -5,7 +5,7 @@ from libpathod import language
def parse_request(s): def parse_request(s):
return language.parse_requests(s)[0] return language.parse_pathoc(s)[0]
def test_unique_name(): def test_unique_name():
@ -14,10 +14,10 @@ def test_unique_name():
class TestDisconnects: class TestDisconnects:
def test_parse_response(self): def test_parse_pathod(self):
a = language.parse_response("400:d0").actions[0] a = language.parse_pathod("400:d0").actions[0]
assert a.spec() == "d0" assert a.spec() == "d0"
a = language.parse_response("400:dr").actions[0] a = language.parse_pathod("400:dr").actions[0]
assert a.spec() == "dr" assert a.spec() == "dr"
def test_at(self): def test_at(self):
@ -39,13 +39,13 @@ class TestDisconnects:
class TestInject: class TestInject:
def test_parse_response(self): def test_parse_pathod(self):
a = language.parse_response("400:ir,@100").actions[0] a = language.parse_pathod("400:ir,@100").actions[0]
assert a.offset == "r" assert a.offset == "r"
assert a.value.datatype == "bytes" assert a.value.datatype == "bytes"
assert a.value.usize == 100 assert a.value.usize == 100
a = language.parse_response("400:ia,@100").actions[0] a = language.parse_pathod("400:ia,@100").actions[0]
assert a.offset == "a" assert a.offset == "a"
def test_at(self): def test_at(self):
@ -60,7 +60,7 @@ class TestInject:
def test_serve(self): def test_serve(self):
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = language.parse_response("400:i0,'foo'") r = language.parse_pathod("400:i0,'foo'")
assert language.serve(r, s, {}) assert language.serve(r, s, {})
def test_spec(self): def test_spec(self):
@ -77,7 +77,7 @@ class TestInject:
class TestPauses: class TestPauses:
def test_parse_response(self): def test_parse_pathod(self):
e = actions.PauseAt.expr() e = actions.PauseAt.expr()
v = e.parseString("p10,10")[0] v = e.parseString("p10,10")[0]
assert v.seconds == 10 assert v.seconds == 10
@ -93,7 +93,7 @@ class TestPauses:
assert v.offset == "a" assert v.offset == "a"
def test_request(self): def test_request(self):
r = language.parse_response('400:p10,10') r = language.parse_pathod('400:p10,10')
assert r.actions[0].spec() == "p10,10" assert r.actions[0].spec() == "p10,10"
def test_spec(self): def test_spec(self):

View File

@ -6,11 +6,11 @@ import nose.tools as nt
def parse_request(s): def parse_request(s):
return language.parse_requests(s)[0] return language.parse_pathoc(s)[0]
def test_times(): def test_times():
reqs = language.parse_requests("get:/:x5") reqs = language.parse_pathoc("get:/:x5")
assert len(reqs) == 5 assert len(reqs) == 5
assert not reqs[0].times assert not reqs[0].times

View File

@ -6,7 +6,7 @@ import tutils
def parse_request(s): def parse_request(s):
return language.parse_requests(s)[0] return language.parse_pathoc(s)[0]
def test_make_error_response(): def test_make_error_response():
@ -32,7 +32,7 @@ class TestRequest:
assert len(r.path.string()) == 1024 assert len(r.path.string()) == 1024
def test_multiple(self): def test_multiple(self):
r = language.parse_requests("GET:/ PUT:/") r = language.parse_pathoc("GET:/ PUT:/")
assert r[0].method.string() == "GET" assert r[0].method.string() == "GET"
assert r[1].method.string() == "PUT" assert r[1].method.string() == "PUT"
assert len(r) == 2 assert len(r) == 2
@ -52,7 +52,7 @@ class TestRequest:
ir,@1 ir,@1
""" """
r = language.parse_requests(l) r = language.parse_pathoc(l)
assert len(r) == 2 assert len(r) == 2
assert r[0].method.string() == "GET" assert r[0].method.string() == "GET"
assert r[1].method.string() == "PUT" assert r[1].method.string() == "PUT"
@ -61,14 +61,14 @@ class TestRequest:
get:"http://localhost:9999/p/200":ir,@1 get:"http://localhost:9999/p/200":ir,@1
get:"http://localhost:9999/p/200":ir,@2 get:"http://localhost:9999/p/200":ir,@2
""" """
r = language.parse_requests(l) r = language.parse_pathoc(l)
assert len(r) == 2 assert len(r) == 2
assert r[0].method.string() == "GET" assert r[0].method.string() == "GET"
assert r[1].method.string() == "GET" assert r[1].method.string() == "GET"
def test_pathodspec(self): def test_pathodspec(self):
l = "get:/p:s'200'" l = "get:/p:s'200'"
r = language.parse_requests(l) r = language.parse_pathoc(l)
assert len(r) == 1 assert len(r) == 1
assert len(r[0].tokens) == 3 assert len(r[0].tokens) == 3
assert isinstance(r[0].tokens[2], http.PathodResponse) assert isinstance(r[0].tokens[2], http.PathodResponse)
@ -143,42 +143,42 @@ class TestRequest:
class TestResponse: class TestResponse:
def dummy_response(self): def dummy_response(self):
return language.parse_response("400'msg'") return language.parse_pathod("400'msg'")
def test_response(self): def test_response(self):
r = language.parse_response("400:m'msg'") r = language.parse_pathod("400:m'msg'")
assert r.code.string() == "400" assert r.code.string() == "400"
assert r.reason.string() == "msg" assert r.reason.string() == "msg"
r = language.parse_response("400:m'msg':b@100b") r = language.parse_pathod("400:m'msg':b@100b")
assert r.reason.string() == "msg" assert r.reason.string() == "msg"
assert r.body.values({}) assert r.body.values({})
assert str(r) assert str(r)
r = language.parse_response("200") r = language.parse_pathod("200")
assert r.code.string() == "200" assert r.code.string() == "200"
assert not r.reason assert not r.reason
assert "OK" in [i[:] for i in r.preamble({})] assert "OK" in [i[:] for i in r.preamble({})]
def test_render(self): def test_render(self):
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = language.parse_response("400:m'msg'") r = language.parse_pathod("400:m'msg'")
assert language.serve(r, s, {}) assert language.serve(r, s, {})
r = language.parse_response("400:p0,100:dr") r = language.parse_pathod("400:p0,100:dr")
assert "p0" in r.spec() assert "p0" in r.spec()
s = r.preview_safe() s = r.preview_safe()
assert "p0" not in s.spec() assert "p0" not in s.spec()
def test_raw(self): def test_raw(self):
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = language.parse_response("400:b'foo'") r = language.parse_pathod("400:b'foo'")
language.serve(r, s, {}) language.serve(r, s, {})
v = s.getvalue() v = s.getvalue()
assert "Content-Length" in v assert "Content-Length" in v
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = language.parse_response("400:b'foo':r") r = language.parse_pathod("400:b'foo':r")
language.serve(r, s, {}) language.serve(r, s, {})
v = s.getvalue() v = s.getvalue()
assert "Content-Length" not in v assert "Content-Length" not in v
@ -188,9 +188,9 @@ class TestResponse:
s = cStringIO.StringIO() s = cStringIO.StringIO()
language.serve(x, s, language.Settings()) language.serve(x, s, language.Settings())
assert x.length(language.Settings()) == len(s.getvalue()) assert x.length(language.Settings()) == len(s.getvalue())
testlen(language.parse_response("400:m'msg':r")) testlen(language.parse_pathod("400:m'msg':r"))
testlen(language.parse_response("400:m'msg':h'foo'='bar':r")) testlen(language.parse_pathod("400:m'msg':h'foo'='bar':r"))
testlen(language.parse_response("400:m'msg':h'foo'='bar':b@100b:r")) testlen(language.parse_pathod("400:m'msg':h'foo'='bar':b@100b:r"))
def test_maximum_length(self): def test_maximum_length(self):
def testlen(x): def testlen(x):
@ -199,42 +199,42 @@ class TestResponse:
language.serve(x, s, {}) language.serve(x, s, {})
assert m >= len(s.getvalue()) assert m >= len(s.getvalue())
r = language.parse_response("400:m'msg':b@100:d0") r = language.parse_pathod("400:m'msg':b@100:d0")
testlen(r) testlen(r)
r = language.parse_response("400:m'msg':b@100:d0:i0,'foo'") r = language.parse_pathod("400:m'msg':b@100:d0:i0,'foo'")
testlen(r) testlen(r)
r = language.parse_response("400:m'msg':b@100:d0:i0,'foo'") r = language.parse_pathod("400:m'msg':b@100:d0:i0,'foo'")
testlen(r) testlen(r)
def test_parse_err(self): def test_parse_err(self):
tutils.raises( tutils.raises(
language.ParseException, language.parse_response, "400:msg,b:" language.ParseException, language.parse_pathod, "400:msg,b:"
) )
try: try:
language.parse_response("400'msg':b:") language.parse_pathod("400'msg':b:")
except language.ParseException as v: except language.ParseException as v:
assert v.marked() assert v.marked()
assert str(v) assert str(v)
def test_nonascii(self): def test_nonascii(self):
tutils.raises("ascii", language.parse_response, "foo:b\xf0") tutils.raises("ascii", language.parse_pathod, "foo:b\xf0")
def test_parse_header(self): def test_parse_header(self):
r = language.parse_response('400:h"foo"="bar"') r = language.parse_pathod('400:h"foo"="bar"')
assert http.get_header("foo", r.headers) assert http.get_header("foo", r.headers)
def test_parse_pause_before(self): def test_parse_pause_before(self):
r = language.parse_response("400:p0,10") r = language.parse_pathod("400:p0,10")
assert r.actions[0].spec() == "p0,10" assert r.actions[0].spec() == "p0,10"
def test_parse_pause_after(self): def test_parse_pause_after(self):
r = language.parse_response("400:pa,10") r = language.parse_pathod("400:pa,10")
assert r.actions[0].spec() == "pa,10" assert r.actions[0].spec() == "pa,10"
def test_parse_pause_random(self): def test_parse_pause_random(self):
r = language.parse_response("400:pr,10") r = language.parse_pathod("400:pr,10")
assert r.actions[0].spec() == "pr,10" assert r.actions[0].spec() == "pr,10"
def test_parse_stress(self): def test_parse_stress(self):
@ -242,19 +242,19 @@ class TestResponse:
# returns an int and a python 2.7 int on windows has 32bit precision. # returns an int and a python 2.7 int on windows has 32bit precision.
# Therefore, we should keep the body length < 2147483647 bytes in our # Therefore, we should keep the body length < 2147483647 bytes in our
# tests. # tests.
r = language.parse_response("400:b@1g") r = language.parse_pathod("400:b@1g")
assert r.length({}) assert r.length({})
def test_spec(self): def test_spec(self):
def rt(s): def rt(s):
s = language.parse_response(s).spec() s = language.parse_pathod(s).spec()
assert language.parse_response(s).spec() == s assert language.parse_pathod(s).spec() == s
rt("400:b@100g") rt("400:b@100g")
rt("400") rt("400")
rt("400:da") rt("400:da")
def test_websockets(self): def test_websockets(self):
r = language.parse_response("ws") r = language.parse_pathod("ws")
tutils.raises("no websocket key", r.resolve, language.Settings()) tutils.raises("no websocket key", r.resolve, language.Settings())
res = r.resolve(language.Settings(websocket_key="foo")) res = r.resolve(language.Settings(websocket_key="foo"))
assert res.code.string() == "101" assert res.code.string() == "101"
@ -293,9 +293,9 @@ def test_location_shortcut():
def test_shortcuts(): def test_shortcuts():
assert language.parse_response( assert language.parse_pathod(
"400:c'foo'").headers[0].key.val == "Content-Type" "400:c'foo'").headers[0].key.val == "Content-Type"
assert language.parse_response( assert language.parse_pathod(
"400:l'foo'").headers[0].key.val == "Location" "400:l'foo'").headers[0].key.val == "Location"
assert "Android" in tutils.render(parse_request("get:/:ua")) assert "Android" in tutils.render(parse_request("get:/:ua"))
@ -349,6 +349,6 @@ def test_pathodspec_freeze():
def test_unique_components(): def test_unique_components():
tutils.raises( tutils.raises(
"multiple body clauses", "multiple body clauses",
language.parse_response, language.parse_pathod,
"400:b@1:b@1" "400:b@1:b@1"
) )

View File

@ -6,7 +6,7 @@ import tutils
def parse_request(s): def parse_request(s):
return language.parse_requests(s)[0] return language.parse_pathoc(s)[0]
class TestWebsocketFrame: class TestWebsocketFrame:
@ -36,9 +36,6 @@ class TestWebsocketFrame:
wf2 = parse_request(spec) wf2 = parse_request(spec)
assert wf2.spec() == spec assert wf2.spec() == spec
def test_raw(self):
pass
def test_flags(self): def test_flags(self):
wf = parse_request("wf:fin:mask:rsv1:rsv2:rsv3") wf = parse_request("wf:fin:mask:rsv1:rsv2:rsv3")
frm = netlib.websockets.Frame.from_bytes(tutils.render(wf)) frm = netlib.websockets.Frame.from_bytes(tutils.render(wf))

View File

@ -78,14 +78,14 @@ def test_write_values_pauses():
def test_write_values_after(): def test_write_values_after():
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = language.parse_response("400:da") r = language.parse_pathod("400:da")
language.serve(r, s, {}) language.serve(r, s, {})
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = language.parse_response("400:pa,0") r = language.parse_pathod("400:pa,0")
language.serve(r, s, {}) language.serve(r, s, {})
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = language.parse_response("400:ia,'xx'") r = language.parse_pathod("400:ia,'xx'")
language.serve(r, s, {}) language.serve(r, s, {})
assert s.getvalue().endswith('xx') assert s.getvalue().endswith('xx')

View File

@ -22,7 +22,7 @@ class _TestDaemon:
ssloptions = self.ssloptions, ssloptions = self.ssloptions,
staticdir = tutils.test_data.path("data"), staticdir = tutils.test_data.path("data"),
anchors = [ anchors = [
(re.compile("/anchor/.*"), language.parse_response("202")) (re.compile("/anchor/.*"), language.parse_pathod("202"))
] ]
) )
@ -73,7 +73,7 @@ class _TestDaemon:
if timeout: if timeout:
c.settimeout(timeout) c.settimeout(timeout)
for i in requests: for i in requests:
r = language.parse_requests(i)[0] r = language.parse_pathoc(i)[0]
if explain: if explain:
r = r.freeze(language.Settings()) r = r.freeze(language.Settings())
try: try:

View File

@ -26,7 +26,7 @@ class DaemonTests(object):
klass.d = test.Daemon( klass.d = test.Daemon(
staticdir=test_data.path("data"), staticdir=test_data.path("data"),
anchors=[ anchors=[
(re.compile("/anchor/.*"), language.parse_response("202:da")) (re.compile("/anchor/.*"), language.parse_pathod("202:da"))
], ],
ssl = klass.ssl, ssl = klass.ssl,
ssloptions = so, ssloptions = so,