1. cherrypy
  2. CherryPy
Issue #895 new

staticdir doesn't cause trailing slash to be added to directory URLs served by index.html

Anonymous created an issue

Configured a staticdir.dir and staticdir.index requested a URL of a directory without a trailing slash. Such was not supplied via redirect to the browser, so relative URLs failed.

The problem seems to be in static.py, in staticdir, the next to last line in the function.

Instead of

cherrypy.request.is_index = filename[-1] in (r"\/")

it should read

cherrypy.request.is_index = True

Note that filename[-1] is not a slash when this line is executed (in all my test cases, anyway, maybe sometimes it is), but the condition that it is an index has been determined by previous conditions.

It looks like the trailing_slash tool is robust enough to deal with the case when filename[-1] would be a slash (if there are any such cases), as long as is_index is set properly.

Reported by v+python

Comments (1)

  1. guest

    While I still believe that the above patch is correct, it may not be complete. There may be a case for setting cherrypy.request.is_index to false, but that should be done if the first _attempt succeeds. Instead of

        handled = _attempt(filename, content_types)
        if not handled:
            # Check for an index file if a folder was requested.
            if index:
                handled = _attempt(os.path.join(filename, index), content_types)
                if handled:
                    cherrypy.request.is_index = filename[-1] in (r"\/")
    
    

    It should be (I propose)

        handled = _attempt(filename, content_types)
        if handled:
            cherrypy.request.is_index = False
        else:
            # Check for an index file if a folder was requested.
            if index:
                handled = _attempt(os.path.join(filename, index), content_types)
                if handled:
                    cherrypy.request.is_index = True
    
    

    The theory here is that the dispatcher may have already set is_index one way or the other based in the handler found. But if the tool overrides the handler, which it does only in the case where it finds a file to serve, then it needs to set is_index based on whether the file it served was fully specified in the name, or whether the index parameter was included. The filename[-1] does not always reflect that, and the location of the code cannot deal with both cases.

  2. Log in to comment