[Python-checkins] CVS: python/dist/src/Lib urllib.py,1.127,1.128

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 09 Aug 2001 10:43:37 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv20420

Modified Files:
	urllib.py 
Log Message:
SF Patch #420725 by Walter Doerwald:

  For local files urllib.py doesn't return the MIME 
  headers that the documentation says it does: 

  http://www.python.org/doc/current/lib/module- 
  urllib.html#l2h-2187 states that "When the method is 
  local-file, returned headers will include a Date 
  representing the file's last-modified time, a Content- 
  Length giving file size, and a Content-Type containing 
  a guess at the file's type" 

  But in Python 2.1 the only header that gets returned 
  is the Content-Type: 

  >>> import urllib 
  >>> f = urllib.urlopen("gurk.txt") 
  >>> f.info().headers 
  ['Content-Type: text/plain\n']


Index: urllib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib.py,v
retrieving revision 1.127
retrieving revision 1.128
diff -C2 -d -r1.127 -r1.128
*** urllib.py	2001/07/04 05:18:29	1.127
--- urllib.py	2001/08/09 17:43:35	1.128
***************
*** 26,29 ****
--- 26,31 ----
  import socket
  import os
+ import stat
+ import time
  import sys
  import types
***************
*** 403,415 ****
          """Use local file."""
          import mimetypes, mimetools, StringIO
          mtype = mimetypes.guess_type(url)[0]
          headers = mimetools.Message(StringIO.StringIO(
!             'Content-Type: %s\n' % (mtype or 'text/plain')))
!         host, file = splithost(url)
          if not host:
              urlfile = file
              if file[:1] == '/':
                  urlfile = 'file://' + file
!             return addinfourl(open(url2pathname(file), 'rb'),
                                headers, urlfile)
          host, port = splitport(host)
--- 405,428 ----
          """Use local file."""
          import mimetypes, mimetools, 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])
          mtype = mimetypes.guess_type(url)[0]
          headers = mimetools.Message(StringIO.StringIO(
!             'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
!             (mtype or 'text/plain', size, modified)))
          if not host:
              urlfile = file
              if file[:1] == '/':
                  urlfile = 'file://' + file
!             return addinfourl(open(localname, 'rb'),
                                headers, urlfile)
          host, port = splitport(host)
***************
*** 419,423 ****
              if file[:1] == '/':
                  urlfile = 'file://' + file
!             return addinfourl(open(url2pathname(file), 'rb'),
                                headers, urlfile)
          raise IOError, ('local file error', 'not on local host')
--- 432,436 ----
              if file[:1] == '/':
                  urlfile = 'file://' + file
!             return addinfourl(open(localname, 'rb'),
                                headers, urlfile)
          raise IOError, ('local file error', 'not on local host')