/usr/share/pyshared/webhelpers/mimehelper.py is in python-webhelpers 1.3-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | """MIME Type helpers
This helper depends on the WebOb package, and has optional Pylons support.
"""
import mimetypes
class MIMETypes(object):
"""MIMETypes registration mapping
The MIMETypes object class provides a single point to hold onto all
the registered mimetypes, and their association extensions. It's
used by the mimetypes method to determine the appropriate content
type to return to a client.
"""
aliases = {}
def init(cls):
"""Loads a default mapping of extensions and mimetypes
These are suitable for most web applications by default.
Additional types can be added by using the mimetypes module.
"""
mimetypes.init()
init = classmethod(init)
def add_alias(cls, alias, mimetype):
"""Create a MIMEType alias to a full mimetype.
Examples:
- ``add_alias('html', 'text/html')``
- ``add_alias('xml', 'application/xml')``
An ``alias`` may not contain the ``/`` character.
"""
if '/' in alias:
raise ValueError("MIMEType aliases may not contain '/'")
cls.aliases[alias] = mimetype
add_alias = classmethod(add_alias)
def __init__(self, environ):
"""``environ`` is the WSGI environment of the current request."""
self.env = environ
def _set_response_content_type(self, mimetype):
if 'pylons.pylons' in self.env:
self.env['pylons.pylons'].response.content_type = mimetype
return mimetype
def mimetype(self, content_type):
"""Check the PATH_INFO of the current request and client's HTTP Accept
to attempt to use the appropriate mime-type.
If a content-type is matched, return the appropriate response
content type, and if running under Pylons, set the response content
type directly. If a content-type is not matched, return ``False``.
This works best with URLs that end in extensions that differentiate
content-type. Examples: ``http://example.com/example``,
``http://example.com/example.xml``, ``http://example.com/example.csv``
Since browsers generally allow for any content-type, but should be
sent HTML when possible, the html mimetype check should always come
first, as shown in the example below.
Example::
# some code likely in environment.py
MIMETypes.init()
MIMETypes.add_alias('html', 'text/html')
MIMETypes.add_alias('xml', 'application/xml')
MIMETypes.add_alias('csv', 'text/csv')
# code in a Pylons controller
def someaction(self):
# prepare a bunch of data
# ......
# prepare MIMETypes object
m = MIMETypes(request.environ)
if m.mimetype('html'):
return render('/some/template.html')
elif m.mimetype('atom'):
return render('/some/xml_template.xml')
elif m.mimetype('csv'):
# write the data to a csv file
return csvfile
else:
abort(404)
# Code in a non-Pylons controller.
m = MIMETypes(environ)
response_type = m.mimetype('html')
# ``response_type`` is a MIME type or ``False``.
"""
import webob
if content_type in MIMETypes.aliases:
content_type = MIMETypes.aliases[content_type]
path = self.env['PATH_INFO']
guess_from_url = mimetypes.guess_type(path)[0]
possible_from_accept_header = None
has_extension = False
if len(path.split('/')) > 1:
last_part = path.split('/')[-1]
if '.' in last_part:
has_extension = True
if 'HTTP_ACCEPT' in self.env:
try:
# WebOb >= 1.1.1
possible_from_accept_header = webob.acceptparse.MIMEAccept(
self.env['HTTP_ACCEPT'])
except TypeError:
# WebOb < 1.1.1
possible_from_accept_header = webob.acceptparse.MIMEAccept('ACCEPT',
self.env['HTTP_ACCEPT'])
if has_extension == False:
if possible_from_accept_header is None:
return self._set_response_content_type(content_type)
elif content_type in possible_from_accept_header:
return self._set_response_content_type(content_type)
else:
return False
if content_type == guess_from_url:
# Guessed same mimetype
return self._set_response_content_type(content_type)
elif guess_from_url is None and content_type in possible_from_accept_header:
return self._set_response_content_type(content_type)
else:
return False
|