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
import version, app
import version, app, rparse
class PathodHandler(tcp.BaseHandler):
@ -11,6 +12,19 @@ class PathodHandler(tcp.BaseHandler):
return None
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))
content = protocol.read_http_body_request(
self.rfile, self.wfile, headers, httpversion, None
@ -27,10 +41,10 @@ class PathodHandler(tcp.BaseHandler):
app.serve(req, self.wfile)
class Pathod(tcp.TCPServer):
def __init__(self, addr):
def __init__(self, addr, prefix="/p/"):
tcp.TCPServer.__init__(self, addr)
self.prefix = prefix
self.app = app.app
self.app.config["pathod"] = self

View File

@ -1,7 +1,6 @@
import operator, string, random, mmap, os, time
import contrib.pyparsing as pp
import http
import tornado.ioloop
TESTING = False
@ -390,12 +389,6 @@ class Response:
ret.sort()
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):
while vals:
part = vals.pop()
@ -406,17 +399,14 @@ class Response:
offset = p[0]-sofar
vals.append(part)
if p[1] == "pause":
def pause_callback():
self.write_values(
fp, vals, actions,
sofar=sofar+offset,
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)
fp.write(d[:offset])
time.sleep(p[2])
self.write_values(
fp, vals, actions,
sofar=sofar+offset,
skip=i+offset,
blocksize=blocksize
)
return
elif p[1] == "disconnect":
fp.write(d[:offset])
@ -426,8 +416,6 @@ class Response:
fp.write(d)
sofar += len(d)
skip = 0
fp.finish()
fp.connection.stream.close()
def serve(self, fp):
started = time.time()