mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-19 18:29:37 +01:00
85 lines
2.0 KiB
Python
85 lines
2.0 KiB
Python
from query import QueryHandler
|
|
from replay import FullHandler
|
|
import wbexceptions
|
|
|
|
from wbrequestresponse import WbResponse, StatusAndHeaders
|
|
from archivalrouter import ArchivalRequestRouter
|
|
|
|
|
|
## ===========
|
|
class EchoEnv:
|
|
def __call__(self, wbrequest, _):
|
|
return WbResponse.text_response(str(wbrequest.env))
|
|
|
|
class WBHandler:
|
|
def __call__(self, wbrequest, _):
|
|
return WbResponse.text_response(str(wbrequest))
|
|
|
|
|
|
## ===========
|
|
|
|
import testwb
|
|
|
|
query = QueryHandler(testwb.createCdxServer())
|
|
|
|
headInsert = """
|
|
|
|
<!-- WB Insert -->
|
|
<script src='/static/wb.js'> </script>
|
|
<link rel='stylesheet' href='/static/wb.css'/>
|
|
<!-- End WB Insert -->
|
|
"""
|
|
|
|
replay = testwb.createReplay(headInsert)
|
|
|
|
## ===========
|
|
parser = ArchivalRequestRouter(
|
|
{
|
|
't0' : [EchoEnv()],
|
|
't1' : [WBHandler()],
|
|
't2' : [query],
|
|
't3' : [query, replay],
|
|
'web': FullHandler(query, replay)
|
|
},
|
|
hostpaths = ['http://localhost:9090/'])
|
|
## ===========
|
|
|
|
|
|
def application(env, start_response):
|
|
response = None
|
|
|
|
try:
|
|
response = parser.handleRequest(env)
|
|
|
|
if not response:
|
|
raise wbexceptions.NotFoundException(env['REQUEST_URI'] + ' was not found')
|
|
|
|
except wbexceptions.InternalRedirect as ir:
|
|
response = WbResponse(StatusAndHeaders(ir.status, ir.httpHeaders))
|
|
|
|
except (wbexceptions.NotFoundException, wbexceptions.AccessException) as e:
|
|
print "[INFO]: " + str(e)
|
|
response = handleException(env, e)
|
|
|
|
except Exception as e:
|
|
last_exc = e
|
|
import traceback
|
|
traceback.print_exc()
|
|
response = handleException(env, e)
|
|
|
|
return response(env, start_response)
|
|
|
|
|
|
def handleException(env, exc):
|
|
if hasattr(exc, 'status'):
|
|
status = exc.status()
|
|
else:
|
|
status = '400 Bad Request'
|
|
|
|
return WbResponse.text_response(status + ' Error: ' + str(exc), status = status)
|
|
|
|
#def handle_not_found(env):
|
|
# return WbResponse.text_response('Not Found: ' + env['REQUEST_URI'], status = '404 Not Found')
|
|
|
|
|