2011-09-09 22:26:57 +02:00
|
|
|
import logging
|
2011-10-13 15:43:46 +02:00
|
|
|
import xtraceback
|
|
|
|
import pprint
|
|
|
|
import sys
|
2011-09-09 22:26:57 +02:00
|
|
|
|
|
|
|
from cgi import escape
|
2011-09-07 22:19:30 +02:00
|
|
|
|
|
|
|
from mandaye.http import HTTPResponse, HTTPHeader
|
2011-09-19 19:18:32 +02:00
|
|
|
from mandaye.config import debug
|
2011-09-07 22:19:30 +02:00
|
|
|
|
2011-09-19 19:18:32 +02:00
|
|
|
from template import serve_template
|
2011-09-07 22:19:30 +02:00
|
|
|
|
|
|
|
|
2011-10-13 15:43:46 +02:00
|
|
|
def _get_html_traceback(tb_str, path, env=None):
|
|
|
|
msg = "<h2>%s</h2><h3>Exception</h3><pre>%s</pre>" % \
|
|
|
|
(path, escape(tb_str))
|
|
|
|
if env:
|
|
|
|
env = pprint.pformat(env)
|
|
|
|
msg += '<h3>Environment</h3><pre>%s</pre>' % escape(env)
|
2011-09-09 22:26:57 +02:00
|
|
|
return msg
|
|
|
|
|
2011-09-07 22:19:30 +02:00
|
|
|
def _302(location, cookies=None):
|
|
|
|
headers = HTTPHeader({'Location': [location],
|
|
|
|
'Content-Type': ['text/html']})
|
|
|
|
msg = "Reload the page to get source for: %s" % location
|
|
|
|
return HTTPResponse(302, 'Found', headers, msg, cookies)
|
|
|
|
|
|
|
|
def _401(msg):
|
|
|
|
title='Unauthorized'
|
2011-09-28 16:08:26 +02:00
|
|
|
logging.warning('401 %s' % msg)
|
2011-09-07 22:19:30 +02:00
|
|
|
headers = HTTPHeader({'Content-Type': ['text/html']})
|
|
|
|
return HTTPResponse(401, 'Unauthorized', headers,
|
|
|
|
serve_template("response.html", title=title, body=msg))
|
|
|
|
|
2011-09-09 22:26:57 +02:00
|
|
|
def _404(path):
|
2011-09-28 16:08:26 +02:00
|
|
|
logging.warning("404 %s not found" % path)
|
2011-09-07 22:19:30 +02:00
|
|
|
title = "Not Found"
|
2011-09-09 22:26:57 +02:00
|
|
|
body = "The requested URL %s was not found on this server." % path
|
2011-09-07 22:19:30 +02:00
|
|
|
headers = HTTPHeader({'Content-Type': ['text/html']})
|
|
|
|
return HTTPResponse(404, 'Not found', headers,
|
|
|
|
serve_template("response.html", title=title, body=body))
|
|
|
|
|
2011-10-13 15:43:46 +02:00
|
|
|
def _500(path, msg, exception=None, env=None):
|
2011-09-09 22:26:57 +02:00
|
|
|
""" Return a 500 error
|
|
|
|
path: the path of this error
|
|
|
|
exception: this exception instance if any """
|
2011-10-13 15:43:46 +02:00
|
|
|
if exception:
|
|
|
|
tb_str = str(xtraceback.XTraceback(*sys.exc_info(), color=False))
|
|
|
|
logging.exception("500 %s: %s, %s, env: %r" % (path, msg, tb_str, env))
|
|
|
|
else:
|
|
|
|
logging.error("500 %s: %s, env: %r" % (path, msg, env))
|
2011-09-07 22:19:30 +02:00
|
|
|
title = 'Internal Server Error'
|
|
|
|
headers = HTTPHeader({'Content-Type': ['text/html']})
|
2011-09-09 22:26:57 +02:00
|
|
|
if debug:
|
|
|
|
if exception:
|
2011-10-13 15:43:46 +02:00
|
|
|
msg += _get_html_traceback(tb_str, path, env=env)
|
2011-09-09 22:26:57 +02:00
|
|
|
else:
|
|
|
|
msg=msg
|
|
|
|
else:
|
|
|
|
msg = "The 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."
|
2011-09-07 22:19:30 +02:00
|
|
|
return HTTPResponse(500, 'Internal Server Error', headers,
|
|
|
|
serve_template("response.html", title=title, body=msg))
|
|
|
|
|
2011-09-09 22:26:57 +02:00
|
|
|
def _502(path, target_hostname, exception=None):
|
|
|
|
""" Return a 502 error
|
|
|
|
path: the path of this error
|
|
|
|
exception: this exception instance if any """
|
|
|
|
logging.critical("[502] error while reading %s:\n" % path)
|
2011-09-07 22:19:30 +02:00
|
|
|
title = 'Bad Gateway'
|
|
|
|
headers = HTTPHeader({'Content-Type': ['text/html']})
|
2011-09-09 22:26:57 +02:00
|
|
|
msg = "The 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:
|
2011-09-19 19:18:32 +02:00
|
|
|
traceback.print_exc()
|
2011-09-09 22:26:57 +02:00
|
|
|
msg = "<h2>Target hostname: %s</h2>" % target_hostname
|
|
|
|
msg += _get_html_traceback(exception, path)
|
2011-09-07 22:19:30 +02:00
|
|
|
return HTTPResponse(502, 'Bad Gateway', headers,
|
|
|
|
serve_template("response.html", title=title, body=msg))
|
|
|
|
|
2011-09-13 19:13:14 +02:00
|
|
|
def template_response(templatename, values):
|
|
|
|
headers = HTTPHeader({'Content-Type': ['text/html']})
|
|
|
|
msg = serve_template(templatename, **values)
|
|
|
|
return HTTPResponse(200, 'OK', headers, msg)
|
|
|
|
|