[issue33181] SimpleHTTPRequestHandler shouldn't redirect to directories with code 301

Matt Eaton report at bugs.python.org
Fri Mar 30 11:54:26 EDT 2018


Matt Eaton <agnosticdev at gmail.com> added the comment:

Oliver,

A possible option that would work for both the client side caching and the server would be to pass back a Cache-Control header with a max-age attached when the 301 is returned.

I am thinking something like this:

if os.path.isdir(path):
    parts = urllib.parse.urlsplit(self.path)
    if not parts.path.endswith('/'):
        # redirect browser - doing basically what apache does
        self.send_response(HTTPStatus.MOVED_PERMANENTLY)
        new_parts = (parts[0], parts[1], parts[2] + '/',
                     parts[3], parts[4])
        new_url = urllib.parse.urlunsplit(new_parts)
        self.send_header("Location", new_url)
        self.send_header("Cache-Control", "max-age=600")
        self.end_headers()
        return None
    for index in "index.html", "index.htm":
        index = os.path.join(path, index)
        if os.path.exists(index):
            path = index
            break


Possibly we could even provide some way for the max age to be variable.
Thoughts?

----------
nosy: +agnosticdev

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33181>
_______________________________________


More information about the Python-bugs-list mailing list