This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
mandaye/mandaye/response.py

101 lines
3.7 KiB
Python

import traceback
import pprint
import sys
from cgi import escape
from mandaye.log import logger
from mandaye.http import HTTPResponse, HTTPHeader
from mandaye.config import debug, email_notification
from mandaye.emails import Email
from template import serve_template
email = None
if email_notification:
email = Email()
def _get_html_traceback(tb_str, path, env=None):
msg = "<html><head><title>Mandaye traceback</title></head>"
msg += "<h2>%s</h2><h3>Exception</h3><pre>%s</pre>" % (path, tb_str)
if env:
env = pprint.pformat(env)
msg += '<h3>Environment</h3><pre>%s</pre>' % escape(env)
msg += '</html>'
return msg
def _302(location, cookies=None):
headers = HTTPHeader({'Location': [location],
'Content-Type': ['text/html']})
msg = "Reload the page to get source for: %s" % location
logger.info('302 redirect to %s' % location)
return HTTPResponse(302, 'Found', headers, msg, cookies)
def _401(msg):
title='Unauthorized'
logger.warning('401 %s' % msg)
headers = HTTPHeader({'Content-Type': ['text/html']})
return HTTPResponse(401, 'Unauthorized', headers,
serve_template("response.html", title=title, body=msg))
def _404(path):
logger.warning("404 %s not found" % path)
title = "Not Found"
body = "The requested URL %s was not found on this server." % path
headers = HTTPHeader({'Content-Type': ['text/html']})
return HTTPResponse(404, 'Not found', headers,
serve_template("response.html", title=title, body=body))
def _500(path, msg, exception=None, env=None):
""" Return a 500 error
path: the path of this error
exception: this exception instance if any """
if exception:
tb_str = traceback.format_exc()
if email:
try:
email.sent('Mandaye internal server error',
_get_html_traceback(tb_str, path, env=env), 'html')
except Exception as detail:
logger.warning('Sent mail failed with error: %s' % detail)
logger.exception("500 %s: %s, %s, env: %r" % (path, msg, tb_str, env))
else:
logger.error("500 %s: %s, env: %r" % (path, msg, env))
title = 'Internal Server Error'
headers = HTTPHeader({'Content-Type': ['text/html']})
if debug:
if exception:
msg += _get_html_traceback(tb_str, path, env=env)
else:
msg=msg
else:
msg = "The Mandaye server encountered an internal error or misconfiguration \
and was unable to complete your request. Please contact the server \
administrator and inform them of the time the error occurred."
msg = msg.encode('utf8')
return HTTPResponse(500, 'Internal Server Error', headers,
serve_template("response.html", title=title, body=msg))
def _502(path, target_hostname, exception=None):
""" Return a 502 error
path: the path of this error
exception: this exception instance if any """
logger.error("502 error while reading %s" % path)
title = 'Bad Gateway'
headers = HTTPHeader({'Content-Type': ['text/html']})
msg = "The Manaye server can't join the target server or misconfiguration \
and was unable to complete your request. Please contact the server \
administrator and inform them of the time the error occurred."
if debug and exception:
traceback.print_exc()
msg += "<h2>Target hostname: %s</h2>" % target_hostname
msg += _get_html_traceback(exception, path)
return HTTPResponse(502, 'Bad Gateway', headers,
serve_template("response.html", title=title, body=msg))
def template_response(templatename, values):
headers = HTTPHeader({'Content-Type': ['text/html']})
msg = serve_template(templatename, **values)
return HTTPResponse(200, 'OK', headers, msg)