Domanda

I'm having a difficult time with CherryPy's handling of the static files. The problem has to do with the dealing of the URL's trailing slash. Sometimes the missing slash is added and sometimes it is not: the behavior is unpredictable. I'm convinced there is a bug lurking in there. The bug seems to show up when I add or remove some static files. It looks like there could be an uninitialized variable creating an undetermined state in the function handling the slash. I have put together a minimal application that exhibits the problem (in my machine at least). I'm using CherryPy 3.2.2 under Ubuntu 12.10.

import cherrypy

class Root(object): pass

conf = {
    '/':{
    'tools.staticdir.on': True,
    'tools.staticdir.dir': '/home/bob/www',
    'tools.staticdir.index': 'index.htm',
    'tools.staticdir.debug': True,
    },
}

cherrypy.tree.mount(Root(), '/', config=conf)
cherrypy.engine.start()
cherrypy.engine.block()

My experimental static folder (/home/bob/www) is very simply structured:

/home/bob/www/index.htm         <-- This file is just <html><body>Hello</body></html>
/home/bob/www/dir/index.htm     <-- This files includes a <img src="picture.jpg"> tag in it
/home/bob/www/dir/picture.jpg   <-- Any picture will do

If I browse localhost:8080/dir/, the resulting page displays the image.
If I browse localhost:8080/dir, the resulting page does not display the image.

Any help would be appreciated. Maybe I'm not configuring the app correctly since I'm new to CherryPy.

Thanks,
Bob

P.S. I read already stackoverflow.com/questions/10276060 which has a similar problem but using Routes. I do not use Routes.

È stato utile?

Soluzione

Temporary solution

The temporary solution shown here requires a small patch to one of the CherryPy source files. I want to thank Felix Bonkoski who told me about it. I did some google searches and found references to it at bitbucket.org/cherrypy/cherrypy/issue/895/.

Here are the instructions:

Locate and edit the file static.py (In my Ubuntu machine it was located at /usr/local/lib/python2.7/dist-packages/cherrypy/lib/static.py). Within the function staticdir(), locate the lines below. They are at the end of that function.

 if handled:
    request.is_index = filename[-1] in (r"\/")   <--- Line in question

Edit the line in question so it looks like this:

 if handled:
    request.is_index = True

and save the file.

At this point I rebooted the computer and CherryPy is now working as I think it should work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top