Basic response generation.

This commit is contained in:
Aldo Cortesi 2012-06-20 10:51:37 +12:00
parent 892ee2a904
commit 7922e70037
2 changed files with 25 additions and 23 deletions

View File

@ -1,5 +1,6 @@
import urllib
from netlib import tcp, protocol, odict, wsgi from netlib import tcp, protocol, odict, wsgi
import version, app import version, app, rparse
class PathodHandler(tcp.BaseHandler): class PathodHandler(tcp.BaseHandler):
@ -11,6 +12,19 @@ class PathodHandler(tcp.BaseHandler):
return None return None
method, path, httpversion = protocol.parse_init_http(line) method, path, httpversion = protocol.parse_init_http(line)
if path.startswith(self.server.prefix):
spec = urllib.unquote(path)[len(self.server.prefix):]
try:
presp = rparse.parse({}, spec)
except rparse.ParseException, v:
presp = rparse.InternalResponse(
800,
"Error parsing response spec: %s\n"%v.msg + v.marked()
)
presp.serve(self.wfile)
self.finish()
return
headers = odict.ODictCaseless(protocol.read_headers(self.rfile)) headers = odict.ODictCaseless(protocol.read_headers(self.rfile))
content = protocol.read_http_body_request( content = protocol.read_http_body_request(
self.rfile, self.wfile, headers, httpversion, None self.rfile, self.wfile, headers, httpversion, None
@ -27,10 +41,10 @@ class PathodHandler(tcp.BaseHandler):
app.serve(req, self.wfile) app.serve(req, self.wfile)
class Pathod(tcp.TCPServer): class Pathod(tcp.TCPServer):
def __init__(self, addr): def __init__(self, addr, prefix="/p/"):
tcp.TCPServer.__init__(self, addr) tcp.TCPServer.__init__(self, addr)
self.prefix = prefix
self.app = app.app self.app = app.app
self.app.config["pathod"] = self self.app.config["pathod"] = self

View File

@ -1,7 +1,6 @@
import operator, string, random, mmap, os, time import operator, string, random, mmap, os, time
import contrib.pyparsing as pp import contrib.pyparsing as pp
import http import http
import tornado.ioloop
TESTING = False TESTING = False
@ -390,12 +389,6 @@ class Response:
ret.sort() ret.sort()
return ret return ret
def add_timeout(self, s, callback):
if TESTING:
callback()
else: # pragma: no cover
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + s, callback)
def write_values(self, fp, vals, actions, sofar=0, skip=0, blocksize=BLOCKSIZE): def write_values(self, fp, vals, actions, sofar=0, skip=0, blocksize=BLOCKSIZE):
while vals: while vals:
part = vals.pop() part = vals.pop()
@ -406,17 +399,14 @@ class Response:
offset = p[0]-sofar offset = p[0]-sofar
vals.append(part) vals.append(part)
if p[1] == "pause": if p[1] == "pause":
def pause_callback(): fp.write(d[:offset])
self.write_values( time.sleep(p[2])
fp, vals, actions, self.write_values(
sofar=sofar+offset, fp, vals, actions,
skip=i+offset, sofar=sofar+offset,
blocksize=blocksize skip=i+offset,
) blocksize=blocksize
def flushed_callback(): )
# Data has been flushed, set the timeout.
self.add_timeout(p[2], pause_callback)
fp.write(d[:offset], callback=flushed_callback)
return return
elif p[1] == "disconnect": elif p[1] == "disconnect":
fp.write(d[:offset]) fp.write(d[:offset])
@ -426,8 +416,6 @@ class Response:
fp.write(d) fp.write(d)
sofar += len(d) sofar += len(d)
skip = 0 skip = 0
fp.finish()
fp.connection.stream.close()
def serve(self, fp): def serve(self, fp):
started = time.time() started = time.time()