From 7922e70037f56d71494cbe0c24fd7536ff76eeee Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 20 Jun 2012 10:51:37 +1200 Subject: [PATCH] Basic response generation. --- libpathod/pathod.py | 20 +++++++++++++++++--- libpathod/rparse.py | 28 ++++++++-------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/libpathod/pathod.py b/libpathod/pathod.py index c8608bd67..9fabc026e 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -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 diff --git a/libpathod/rparse.py b/libpathod/rparse.py index aa59d021f..677c6b546 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -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()