From 072befe3c8bd16de32362592edd8d6a24da7877c Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Fri, 20 Dec 2013 15:10:12 -0800 Subject: [PATCH] archivalrouter: support handler chaining, using call convention and pass prev response --- pywb/archivalrouter.py | 18 +++++++++++++----- pywb/wbapp.py | 12 ++++++------ pywb/wbrequestresponse.py | 10 +++++----- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/pywb/archivalrouter.py b/pywb/archivalrouter.py index 1e3afe57..da122a9e 100644 --- a/pywb/archivalrouter.py +++ b/pywb/archivalrouter.py @@ -27,11 +27,19 @@ class ArchivalRequestRouter: return handler, req - return self.fallback, WbRequest(env) + return self.fallback, WbRequest.from_uri(request_uri, env) def handleRequest(self, env): handler, wbrequest = self._parseRequest(env) - return handler.run(wbrequest) + resp = None + + if isinstance(handler, list): + for x in handler: + resp = x(wbrequest, resp) + else: + resp = handler(wbrequest, resp) + + return resp def getPrefix(self, env, rel_prefix): if self.abs_path: @@ -75,7 +83,7 @@ class ReferRedirect: self.matchPrefixs = [matchPrefixs] - def run(self, wbrequest): + def __call__(self, wbrequest, _): if wbrequest.referrer is None: return None @@ -109,8 +117,8 @@ if __name__ == "__main__": env = {'REQUEST_URI': request_uri, 'HTTP_REFERER': referrer} redir = ReferRedirect(matchHost) - req = WbRequest.parse(env) - rep = redir.run(req) + req = WbRequest.from_uri(request_uri, env) + rep = redir(req, None) if not rep: return False diff --git a/pywb/wbapp.py b/pywb/wbapp.py index 125a820d..53edd319 100644 --- a/pywb/wbapp.py +++ b/pywb/wbapp.py @@ -7,11 +7,11 @@ from wbrequestresponse import WbResponse from archivalrouter import ArchivalRequestRouter class EchoEnv: - def run(self, wbrequest): + def __call__(self, wbrequest, _): return WbResponse.text_response(str(wbrequest.env)) class WBHandler: - def run(self, wbrequest): + def __call__(self, wbrequest, _): return WbResponse.text_response(str(wbrequest)) class QueryHandler: @@ -19,7 +19,7 @@ class QueryHandler: self.cdxserver = indexreader.RemoteCDXServer('http://web.archive.org/cdx/search/cdx') - def run(self, wbrequest): + def __call__(self, wbrequest, prev_wbresponse): wburl = wbrequest.wb_url params = self.cdxserver.getQueryParams(wburl) @@ -38,9 +38,9 @@ class QueryHandler: ## =========== parser = ArchivalRequestRouter( { - 't0' : EchoEnv(), - 't1' : WBHandler(), - 't2' : QueryHandler() + 't0' : [EchoEnv()], + 't1' : [WBHandler()], + 't2' : [QueryHandler()] }, hostpaths = ['http://localhost:9090/']) ## =========== diff --git a/pywb/wbrequestresponse.py b/pywb/wbrequestresponse.py index 51f81b63..a045edd5 100644 --- a/pywb/wbrequestresponse.py +++ b/pywb/wbrequestresponse.py @@ -3,21 +3,21 @@ from wbarchivalurl import ArchivalUrl class WbRequest: """ - >>> WbRequest.parse({'REQUEST_URI': '/save/_embed/example.com/?a=b'}) + >>> WbRequest.from_uri('/save/_embed/example.com/?a=b') {'wb_url': ('latest_replay', '', '', 'http://_embed/example.com/?a=b', '/http://_embed/example.com/?a=b'), 'coll': 'save', 'wb_prefix': '/save/', 'request_uri': '/save/_embed/example.com/?a=b'} - >>> WbRequest.parse({'REQUEST_URI': '/2345/20101024101112im_/example.com/?b=c'}) + >>> WbRequest.from_uri('/2345/20101024101112im_/example.com/?b=c') {'wb_url': ('replay', '20101024101112', 'im_', 'http://example.com/?b=c', '/20101024101112im_/http://example.com/?b=c'), 'coll': '2345', 'wb_prefix': '/2345/', 'request_uri': '/2345/20101024101112im_/example.com/?b=c'} - >>> WbRequest.parse({'REQUEST_URI': '/2010/example.com'}) + >>> WbRequest.from_uri('/2010/example.com') {'wb_url': ('latest_replay', '', '', 'http://example.com', '/http://example.com'), 'coll': '2010', 'wb_prefix': '/2010/', 'request_uri': '/2010/example.com'} - >>> WbRequest.parse({'REQUEST_URI': '../example.com'}) + >>> WbRequest.from_uri('../example.com') {'wb_url': ('latest_replay', '', '', 'http://example.com', '/http://example.com'), 'coll': '', 'wb_prefix': '/', 'request_uri': '../example.com'} """ @staticmethod - def parse(env, request_uri = ''): + def from_uri(request_uri, env = {}): if not request_uri: request_uri = env.get('REQUEST_URI')