summaryrefslogtreecommitdiff
path: root/Lib/mimetypes.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-10-12 15:12:28 +0000
committerGuido van Rossum <guido@python.org>1998-10-12 15:12:28 +0000
commit0b1e5f97f06374a08b1ead930a5022e88a4fa81d (patch)
treeafd8b565e2f59c1ac229fb124bfaf31a72667b92 /Lib/mimetypes.py
parent618be510aecaadb20877cd70e66178a8dd20e93c (diff)
downloadcpython-0b1e5f97f06374a08b1ead930a5022e88a4fa81d.tar.gz
Make mimetypes.guess_type understand data URLs. (Sjoerd Mullender)
Diffstat (limited to 'Lib/mimetypes.py')
-rw-r--r--Lib/mimetypes.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index b35d0ff84b..3d6510e23d 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -25,6 +25,7 @@ read_mime_types(file) -- parse one file, return a dictionary or None
import string
import posixpath
+import urllib
knownfiles = [
"/usr/local/etc/httpd/conf/mime.types",
@@ -53,6 +54,26 @@ def guess_type(url):
"""
if not inited:
init()
+ scheme, url = urllib.splittype(url)
+ if scheme == 'data':
+ # syntax of data URLs:
+ # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
+ # mediatype := [ type "/" subtype ] *( ";" parameter )
+ # data := *urlchar
+ # parameter := attribute "=" value
+ # type/subtype defaults to "text/plain"
+ comma = string.find(url, ',')
+ if comma < 0:
+ # bad data URL
+ return None, None
+ semi = string.find(url, ';', 0, comma)
+ if semi >= 0:
+ type = url[:semi]
+ else:
+ type = url[:comma]
+ if '=' in type or '/' not in type:
+ type = 'text/plain'
+ return type, None # never compressed, so encoding is None
base, ext = posixpath.splitext(url)
while suffix_map.has_key(ext):
base, ext = posixpath.splitext(base + suffix_map[ext])