plone/diazo — pull request #9 by davidjb (2012-08-07 04:17:20+00:00)
Title: Handle empty responses by returning an unthemed response

There's an issue present whereby if an empty text/html response is returned for transformation, a lxml.etree.XMLSyntaxError will be raised by lxml thusly:

```
File "/opt/diazo/eggs/Paste-1.7.5.1-py2.6.egg/paste/urlmap.py", line 203, in __call__
return app(environ, start_response)
File "/opt/diazo/src/diazo/lib/diazo/wsgi.py", line 614, in __call__
return transform_middleware(environ, start_response)
File "/opt/diazo/src/diazo/lib/diazo/wsgi.py", line 328, in __call__
serializer = getHTMLSerializer(response.app_iter, encoding=input_encoding)
File "/opt/diazo/eggs/repoze.xmliter-0.5-py2.6.egg/repoze/xmliter/utils.py", line 32, in getHTMLSerializer
doctype=doctype,
File "/opt/diazo/eggs/repoze.xmliter-0.5-py2.6.egg/repoze/xmliter/utils.py", line 17, in getXMLSerializer
root = p.close()
File "parser.pxi", line 1171, in lxml.etree._FeedParser.close (src/lxml/lxml.etree.c:79886)
File "parser.pxi", line 561, in lxml.etree._ParserContext._handleParseResult (src/lxml/lxml.etree.c:74504)
File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75458)
File "parser.pxi", line 601, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74958)
lxml.etree.XMLSyntaxError: None
```

Such responses can be produced from some applications that only consider data in a header and not the body - one such example is Jenkins CI.

This pull request catches this syntax error and returns an unthemed response, and provides tests for the same. The changes move the preparation of the serializer to be earlier in the WSGI processing to ensure the response is returned unchanged (eg line 260 of lib/diazo/uwsgi.py onwards).