Add Path specification to request parser.

This commit is contained in:
Aldo Cortesi 2012-06-24 18:38:22 +12:00
parent f8622ea914
commit 2ac84be7cb
3 changed files with 31 additions and 5 deletions

View File

@ -5,6 +5,6 @@
</h1> </h1>
</div> </div>
pathoc hostname get:/foo/bar:h"foo"="bar" get:/wah:b@1m pathoc hostname get:"/foo/bar":h"foo"="bar" get:/wah:b@1m
pathoc --ssl hostname get:/foo/bar:h"foo"="bar" get:/wah:b@1m pathoc --ssl hostname get:"/foo/bar":h"foo"="bar" get:"/wah":b@1m

View File

@ -290,6 +290,20 @@ class Body:
return e.setParseAction(lambda x: klass(*x)) return e.setParseAction(lambda x: klass(*x))
class Path:
def __init__(self, value):
self.value = value
def accept(self, settings, r):
r.path = self.value.get_generator(settings)
@classmethod
def expr(klass):
e = Value.copy()
return e.setParseAction(lambda x: klass(*x))
class Method: class Method:
methods = [ methods = [
"get", "get",
@ -416,8 +430,10 @@ class Request:
ShortcutContentType, ShortcutContentType,
) )
version = "HTTP/1.1" version = "HTTP/1.1"
body = LiteralGenerator("")
def __init__(self): def __init__(self):
self.method = None
self.path = None
self.body = LiteralGenerator("")
self.headers = [] self.headers = []
self.actions = [] self.actions = []
@ -428,6 +444,8 @@ class Request:
resp = pp.And( resp = pp.And(
[ [
Method.expr(), Method.expr(),
pp.Literal(":").suppress(),
Path.expr(),
pp.ZeroOrMore(pp.Literal(":").suppress() + atom) pp.ZeroOrMore(pp.Literal(":").suppress() + atom)
] ]
) )

View File

@ -88,9 +88,13 @@ class TestMisc:
assert rparse.Value.parseString('"val"')[0].val == "val" assert rparse.Value.parseString('"val"')[0].val == "val"
assert rparse.Value.parseString('"\'val\'"')[0].val == "'val'" assert rparse.Value.parseString('"\'val\'"')[0].val == "'val'"
def test_path(self):
e = rparse.Path.expr()
assert e.parseString('"/foo"')[0].value.val == "/foo"
def test_method(self): def test_method(self):
e = rparse.Method.expr() e = rparse.Method.expr()
assert e.parseString("get")[0].value == "GET" assert e.parseString("get")[0].value.val == "GET"
assert e.parseString("'foo'")[0].value.val == "foo" assert e.parseString("'foo'")[0].value.val == "foo"
assert e.parseString("'get'")[0].value.val == "get" assert e.parseString("'get'")[0].value.val == "get"
@ -189,9 +193,13 @@ class TestPauses:
class TestParseRequest: class TestParseRequest:
def test_err(self):
tutils.raises(rparse.ParseException, rparse.parse_request, {}, 'GET')
def test_simple(self): def test_simple(self):
r = rparse.parse_request({}, "GET") r = rparse.parse_request({}, 'GET:"/foo"')
assert r.method == "GET" assert r.method == "GET"
assert r.path == "/foo"
class TestParseResponse: class TestParseResponse: