Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#=================================================================
class AccessException(WbException):
def status(self):
return '403 Access Denied'
#=================================================================
class BadRequestException(WbException):
def status(self):
return '400 Bad Request'
#=================================================================
class NotFoundException(WbException):
def status(self):
return '404 Not Found'
#=================================================================
class LiveResourceException(WbException):
def status(self):
return '400 Bad Live Resource'
def load_child_source(self, name, source, params):
try:
params['_name'] = name
params['_formatter'] = ParamFormatter(params, name)
res = source.load_index(params)
if isinstance(res, tuple):
cdx_iter, err_list = res
else:
cdx_iter = res
err_list = []
except WbException as wbe:
#print('Not found in ' + name)
cdx_iter = iter([])
err_list = [(name, repr(wbe))]
def add_source(cdx, name):
if not cdx.get('url'):
return cdx
if cdx.get('source'):
cdx['source'] = name + ':' + cdx['source']
else:
cdx['source'] = name
cdx['source-coll'] = self._get_coll(name)
return cdx
def __repr__(self):
return "{0}('{1}',)".format(self.__class__.__name__, self.msg)
# Default Error Code
# def status(self):
# return '500 Internal Server Error'
#=================================================================
class AccessException(WbException):
def status(self):
return '403 Access Denied'
#=================================================================
class BadRequestException(WbException):
def status(self):
return '400 Bad Request'
#=================================================================
class NotFoundException(WbException):
def status(self):
return '404 Not Found'
#=================================================================
class LiveResourceException(WbException):
def status(self):
return '400 Bad Live Resource'
def handle_methods(self, env, start_response):
wb_router = self.wb_router
response = None
try:
response = wb_router(env)
if not response:
if self.fallback_app:
return self.fallback_app(env, start_response)
else:
msg = 'No handler for "{0}".'.format(env['REL_REQUEST_URI'])
raise NotFoundException(msg)
except WbException as e:
response = self.handle_exception(env, e, False)
except Exception as e:
response = self.handle_exception(env, e, True)
return response(env, start_response)
if self.cookie:
request.add_header('Cookie', self.cookie)
response = urllib2.urlopen(request)
except urllib2.HTTPError as e:
if e.code == 403:
raise AccessException('Access Denied')
elif e.code == 404:
# return empty list for consistency with other cdx sources
# will be converted to 404 if no other retry
return []
elif e.code == 400:
raise BadRequestException()
else:
raise WbException('Invalid response from remote cdx server')
return iter(response)
if not timestamp:
# if timestamp is not available, fall back on today's date
now = datetime.now()
timestamp = '{:%Y%m%d%H%M%S}'.format(now)
raise CustomTemplateException(status='404 Not Found',
template_path='archive/archive-error.html',
template_kwargs={
'content_host': settings.PLAYBACK_HOST,
'err_url': url,
'timestamp': timestamp,
})
raise NotFoundException('No Captures found for: %s' % url, url=url)
class CustomTemplateException(WbException):
def __init__(self, msg=None, url=None, status='500 Internal Server Error', template_path=None, template_kwargs=None):
if not template_kwargs:
template_kwargs = {}
self.status_string = status
self.template_path = template_path
self.template_kwargs = template_kwargs
super(CustomTemplateException, self).__init__(msg, url)
def status(self):
return self.status_string
def render_response(self):
return PermaTemplateView(self.template_path).render_response(status=self.status(), **self.template_kwargs)
# monkey-patch WSGIApp.handle_exception to add custom exception handling
real_handle_exception = WSGIApp.handle_exception
class WbException(Exception):
def __init__(self, msg=None, url=None):
Exception.__init__(self, msg)
self.msg = msg
self.url = url
def __repr__(self):
return "{0}('{1}',)".format(self.__class__.__name__, self.msg)
# Default Error Code
# def status(self):
# return '500 Internal Server Error'
#=================================================================
class AccessException(WbException):
def status(self):
return '403 Access Denied'
#=================================================================
class BadRequestException(WbException):
def status(self):
return '400 Bad Request'
#=================================================================
class NotFoundException(WbException):
def status(self):
return '404 Not Found'
from pywb.utils.wbexception import WbException
# Exceptions that effect a specific capture and result in a retry
class CaptureException(WbException):
def status(self):
return '502 Internal Server Error'
#=================================================================
#ArcWarcRecord = collections.namedtuple('ArcWarcRecord',
# 'format, rec_type, rec_headers, ' +
# 'stream, status_headers, ' +
# 'content_type, length')
#=================================================================
class ArcWarcRecord(object):
def __init__(self, *args):
(self.format, self.rec_type, self.rec_headers, self.stream,
self.status_headers, self.content_type, self.length) = args
#=================================================================
class ArchiveLoadFailed(WbException):
def __init__(self, reason, filename=''):
if filename:
msg = filename + ': ' + str(reason)
else:
msg = str(reason)
super(ArchiveLoadFailed, self).__init__(msg)
def status(self):
return '503 Service Unavailable'
#=================================================================
class ArcWarcRecordLoader(object):
WARC_TYPES = ['WARC/1.0', 'WARC/0.17', 'WARC/0.18']
def select_coll_response(self, env, default_coll=None):
raise WbException('Invalid Proxy Collection Specified: ' + str(default_coll))