1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +01:00

archivalrouter: support handler chaining, using call convention and pass prev response

This commit is contained in:
Ilya Kreymer 2013-12-20 15:10:12 -08:00
parent 4cf4bf3bbb
commit 072befe3c8
3 changed files with 24 additions and 16 deletions

View File

@ -27,11 +27,19 @@ class ArchivalRequestRouter:
return handler, req return handler, req
return self.fallback, WbRequest(env) return self.fallback, WbRequest.from_uri(request_uri, env)
def handleRequest(self, env): def handleRequest(self, env):
handler, wbrequest = self._parseRequest(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): def getPrefix(self, env, rel_prefix):
if self.abs_path: if self.abs_path:
@ -75,7 +83,7 @@ class ReferRedirect:
self.matchPrefixs = [matchPrefixs] self.matchPrefixs = [matchPrefixs]
def run(self, wbrequest): def __call__(self, wbrequest, _):
if wbrequest.referrer is None: if wbrequest.referrer is None:
return None return None
@ -109,8 +117,8 @@ if __name__ == "__main__":
env = {'REQUEST_URI': request_uri, 'HTTP_REFERER': referrer} env = {'REQUEST_URI': request_uri, 'HTTP_REFERER': referrer}
redir = ReferRedirect(matchHost) redir = ReferRedirect(matchHost)
req = WbRequest.parse(env) req = WbRequest.from_uri(request_uri, env)
rep = redir.run(req) rep = redir(req, None)
if not rep: if not rep:
return False return False

View File

@ -7,11 +7,11 @@ from wbrequestresponse import WbResponse
from archivalrouter import ArchivalRequestRouter from archivalrouter import ArchivalRequestRouter
class EchoEnv: class EchoEnv:
def run(self, wbrequest): def __call__(self, wbrequest, _):
return WbResponse.text_response(str(wbrequest.env)) return WbResponse.text_response(str(wbrequest.env))
class WBHandler: class WBHandler:
def run(self, wbrequest): def __call__(self, wbrequest, _):
return WbResponse.text_response(str(wbrequest)) return WbResponse.text_response(str(wbrequest))
class QueryHandler: class QueryHandler:
@ -19,7 +19,7 @@ class QueryHandler:
self.cdxserver = indexreader.RemoteCDXServer('http://web.archive.org/cdx/search/cdx') 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 wburl = wbrequest.wb_url
params = self.cdxserver.getQueryParams(wburl) params = self.cdxserver.getQueryParams(wburl)
@ -38,9 +38,9 @@ class QueryHandler:
## =========== ## ===========
parser = ArchivalRequestRouter( parser = ArchivalRequestRouter(
{ {
't0' : EchoEnv(), 't0' : [EchoEnv()],
't1' : WBHandler(), 't1' : [WBHandler()],
't2' : QueryHandler() 't2' : [QueryHandler()]
}, },
hostpaths = ['http://localhost:9090/']) hostpaths = ['http://localhost:9090/'])
## =========== ## ===========

View File

@ -3,21 +3,21 @@ from wbarchivalurl import ArchivalUrl
class WbRequest: 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'} {'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'} {'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'} {'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'} {'wb_url': ('latest_replay', '', '', 'http://example.com', '/http://example.com'), 'coll': '', 'wb_prefix': '/', 'request_uri': '../example.com'}
""" """
@staticmethod @staticmethod
def parse(env, request_uri = ''): def from_uri(request_uri, env = {}):
if not request_uri: if not request_uri:
request_uri = env.get('REQUEST_URI') request_uri = env.get('REQUEST_URI')