]> granicus.if.org Git - python/commitdiff
Issue839496: SimpleHTTPServer should open all files in binary mode.
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sun, 6 Jul 2008 21:34:39 +0000 (21:34 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sun, 6 Jul 2008 21:34:39 +0000 (21:34 +0000)
Forward-port of 38255 (2005/01/15!)
This was already fixed in 2.4, but never merged into trunk...
py3k is already right, thanks to the bytes/str distinction!

Should be backported to 2.5.

Lib/SimpleHTTPServer.py
Misc/NEWS

index e79a478d97e93648e09a01509ade28bdaad120af..0110da018184b7c102f5f5582d39a3a079e9c990 100644 (file)
@@ -79,12 +79,11 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
             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
index f50459c770559abc1528054398eeb89b500c22c3..2dfa33ab51ff2ba2d3444ddd98b661d4034493d9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,12 @@ Core and Builtins
 Library
 -------
 
+- Issue #839496: SimpleHTTPServer used to open text files in text mode. This is
+  both unnecessary (HTTP allows text content to be sent in several forms) and
+  wrong because the actual transmitted size could differ with the
+  content-length.  The problem had been corrected in the 2.4 branch, but never
+  merged into trunk.
+
 - Issue #2663: add filtering capability to shutil.copytree().
 
 - Issue #1622: Correct interpretation of various ZIP header fields.