Commit ba6da303 authored by 's avatar

Consolidated content-type checking into OFS.content_types and provided for

content-type extensibility, and added logic to DMTLMethods/Documents to attempt
to set a content-type - otherwise ZPublisher will assume that xml is html and
do bad things with the content.
parent 6718f182
......@@ -84,10 +84,11 @@
##############################################################################
"""DTML Document objects."""
__version__='$Revision: 1.19 $'[11:-2]
__version__='$Revision: 1.20 $'[11:-2]
from DocumentTemplate.DT_Util import InstanceDict, TemplateDict
from ZPublisher.Converters import type_converters
from Globals import HTML, HTMLFile, MessageDialog
from OFS.content_types import guess_content_type
from DTMLMethod import DTMLMethod, decapitate
from PropertyManager import PropertyManager
from webdav.common import rfc1123_date
......@@ -181,6 +182,9 @@ class DTMLDocument(DTMLMethod, PropertyManager):
return self.raise_standardErrorMessage(client, REQUEST)
if RESPONSE is None: return r
RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
# Try to handle content types intelligently...
c, e=guess_content_type(self.__name__, r)
RESPONSE.setHeader('Content-Type', c)
return decapitate(r, RESPONSE)
......
......@@ -84,11 +84,12 @@
##############################################################################
"""DTML Method objects."""
__version__='$Revision: 1.15 $'[11:-2]
__version__='$Revision: 1.16 $'[11:-2]
from Globals import HTML, HTMLFile, MessageDialog
from string import join,split,strip,rfind,atoi,lower
from SimpleItem import Item_w__name__, pretty_tb
from OFS.content_types import guess_content_type
from DocumentTemplate.DT_Util import cDocument
from PropertyManager import PropertyManager
from AccessControl.Role import RoleManager
......@@ -158,6 +159,9 @@ class DTMLMethod(cDocument, HTML, Acquisition.Implicit, RoleManager,
if RESPONSE is None: return r
RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
# Try to handle content types intelligently...
c, e=guess_content_type(self.__name__, r)
RESPONSE.setHeader('Content-Type', c)
return decapitate(r, RESPONSE)
def get_size(self):
......
......@@ -82,6 +82,9 @@
# attributions are listed in the accompanying credits file.
#
##############################################################################
"""A utility module for content-type handling."""
__version__='$Revision: 1.4 $'[11:-2]
src="""
htm, html: text/html
gif: image/gif
......@@ -98,8 +101,8 @@ tar: application/x-tar
zip: application/x-zip
"""
from string import split, strip
import regex
from string import split, strip, lower
import ts_regex, mimetypes
content_type={}
for l in filter(lambda s: s and s[:1] != '#', map(strip, split(src,'\n'))):
......@@ -107,13 +110,42 @@ for l in filter(lambda s: s and s[:1] != '#', map(strip, split(src,'\n'))):
t=strip(t)
for e in map(strip, split(e, ',')):
content_type[e]=t
find_binary=regex.compile('[\0-\6\177-\277]').search
html_re=regex.compile('<html>', regex.casefold)
find_binary=ts_regex.compile('[\0-\6\177-\277]').search
html_re=ts_regex.compile('<html>', ts_regex.casefold)
def text_type(s):
return "text/" + (html_re.search(s) >= 0 and 'html' or 'plain')
# This gives us a hook to add content types that
# aren't currently listed in the mimetypes module.
_addtypes=(
('.css', 'text/css'),
('.xml', 'text/xml'),
('.xsl', 'text/xsl'),
('.xul', 'text/xul'),
)
for name, val in _addtypes:
mimetypes.types_map[name]=val
def guess_content_type(name='', body=''):
# Attempt to determine the content type (and possibly
# content-encoding) based on an an object's name and
# entity body.
type, enc=None, None
type, enc=mimetypes.guess_type(name)
if (type is None) and body and find_binary(body) >= 0:
type='application/octet-stream'
elif (type is None) and body:
type=text_type(body)
elif type is None:
type='text/x-unknown-content-type'
return lower(type), enc and lower(enc) or None
if __name__=='__main__':
items=content_type.items()
items.sort()
......
......@@ -85,7 +85,7 @@
"""WebDAV support - null resource objects."""
__version__='$Revision: 1.20 $'[11:-2]
__version__='$Revision: 1.21 $'[11:-2]
import sys, os, string, mimetypes, Globals
import Acquisition, OFS.content_types
......@@ -140,12 +140,7 @@ class NullResource(Persistent, Acquisition.Implicit, Resource):
body=REQUEST.get('BODY', '')
name=self.__name__
if type is None:
type, enc=mimetypes.guess_type(name)
if type is None:
if OFS.content_types.find_binary(body) >= 0:
type='application/octet-stream'
else: type=OFS.content_types.text_type(body)
type=string.lower(type)
type, enc=OFS.content_types.guess_content_type(name, body)
from OFS.Image import Image, File
if type in ('text/html', 'text/xml', 'text/plain'):
self.__parent__.manage_addDTMLDocument(name, '', body)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment