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:
parent
4cf4bf3bbb
commit
072befe3c8
@ -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
|
||||
|
||||
|
@ -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/'])
|
||||
## ===========
|
||||
|
@ -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')
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user