/usr/share/pyshared/cherrypy/filters/nsgmlsfilter.py is in python-cherrypy 2.3.0-3.
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 | import os, cgi
import cherrypy
from basefilter import BaseFilter
class NsgmlsFilter(BaseFilter):
"""Filter that runs the response through Nsgmls.
"""
def before_finalize(self):
if not cherrypy.config.get('nsgmls_filter.on', False):
return
# the tidy filter, by its very nature it's not generator friendly,
# so we just collect the body and work with it.
original_body = cherrypy.response.collapse_body()
fct = cherrypy.response.headers.get('Content-Type', '')
ct = fct.split(';')[0]
encoding = ''
i = fct.find('charset=')
if i != -1:
encoding = fct[i+8:]
if ct == 'text/html':
# Remove bits of Javascript (nsgmls doesn't seem to handle
# them correctly (for instance, if <a appears in your
# Javascript code nsgmls complains about it)
while True:
i = original_body.find('<script')
if i == -1:
break
j = original_body.find('</script>', i)
if j == -1:
break
original_body = original_body[:i] + original_body[j+9:]
tmpdir = cherrypy.config.get('nsgmls_filter.tmp_dir')
page_file = os.path.join(tmpdir, 'page.html')
err_file = os.path.join(tmpdir, 'nsgmls.err')
f = open(page_file, 'wb')
f.write(original_body)
f.close()
nsgmls_path = cherrypy.config.get('nsgmls_filter.nsgmls_path')
catalog_path = cherrypy.config.get('nsgmls_filter.catalog_path')
command = '%s -c%s -f%s -s -E10 %s' % (
nsgmls_path, catalog_path, err_file, page_file)
command = command.replace('\\', '/')
os.system(command)
f = open(err_file, 'rb')
err = f.read()
f.close()
errs = err.splitlines()
new_errs = []
for err in errs:
ignore = False
for err_ign in cherrypy.config.get('nsgmls_filter.errors_to_ignore', []):
if err.find(err_ign) != -1:
ignore = True
break
if not ignore:
new_errs.append(err)
if new_errs:
new_body = "Wrong HTML:<br />" + cgi.escape('\n'.join(new_errs)).replace('\n','<br />')
new_body += '<br /><br />'
i = 0
for line in original_body.splitlines():
i += 1
new_body += "%03d - "%i + cgi.escape(line).replace('\t',' ').replace(' ',' ') + '<br />'
cherrypy.response.body = new_body
# Delete Content-Length header so finalize() recalcs it.
cherrypy.response.headers.pop("Content-Length", None)
|