class StaticHandler(object): def __init__(self, static_path): mimetypes.init() self.static_path = static_path self.block_loader = LocalFileLoader() def __call__(self, environ, url_str): url = url_str.split('?')[0] if url.endswith('/'): url += 'index.html' full_path = environ.get('pywb.static_dir') if full_path: full_path = os.path.join(full_path, url) if not os.path.isfile(full_path): full_path = None if not full_path: full_path = os.path.join(self.static_path, url) try: data = self.block_loader.load(full_path) data.seek(0, 2) size = data.tell() data.seek(0) headers = [('Content-Length', str(size))] reader = None if 'wsgi.file_wrapper' in environ: try: reader = environ['wsgi.file_wrapper'](data) except: pass if not reader: reader = iter(lambda: data.read(), b'') content_type = 'application/octet-stream' guessed = mimetypes.guess_type(full_path) if guessed[0]: content_type = guessed[0] return WbResponse.bin_stream(reader, content_type=content_type, headers=headers) except IOError: raise NotFoundException('Static File Not Found: ' + url_str)
def fetch_local_file(self, uri): #fh = open(uri) fh = LocalFileLoader().load(uri) content_type, _ = mimetypes.guess_type(uri) # create fake headers for local file status_headers = StatusAndHeaders('200 OK', [('Content-Type', content_type)]) stream = fh return (status_headers, stream)
class StaticHandler(object): def __init__(self, static_path): mimetypes.init() self.static_path = static_path self.block_loader = LocalFileLoader() def __call__(self, environ, url_str): url = url_str.split('?')[0] full_path = environ.get('pywb.static_dir') if full_path: full_path = os.path.join(full_path, url) if not os.path.isfile(full_path): full_path = None if not full_path: full_path = os.path.join(self.static_path, url) try: data = self.block_loader.load(full_path) data.seek(0, 2) size = data.tell() data.seek(0) headers = [('Content-Length', str(size))] reader = None if 'wsgi.file_wrapper' in environ: try: reader = environ['wsgi.file_wrapper'](data) except: pass if not reader: reader = iter(lambda: data.read(), b'') content_type = 'application/octet-stream' guessed = mimetypes.guess_type(full_path) if guessed[0]: content_type = guessed[0] return WbResponse.bin_stream(reader, content_type=content_type, headers=headers) except IOError: raise NotFoundException('Static File Not Found: ' + url_str)
class RWApp(RewriterApp): def __init__(self, upstream_urls, cookie_key_templ, redis): config = {} config['url_templates'] = upstream_urls self.cookie_key_templ = cookie_key_templ self.app = Bottle() self.block_loader = LocalFileLoader() self.init_routes() super(RWApp, self).__init__(True, config=config) self.cookie_tracker = CookieTracker(redis) self.orig_error_handler = self.app.default_error_handler self.app.default_error_handler = self.err_handler def err_handler(self, exc): print(exc) import traceback traceback.print_exc() return self.orig_error_handler(exc) def get_cookie_key(self, kwargs): return self.cookie_key_templ.format(**kwargs) def init_routes(self): @self.app.get('/static/__pywb/<filepath:path>') def server_static(filepath): data = self.block_loader.load('pywb/static/' + filepath) guessed = mimetypes.guess_type(filepath) if guessed[0]: response.headers['Content-Type'] = guessed[0] return data self.app.mount('/live/', self.call_with_params(type='live')) self.app.mount('/record/', self.call_with_params(type='record')) self.app.mount('/replay/', self.call_with_params(type='replay')) @staticmethod def create_app(replay_port=8080, record_port=8010): upstream_urls = {'live': 'http://localhost:%s/live/resource/postreq?' % replay_port, 'record': 'http://localhost:%s/live/resource/postreq?' % record_port, 'replay': 'http://localhost:%s/replay/resource/postreq?' % replay_port, } r = redis.StrictRedis.from_url('redis://localhost/2') rwapp = RWApp(upstream_urls, 'cookies:', r) return rwapp
def __init__(self, upstream_urls, cookie_key_templ, redis): config = {} config['url_templates'] = upstream_urls self.cookie_key_templ = cookie_key_templ self.app = Bottle() self.block_loader = LocalFileLoader() self.init_routes() super(RWApp, self).__init__(True, config=config) self.cookie_tracker = CookieTracker(redis) self.orig_error_handler = self.app.default_error_handler self.app.default_error_handler = self.err_handler
class StaticHandler(BaseHandler): def __init__(self, static_path): mimetypes.init() self.static_path = static_path self.block_loader = LocalFileLoader() def __call__(self, wbrequest): url = wbrequest.wb_url_str.split('?')[0] full_path = self.static_path + url try: data = self.block_loader.load(full_path) data.seek(0, 2) size = data.tell() data.seek(0) headers = [('Content-Length', str(size))] reader = None if 'wsgi.file_wrapper' in wbrequest.env: try: reader = wbrequest.env['wsgi.file_wrapper'](data) except: pass if not reader: reader = iter(lambda: data.read(), b'') content_type = 'application/octet-stream' guessed = mimetypes.guess_type(full_path) if guessed[0]: content_type = guessed[0] return WbResponse.bin_stream(reader, content_type=content_type, headers=headers) except IOError: raise NotFoundException('Static File Not Found: ' + wbrequest.wb_url_str)
def __init__(self, static_path): mimetypes.init() self.static_path = static_path self.block_loader = LocalFileLoader()