Commit d5a1f1f4 authored by Guido van Rossum's avatar Guido van Rossum

Changed list_directory() somewhat. It is now only called when there

is no index.htm[l] file, and when it is called, it also spits out the
headers.  When an index.htm[l] file is present, the regular (file
access) path is followed.  Also, when the guessed content-type matches
text/*, open the file in text mode; otherwise in binary mode.
parent 47c79c27
...@@ -59,24 +59,38 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -59,24 +59,38 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
""" """
path = self.translate_path(self.path) path = self.translate_path(self.path)
f = None
if os.path.isdir(path): if os.path.isdir(path):
f = self.list_directory(path) for index in "index.html", "index.htm":
if f is None: index = os.path.join(path, index)
return None if os.path.exists(index):
ctype = "text/HTML" path = index
break
else:
return self.list_directory(path)
ctype = self.guess_type(path)
if ctype.startswith('text/'):
mode = 'r'
else: else:
mode = 'rb'
try: try:
f = open(path, 'rb') f = open(path, mode)
except IOError: except IOError:
self.send_error(404, "File not found") self.send_error(404, "File not found")
return None return None
ctype = self.guess_type(path)
self.send_response(200) self.send_response(200)
self.send_header("Content-type", ctype) self.send_header("Content-type", ctype)
self.end_headers() self.end_headers()
return f return f
def list_directory(self, path): def list_directory(self, path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
try: try:
list = os.listdir(path) list = os.listdir(path)
except os.error: except os.error:
...@@ -88,15 +102,20 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -88,15 +102,20 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
f.write("<hr>\n<ul>\n") f.write("<hr>\n<ul>\n")
for name in list: for name in list:
fullname = os.path.join(path, name) fullname = os.path.join(path, name)
displayname = name = cgi.escape(name) displayname = linkname = name = cgi.escape(name)
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + os.sep
if os.path.islink(fullname): if os.path.islink(fullname):
displayname = name + "@" displayname = name + "@"
elif os.path.isdir(fullname): # Note: a link to a directory displays with @ and links with /
displayname = name + "/" f.write('<li><a href="%s">%s</a>\n' % (linkname, displayname))
name = name + os.sep
f.write('<li><a href="%s">%s</a>\n' % (name, displayname))
f.write("</ul>\n<hr>\n") f.write("</ul>\n<hr>\n")
f.seek(0) f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
return f return f
def translate_path(self, path): def translate_path(self, path):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment