Re: Patch for 404 customization

Philip Semanchuk <[email protected]>
Newsgroups gmane.comp.python.spyce.general
Message-ID <[email protected]>
On May 16, 2006, at 11:57 AM, Jonathan Ellis wrote:

> On Tue, 16 May 2006 11:24:56 -0400, "Philip Semanchuk"
> <[email protected]> said:
>> 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>
>
> Hey Philip,
>
> thanks for the patch.  But I think this is more complicated than it
> needs to be.  All you should have to do call spyceFileHandler with the
> (invalid) path instead of handling the 404 directly in spyceWWW, and it
> should get routed through the normal exception handling stuff in
> commonHandler.  Then the docs would be correct. :)
>
> Does that make sense?

Jonathan,
How will I get anywhere in life if I do things the easy way? =)

You suggestion works great, *but* I can't figure out exactly how to 
implement it. At the bottom of this email is one diff that gets the job 
done for my setup, but I commented out lines 144 and 145 of spyceWWW.py 
to make my code work. I can't figure out what those lines were meant to 
accomplish, but Spyce's code is pretty tight and so I guess they do 
*something*. But it seems like any added code that calls "not 
os.path.exists(path)" will usurp whatever 144 and 145 do.

Confused,
Philip




--- spyceWWW.py.original        Tue May 16 16:58:02 2006
+++ spyceWWW.py Tue May 16 18:10:34 2006
@@ -141,10 +141,10 @@
            if not indexExists:
              return self.handler_dir(path)
          # search up path (path_info)
-        while len(path)>len(self.server.documentRoot) and not 
os.path.exists(path):
-          path, _ = os.path.split(path)
+#         while len(path)>len(self.server.documentRoot) and not 
os.path.exists(path):
+#           path, _ = os.path.split(path)
          # for files (or links), find appropriate handler
-        if os.path.isfile(path) or os.path.islink(path):
+        if os.path.isfile(path) or os.path.islink(path) or not 
os.path.exists(path):
            _, ext = os.path.splitext(path)
            if ext: ext = ext[1:]
            try:





-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.