Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c5fb1c

Browse files
committed
Make mimetypes.guess_type understand data URLs. (Sjoerd Mullender)
1 parent 8571ed8 commit 1c5fb1c

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lib/mimetypes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import string
2727
import posixpath
28+
import urllib
2829

2930
knownfiles = [
3031
"/usr/local/etc/httpd/conf/mime.types",
@@ -53,6 +54,26 @@ def guess_type(url):
5354
"""
5455
if not inited:
5556
init()
57+
scheme, url = urllib.splittype(url)
58+
if scheme == 'data':
59+
# syntax of data URLs:
60+
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
61+
# mediatype := [ type "/" subtype ] *( ";" parameter )
62+
# data := *urlchar
63+
# parameter := attribute "=" value
64+
# type/subtype defaults to "text/plain"
65+
comma = string.find(url, ',')
66+
if comma < 0:
67+
# bad data URL
68+
return None, None
69+
semi = string.find(url, ';', 0, comma)
70+
if semi >= 0:
71+
type = url[:semi]
72+
else:
73+
type = url[:comma]
74+
if '=' in type or '/' not in type:
75+
type = 'text/plain'
76+
return type, None # never compressed, so encoding is None
5677
base, ext = posixpath.splitext(url)
5778
while suffix_map.has_key(ext):
5879
base, ext = posixpath.splitext(base + suffix_map[ext])

0 commit comments

Comments
 (0)