From: Philip S. <ph...@se...> - 2006-05-16 15:25:09
|
Hi all, I have hacked Spyce to permit customization of 404 errors and I'd like to hear other people's comments on my code. The 404 topic was discussed on the list last fall. The short story as I understand it is that (a) the comments in spyceconf.py (the part about "...errors include spyce.spyceNotFound, spyce.spyceForbidden...") is incorrect and (b) there's no way to override 404s. The last message on the topic was here (although you might not be able to get there at the moment because Sourceforge is having a bad hair day): http://sourceforge.net/mailarchive/message.php?msg_id=13225440 Conceptually, this code allows for customization of any error response code, but I think the only one that Spyce returns at the moment is 404. Since I don't understand Spyce internals, my goal was to change as little code as possible. That also means less typing. =) The first part of the change is to patch spyceWWW.py as per the diff at the end of this message. Next, add a dict to spyceconf.py that looks something like this: errorresponsecodehandlers = { 404 : "/Error404.spy" } Each entry in the dict consists of the response code you want to trap and the file by which it should be processed. Last but not least, you have to create /Error404.spy. Mine looks something like this: <html> <body> <p>Response code is [[=response._response.returncode]]</p> <p>URL stack is [[=request.stack()]]</p> </body> </html> Comments are welcome Philip --- /usr/local/spyce-2.0.3/spyceWWW.py Thu Jul 21 09:24:17 2005 +++ spyceWWW.py Tue May 16 10:59:23 2006 @@ -164,6 +164,21 @@ finally: spyce.DEBUG('total for %s was %s' % (self.path, time.time() - start)) do_POST=do_GET + + def send_error(self, code, message = None): + config = spyce.getServer().config + if code in config.errorresponsecodehandlers: + # There's a custom handler defined for this response code. Use that Spyce + # file to generate the output. + request = spyceHTTPRequest(self, self.server.documentRoot) + request._stack.append(self.server.documentRoot + self.path) + response = spyceHTTPResponse(self) + response.setReturnCode(code) + spyce.spyceFileHandler(request, response, self.server.documentRoot + config.errorresponsecodehandlers[code]) + response.close() + else: + BaseHTTPServer.BaseHTTPRequestHandler.send_error(self, code, message) + def handler_spyce(self, path): # process spyce request = spyceHTTPRequest(self, self.server.documentRoot) |