From 88e0b5bee0e69b628a9358c987bf49ac885d1c21 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 23 Aug 2001 13:38:15 +0000 Subject: =?UTF-8?q?SF=20patch=20#454553=20by=20Walter=20D=C3=B6rwald:=20au?= =?UTF-8?q?to-guess=20content-type=20header=20for=20ftp=20urls.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Doc/lib/liburllib.tex | 3 ++- Lib/urllib.py | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Doc/lib/liburllib.tex b/Doc/lib/liburllib.tex index 6522cf7..fb9524a 100644 --- a/Doc/lib/liburllib.tex +++ b/Doc/lib/liburllib.tex @@ -43,7 +43,8 @@ returned by the server at the head of the retrieved HTML page (including Content-Length and Content-Type). When the method is FTP, a Content-Length header will be present if (as is now usual) the server passed back a file length in response to the FTP retrieval -request. When the method is local-file, returned headers will include +request. A Content-Type header will be present if the MIME type can +be guessed. 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. See also the description of the diff --git a/Lib/urllib.py b/Lib/urllib.py index f60a841..a255956 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -440,6 +440,7 @@ class URLopener: def open_ftp(self, url): """Use FTP protocol.""" + import mimetypes, mimetools, StringIO host, path = splithost(url) if not host: raise IOError, ('ftp error', 'no host given') host, port = splitport(host) @@ -482,12 +483,13 @@ class URLopener: value in ('a', 'A', 'i', 'I', 'd', 'D'): type = value.upper() (fp, retrlen) = self.ftpcache[key].retrfile(file, type) + mtype = mimetypes.guess_type("ftp:" + url)[0] + headers = "" + if mtype: + headers += "Content-Type: %s\n" % mtype if retrlen is not None and retrlen >= 0: - import mimetools, StringIO - headers = mimetools.Message(StringIO.StringIO( - 'Content-Length: %d\n' % retrlen)) - else: - headers = noheaders() + headers += "Content-Length: %d\n" % retrlen + headers = mimetools.Message(StringIO.StringIO(headers)) return addinfourl(fp, headers, "ftp:" + url) except ftperrors(), msg: raise IOError, ('ftp error', msg), sys.exc_info()[2] -- cgit v0.12