Menu

#256 _CGISendXML() writes newline when 'text' is empty string

open
nobody
None
5
2008-11-25
2008-11-25
Michael W
No

_CGISendXML() should probably not print a newline character when the 'text' argument passed to _CGISendXML() is an empty string (occurs when making one-way calls, with no response message returned).

The current code in ZSI/dispatch.py:

def _CGISendXML(text, code=200, **kw):
print 'Status: %d' % code
print 'Content-Type: text/xml; charset="%s"' %UNICODE_ENCODING
print 'Content-Length: %d' % len(text)
print ''
print text

In the code above, 'text' is an empty string, 'print' is still called which will result in a newline being printed. This extra newline is causing problems because the content-length is no longer zero. It seems that apache actually overwrites the original content length of 0 and set it to 1.

The output looks like this:
_________________________________ Mon Nov 24 16:41:42 2008 RESPONSE:
200
OK
-------
Date: Tue, 25 Nov 2008 00:41:42 GMT
Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.7l DAV/2
Content-Length: 1
Content-Type: text/xml; charset="utf-8"
<carriage-return><newline>
<newline>

I think the code should look like this:

def _CGISendXML(text, code=200, **kw):
print 'Status: %d' % code
print 'Content-Type: text/xml; charset="%s"' %UNICODE_ENCODING
print 'Content-Length: %d' % len(text)
print ''
if len(text):
# If 'text' is empty string, don't print anything.
# Note that without the 'if' statement print is called and a newline is inadvertently printed to
# standard output.
print text

The new output will look like this:
_________________________________ Mon Nov 24 16:41:42 2008 RESPONSE:
200
OK
-------
Date: Tue, 25 Nov 2008 00:41:42 GMT
Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.7l DAV/2
Content-Length: 0
Content-Type: text/xml; charset="utf-8"
<carriage-return><newline>

Discussion


Log in to post a comment.