Commit f0713d3f authored by Guido van Rossum's avatar Guido van Rossum

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']
parent c8718c13
...@@ -25,6 +25,8 @@ used to query various info about the object, if available. ...@@ -25,6 +25,8 @@ used to query various info about the object, if available.
import string import string
import socket import socket
import os import os
import stat
import time
import sys import sys
import types import types
...@@ -402,15 +404,26 @@ class URLopener: ...@@ -402,15 +404,26 @@ class URLopener:
def open_local_file(self, url): def open_local_file(self, url):
"""Use local file.""" """Use local file."""
import mimetypes, mimetools, StringIO 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] mtype = mimetypes.guess_type(url)[0]
headers = mimetools.Message(StringIO.StringIO( headers = mimetools.Message(StringIO.StringIO(
'Content-Type: %s\n' % (mtype or 'text/plain'))) 'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
host, file = splithost(url) (mtype or 'text/plain', size, modified)))
if not host: if not host:
urlfile = file urlfile = file
if file[:1] == '/': if file[:1] == '/':
urlfile = 'file://' + file urlfile = 'file://' + file
return addinfourl(open(url2pathname(file), 'rb'), return addinfourl(open(localname, 'rb'),
headers, urlfile) headers, urlfile)
host, port = splitport(host) host, port = splitport(host)
if not port \ if not port \
...@@ -418,7 +431,7 @@ class URLopener: ...@@ -418,7 +431,7 @@ class URLopener:
urlfile = file urlfile = file
if file[:1] == '/': if file[:1] == '/':
urlfile = 'file://' + file urlfile = 'file://' + file
return addinfourl(open(url2pathname(file), 'rb'), return addinfourl(open(localname, 'rb'),
headers, urlfile) headers, urlfile)
raise IOError, ('local file error', 'not on local host') raise IOError, ('local file error', 'not on local host')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment