mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Added support for "data" URL, by Sjoerd Mullender.
This commit is contained in:
parent
fb34c92628
commit
6d4d1c2a25
1 changed files with 40 additions and 0 deletions
|
@ -354,6 +354,46 @@ class URLopener:
|
||||||
except ftperrors(), msg:
|
except ftperrors(), msg:
|
||||||
raise IOError, ('ftp error', msg), sys.exc_info()[2]
|
raise IOError, ('ftp error', msg), sys.exc_info()[2]
|
||||||
|
|
||||||
|
# Use "data" URL
|
||||||
|
def open_data(self, url, data=None):
|
||||||
|
# ignore POSTed data
|
||||||
|
#
|
||||||
|
# syntax of data URLs:
|
||||||
|
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
|
||||||
|
# mediatype := [ type "/" subtype ] *( ";" parameter )
|
||||||
|
# data := *urlchar
|
||||||
|
# parameter := attribute "=" value
|
||||||
|
import StringIO, mimetools, time
|
||||||
|
try:
|
||||||
|
[type, data] = string.split(url, ',', 1)
|
||||||
|
except ValueError:
|
||||||
|
raise IOError, ('data error', 'bad data URL')
|
||||||
|
if not type:
|
||||||
|
type = 'text/plain;charset=US-ASCII'
|
||||||
|
semi = string.rfind(type, ';')
|
||||||
|
if semi >= 0 and '=' not in type[semi:]:
|
||||||
|
encoding = type[semi+1:]
|
||||||
|
type = type[:semi]
|
||||||
|
else:
|
||||||
|
encoding = ''
|
||||||
|
msg = []
|
||||||
|
msg.append('Date: %s'%time.strftime('%a, %d %b %Y %T GMT',
|
||||||
|
time.gmtime(time.time())))
|
||||||
|
msg.append('Content-type: %s' % type)
|
||||||
|
if encoding == 'base64':
|
||||||
|
import base64
|
||||||
|
data = base64.decodestring(data)
|
||||||
|
else:
|
||||||
|
data = unquote(data)
|
||||||
|
msg.append('Content-length: %d' % len(data))
|
||||||
|
msg.append('')
|
||||||
|
msg.append(data)
|
||||||
|
msg = string.join(msg, '\n')
|
||||||
|
f = StringIO.StringIO(msg)
|
||||||
|
headers = mimetools.Message(f, 0)
|
||||||
|
f.fileno = None # needed for addinfourl
|
||||||
|
return addinfourl(f, headers, url)
|
||||||
|
|
||||||
|
|
||||||
# Derived class with handlers for errors we can handle (perhaps)
|
# Derived class with handlers for errors we can handle (perhaps)
|
||||||
class FancyURLopener(URLopener):
|
class FancyURLopener(URLopener):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue