From 0a59819dcc209fe5c672f4784088ee44c8f6b6c0 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 30 May 2011 08:50:33 +0200 Subject: [PATCH] Made requests shallow-cloneable. --- wrw/__init__.py | 3 +-- wrw/req.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ wrw/util.py | 2 +- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/wrw/__init__.py b/wrw/__init__.py index 61c6e04..aec9825 100644 --- a/wrw/__init__.py +++ b/wrw/__init__.py @@ -1,6 +1,5 @@ -__all__ = ["request", "wsgiwrap", "restart", "cookie", "formdata"] +__all__ = ["wsgiwrap", "restart", "cookie", "formdata"] -from req import request from util import wsgiwrap, persession from dispatch import restart import cookie diff --git a/wrw/req.py b/wrw/req.py index f3dc31f..0d26964 100644 --- a/wrw/req.py +++ b/wrw/req.py @@ -50,6 +50,16 @@ def fixcase(str): return str class request(object): + def copy(self): + return copyrequest(self) + + def shift(self, n): + new = self.copy() + new.uriname = self.uriname + self.pathinfo[:n] + new.pathinfo = self.pathinfo[n:] + return new + +class origrequest(request): def __init__(self, env): self.env = env self.uriname = env["SCRIPT_NAME"] @@ -117,3 +127,37 @@ class request(object): for val in self.ohead.getlist(nm): hdrs.append((nm, val)) startreq("%s %s" % self.statuscode, hdrs) + + def topreq(self): + return self + +class copyrequest(request): + def __init__(self, p): + self.parent = p + self.top = p.topreq() + self.env = p.env + self.uriname = p.uriname + self.filename = p.filename + self.uri = p.uri + self.pathinfo = p.pathinfo + self.query = p.query + self.remoteaddr = p.remoteaddr + self.serverport = p.serverport + self.https = p.https + self.ihead = p.ihead + self.ohead = p.ohead + + def status(self, code, msg): + return self.parent.status(code, msg) + + def item(self, id): + return self.top.item(id) + + def withres(self, res): + return self.top.withres(res) + + def oncommit(self, fn): + return self.top.oncommit(fn) + + def topreq(self): + return self.parent.topreq() diff --git a/wrw/util.py b/wrw/util.py index 86836bd..09b9e36 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -2,7 +2,7 @@ import req, dispatch, session def wsgiwrap(callable): def wrapper(env, startreq): - return dispatch.handle(req.request(env), startreq, callable) + return dispatch.handle(req.origrequest(env), startreq, callable) return wrapper def persession(data = None): -- 2.11.0