summaryrefslogtreecommitdiffstats
path: root/Lib/SimpleHTTPServer.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2000-05-21 16:25:29 (GMT)
committerGuido van Rossum <guido@python.org>2000-05-21 16:25:29 (GMT)
commit1d10f3eac2248ca0af65eeb6414d3ef841419761 (patch)
tree7be7c41fe683b059552dec00c9477e9f2262af30 /Lib/SimpleHTTPServer.py
parent4355093f94299572498d31fa50eec38df721cadb (diff)
downloadcpython-1d10f3eac2248ca0af65eeb6414d3ef841419761.zip
cpython-1d10f3eac2248ca0af65eeb6414d3ef841419761.tar.gz
cpython-1d10f3eac2248ca0af65eeb6414d3ef841419761.tar.bz2
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.
Diffstat (limited to 'Lib/SimpleHTTPServer.py')
-rw-r--r--Lib/SimpleHTTPServer.py49
1 files changed, 34 insertions, 15 deletions
diff --git a/Lib/SimpleHTTPServer.py b/Lib/SimpleHTTPServer.py
index 6def14e..7125413 100644
--- a/Lib/SimpleHTTPServer.py
+++ b/Lib/SimpleHTTPServer.py
@@ -59,24 +59,38 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""
path = self.translate_path(self.path)
+ f = None
if os.path.isdir(path):
- f = self.list_directory(path)
- if f is None:
- return None
- ctype = "text/HTML"
+ 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)
+ if ctype.startswith('text/'):
+ mode = 'r'
else:
- try:
- f = open(path, 'rb')
- except IOError:
- self.send_error(404, "File not found")
- return None
- ctype = self.guess_type(path)
+ mode = 'rb'
+ try:
+ f = open(path, mode)
+ except IOError:
+ self.send_error(404, "File not found")
+ return None
self.send_response(200)
self.send_header("Content-type", ctype)
self.end_headers()
return f
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:
list = os.listdir(path)
except os.error:
@@ -88,15 +102,20 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
f.write("<hr>\n<ul>\n")
for name in list:
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):
displayname = name + "@"
- elif os.path.isdir(fullname):
- displayname = name + "/"
- name = name + os.sep
- f.write('<li><a href="%s">%s</a>\n' % (name, displayname))
+ # Note: a link to a directory displays with @ and links with /
+ f.write('<li><a href="%s">%s</a>\n' % (linkname, displayname))
f.write("</ul>\n<hr>\n")
f.seek(0)
+ self.send_response(200)
+ self.send_header("Content-type", "text/html")
+ self.end_headers()
return f
def translate_path(self, path):