]> granicus.if.org Git - python/commitdiff
Add content-type header to ftp URLs (SF patch #454553)
authorJeremy Hylton <jeremy@alum.mit.edu>
Mon, 27 Aug 2001 20:16:53 +0000 (20:16 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Mon, 27 Aug 2001 20:16:53 +0000 (20:16 +0000)
Modify rfc822.formatdate() to always generate English names,
regardless of locale.  This is required by RFC 1123.

In open_local_file() of urllib and urllib2, use new formatdate() from
rfc822.

Lib/rfc822.py
Lib/urllib.py
Lib/urllib2.py

index c2249f368166ec4e4e5b94b73170bfd3a368fdf6..e69c4cbcde2d2371094218530769751353d514a8 100644 (file)
@@ -953,11 +953,21 @@ def formatdate(timeval=None):
     """Returns time format preferred for Internet standards.
 
     Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
+
+    According to RFC 1123, day and month names must always be in
+    English.  If not for that, this code could use strftime().  It
+    can't because strftime() honors the locale and could generated
+    non-English names.
     """
     if timeval is None:
         timeval = time.time()
-    return "%s" % time.strftime('%a, %d %b %Y %H:%M:%S GMT',
-                                time.gmtime(timeval))
+    timeval = time.gmtime(timeval)
+    return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (
+            ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][timeval[6]],
+            timeval[2],
+            ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][timeval[1]-1],
+                               timeval[0], timeval[3], timeval[4], timeval[5])
 
 
 # When used as script, run a small test program.
index a255956efd2914dbeb67bde87365146969515d2c..8c1852ef17d36c8c528903d3d7019dcd1cd18d77 100644 (file)
@@ -406,18 +406,12 @@ class URLopener:
 
     def open_local_file(self, url):
         """Use local file."""
-        import mimetypes, mimetools, StringIO
+        import mimetypes, mimetools, rfc822, StringIO
         host, file = splithost(url)
         localname = url2pathname(file)
         stats = os.stat(localname)
         size = stats[stat.ST_SIZE]
-        modified = time.gmtime(stats[stat.ST_MTIME])
-        modified = "%s, %02d %s %04d %02d:%02d:%02d GMT" % (
-            ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][modified[6]],
-            modified[2],
-            ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
-             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][modified[1]-1],
-            modified[0], modified[3], modified[4], modified[5])
+        modified = rfc822.formatdate(stats[stat.ST_MTIME])
         mtype = mimetypes.guess_type(url)[0]
         headers = mimetools.Message(StringIO.StringIO(
             'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
index 5882de6c457b9cd12302206fb425c9de3cf22b8b..beb9fa5af074947b6694580f7de1a5f0d9be82d0 100644 (file)
@@ -96,9 +96,12 @@ import urlparse
 import md5
 import mimetypes
 import mimetools
+import rfc822
 import ftplib
 import sys
 import time
+import os
+import stat
 import gopherlib
 import posixpath
 
@@ -914,16 +917,22 @@ class FileHandler(BaseHandler):
 
     # not entirely sure what the rules are here
     def open_local_file(self, req):
-        mtype = mimetypes.guess_type(req.get_selector())[0]
-        headers = mimetools.Message(StringIO('Content-Type: %s\n' \
-                                             % (mtype or 'text/plain')))
         host = req.get_host()
         file = req.get_selector()
+        localfile = url2pathname(file)
+        stats = os.stat(localfile)
+        size = stats[stat.ST_SIZE]
+        modified = rfc822.formatdate(stats[stat.ST_MTIME])
+        mtype = mimetypes.guess_type(file)[0]
+        stats = os.stat(localfile)
+        headers = mimetools.Message(StringIO(
+            'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
+            (mtype or 'text/plain', size, modified)))
         if host:
             host, port = splitport(host)
         if not host or \
            (not port and socket.gethostbyname(host) in self.get_names()):
-            return addinfourl(open(url2pathname(file), 'rb'),
+            return addinfourl(open(localfile, 'rb'),
                               headers, 'file:'+file)
         raise URLError('file not on local host')