[ python-Bugs-1314572 ] Trailing slash redirection for SimpleHTTPServer

SourceForge.net noreply at sourceforge.net
Thu Oct 6 09:00:34 CEST 2005


Bugs item #1314572, was opened at 2005-10-05 23:49
Message generated for change (Comment added) made by josiahcarlson
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1314572&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Josiah Carlson (josiahcarlson)
Assigned to: Nobody/Anonymous (nobody)
Summary: Trailing slash redirection for SimpleHTTPServer

Initial Comment:
As known by every serious web server developer, the
lack of a trailing slash on direcories can cause some
serious web page loading issues.  Take the following
examples...

Let us imagine that: 'http://www.foo.com/foo' points to
a directory containing 'index.html'.  A web server
could return the contents of 'index.html', but then any
relative urls would be relative to the / path of the
web server.

A better web server would instead redirect the user to
'http://www.foo.com/foo/'.  SimpleHTTPServer does not
do any such redirection, and the documentation for
forcing a client to redirect is difficult to find on
the internet.  In the comments I will post a
replacement
SimpleHTTPServer.SimpleHTTPRequestHandler.send_head
method which does automatic trailing slash redirection.

----------------------------------------------------------------------

>Comment By: Josiah Carlson (josiahcarlson)
Date: 2005-10-06 00:00

Message:
Logged In: YES 
user_id=341410

I've attached a file which contains the method, which should
save everyone from having to deal with SF's line wrapping.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2005-10-05 23:58

Message:
Logged In: YES 
user_id=341410

    def send_head(self):
        """Common code for GET and HEAD commands.

        This sends the response code and MIME headers.

        Return value is either a file object (which has to
be copied
        to the outputfile by the caller unless the command
was HEAD,
        and must be closed by the caller under all
circumstances), or
        None, in which case the caller has nothing further
to do.

        """
        path = self.translate_path(self.path)
        f = None
        if os.path.isdir(path):
            # Redirect path urls which lack a trailing slash
            if not self.path.endswith('/'):
                self.send_response(302)
                self.send_header("Content-type", "text/html")
                self.send_header("Content-Location",
self.path + '/')
                self.end_headers()
                return StringIO('<META HTTP-EQUIV="refresh"
CONTENT="0;URL=%s/">'%self.path)
            for index in "index.html", "index.htm":
                index = os.path.join(path, index)
                if os.path.exists(index):
                    path = index
                    break
            else:
                return self.list_directory(path)
        ctype = self.guess_type(path)
        try:
            # Always read in binary mode. Opening files in
text mode may cause
            # newline translations, making the actual size
of the content
            # transmitted *less* than the content-length!
            f = open(path, 'rb')
        except IOError:
            self.send_error(404, "File not found")
            return None
        self.send_response(200)
        self.send_header("Content-type", ctype)
        self.send_header("Content-Length",
str(os.fstat(f.fileno())[6]))
        self.end_headers()
        return f


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1314572&group_id=5470


More information about the Python-bugs-list mailing list