Make mimetypes.guess_type understand data URLs. (Sjoerd Mullender)

This commit is contained in:
Guido van Rossum 1998-10-12 15:12:28 +00:00
parent 8571ed8647
commit 1c5fb1cd1b
1 changed files with 21 additions and 0 deletions

View File

@ -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])