mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-15 00:03:28 +01:00
- merge pywb.urlrewrite -> pywb.rewrite, remove obsolete stuff (rewrite_content.py, rewrite_live.py, dsrules.py) - move wbrequestresponse -> pywb.apps - move pywb.webapp.handlers -> pywb.apps.static_handler - remove pywb.webapp, pywb.framework packages - disable old header_rewriter, content_rewriter tests - finish renaming from previous warcserver refactor - all other tests passing!
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
import mimetypes
|
|
import os
|
|
|
|
from pywb.utils.loaders import LocalFileLoader
|
|
|
|
from pywb.apps.wbrequestresponse import WbResponse
|
|
|
|
|
|
#=================================================================
|
|
# Static Content Handler
|
|
#=================================================================
|
|
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)
|
|
|
|
|