2012-04-29 22:09:16 +00:00
|
|
|
import operator, string, random, mmap, os, time
|
2012-04-28 00:42:03 +00:00
|
|
|
import contrib.pyparsing as pp
|
2012-06-23 06:34:35 +00:00
|
|
|
from netlib import http_status
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-06-24 05:01:04 +00:00
|
|
|
BLOCKSIZE = 1024
|
|
|
|
|
2012-04-28 02:43:57 +00:00
|
|
|
class ParseException(Exception):
|
|
|
|
def __init__(self, msg, s, col):
|
|
|
|
Exception.__init__(self)
|
|
|
|
self.msg = msg
|
|
|
|
self.s = s
|
|
|
|
self.col = col
|
|
|
|
|
|
|
|
def marked(self):
|
|
|
|
return "%s\n%s"%(self.s, " "*(self.col-1) + "^")
|
|
|
|
|
2012-04-28 05:12:39 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.msg
|
|
|
|
|
2012-04-28 02:43:57 +00:00
|
|
|
|
2012-04-28 01:16:51 +00:00
|
|
|
class ServerError(Exception): pass
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def ready_actions(length, lst):
|
2012-06-24 05:01:04 +00:00
|
|
|
ret = []
|
|
|
|
for i in lst:
|
|
|
|
itms = list(i)
|
|
|
|
if i[0] == "r":
|
2012-06-24 05:23:37 +00:00
|
|
|
itms[0] = random.randrange(length)
|
2012-06-24 05:01:04 +00:00
|
|
|
if i[0] == "a":
|
2012-06-24 05:23:37 +00:00
|
|
|
itms[0] = length+1
|
2012-06-24 05:01:04 +00:00
|
|
|
ret.append(tuple(itms))
|
|
|
|
ret.sort()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def write_values(fp, vals, actions, sofar=0, skip=0, blocksize=BLOCKSIZE):
|
|
|
|
"""
|
|
|
|
vals: A list of values, which may be strings or Value objects.
|
|
|
|
actions: A list of (offset, action, arg) tuples. Action may be "pause" or "disconnect".
|
|
|
|
|
|
|
|
Return True if connection should disconnect.
|
|
|
|
"""
|
|
|
|
while vals:
|
|
|
|
part = vals.pop()
|
|
|
|
for i in range(skip, len(part), blocksize):
|
|
|
|
d = part[i:i+blocksize]
|
|
|
|
if actions and actions[-1][0] < (sofar + len(d)):
|
|
|
|
p = actions.pop()
|
|
|
|
offset = p[0]-sofar
|
|
|
|
vals.append(part)
|
|
|
|
if p[1] == "pause":
|
|
|
|
fp.write(d[:offset])
|
|
|
|
time.sleep(p[2])
|
|
|
|
return write_values(
|
|
|
|
fp, vals, actions,
|
|
|
|
sofar=sofar+offset,
|
|
|
|
skip=i+offset,
|
|
|
|
blocksize=blocksize
|
|
|
|
)
|
|
|
|
elif p[1] == "disconnect":
|
|
|
|
fp.write(d[:offset])
|
|
|
|
return True
|
|
|
|
fp.write(d)
|
|
|
|
sofar += len(d)
|
|
|
|
skip = 0
|
|
|
|
|
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
DATATYPES = dict(
|
|
|
|
ascii_letters = string.ascii_letters,
|
|
|
|
ascii_lowercase = string.ascii_lowercase,
|
|
|
|
ascii_uppercase = string.ascii_uppercase,
|
|
|
|
digits = string.digits,
|
|
|
|
hexdigits = string.hexdigits,
|
|
|
|
letters = string.letters,
|
|
|
|
lowercase = string.lowercase,
|
|
|
|
octdigits = string.octdigits,
|
|
|
|
printable = string.printable,
|
|
|
|
punctuation = string.punctuation,
|
|
|
|
uppercase = string.uppercase,
|
|
|
|
whitespace = string.whitespace,
|
|
|
|
ascii = string.printable,
|
|
|
|
bytes = "".join(chr(i) for i in range(256))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
v_integer = pp.Regex(r"[+-]?\d+")\
|
|
|
|
.setName("integer")\
|
|
|
|
.setParseAction(lambda toks: int(toks[0]))
|
|
|
|
|
2012-04-28 05:12:39 +00:00
|
|
|
|
|
|
|
v_literal = pp.MatchFirst(
|
2012-04-28 00:42:03 +00:00
|
|
|
[
|
|
|
|
pp.QuotedString("\"", escChar="\\", unquoteResults=True),
|
|
|
|
pp.QuotedString("'", escChar="\\", unquoteResults=True),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2012-04-28 05:12:39 +00:00
|
|
|
v_naked_literal = pp.MatchFirst(
|
2012-04-28 00:42:03 +00:00
|
|
|
[
|
2012-04-28 05:12:39 +00:00
|
|
|
v_literal,
|
2012-04-28 00:42:03 +00:00
|
|
|
pp.Word("".join(i for i in pp.printables if i not in ",:"))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LiteralGenerator:
|
|
|
|
def __init__(self, s):
|
|
|
|
self.s = s
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self[:] == other
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.s)
|
|
|
|
|
|
|
|
def __getitem__(self, x):
|
|
|
|
return self.s.__getitem__(x)
|
|
|
|
|
|
|
|
def __getslice__(self, a, b):
|
|
|
|
return self.s.__getslice__(a, b)
|
|
|
|
|
|
|
|
|
|
|
|
class RandomGenerator:
|
|
|
|
def __init__(self, chars, length):
|
|
|
|
self.chars = chars
|
|
|
|
self.length = length
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.length
|
|
|
|
|
|
|
|
def __getitem__(self, x):
|
|
|
|
return random.choice(self.chars)
|
|
|
|
|
|
|
|
def __getslice__(self, a, b):
|
|
|
|
b = min(b, self.length)
|
|
|
|
return "".join(random.choice(self.chars) for x in range(a, b))
|
|
|
|
|
|
|
|
|
|
|
|
class FileGenerator:
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
2012-04-28 01:16:51 +00:00
|
|
|
self.fp = file(path, "r")
|
|
|
|
self.map = mmap.mmap(self.fp.fileno(), 0, prot=mmap.PROT_READ)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.map)
|
|
|
|
|
|
|
|
def __getitem__(self, x):
|
|
|
|
return self.map.__getitem__(x)
|
|
|
|
|
|
|
|
def __getslice__(self, a, b):
|
|
|
|
return self.map.__getslice__(a, b)
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ValueLiteral:
|
|
|
|
def __init__(self, val):
|
|
|
|
self.val = val
|
|
|
|
|
|
|
|
def get_generator(self, settings):
|
|
|
|
return LiteralGenerator(self.val)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
e = v_literal.copy()
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.val
|
|
|
|
|
|
|
|
|
|
|
|
class ValueGenerate:
|
|
|
|
UNITS = dict(
|
|
|
|
b = 1024**0,
|
|
|
|
k = 1024**1,
|
|
|
|
m = 1024**2,
|
|
|
|
g = 1024**3,
|
|
|
|
t = 1024**4,
|
|
|
|
)
|
|
|
|
def __init__(self, usize, unit, datatype):
|
|
|
|
if not unit:
|
|
|
|
unit = "b"
|
|
|
|
self.usize, self.unit, self.datatype = usize, unit, datatype
|
|
|
|
|
|
|
|
def bytes(self):
|
|
|
|
return self.usize * self.UNITS[self.unit]
|
|
|
|
|
|
|
|
def get_generator(self, settings):
|
|
|
|
return RandomGenerator(DATATYPES[self.datatype], self.bytes())
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
2012-04-29 02:20:27 +00:00
|
|
|
e = pp.Literal("@").suppress() + v_integer
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
u = reduce(operator.or_, [pp.Literal(i) for i in klass.UNITS.keys()])
|
|
|
|
e = e + pp.Optional(u, default=None)
|
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
s = pp.Literal(",").suppress()
|
2012-04-28 00:42:03 +00:00
|
|
|
s += reduce(operator.or_, [pp.Literal(i) for i in DATATYPES.keys()])
|
|
|
|
e += pp.Optional(s, default="bytes")
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
def __str__(self):
|
2012-04-29 02:20:27 +00:00
|
|
|
return "@%s%s,%s"%(self.usize, self.unit, self.datatype)
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ValueFile:
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
e = pp.Literal("<").suppress()
|
2012-04-28 05:12:39 +00:00
|
|
|
e = e + v_naked_literal
|
2012-04-28 00:42:03 +00:00
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
def get_generator(self, settings):
|
2012-04-28 01:16:51 +00:00
|
|
|
sd = settings.get("staticdir")
|
|
|
|
if not sd:
|
|
|
|
raise ServerError("No static directory specified.")
|
|
|
|
path = os.path.join(sd, self.path)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
raise ServerError("Static file does not exist: %s"%path)
|
|
|
|
return FileGenerator(path)
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "<%s"%(self.path)
|
|
|
|
|
|
|
|
|
|
|
|
Value = pp.MatchFirst(
|
|
|
|
[
|
|
|
|
ValueGenerate.expr(),
|
|
|
|
ValueFile.expr(),
|
|
|
|
ValueLiteral.expr()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-04-28 10:51:36 +00:00
|
|
|
class ShortcutContentType:
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def accept(self, settings, r):
|
2012-04-28 10:51:36 +00:00
|
|
|
r.headers.append(
|
|
|
|
(
|
|
|
|
LiteralGenerator("Content-Type"),
|
|
|
|
self.value.get_generator(settings)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
e = pp.Literal("c").suppress()
|
|
|
|
e = e + Value
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
|
2012-04-28 10:54:45 +00:00
|
|
|
|
|
|
|
class ShortcutLocation:
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def accept(self, settings, r):
|
2012-04-28 10:54:45 +00:00
|
|
|
r.headers.append(
|
|
|
|
(
|
|
|
|
LiteralGenerator("Location"),
|
|
|
|
self.value.get_generator(settings)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
e = pp.Literal("l").suppress()
|
|
|
|
e = e + Value
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
class Body:
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def accept(self, settings, r):
|
2012-04-28 00:42:03 +00:00
|
|
|
r.body = self.value.get_generator(settings)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
2012-04-28 05:12:39 +00:00
|
|
|
e = pp.Literal("b").suppress()
|
2012-04-28 00:42:03 +00:00
|
|
|
e = e + Value
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
class Method:
|
|
|
|
methods = [
|
|
|
|
"get",
|
|
|
|
"head",
|
|
|
|
"post",
|
|
|
|
"put",
|
|
|
|
"delete",
|
|
|
|
"options",
|
|
|
|
"trace",
|
|
|
|
"connect",
|
|
|
|
]
|
|
|
|
def __init__(self, value):
|
|
|
|
# If it's a string, we were passed one of the methods, so we upper-case
|
|
|
|
# it to be canonical. The user can specify a different case by using a
|
|
|
|
# string value literal.
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
value = value.upper()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def accept(self, settings, r):
|
|
|
|
r.method = self.value.get_generator(settings)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
parts = [pp.CaselessLiteral(i) for i in klass.methods]
|
|
|
|
m = pp.MatchFirst(parts)
|
|
|
|
spec = m | Value.copy()
|
|
|
|
spec = spec.setParseAction(lambda x: klass(*x))
|
|
|
|
return spec
|
|
|
|
|
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
class PauseAt:
|
|
|
|
def __init__(self, seconds, offset):
|
|
|
|
self.seconds, self.offset = seconds, offset
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
2012-04-29 02:20:27 +00:00
|
|
|
e = pp.Literal("p").suppress()
|
2012-06-24 05:23:37 +00:00
|
|
|
e += pp.MatchFirst(
|
2012-04-29 02:20:27 +00:00
|
|
|
[
|
|
|
|
v_integer,
|
|
|
|
pp.Literal("f")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
e += pp.Literal(",").suppress()
|
|
|
|
e += pp.MatchFirst(
|
2012-04-28 00:42:03 +00:00
|
|
|
[
|
|
|
|
v_integer,
|
2012-04-29 02:20:27 +00:00
|
|
|
pp.Literal("r"),
|
|
|
|
pp.Literal("a"),
|
2012-04-28 00:42:03 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def accept(self, settings, r):
|
2012-04-29 02:20:27 +00:00
|
|
|
r.actions.append((self.offset, "pause", self.seconds))
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
class DisconnectAt:
|
2012-04-28 00:42:03 +00:00
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def accept(self, settings, r):
|
2012-04-29 02:20:27 +00:00
|
|
|
r.actions.append((self.value, "disconnect"))
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
e = pp.Literal("d").suppress()
|
|
|
|
e = e + pp.MatchFirst(
|
|
|
|
[
|
|
|
|
v_integer,
|
|
|
|
pp.Literal("r")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Header:
|
|
|
|
def __init__(self, key, value):
|
|
|
|
self.key, self.value = key, value
|
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def accept(self, settings, r):
|
2012-04-28 00:42:03 +00:00
|
|
|
r.headers.append(
|
|
|
|
(
|
|
|
|
self.key.get_generator(settings),
|
|
|
|
self.value.get_generator(settings)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
2012-04-28 05:12:39 +00:00
|
|
|
e = pp.Literal("h").suppress()
|
2012-04-28 00:42:03 +00:00
|
|
|
e += Value
|
2012-04-28 05:28:40 +00:00
|
|
|
e += pp.Literal("=").suppress()
|
2012-04-28 00:42:03 +00:00
|
|
|
e += Value
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
|
|
|
|
class Code:
|
|
|
|
def __init__(self, code, msg=None):
|
|
|
|
self.code, self.msg = code, msg
|
|
|
|
if msg is None:
|
2012-06-23 06:34:35 +00:00
|
|
|
self.msg = ValueLiteral(http_status.RESPONSES.get(self.code, "Unknown code"))
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-06-24 05:23:37 +00:00
|
|
|
def accept(self, settings, r):
|
2012-04-28 00:42:03 +00:00
|
|
|
r.code = self.code
|
|
|
|
r.msg = self.msg.get_generator(settings)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
e = v_integer
|
|
|
|
e = e + pp.Optional(
|
2012-04-28 05:12:39 +00:00
|
|
|
Value
|
2012-04-28 00:42:03 +00:00
|
|
|
)
|
|
|
|
return e.setParseAction(lambda x: klass(*x))
|
|
|
|
|
|
|
|
|
|
|
|
class Response:
|
2012-04-28 10:28:28 +00:00
|
|
|
comps = (
|
2012-04-28 00:42:03 +00:00
|
|
|
Body,
|
|
|
|
Header,
|
2012-04-29 02:20:27 +00:00
|
|
|
PauseAt,
|
|
|
|
DisconnectAt,
|
2012-04-28 10:51:36 +00:00
|
|
|
ShortcutContentType,
|
2012-04-28 10:54:45 +00:00
|
|
|
ShortcutLocation,
|
2012-04-28 10:28:28 +00:00
|
|
|
)
|
2012-04-28 00:42:03 +00:00
|
|
|
version = "HTTP/1.1"
|
|
|
|
code = 200
|
2012-06-23 06:34:35 +00:00
|
|
|
msg = LiteralGenerator(http_status.RESPONSES[code])
|
2012-04-29 05:37:47 +00:00
|
|
|
body = LiteralGenerator("")
|
2012-04-28 02:43:57 +00:00
|
|
|
def __init__(self):
|
2012-04-28 00:42:03 +00:00
|
|
|
self.headers = []
|
2012-04-28 10:28:28 +00:00
|
|
|
self.actions = []
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
def get_header(self, hdr):
|
|
|
|
for k, v in self.headers:
|
|
|
|
if k[:len(hdr)].lower() == hdr:
|
|
|
|
return v
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def expr(klass):
|
|
|
|
parts = [i.expr() for i in klass.comps]
|
|
|
|
atom = pp.MatchFirst(parts)
|
|
|
|
resp = pp.And(
|
|
|
|
[
|
|
|
|
Code.expr(),
|
2012-04-28 05:28:40 +00:00
|
|
|
pp.ZeroOrMore(pp.Literal(":").suppress() + atom)
|
2012-04-28 00:42:03 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
def length(self):
|
|
|
|
l = len("%s %s "%(self.version, self.code))
|
|
|
|
l += len(self.msg)
|
|
|
|
l += 2
|
|
|
|
for i in self.headers:
|
|
|
|
l += len(i[0]) + len(i[1])
|
|
|
|
l += 4
|
|
|
|
l += 2
|
|
|
|
l += len(self.body)
|
|
|
|
return l
|
|
|
|
|
2012-04-29 02:59:54 +00:00
|
|
|
def serve(self, fp):
|
|
|
|
started = time.time()
|
2012-04-28 02:43:57 +00:00
|
|
|
if self.body and not self.get_header("Content-Length"):
|
|
|
|
self.headers.append(
|
|
|
|
(
|
|
|
|
LiteralGenerator("Content-Length"),
|
|
|
|
LiteralGenerator(str(len(self.body))),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
hdrs = []
|
|
|
|
for k, v in self.headers:
|
|
|
|
hdrs.extend([
|
|
|
|
k,
|
|
|
|
": ",
|
|
|
|
v,
|
|
|
|
"\r\n",
|
|
|
|
])
|
|
|
|
vals = [
|
|
|
|
"%s %s "%(self.version, self.code),
|
|
|
|
self.msg,
|
|
|
|
"\r\n",
|
|
|
|
]
|
|
|
|
vals.extend(hdrs)
|
2012-04-29 05:37:47 +00:00
|
|
|
vals.append("\r\n")
|
|
|
|
if self.body:
|
|
|
|
vals.append(self.body)
|
2012-04-28 00:42:03 +00:00
|
|
|
vals.reverse()
|
2012-06-24 05:01:04 +00:00
|
|
|
actions = ready_actions(self.length(), self.actions)
|
2012-04-28 10:28:28 +00:00
|
|
|
actions.reverse()
|
2012-06-24 05:01:04 +00:00
|
|
|
disconnect = write_values(fp, vals, actions[:])
|
2012-04-29 02:59:54 +00:00
|
|
|
duration = time.time() - started
|
|
|
|
return dict(
|
2012-06-20 22:56:30 +00:00
|
|
|
disconnect = disconnect,
|
2012-04-29 02:59:54 +00:00
|
|
|
started = started,
|
|
|
|
duration = duration,
|
|
|
|
actions = actions,
|
2012-04-29 04:22:33 +00:00
|
|
|
code = self.code,
|
2012-04-29 02:59:54 +00:00
|
|
|
)
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
parts = [
|
|
|
|
"%s %s"%(self.code, self.msg[:])
|
|
|
|
]
|
|
|
|
return "\n".join(parts)
|
|
|
|
|
|
|
|
|
2012-04-28 02:43:57 +00:00
|
|
|
class CraftedResponse(Response):
|
2012-04-29 02:59:54 +00:00
|
|
|
def __init__(self, settings, spec, tokens):
|
2012-04-28 02:43:57 +00:00
|
|
|
Response.__init__(self)
|
2012-04-29 02:59:54 +00:00
|
|
|
self.spec, self.tokens = spec, tokens
|
2012-04-28 02:43:57 +00:00
|
|
|
for i in tokens:
|
2012-06-24 05:23:37 +00:00
|
|
|
i.accept(settings, self)
|
2012-04-28 02:43:57 +00:00
|
|
|
|
2012-04-29 02:59:54 +00:00
|
|
|
def serve(self, fp):
|
|
|
|
d = Response.serve(self, fp)
|
|
|
|
d["spec"] = self.spec
|
|
|
|
return d
|
|
|
|
|
2012-04-28 02:43:57 +00:00
|
|
|
|
|
|
|
class InternalResponse(Response):
|
2012-04-28 00:42:03 +00:00
|
|
|
def __init__(self, code, body):
|
2012-04-28 02:43:57 +00:00
|
|
|
Response.__init__(self)
|
2012-04-28 00:42:03 +00:00
|
|
|
self.code = code
|
2012-06-23 06:34:35 +00:00
|
|
|
self.msg = LiteralGenerator(http_status.RESPONSES.get(code, "Unknown error"))
|
2012-04-28 00:42:03 +00:00
|
|
|
self.body = LiteralGenerator(body)
|
|
|
|
self.headers = [
|
|
|
|
(
|
|
|
|
LiteralGenerator("Content-Type"),
|
|
|
|
LiteralGenerator("text/plain")
|
|
|
|
),
|
|
|
|
(
|
|
|
|
LiteralGenerator("Content-Length"),
|
|
|
|
LiteralGenerator(str(len(self.body)))
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2012-04-29 02:59:54 +00:00
|
|
|
def serve(self, fp):
|
|
|
|
d = Response.serve(self, fp)
|
|
|
|
d["internal"] = True
|
|
|
|
return d
|
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-06-24 05:01:04 +00:00
|
|
|
def parse_response(settings, s):
|
2012-04-28 00:42:03 +00:00
|
|
|
try:
|
2012-04-29 02:59:54 +00:00
|
|
|
return CraftedResponse(settings, s, Response.expr().parseString(s, parseAll=True))
|
2012-04-28 00:42:03 +00:00
|
|
|
except pp.ParseException, v:
|
2012-04-28 02:43:57 +00:00
|
|
|
raise ParseException(v.msg, v.line, v.col)
|