[Jython-checkins] jython: Import from cpython 2.6:

frank.wierzbicki jython-checkins at python.org
Mon Oct 10 03:28:41 CEST 2011


http://hg.python.org/jython/rev/e87c8777e6a0
changeset:   6248:e87c8777e6a0
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Sun Oct 09 18:17:14 2011 -0700
summary:
  Import  from cpython 2.6:
Lib/SimpleHTTPServer.py at 71579:b9a95ce2692c

files:
  Lib/SimpleHTTPServer.py |  26 ++++++++++++++------------
  1 files changed, 14 insertions(+), 12 deletions(-)


diff --git a/Lib/SimpleHTTPServer.py b/Lib/SimpleHTTPServer.py
--- a/Lib/SimpleHTTPServer.py
+++ b/Lib/SimpleHTTPServer.py
@@ -14,8 +14,8 @@
 import posixpath
 import BaseHTTPServer
 import urllib
-import urlparse
 import cgi
+import sys
 import shutil
 import mimetypes
 try:
@@ -80,18 +80,17 @@
             else:
                 return self.list_directory(path)
         ctype = self.guess_type(path)
-        if ctype.startswith('text/'):
-            mode = 'r'
-        else:
-            mode = 'rb'
         try:
-            f = open(path, mode)
+            # 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)
-        fs = os.fstat(f.fileno()) if hasattr(os, 'fstat') else os.stat(path)
+        fs = os.fstat(f.fileno())
         self.send_header("Content-Length", str(fs[6]))
         self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
         self.end_headers()
@@ -113,8 +112,9 @@
         list.sort(key=lambda a: a.lower())
         f = StringIO()
         displaypath = cgi.escape(urllib.unquote(self.path))
-        f.write("<title>Directory listing for %s</title>\n" % displaypath)
-        f.write("<h2>Directory listing for %s</h2>\n" % displaypath)
+        f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
+        f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
+        f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
         f.write("<hr>\n<ul>\n")
         for name in list:
             fullname = os.path.join(path, name)
@@ -128,11 +128,12 @@
                 # Note: a link to a directory displays with @ and links with /
             f.write('<li><a href="%s">%s</a>\n'
                     % (urllib.quote(linkname), cgi.escape(displayname)))
-        f.write("</ul>\n<hr>\n")
+        f.write("</ul>\n<hr>\n</body>\n</html>\n")
         length = f.tell()
         f.seek(0)
         self.send_response(200)
-        self.send_header("Content-type", "text/html")
+        encoding = sys.getfilesystemencoding()
+        self.send_header("Content-type", "text/html; charset=%s" % encoding)
         self.send_header("Content-Length", str(length))
         self.end_headers()
         return f
@@ -146,7 +147,8 @@
 
         """
         # abandon query parameters
-        path = urlparse.urlparse(path)[2]
+        path = path.split('?',1)[0]
+        path = path.split('#',1)[0]
         path = posixpath.normpath(urllib.unquote(path))
         words = path.split('/')
         words = filter(None, words)

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list