From: Brian Warner Date: Wed, 4 Mar 2009 04:54:57 +0000 (-0700) Subject: web/common.py: use 'Accept:' header to control HTML-vs-text/plain traceback renderings X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/%22file:/vdrive?a=commitdiff_plain;h=90226f335f72df525c6720b712c7dfd446fd1a0a;p=tahoe-lafs%2Ftahoe-lafs.git web/common.py: use 'Accept:' header to control HTML-vs-text/plain traceback renderings --- diff --git a/src/allmydata/web/common.py b/src/allmydata/web/common.py index 5f7e76e6..c012be8d 100644 --- a/src/allmydata/web/common.py +++ b/src/allmydata/web/common.py @@ -116,14 +116,18 @@ class MyExceptionHandler(appserver.DefaultExceptionHandler): def simple(self, ctx, text, code=http.BAD_REQUEST): req = IRequest(ctx) req.setResponseCode(code) + #req.responseHeaders.setRawHeaders("content-encoding", []) + #req.responseHeaders.setRawHeaders("content-disposition", []) req.setHeader("content-type", "text/plain;charset=utf-8") if isinstance(text, unicode): text = text.encode("utf-8") + req.setHeader("content-length", str(len(text))) req.write(text) # TODO: consider putting the requested URL here req.finishRequest(False) def renderHTTP_exception(self, ctx, f): + traceback = f.getTraceback() if f.check(ExistingChildError): return self.simple(ctx, "There was already a child by that " @@ -150,8 +154,15 @@ class MyExceptionHandler(appserver.DefaultExceptionHandler): return self.simple(ctx, "I don't know how to treat a %s request." % method, http.NOT_IMPLEMENTED) - super = appserver.DefaultExceptionHandler - return super.renderHTTP_exception(self, ctx, f) + req = IRequest(ctx) + accept = req.getHeader("accept") + if not accept: + accept = "*/*" + if "*/*" in accept or "text/*" in accept or "text/html" in accept: + super = appserver.DefaultExceptionHandler + return super.renderHTTP_exception(self, ctx, f) + # use plain text + return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR) class NeedOperationHandleError(WebError): pass