def handle(self, request):
filename = request.match_info['filename']
try:
filepath = self._directory.joinpath(filename).resolve()
except (ValueError, FileNotFoundError, OSError):
pass
else:
if filepath.is_dir():
request.match_info['filename'] = str(filepath.joinpath('index.html').relative_to(self._directory))
status, length = 'unknown', ''
try:
response = await super().handle(request)
except HTTPNotModified:
status, length = 304, 0
raise
except HTTPNotFound:
_404_msg = '404: Not Found\n\n' + _get_asset_content(self._asset_path)
response = web.Response(body=_404_msg.encode('utf8'), status=404)
status, length = response.status, response.content_length
else:
status, length = response.status, response.content_length
finally:
l = logger.info if status in {200, 304} else logger.warning
l(' > %s %s %s %s', request.method, request.path, status, _fmt_size(length))
return response
评论列表
文章目录