diff --git a/gunicorn/util.py b/gunicorn/util.py index 2b86b592..fac14e26 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -178,19 +178,18 @@ def writelines(sock, lines, chunked=False): for line in list(lines): write(sock, line, chunked) -def write_error(sock, msg, status_int=500, - reason="Internal Server Error"): +def write_error(sock, status_int, reason, mesg): html = textwrap.dedent("""\ - - %(reason)s - - -

%(reason)s

- %(msg)s - + + %(reason)s + + +

%(reason)s

+ %(mesg)s + - """) % {"reason": reason, "msg": msg} + """) % {"reason": reason, "mesg": mesg} http = textwrap.dedent("""\ HTTP/1.1 %s %s\r diff --git a/gunicorn/workers/base.py b/gunicorn/workers/base.py index 41d623d7..2d4abaa7 100644 --- a/gunicorn/workers/base.py +++ b/gunicorn/workers/base.py @@ -116,9 +116,18 @@ class Worker(object): sys.exit(0) def handle_error(self, client, exc): + self.log.exception("Error hanlding request") + + status_int = 500 + reason = "Internal Server Error" + mesg = "" + if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)): + status_int = 400 + reason = "Bad Request" + if isinstance(exc, InvalidRequestLine): mesg = "

Invalid Request Line '%s'

" % str(exc) elif isinstance(exc, InvalidRequestMethod): @@ -127,22 +136,15 @@ class Worker(object): mesg = "

Invalid HTTP Version '%s'

" % str(exc) elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)): mesg = "

Invalid Header'%s'

" % str(exc) - reason = "Bad Request" - status_int = 400 - else: - mesg = reason = "Internal Server reason" - status_int = 500 if self.debug: tb = traceback.format_exc() - mesg += "

Traceback:
%s
" % tb + mesg += "

Traceback:

\n
%s
" % tb try: - util.write_error(client, mesg, status_int=status_int, - reason=reason) + util.write_error(client, status_int, reason, mesg) except: - if self.debug: - self.log.warning("Unexpected error %s" % traceback.format_exc()) + self.log.exception("Failed to send error message.") def handle_winch(self, sig, fname): # Ignore SIGWINCH in worker. Fixes a crash on OpenBSD.